StoreProduct.php 32 KB

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