StoreProduct.php 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\store\{
  5. StoreDescription,
  6. StoreProductAttrValue,
  7. StoreProductAttr,
  8. StoreProductAttrResult,
  9. StoreProductCate,
  10. StoreProductRelation,
  11. StoreCategory as CategoryModel,
  12. StoreProduct as ProductModel
  13. };
  14. use app\admin\model\ump\StoreBargain;
  15. use app\admin\model\ump\StoreCombination;
  16. use app\admin\model\ump\StoreSeckill;
  17. use crmeb\services\{
  18. JsonService, UtilService as Util, JsonService as Json, FormBuilder as Form
  19. };
  20. use crmeb\traits\CurdControllerTrait;
  21. use think\facade\Route as Url;
  22. use app\admin\model\system\{SystemAttachment, ShippingTemplates, SystemUserLevel};
  23. /**
  24. * 产品管理
  25. * Class StoreProduct
  26. * @package app\admin\controller\store
  27. */
  28. class StoreProduct extends AuthController
  29. {
  30. use CurdControllerTrait;
  31. protected $bindModel = ProductModel::class;
  32. /**
  33. * 显示资源列表
  34. *
  35. * @return \think\Response
  36. */
  37. public function index()
  38. {
  39. $type = $this->request->param('type', 1);
  40. //获取分类
  41. $this->assign('cate', CategoryModel::getTierList(null, 1));
  42. //出售中产品
  43. $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->count();
  44. //待上架产品
  45. $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->count();
  46. //仓库中产品
  47. $warehouse = ProductModel::where('is_del', 0)->count();
  48. //已经售馨产品
  49. $outofstock = ProductModel::getModelObject(['type' => 4])->count();
  50. //警戒库存
  51. $policeforce = ProductModel::getModelObject(['type' => 5])->count();
  52. //回收站
  53. $recycle = ProductModel::where('is_del', 1)->count();
  54. $this->assign(compact('type', 'onsale', 'forsale', 'warehouse', 'outofstock', 'policeforce', 'recycle'));
  55. return $this->fetch();
  56. }
  57. /**
  58. * 异步查找产品
  59. *
  60. * @return json
  61. */
  62. public function product_ist()
  63. {
  64. $where = Util::getMore([
  65. ['page', 1],
  66. ['limit', 20],
  67. ['store_name', ''],
  68. ['cate_id', ''],
  69. ['excel', 0],
  70. ['order', ''],
  71. [['type', 'd'], $this->request->param('type/d')]
  72. ]);
  73. return Json::successlayui(ProductModel::ProductList($where));
  74. }
  75. /**
  76. * 设置单个产品上架|下架
  77. *
  78. * @return json
  79. */
  80. public function set_show($is_show = '', $id = '')
  81. {
  82. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  83. $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  84. if ($res) {
  85. return Json::successful($is_show == 1 ? '上架成功' : '下架成功');
  86. } else {
  87. return Json::fail($is_show == 1 ? '上架失败' : '下架失败');
  88. }
  89. }
  90. /**
  91. * 快速编辑
  92. *
  93. * @return json
  94. */
  95. public function set_product($field = '', $id = '', $value = '')
  96. {
  97. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  98. if (ProductModel::where(['id' => $id])->update([$field => $value]))
  99. return Json::successful('保存成功');
  100. else
  101. return Json::fail('保存失败');
  102. }
  103. /**
  104. * 设置批量产品上架
  105. *
  106. * @return json
  107. */
  108. public function product_show()
  109. {
  110. $post = Util::postMore([
  111. ['ids', []]
  112. ]);
  113. if (empty($post['ids'])) {
  114. return Json::fail('请选择需要上架的产品');
  115. } else {
  116. $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
  117. if ($res)
  118. return Json::successful('上架成功');
  119. else
  120. return Json::fail('上架失败');
  121. }
  122. }
  123. /**
  124. * 显示创建资源表单页.
  125. *
  126. * @return \think\Response
  127. */
  128. public function create($id = 0)
  129. {
  130. $this->assign('id', (int)$id);
  131. return $this->fetch();
  132. }
  133. /**
  134. * 获取规则属性模板
  135. * @throws \think\db\exception\DataNotFoundException
  136. * @throws \think\db\exception\DbException
  137. * @throws \think\db\exception\ModelNotFoundException
  138. */
  139. public function get_rule()
  140. {
  141. return Json::successful(\app\models\store\StoreProductRule::field(['rule_name', 'rule_value'])->select()->each(function ($item) {
  142. $item['rule_value'] = json_decode($item['rule_value'], true);
  143. })->toArray());
  144. }
  145. /**
  146. * 获取产品详细信息
  147. * @param int $id
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. */
  152. public function get_product_info($id = 0)
  153. {
  154. $list = CategoryModel::getTierList(null, 1);
  155. $menus = [];
  156. foreach ($list as $menu) {
  157. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0 ? 0 : 1];//,'disabled'=>$menu['pid']== 0];
  158. }
  159. $data['tempList'] = ShippingTemplates::order('sort', 'desc')->field(['id', 'name'])->select()->toArray();
  160. $data['cateList'] = $menus;
  161. $level_list = SystemUserLevel::where(['is_del' => 0])->order('grade asc')->select();
  162. $level_price = [];
  163. foreach ($level_list as $v) {
  164. $level_price['level_' . $v['id']] = 0;
  165. }
  166. $data['level_list'] = $level_list;
  167. $data['productInfo'] = [];
  168. if ($id) {
  169. $productInfo = ProductModel::get($id);
  170. if (!$productInfo) {
  171. return Json::fail('修改的产品不存在');
  172. }
  173. $productInfo['cate_id'] = explode(',', $productInfo['cate_id']);
  174. $productInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id));
  175. $productInfo['slider_image'] = is_string($productInfo['slider_image']) ? json_decode($productInfo['slider_image'], true) : [];
  176. if ($productInfo['spec_type'] == 1) {
  177. $result = StoreProductAttrResult::getResult($id, 0);
  178. foreach ($result['value'] as $k => $v) {
  179. $num = 1;
  180. foreach ($v['detail'] as $dv) {
  181. $result['value'][$k]['value' . $num] = $dv;
  182. $num++;
  183. if (!isset($result['value'][$k]['level_price'])) {
  184. $result['value'][$k]['level_price'] = $level_price;
  185. } else {
  186. $result['value'][$k]['level_price'] = is_string($result['value'][$k]['level_price']) ? json_decode($result['value'][$k]['level_price'], true) : $result['value'][$k]['level_price'];
  187. $level_price_real = [];
  188. foreach ($level_price as $check => $v) {
  189. $level_price_real[$check] = $result['value'][$k]['level_price'][$check] ?? $v;
  190. }
  191. $result['value'][$k]['level_price'] = $level_price_real;
  192. }
  193. }
  194. foreach ($level_list as $vv) {
  195. $result['value'][$k]['priceList'][] = [
  196. 'id' => $vv['id'],
  197. 'name' => $vv['name'],
  198. 'value' => $result['value'][$k]['level_price']['level_' . $vv['id']],
  199. 'k' => $k,
  200. ];
  201. }
  202. }
  203. $productInfo['items'] = $result['attr'];
  204. $productInfo['attrs'] = $result['value'];
  205. $productInfo['attr'] = ['pic' => '', 'price' => 0, 'integral' => 0, 'cost' => 0,'level_price' => $level_price, 'ot_price' => 0, 'stock' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0, 'brokerage_two' => 0];
  206. // $productInfo['attr'] = ['pic' => '', 'price' => 0, 'cost' => 0, 'ot_price' => 0, 'stock' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0, 'brokerage_two' => 0];
  207. } else {
  208. $result = StoreProductAttrValue::where('product_id', $id)->where('type', 0)->find();
  209. if ($result) {
  210. $single = $result->toArray();
  211. } else {
  212. $single = [];
  213. }
  214. $productInfo['items'] = [];
  215. $productInfo['attrs'] = [];
  216. if (!isset($single['level_price'])) {
  217. $single['level_price'] = $level_price;
  218. } else {
  219. $single['level_price'] = is_string($single['level_price']) ? json_decode($single['level_price'], true) : $single['level_price'];
  220. $level_price_real = [];
  221. foreach ($level_price as $check => $v) {
  222. $level_price_real[$check] = $single['level_price'][$check] ?? $v;
  223. }
  224. $single['level_price'] = $level_price_real;
  225. }
  226. $productInfo['attr'] = [
  227. 'pic' => $single['image'] ?? '',
  228. 'price' => $single['price'] ?? 0,
  229. 'cost' => $single['cost'] ?? 0,
  230. 'ot_price' => $single['ot_price'] ?? 0,
  231. 'stock' => $single['stock'] ?? 0,
  232. 'bar_code' => $single['bar_code'] ?? '',
  233. 'weight' => $single['weight'] ?? 0,
  234. 'volume' => $single['volume'] ?? 0,
  235. 'brokerage' => $single['brokerage'] ?? 0,
  236. 'brokerage_two' => $single['brokerage_two'] ?? 0,
  237. 'level_price' => $single['level_price'],
  238. ];
  239. }
  240. if ($productInfo['activity']) {
  241. $activity = explode(',', $productInfo['activity']);
  242. foreach ($activity as $k => $v) {
  243. if ($v == 1) {
  244. $activity[$k] = '秒杀';
  245. } elseif ($v == 2) {
  246. $activity[$k] = '砍价';
  247. } elseif ($v == 3) {
  248. $activity[$k] = '拼团';
  249. }
  250. }
  251. $productInfo['activity'] = $activity;
  252. } else {
  253. $productInfo['activity'] = ['秒杀', '砍价', '拼团'];
  254. }
  255. $data['productInfo'] = $productInfo;
  256. }
  257. return JsonService::successful($data);
  258. }
  259. /**
  260. * 保存新建的资源
  261. *
  262. *
  263. */
  264. public function save($id)
  265. {
  266. $data = Util::postMore([
  267. ['cate_id', []],
  268. 'store_name',
  269. 'store_info',
  270. 'keyword',
  271. ['unit_name', '件'],
  272. ['image', []],
  273. ['slider_image', []],
  274. ['postage', 0],
  275. ['is_sub', 0],
  276. ['sort', 0],
  277. ['sales', 0],
  278. ['ficti', 100],
  279. ['give_integral', 0],
  280. ['is_show', 0],
  281. ['temp_id', 0],
  282. ['is_hot', 0],
  283. ['is_benefit', 0],
  284. ['is_best', 0],
  285. ['is_new', 0],
  286. ['mer_use', 0],
  287. ['is_postage', 0],
  288. ['is_good', 0],
  289. ['description', ''],
  290. ['spec_type', 0],
  291. ['video_link', ''],
  292. ['items', []],
  293. ['attrs', []],
  294. ['activity', []],
  295. ['is_level',0],
  296. ['level',0],
  297. ['max_integral',0],
  298. ['is_integral',0],
  299. ['is_one',0],
  300. ]);
  301. foreach ($data['activity'] as $k => $v) {
  302. if ($v == '秒杀') {
  303. $data['activity'][$k] = 1;
  304. } elseif ($v == '砍价') {
  305. $data['activity'][$k] = 2;
  306. } else {
  307. $data['activity'][$k] = 3;
  308. }
  309. }
  310. $data['activity'] = implode(',', $data['activity']);
  311. $detail = $data['attrs'];
  312. $data['price'] = min(array_column($detail, 'price'));
  313. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  314. $data['cost'] = min(array_column($detail, 'cost'));
  315. $attr = $data['items'];
  316. unset($data['items'], $data['video'], $data['attrs']);
  317. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  318. $cate_id = $data['cate_id'];
  319. $data['cate_id'] = implode(',', $data['cate_id']);
  320. if (!$data['store_name']) return Json::fail('请输入产品名称');
  321. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  322. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  323. $data['image'] = $data['image'][0];
  324. $data['slider_image'] = json_encode($data['slider_image']);
  325. $data['stock'] = array_sum(array_column($detail, 'stock'));
  326. ProductModel::beginTrans();
  327. foreach ($detail as &$item) {
  328. if (($item['brokerage'] + $item['brokerage_two']) > $item['price']) {
  329. return Json::fail('一二级返佣相加不能大于商品售价');
  330. }
  331. }
  332. if ($id) {
  333. unset($data['sales']);
  334. ProductModel::edit($data, $id);
  335. $description = $data['description'];
  336. unset($data['description']);
  337. StoreDescription::saveDescription($description, $id);
  338. StoreProductCate::where('product_id', $id)->delete();
  339. $cateData = [];
  340. foreach ($cate_id as $cid) {
  341. $cateData[] = ['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()];
  342. }
  343. StoreProductCate::insertAll($cateData);
  344. if ($data['spec_type'] == 0) {
  345. $attr = [
  346. [
  347. 'value' => '规格',
  348. 'detailValue' => '',
  349. 'attrHidden' => '',
  350. 'detail' => ['默认']
  351. ]
  352. ];
  353. $detail[0]['value1'] = '规格';
  354. $detail[0]['detail'] = ['规格' => '默认'];
  355. }
  356. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  357. if ($attr_res) {
  358. ProductModel::commitTrans();
  359. return Json::success('修改成功!');
  360. } else {
  361. ProductModel::rollbackTrans();
  362. return Json::fail(StoreProductAttr::getErrorInfo());
  363. }
  364. } else {
  365. $data['add_time'] = time();
  366. $data['code_path'] = '';
  367. $res = ProductModel::create($data);
  368. $description = $data['description'];
  369. StoreDescription::saveDescription($description, $res['id']);
  370. $cateData = [];
  371. foreach ($cate_id as $cid) {
  372. $cateData[] = ['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()];
  373. }
  374. StoreProductCate::insertAll($cateData);
  375. if ($data['spec_type'] == 0) {
  376. $attr = [
  377. [
  378. 'value' => '规格',
  379. 'detailValue' => '',
  380. 'attrHidden' => '',
  381. 'detail' => ['默认']
  382. ]
  383. ];
  384. $detail[0]['value1'] = '规格';
  385. $detail[0]['detail'] = ['规格' => '默认'];
  386. }
  387. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $res['id']);
  388. if ($attr_res) {
  389. ProductModel::commitTrans();
  390. return Json::success('添加产品成功!');
  391. } else {
  392. ProductModel::rollbackTrans();
  393. return Json::fail(StoreProductAttr::getErrorInfo());
  394. }
  395. }
  396. }
  397. public function edit_content($id)
  398. {
  399. if (!$id) return $this->failed('数据不存在');
  400. $product = ProductModel::get($id);
  401. if (!$product) return Json::fail('数据不存在!');
  402. $this->assign([
  403. 'content' => $product->description,
  404. 'field' => 'description',
  405. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  406. ]);
  407. return $this->fetch('public/edit_content');
  408. }
  409. /**
  410. * 显示编辑资源表单页.
  411. *
  412. * @param int $id
  413. * @return \think\Response
  414. */
  415. public function edit($id)
  416. {
  417. if (!$id) return $this->failed('数据不存在');
  418. $product = ProductModel::get($id);
  419. if (!$product) return Json::fail('数据不存在!');
  420. $field = [
  421. Form::select('cate_id', '产品分类', explode(',', $product->getData('cate_id')))->setOptions(function () {
  422. $list = CategoryModel::getTierList(null, 1);
  423. $menus = [];
  424. foreach ($list as $menu) {
  425. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0];//,'disabled'=>$menu['pid']== 0];
  426. }
  427. return $menus;
  428. })->filterable(1)->multiple(1),
  429. Form::input('store_name', '产品名称', $product->getData('store_name')),
  430. Form::input('store_info', '产品简介', $product->getData('store_info'))->type('textarea'),
  431. Form::input('keyword', '产品关键字', $product->getData('keyword'))->placeholder('多个用英文状态下的逗号隔开'),
  432. Form::input('unit_name', '产品单位', $product->getData('unit_name'))->col(12),
  433. Form::input('bar_code', '产品条码', $product->getData('bar_code'))->col(12),
  434. Form::frameImageOne('image', '产品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'),
  435. Form::frameImages('slider_image', '产品轮播图(640*640px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'slider_image')), json_decode($product->getData('slider_image'), 1) ?: [])->maxLength(5)->icon('images')->width('100%')->height('500px'),
  436. Form::number('price', '产品售价', $product->getData('price'))->min(0)->col(8),
  437. Form::number('ot_price', '产品市场价', $product->getData('ot_price'))->min(0)->col(8),
  438. Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(8),
  439. Form::number('postage', '邮费', $product->getData('postage'))->min(0)->col(8),
  440. Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(8)->readonly(1),
  441. Form::number('ficti', '虚拟销量', $product->getData('ficti'))->min(0)->precision(0)->col(8),
  442. Form::number('stock', '库存', ProductModel::getStock($id) > 0 ? ProductModel::getStock($id) : $product->getData('stock'))->min(0)->precision(0)->col(8),
  443. Form::number('cost', '产品成本价', $product->getData('cost'))->min(0)->col(8),
  444. Form::number('sort', '排序', $product->getData('sort'))->col(8),
  445. Form::radio('is_show', '产品状态', $product->getData('is_show'))->options([['label' => '上架', 'value' => 1], ['label' => '下架', 'value' => 0]])->col(8),
  446. Form::radio('is_hot', '热卖单品', $product->getData('is_hot'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  447. Form::radio('is_benefit', '促销单品', $product->getData('is_benefit'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  448. Form::radio('is_best', '精品推荐', $product->getData('is_best'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  449. Form::radio('is_new', '首发新品', $product->getData('is_new'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  450. Form::radio('is_postage', '是否包邮', $product->getData('is_postage'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  451. Form::radio('is_good', '是否优品推荐', $product->getData('is_good'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  452. ];
  453. $form = Form::make_post_form('编辑产品', $field, Url::buildUrl('update', array('id' => $id)), 2);
  454. $this->assign(compact('form'));
  455. return $this->fetch('public/form-builder');
  456. }
  457. /**
  458. * 保存更新的资源
  459. *
  460. * @param $id
  461. */
  462. public function update($id)
  463. {
  464. $data = Util::postMore([
  465. ['cate_id', []],
  466. 'store_name',
  467. 'store_info',
  468. 'keyword',
  469. 'bar_code',
  470. ['unit_name', '件'],
  471. ['image', []],
  472. ['slider_image', []],
  473. ['postage', 0],
  474. ['ot_price', 0],
  475. ['price', 0],
  476. ['sort', 0],
  477. ['stock', 0],
  478. ['temp_id', 0],
  479. ['ficti', 100],
  480. ['give_integral', 0],
  481. ['is_show', 0],
  482. ['cost', 0],
  483. ['is_hot', 0],
  484. ['is_benefit', 0],
  485. ['is_best', 0],
  486. ['is_new', 0],
  487. ['mer_use', 0],
  488. ['is_postage', 0],
  489. ['is_good', 0],
  490. ]);
  491. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  492. $cate_id = $data['cate_id'];
  493. $data['cate_id'] = implode(',', $data['cate_id']);
  494. if (!$data['store_name']) return Json::fail('请输入产品名称');
  495. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  496. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  497. // if(count($data['slider_image'])>8) return Json::fail('轮播图最多5张图');
  498. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入产品售价');
  499. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入产品市场价');
  500. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入库存');
  501. $data['image'] = $data['image'][0];
  502. $data['slider_image'] = json_encode($data['slider_image']);
  503. ProductModel::edit($data, $id);
  504. StoreProductCate::where('product_id', $id)->delete();
  505. foreach ($cate_id as $cid) {
  506. StoreProductCate::insert(['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()]);
  507. }
  508. return Json::successful('修改成功!');
  509. }
  510. public function attr($id)
  511. {
  512. if (!$id) return $this->failed('数据不存在!');
  513. // $result = StoreProductAttrResult::getResult($id);
  514. $result = StoreProductAttrValue::getStoreProductAttrResult($id);
  515. $image = ProductModel::where('id', $id)->value('image');
  516. $this->assign(compact('id', 'result', 'image'));
  517. return $this->fetch();
  518. }
  519. /**
  520. * 生成属性
  521. * @param int $id
  522. */
  523. public function is_format_attr($id = 0, $type = 0)
  524. {
  525. $data = Util::postMore([
  526. ['attrs', []],
  527. ['items', []]
  528. ]);
  529. $attr = $data['attrs'];
  530. $value = attr_format($attr)[1];
  531. $valueNew = [];
  532. $count = 0;
  533. $level_list = SystemUserLevel::where(['is_del' => 0])->order('grade asc')->select();
  534. $level_price = [];
  535. foreach ($level_list as $v) {
  536. $level_price['level_' . $v['id']] = 0;
  537. }
  538. foreach ($value as $key => $item) {
  539. $detail = $item['detail'];
  540. sort($item['detail'], SORT_STRING);
  541. $suk = implode(',', $item['detail']);
  542. $types = 1;
  543. if ($id) {
  544. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', 0)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two', 'suk');
  545. if (!count($sukValue)) {
  546. if ($type == 0) $types = 0; //编辑商品时,将没有规格的数据不生成默认值
  547. $sukValue[$suk]['pic'] = '';
  548. $sukValue[$suk]['price'] = 0;
  549. $sukValue[$suk]['cost'] = 0;
  550. $sukValue[$suk]['ot_price'] = 0;
  551. $sukValue[$suk]['stock'] = 0;
  552. $sukValue[$suk]['bar_code'] = '';
  553. $sukValue[$suk]['weight'] = 0;
  554. $sukValue[$suk]['volume'] = 0;
  555. $sukValue[$suk]['brokerage'] = 0;
  556. $sukValue[$suk]['brokerage_two'] = 0;
  557. $sukValue[$suk]['level_price'] = $level_price;
  558. foreach ($level_list as $v) {
  559. $sukValue[$suk]['priceList'][] = [
  560. 'id' => $v['id'],
  561. 'name' => $v['name'],
  562. 'value' => $sukValue[$suk]['level_price']['level_' . $v['id']],
  563. 'k' => $key,
  564. ];
  565. }
  566. } else {
  567. if (!isset($sukValue[$suk]['level_price'])) {
  568. $sukValue[$suk]['level_price'] = $level_price;
  569. } else {
  570. $sukValue[$suk]['level_price'] = is_string($sukValue[$suk]['level_price']) ? json_decode($sukValue[$suk]['level_price'], true) : $sukValue[$suk]['level_price'];
  571. $level_price_real = [];
  572. foreach ($level_price as $check => $v) {
  573. $level_price_real[$check] = $sukValue[$suk]['level_price'][$check] ?? $v;
  574. }
  575. $sukValue[$suk]['level_price'] = $level_price_real;
  576. }
  577. foreach ($level_list as $v) {
  578. $sukValue[$suk]['priceList'][] = [
  579. 'id' => $v['id'],
  580. 'name' => $v['name'],
  581. 'value' => $sukValue[$suk]['level_price']['level_' . $v['id']],
  582. 'k' => $key,
  583. ];
  584. }
  585. }
  586. } else {
  587. $sukValue[$suk]['pic'] = '';
  588. $sukValue[$suk]['price'] = 0;
  589. $sukValue[$suk]['cost'] = 0;
  590. $sukValue[$suk]['ot_price'] = 0;
  591. $sukValue[$suk]['stock'] = 0;
  592. $sukValue[$suk]['bar_code'] = '';
  593. $sukValue[$suk]['weight'] = 0;
  594. $sukValue[$suk]['volume'] = 0;
  595. $sukValue[$suk]['brokerage'] = 0;
  596. $sukValue[$suk]['brokerage_two'] = 0;
  597. $sukValue[$suk]['level_price'] = $level_price;
  598. foreach ($level_list as $v) {
  599. $sukValue[$suk]['priceList'][] = [
  600. 'id' => $v['id'], 'name' => $v['name'],
  601. 'value' => $sukValue[$suk]['level_price']['level_' . $v['id']],
  602. 'k' => $key,
  603. ];
  604. }
  605. }
  606. if ($types) { //编辑商品时,将没有规格的数据不生成默认值
  607. foreach (array_keys($detail) as $k => $title) {
  608. $header[$k]['title'] = $title;
  609. $header[$k]['align'] = 'center';
  610. $header[$k]['minWidth'] = 130;
  611. }
  612. foreach (array_values($detail) as $k => $v) {
  613. $valueNew[$count]['value' . ($k + 1)] = $v;
  614. $header[$k]['key'] = 'value' . ($k + 1);
  615. }
  616. $valueNew[$count]['detail'] = $detail;
  617. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  618. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  619. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  620. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  621. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  622. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  623. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  624. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  625. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  626. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  627. $valueNew[$count]['level_price'] = $sukValue[$suk]['level_price'] ?? [];
  628. $valueNew[$count]['priceList'] = $sukValue[$suk]['priceList'] ?? [];
  629. $count++;
  630. }
  631. }
  632. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  633. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
  634. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
  635. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
  636. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
  637. $header[] = ['title' => '产品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
  638. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 140];
  639. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 140];
  640. $header[] = ['title' => '等级价格', 'slot' => 'level_price', 'align' => 'center', 'minWidth' => 200];
  641. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  642. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  643. return Json::successful($info);
  644. }
  645. public function set_attr($id)
  646. {
  647. if (!$id) return $this->failed('产品不存在!');
  648. list($attr, $detail) = Util::postMore([
  649. ['items', []],
  650. ['attrs', []]
  651. ], null, true);
  652. $res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  653. if ($res)
  654. return $this->successful('编辑属性成功!');
  655. else
  656. return $this->failed(StoreProductAttr::getErrorInfo());
  657. }
  658. public function clear_attr($id)
  659. {
  660. if (!$id) return $this->failed('产品不存在!');
  661. if (false !== StoreProductAttr::clearProductAttr($id) && false !== StoreProductAttrResult::clearResult($id))
  662. return $this->successful('清空产品属性成功!');
  663. else
  664. return $this->failed(StoreProductAttr::getErrorInfo('清空产品属性失败!'));
  665. }
  666. /**
  667. * 删除指定资源
  668. *
  669. * @param int $id
  670. * @return \think\Response
  671. */
  672. public function delete($id)
  673. {
  674. if (!$id) return $this->failed('数据不存在');
  675. if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  676. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  677. $data['is_del'] = 0;
  678. if (!ProductModel::edit($data, $id))
  679. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  680. else
  681. return Json::successful('成功恢复产品!');
  682. } else {
  683. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  684. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  685. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  686. if ($res1 || $res2 || $res3) {
  687. return Json::fail(ProductModel::getErrorInfo('该商品已参加活动,无法删除!'));
  688. } else {
  689. $data['is_del'] = 1;
  690. if (!ProductModel::edit($data, $id))
  691. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  692. else
  693. return Json::successful('成功移到回收站!');
  694. }
  695. }
  696. }
  697. /**
  698. * 点赞
  699. * @param $id
  700. * @return mixed|\think\response\Json|void
  701. */
  702. public function collect($id)
  703. {
  704. if (!$id) return $this->failed('数据不存在');
  705. $product = ProductModel::get($id);
  706. if (!$product) return Json::fail('数据不存在!');
  707. $this->assign(StoreProductRelation::getCollect($id));
  708. return $this->fetch();
  709. }
  710. /**
  711. * 收藏
  712. * @param $id
  713. * @return mixed|\think\response\Json|void
  714. */
  715. public function like($id)
  716. {
  717. if (!$id) return $this->failed('数据不存在');
  718. $product = ProductModel::get($id);
  719. if (!$product) return Json::fail('数据不存在!');
  720. $this->assign(StoreProductRelation::getLike($id));
  721. return $this->fetch();
  722. }
  723. /**
  724. * 修改产品价格
  725. */
  726. public function edit_product_price()
  727. {
  728. $data = Util::postMore([
  729. ['id', 0],
  730. ['price', 0],
  731. ]);
  732. if (!$data['id']) return Json::fail('参数错误');
  733. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  734. if ($res) return Json::successful('修改成功');
  735. else return Json::fail('修改失败');
  736. }
  737. /**
  738. * 修改产品库存
  739. *
  740. */
  741. public function edit_product_stock()
  742. {
  743. $data = Util::postMore([
  744. ['id', 0],
  745. ['stock', 0],
  746. ]);
  747. if (!$data['id']) return Json::fail('参数错误');
  748. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  749. if ($res) return Json::successful('修改成功');
  750. else return Json::fail('修改失败');
  751. }
  752. /**
  753. * 检测商品是否开活动
  754. * @param $id
  755. * @throws \think\db\exception\DataNotFoundException
  756. * @throws \think\db\exception\DbException
  757. * @throws \think\db\exception\ModelNotFoundException
  758. */
  759. public function check_activity($id)
  760. {
  761. if ($id != 0) {
  762. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  763. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  764. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  765. if ($res1 || $res2 || $res3) {
  766. return Json::successful('该商品有活动开启,无法删除属性');
  767. } else {
  768. return Json::fail('删除成功');
  769. }
  770. } else {
  771. return Json::fail('没有参数ID');
  772. }
  773. }
  774. }