StoreProduct.php 32 KB

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