StoreProduct.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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. ProductModel::edit($data, $id);
  328. $description = $data['description'];
  329. unset($data['description']);
  330. StoreDescription::saveDescription($description, $id);
  331. StoreProductCate::where('product_id', $id)->delete();
  332. $cateData = [];
  333. foreach ($cate_id as $cid) {
  334. $cateData[] = ['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()];
  335. }
  336. StoreProductCate::insertAll($cateData);
  337. if ($data['spec_type'] == 0) {
  338. $attr = [
  339. [
  340. 'value' => '规格',
  341. 'detailValue' => '',
  342. 'attrHidden' => '',
  343. 'detail' => ['默认']
  344. ]
  345. ];
  346. $detail[0]['value1'] = '规格';
  347. $detail[0]['detail'] = ['规格' => '默认'];
  348. } else {
  349. if (in_array($cate_id[0], get_luckies())) {
  350. if (!array_search($lucky_spec_name, array_column($attr, 'value'))) {
  351. $attr[] = [
  352. 'value' => $lucky_spec_name,
  353. 'detailValue' => '',
  354. 'attrHidden' => '',
  355. 'detail' => $lucky_spec_items,
  356. ];
  357. }
  358. }
  359. }
  360. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  361. if ($attr_res) {
  362. ProductModel::commitTrans();
  363. event('StoreEditProduct', ['productId' => $id,]);
  364. return Json::success('修改成功!');
  365. } else {
  366. ProductModel::rollbackTrans();
  367. return Json::fail(StoreProductAttr::getErrorInfo());
  368. }
  369. } else {
  370. $data['add_time'] = time();
  371. $data['code_path'] = '';
  372. $res = ProductModel::create($data);
  373. $description = $data['description'];
  374. StoreDescription::saveDescription($description, $res['id']);
  375. $cateData = [];
  376. foreach ($cate_id as $cid) {
  377. $cateData[] = ['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()];
  378. }
  379. StoreProductCate::insertAll($cateData);
  380. if ($data['spec_type'] == 0) {
  381. $attr = [
  382. [
  383. 'value' => '规格',
  384. 'detailValue' => '',
  385. 'attrHidden' => '',
  386. 'detail' => ['默认']
  387. ]
  388. ];
  389. $detail[0]['value1'] = '规格';
  390. $detail[0]['detail'] = ['规格' => '默认'];
  391. } else {
  392. if (in_array($cate_id[0], get_luckies())) {
  393. $attr[] = [
  394. 'value' => $lucky_spec_name,
  395. 'detailValue' => '',
  396. 'attrHidden' => '',
  397. 'detail' => $lucky_spec_items,
  398. ];
  399. }
  400. }
  401. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $res['id']);
  402. if ($attr_res) {
  403. ProductModel::commitTrans();
  404. event('AdminAddStoreProduct', ['product' => $res, 'admin' => $this->adminInfo]);
  405. event('StoreProductCreated', ['product' => $res,]);
  406. return Json::success('添加产品成功!');
  407. } else {
  408. ProductModel::rollbackTrans();
  409. return Json::fail(StoreProductAttr::getErrorInfo());
  410. }
  411. }
  412. }
  413. public function edit_content($id)
  414. {
  415. if (!$id) return $this->failed('数据不存在');
  416. $product = ProductModel::get($id);
  417. if (!$product) return Json::fail('数据不存在!');
  418. $this->assign([
  419. 'content' => $product->description,
  420. 'field' => 'description',
  421. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  422. ]);
  423. return $this->fetch('public/edit_content');
  424. }
  425. /**
  426. * 显示编辑资源表单页.
  427. *
  428. * @param int $id
  429. * @return \think\Response
  430. */
  431. public function edit($id)
  432. {
  433. if (!$id) return $this->failed('数据不存在');
  434. $product = ProductModel::get($id);
  435. if (!$product) return Json::fail('数据不存在!');
  436. $field = [
  437. Form::select('cate_id', '产品分类', explode(',', $product->getData('cate_id')))->setOptions(function () {
  438. $list = CategoryModel::getTierList(null, 1);
  439. $menus = [];
  440. foreach ($list as $menu) {
  441. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0]; //,'disabled'=>$menu['pid']== 0];
  442. }
  443. return $menus;
  444. })->filterable(1)->multiple(1),
  445. Form::input('store_name', '产品名称', $product->getData('store_name')),
  446. Form::input('store_info', '产品简介', $product->getData('store_info'))->type('textarea'),
  447. Form::input('keyword', '产品关键字', $product->getData('keyword'))->placeholder('多个用英文状态下的逗号隔开'),
  448. Form::input('unit_name', '产品单位', $product->getData('unit_name'))->col(12),
  449. Form::input('bar_code', '产品条码', $product->getData('bar_code'))->col(12),
  450. Form::frameImageOne('image', '产品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'),
  451. 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'),
  452. Form::number('price', '产品售价', $product->getData('price'))->min(0)->col(8),
  453. Form::number('ot_price', '产品市场价', $product->getData('ot_price'))->min(0)->col(8),
  454. Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(8),
  455. Form::number('postage', '邮费', $product->getData('postage'))->min(0)->col(8),
  456. Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(8)->readonly(1),
  457. Form::number('ficti', '虚拟销量', $product->getData('ficti'))->min(0)->precision(0)->col(8),
  458. Form::number('stock', '库存', ProductModel::getStock($id) > 0 ? ProductModel::getStock($id) : $product->getData('stock'))->min(0)->precision(0)->col(8),
  459. Form::number('cost', '产品成本价', $product->getData('cost'))->min(0)->col(8),
  460. Form::number('sort', '排序', $product->getData('sort'))->col(8),
  461. Form::radio('is_show', '产品状态', $product->getData('is_show'))->options([['label' => '上架', 'value' => 1], ['label' => '下架', 'value' => 0]])->col(8),
  462. Form::radio('is_hot', '热卖单品', $product->getData('is_hot'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  463. Form::radio('is_benefit', '促销单品', $product->getData('is_benefit'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  464. Form::radio('is_best', '精品推荐', $product->getData('is_best'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  465. Form::radio('is_new', '首发新品', $product->getData('is_new'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  466. Form::radio('is_postage', '是否包邮', $product->getData('is_postage'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  467. Form::radio('is_good', '是否优品推荐', $product->getData('is_good'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  468. ];
  469. $form = Form::make_post_form('编辑产品', $field, Url::buildUrl('update', array('id' => $id)), 2);
  470. $this->assign(compact('form'));
  471. return $this->fetch('public/form-builder');
  472. }
  473. /**
  474. * 保存更新的资源
  475. *
  476. * @param $id
  477. */
  478. public function update($id)
  479. {
  480. $data = Util::postMore([
  481. ['cate_id', []],
  482. 'store_name',
  483. 'store_info',
  484. 'keyword',
  485. 'bar_code',
  486. ['unit_name', '件'],
  487. ['image', []],
  488. ['slider_image', []],
  489. ['postage', 0],
  490. ['ot_price', 0],
  491. ['price', 0],
  492. ['sort', 0],
  493. ['stock', 0],
  494. ['temp_id', 0],
  495. ['provider_id', 0],
  496. ['ficti', 100],
  497. ['give_integral', 0],
  498. ['is_show', 0],
  499. ['cost', 0],
  500. ['is_hot', 0],
  501. ['is_benefit', 0],
  502. ['is_best', 0],
  503. ['is_new', 0],
  504. ['mer_use', 0],
  505. ['is_postage', 0],
  506. ['is_good', 0],
  507. ]);
  508. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  509. $cate_id = $data['cate_id'];
  510. $data['cate_id'] = implode(',', $data['cate_id']);
  511. if (!$data['store_name']) return Json::fail('请输入产品名称');
  512. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  513. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  514. // if(count($data['slider_image'])>8) return Json::fail('轮播图最多5张图');
  515. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入产品售价');
  516. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入产品市场价');
  517. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入库存');
  518. $data['image'] = $data['image'][0];
  519. $data['slider_image'] = json_encode($data['slider_image']);
  520. ProductModel::edit($data, $id);
  521. StoreProductCate::where('product_id', $id)->delete();
  522. foreach ($cate_id as $cid) {
  523. StoreProductCate::insert(['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()]);
  524. }
  525. return Json::successful('修改成功!');
  526. }
  527. public function attr($id)
  528. {
  529. if (!$id) return $this->failed('数据不存在!');
  530. // $result = StoreProductAttrResult::getResult($id);
  531. $result = StoreProductAttrValue::getStoreProductAttrResult($id);
  532. $image = ProductModel::where('id', $id)->value('image');
  533. $this->assign(compact('id', 'result', 'image'));
  534. return $this->fetch();
  535. }
  536. /**
  537. * 生成属性
  538. * @param int $id
  539. */
  540. public function is_format_attr($id = 0, $cate_id = 0, $type = 0)
  541. {
  542. $data = Util::postMore([
  543. ['attrs', []],
  544. ['items', []]
  545. ]);
  546. if (is_array($cate_id)) {
  547. return Json::fail('产品只能属于一个分类');
  548. }
  549. $lucky_spec_name = Config::get('activity.lucky_spec_name');
  550. $lucky_spec_items = Config::get('activity.lucky_spec_items');
  551. if (in_array($cate_id, get_luckies())) {
  552. if (!array_search($lucky_spec_name, array_column($data['attrs'], 'value'))) {
  553. $data['attrs'][] = [
  554. 'value' => $lucky_spec_name,
  555. 'detailValue' => '',
  556. 'attrHidden' => '',
  557. 'detail' => $lucky_spec_items,
  558. ];
  559. }
  560. }
  561. $attr = $data['attrs'];
  562. $value = attr_format($attr)[1];
  563. $valueNew = [];
  564. $count = 0;
  565. foreach ($value as $key => $item) {
  566. $detail = $item['detail'];
  567. sort($item['detail'], SORT_STRING);
  568. $suk = implode(',', $item['detail']);
  569. $types = 1;
  570. if ($id) {
  571. $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');
  572. if (!count($sukValue)) {
  573. if ($type == 0) $types = 0; //编辑商品时,将没有规格的数据不生成默认值
  574. $sukValue[$suk]['pic'] = '';
  575. $sukValue[$suk]['price'] = 0;
  576. $sukValue[$suk]['cost'] = 0;
  577. $sukValue[$suk]['ot_price'] = 0;
  578. $sukValue[$suk]['stock'] = 0;
  579. $sukValue[$suk]['bar_code'] = '';
  580. $sukValue[$suk]['weight'] = 0;
  581. $sukValue[$suk]['volume'] = 0;
  582. $sukValue[$suk]['brokerage'] = 0;
  583. $sukValue[$suk]['brokerage_two'] = 0;
  584. }
  585. } else {
  586. $sukValue[$suk]['pic'] = '';
  587. $sukValue[$suk]['price'] = 0;
  588. $sukValue[$suk]['cost'] = 0;
  589. $sukValue[$suk]['ot_price'] = 0;
  590. $sukValue[$suk]['stock'] = 0;
  591. $sukValue[$suk]['bar_code'] = '';
  592. $sukValue[$suk]['weight'] = 0;
  593. $sukValue[$suk]['volume'] = 0;
  594. $sukValue[$suk]['brokerage'] = 0;
  595. $sukValue[$suk]['brokerage_two'] = 0;
  596. }
  597. if ($types) { //编辑商品时,将没有规格的数据不生成默认值
  598. foreach (array_keys($detail) as $k => $title) {
  599. $header[$k]['title'] = $title;
  600. $header[$k]['align'] = 'center';
  601. $header[$k]['minWidth'] = 130;
  602. }
  603. foreach (array_values($detail) as $k => $v) {
  604. $valueNew[$count]['value' . ($k + 1)] = $v;
  605. $header[$k]['key'] = 'value' . ($k + 1);
  606. }
  607. $valueNew[$count]['detail'] = $detail;
  608. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  609. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  610. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  611. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  612. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  613. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  614. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  615. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  616. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  617. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  618. $count++;
  619. }
  620. }
  621. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  622. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
  623. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
  624. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
  625. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
  626. $header[] = ['title' => '产品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
  627. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 140];
  628. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 140];
  629. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  630. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  631. return Json::successful($info);
  632. }
  633. public function set_attr($id)
  634. {
  635. if (!$id) return $this->failed('产品不存在!');
  636. list($attr, $detail) = Util::postMore([
  637. ['items', []],
  638. ['attrs', []]
  639. ], null, true);
  640. $res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  641. if ($res)
  642. return $this->successful('编辑属性成功!');
  643. else
  644. return $this->failed(StoreProductAttr::getErrorInfo());
  645. }
  646. public function clear_attr($id)
  647. {
  648. if (!$id) return $this->failed('产品不存在!');
  649. if (false !== StoreProductAttr::clearProductAttr($id) && false !== StoreProductAttrResult::clearResult($id))
  650. return $this->successful('清空产品属性成功!');
  651. else
  652. return $this->failed(StoreProductAttr::getErrorInfo('清空产品属性失败!'));
  653. }
  654. /**
  655. * 删除指定资源
  656. *
  657. * @param int $id
  658. * @return \think\Response
  659. */
  660. public function delete($id)
  661. {
  662. if (!$id) return $this->failed('数据不存在');
  663. if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  664. if (ProductModel::be(['id' => $id, 'is_del' => 1])) { // 恢復
  665. $data['is_del'] = 0;
  666. if (!ProductModel::edit($data, $id)) {
  667. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  668. } else {
  669. event('StoreProductOnOffShelf', ['is_show' => 1, 'ids' => [$id],]);
  670. return Json::successful('成功恢复产品!');
  671. }
  672. } else { // 刪除
  673. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  674. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  675. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  676. if ($res1 || $res2 || $res3) {
  677. return Json::fail(ProductModel::getErrorInfo('该商品已参加活动,无法删除!'));
  678. } else {
  679. $data['is_del'] = 1;
  680. if (!ProductModel::edit($data, $id)) {
  681. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  682. } else {
  683. event('StoreProductOnOffShelf', ['is_show' => 0, 'ids' => [$id],]);
  684. return Json::successful('成功移到回收站!');
  685. }
  686. }
  687. }
  688. }
  689. /**
  690. * 点赞
  691. * @param $id
  692. * @return mixed|\think\response\Json|void
  693. */
  694. public function collect($id)
  695. {
  696. if (!$id) return $this->failed('数据不存在');
  697. $product = ProductModel::get($id);
  698. if (!$product) return Json::fail('数据不存在!');
  699. $this->assign(StoreProductRelation::getCollect($id));
  700. return $this->fetch();
  701. }
  702. /**
  703. * 收藏
  704. * @param $id
  705. * @return mixed|\think\response\Json|void
  706. */
  707. public function like($id)
  708. {
  709. if (!$id) return $this->failed('数据不存在');
  710. $product = ProductModel::get($id);
  711. if (!$product) return Json::fail('数据不存在!');
  712. $this->assign(StoreProductRelation::getLike($id));
  713. return $this->fetch();
  714. }
  715. /**
  716. * 修改产品价格
  717. */
  718. public function edit_product_price()
  719. {
  720. $data = Util::postMore([
  721. ['id', 0],
  722. ['price', 0],
  723. ]);
  724. if (!$data['id']) return Json::fail('参数错误');
  725. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  726. if ($res) return Json::successful('修改成功');
  727. else return Json::fail('修改失败');
  728. }
  729. /**
  730. * 修改产品库存
  731. *
  732. */
  733. public function edit_product_stock()
  734. {
  735. $data = Util::postMore([
  736. ['id', 0],
  737. ['stock', 0],
  738. ]);
  739. if (!$data['id']) return Json::fail('参数错误');
  740. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  741. if ($res) return Json::successful('修改成功');
  742. else return Json::fail('修改失败');
  743. }
  744. /**
  745. * 检测商品是否开活动
  746. * @param $id
  747. * @throws \think\db\exception\DataNotFoundException
  748. * @throws \think\db\exception\DbException
  749. * @throws \think\db\exception\ModelNotFoundException
  750. */
  751. public function check_activity($id)
  752. {
  753. if ($id != 0) {
  754. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  755. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  756. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  757. if ($res1 || $res2 || $res3) {
  758. return Json::successful('该商品有活动开启,无法删除属性');
  759. } else {
  760. return Json::fail('删除成功');
  761. }
  762. } else {
  763. return Json::fail('没有参数ID');
  764. }
  765. }
  766. }