StoreProduct.php 33 KB

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