StoreProduct.php 36 KB

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