StoreProduct.php 30 KB

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