StoreProduct.php 29 KB

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