StoreProduct.php 31 KB

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