StoreProduct.php 30 KB

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