StoreProduct.php 32 KB

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