StoreProduct.php 36 KB

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