StoreProduct.php 33 KB

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