StoreProduct.php 31 KB

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