StoreProduct.php 31 KB

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