StoreProduct.php 31 KB

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