StoreProduct.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  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\SystemUserLevel;
  6. use app\models\system\Tree;
  7. use app\admin\model\store\{
  8. StoreDescription,
  9. StoreProductAttrValue,
  10. StoreProductAttr,
  11. StoreProductAttrResult,
  12. StoreProductCate,
  13. StoreProductRelation,
  14. StoreCategory as CategoryModel,
  15. StoreProduct as ProductModel
  16. };
  17. use app\admin\model\ump\StoreBargain;
  18. use app\admin\model\ump\StoreCombination;
  19. use app\admin\model\ump\StoreSeckill;
  20. use crmeb\services\{
  21. JsonService, UtilService as Util, JsonService as Json, FormBuilder as Form
  22. };
  23. use crmeb\traits\CurdControllerTrait;
  24. use think\facade\Route as Url;
  25. use app\admin\model\system\{SystemAdmin, SystemAttachment, ShippingTemplates, SystemStore};
  26. /**
  27. * 产品管理
  28. * Class StoreProduct
  29. * @package app\admin\controller\store
  30. */
  31. class StoreProduct extends AuthController
  32. {
  33. use CurdControllerTrait;
  34. protected $bindModel = ProductModel::class;
  35. /**
  36. * 显示资源列表
  37. *
  38. * @return \think\Response
  39. */
  40. public function index()
  41. {
  42. $type = $this->request->param('type');
  43. $admin_id=$this->adminId;
  44. $mer_id = SystemAdmin::where('id',$admin_id)->value('mer_id');
  45. //获取分类
  46. $this->assign('cate', CategoryModel::getTierList(null, 1));
  47. if ($mer_id>0){
  48. //出售中产品
  49. $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->where('mer_id', $mer_id)->count();
  50. //待上架产品
  51. $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->where('mer_id', $mer_id)->count();
  52. //仓库中产品
  53. $warehouse = ProductModel::where('is_del', 0)->where('mer_id', $mer_id)->count();
  54. //已经售馨产品
  55. $outofstock = ProductModel::getModelObject()->where(ProductModel::setData(4))->where('pav.type', 0)->where('mer_id',$mer_id)->count('DISTINCT id');
  56. //警戒库存
  57. $store_stock = sys_config('store_stock');
  58. if ($store_stock < 0) $store_stock = 2;
  59. $policeforce = ProductModel::getModelObject()->where(ProductModel::setData(5))->where('p.stock', '<=', $store_stock)->where('pav.type', 0)->where('mer_id',$mer_id)->count('DISTINCT id');
  60. //回收站
  61. $recycle = ProductModel::where('is_del', 1)->where('mer_id',$mer_id)->count();
  62. }else{
  63. //出售中产品
  64. $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->count();
  65. //待上架产品
  66. $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->count();
  67. //仓库中产品
  68. $warehouse = ProductModel::where('is_del', 0)->count();
  69. //已经售馨产品
  70. $outofstock = ProductModel::getModelObject()->where(ProductModel::setData(4))->where('pav.type', 0)->count('DISTINCT id');
  71. //警戒库存
  72. $store_stock = sys_config('store_stock');
  73. if ($store_stock < 0) $store_stock = 2;
  74. $policeforce = ProductModel::getModelObject()->where(ProductModel::setData(5))->where('p.stock', '<=', $store_stock)->where('pav.type', 0)->count('DISTINCT id');
  75. //回收站
  76. $recycle = ProductModel::where('is_del', 1)->count();
  77. }
  78. if ($type == null) $type = 1;
  79. $adminInfo = $this->adminInfo;
  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. if ($productInfo['spec_type'] == 1) {
  205. $result = StoreProductAttrResult::getResult($id);
  206. foreach ($result['value'] as $k => $v) {
  207. $num = 1;
  208. foreach ($v['detail'] as $dv) {
  209. $result['value'][$k]['value' . $num] = $dv;
  210. $num++;
  211. }
  212. }
  213. $productInfo['items'] = $result['attr'] ?? [];
  214. $productInfo['attrs'] = $result['value'] ?? [];
  215. $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];
  216. } else {
  217. $result = StoreProductAttrResult::getResult($id);
  218. $single = isset($result['value'][0]) ? $result['value'][0] : [];
  219. $productInfo['items'] = [];
  220. $productInfo['attrs'] = [];
  221. $productInfo['attr'] = [
  222. 'pic' => $single['pic'] ?? '',
  223. 'price' => $single['price'] ?? 0,
  224. 'integral' => $single['integral'] ?? 0,
  225. 'cost' => $single['cost'] ?? 0,
  226. 'ot_price' => $single['ot_price'] ?? 0,
  227. 'stock' => $single['stock'] ?? 0,
  228. 'bar_code' => $single['bar_code'] ?? '',
  229. 'weight' => $single['weight'] ?? 0,
  230. 'volume' => $single['volume'] ?? 0,
  231. 'brokerage' => $single['brokerage'] ?? 0,
  232. 'brokerage_two' => $single['brokerage_two'] ?? 0,
  233. ];
  234. }
  235. if ($productInfo['activity']) {
  236. $activity = explode(',', $productInfo['activity']);
  237. foreach ($activity as $k => $v) {
  238. if ($v == 1) {
  239. $activity[$k] = '秒杀';
  240. } elseif ($v == 2) {
  241. $activity[$k] = '砍价';
  242. } elseif ($v == 3) {
  243. $activity[$k] = '拼团';
  244. }
  245. }
  246. $productInfo['activity'] = $activity;
  247. } else {
  248. $productInfo['activity'] = ['秒杀', '砍价', '拼团'];
  249. }
  250. $data['productInfo'] = $productInfo;
  251. $level = SystemUserLevel::where('is_del', 0)->order('grade asc')->field(['id', 'grade', 'name as level_name'])->select()->toArray();
  252. $level_radio = [];
  253. $num = 0;
  254. foreach ($level as $k => $v){
  255. $num = $num+1;
  256. $level_radio_lst = StoreProductLevel::where('product_id', $id)->where('level_id',$v['id'])->where('type', 1)->find();
  257. if (!empty($level_radio_lst)){
  258. $level_radio_lst = $level_radio_lst ->toArray();
  259. $level_radio[] = ['grade'=>$num,'level_name' => $v['level_name'], 'ratio' => $level_radio_lst['rebate_ratio']];
  260. }else{
  261. $level_radio[] = ['grade'=>$num,'level_name' => $v['level_name'], 'ratio' => 0];
  262. }
  263. }
  264. // $level_radio = [
  265. // ['grade'=>1,'level_name' => '青源客', 'ratio' => 1.5],
  266. // ['grade'=>2,'level_name' => '青润使', 'ratio' => 2.0],
  267. // ['grade'=>3,'level_name' => '青金团', 'ratio' => 3.0],
  268. // ['grade'=>4,'level_name' => '青玉团', 'ratio' => 4.0],
  269. // ['grade'=>5,'level_name' => '青谷团', 'ratio' => 5.0]
  270. // ];
  271. $data['productInfo']['level_radio'] = $level_radio;
  272. }
  273. // $data['level_set'] = 1;
  274. return JsonService::successful($data);
  275. }
  276. /**
  277. * 保存新建的资源
  278. *
  279. *
  280. */
  281. public function save($id)
  282. {
  283. $data = Util::postMore([
  284. ['cate_id', []],
  285. 'store_name',
  286. 'store_info',
  287. 'keyword',
  288. ['unit_name', '件'],
  289. ['image', []],
  290. ['slider_image', []],
  291. ['postage', 0],
  292. ['is_sub', 0],
  293. ['sort', 0],
  294. ['sales', 0],
  295. ['ficti', 100],
  296. ['rebate_ratio', 0],
  297. ['give_integral', 0],
  298. ['is_show', 0],
  299. ['temp_id', 0],
  300. ['is_hot', 0],
  301. ['is_benefit', 0],
  302. ['is_best', 0],
  303. ['is_new', 0],
  304. ['is_wholesale', 0],
  305. ['mer_use', 0],
  306. ['is_postage', 0],
  307. ['is_good', 0],
  308. // ['description', ''],
  309. ['spec_type', 0],
  310. ['video_link', ''],
  311. ['items', []],
  312. ['attrs', []],
  313. ['activity', []],
  314. ['store_type', 1],
  315. ['mer_id',0],
  316. // ['level_set', 0],
  317. ['level_radio', []],
  318. ], $this->request, false, true);
  319. // $admin_id=$this->adminId;
  320. // $mer_id = SystemAdmin::where('id',$admin_id)->value('mer_id');
  321. // if ($data['mer_id']>0){
  322. // $data['mer_id'] = $mer_id;
  323. $data['is_pick']=SystemStore::where('id',$data['mer_id'])->value('is_pick');
  324. // var_dump($data);die();
  325. // }
  326. // $level_set = $data['level_set'];
  327. $level_radio = $data['level_radio'];
  328. // unset($data['level_set']);
  329. unset($data['level_radio']);
  330. foreach ($data['activity'] as $k => $v) {
  331. if ($v == '秒杀') {
  332. $data['activity'][$k] = 1;
  333. } elseif ($v == '砍价') {
  334. $data['activity'][$k] = 2;
  335. } else {
  336. $data['activity'][$k] = 3;
  337. }
  338. }
  339. $data['description'] = $this->request->post('description', '');
  340. $data['activity'] = implode(',', $data['activity']);
  341. $detail = $data['attrs'];
  342. $data['price'] = min(array_column($detail, 'price'));
  343. $data['min_integral'] = min(array_column($detail, 'integral'));
  344. $data['max_integral'] = max(array_column($detail, 'integral'));
  345. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  346. $data['cost'] = min(array_column($detail, 'cost'));
  347. $attr = $data['items'];
  348. unset($data['items'], $data['video'], $data['attrs']);
  349. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  350. if (!in_array($data['store_type'], [1, 2])) return Json::fail('请选择正确的商品类型');
  351. $cate_id = $data['cate_id'];
  352. $data['cate_id'] = implode(',', $data['cate_id']);
  353. if (!$data['store_name']) return Json::fail('请输入产品名称');
  354. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  355. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  356. if ($data['rebate_ratio']>10) return Json::fail('返利比例不能大于10%');
  357. $data['image'] = $data['image'][0];
  358. $data['slider_image'] = json_encode($data['slider_image']);
  359. $data['stock'] = array_sum(array_column($detail, 'stock'));
  360. ProductModel::beginTrans();
  361. foreach ($detail as &$item) {
  362. if (($item['brokerage'] + $item['brokerage_two']) > $item['price']) {
  363. return Json::fail('一二级返佣相加不能大于商品售价');
  364. }
  365. }
  366. if ($id) {
  367. unset($data['sales']);
  368. ProductModel::edit($data, $id);
  369. $description = $data['description'];
  370. unset($data['description']);
  371. StoreDescription::saveDescription($description, $id);
  372. StoreProductCate::where('product_id', $id)->delete();
  373. $cateData = [];
  374. foreach ($cate_id as $cid) {
  375. $cateData[] = ['product_id' => $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, $id);
  391. // if ($level_set>0){
  392. StoreProductLevel::where('product_id',$id)->where('type',1)->delete();
  393. foreach ($level_radio as $k => $v) {
  394. $level_id = SystemUserLevel::where('grade',$v['grade'])->value('id');
  395. StoreProductLevel::create(['product_id' => $id, 'type' => 1, 'level_id' => $level_id, 'rebate_ratio' => $v['ratio'],'create_time'=>time()]);
  396. }
  397. // 未完待续
  398. // }
  399. if ($attr_res) {
  400. ProductModel::commitTrans();
  401. return Json::success('修改成功!');
  402. } else {
  403. ProductModel::rollbackTrans();
  404. return Json::fail(StoreProductAttr::getErrorInfo());
  405. }
  406. } else {
  407. $data['add_time'] = time();
  408. $data['code_path'] = '';
  409. $res = ProductModel::create($data);
  410. $id = $res['id'];
  411. $description = $data['description'];
  412. StoreDescription::saveDescription($description, $res['id']);
  413. $cateData = [];
  414. foreach ($cate_id as $cid) {
  415. $cateData[] = ['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()];
  416. }
  417. StoreProductCate::insertAll($cateData);
  418. if ($data['spec_type'] == 0) {
  419. $attr = [
  420. [
  421. 'value' => '规格',
  422. 'detailValue' => '',
  423. 'attrHidden' => '',
  424. 'detail' => ['默认']
  425. ]
  426. ];
  427. $detail[0]['value1'] = '规格';
  428. $detail[0]['detail'] = ['规格' => '默认'];
  429. }
  430. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $res['id']);
  431. StoreProductLevel::where('product_id',$id)->where('type',1)->delete();
  432. foreach ($level_radio as $k => $v) {
  433. $level_id = SystemUserLevel::where('grade',$v['grade'])->value('id');
  434. StoreProductLevel::create(['product_id' => $id, 'type' => 1, 'level_id' => $level_id, 'ratio' => $v['ratio'],'create_time'=>time()]);
  435. }
  436. if ($attr_res) {
  437. ProductModel::commitTrans();
  438. return Json::success('添加产品成功!');
  439. } else {
  440. ProductModel::rollbackTrans();
  441. return Json::fail(StoreProductAttr::getErrorInfo());
  442. }
  443. }
  444. }
  445. public function edit_content($id)
  446. {
  447. if (!$id) return $this->failed('数据不存在');
  448. $product = ProductModel::get($id);
  449. if (!$product) return Json::fail('数据不存在!');
  450. $this->assign([
  451. 'content' => $product->description,
  452. 'field' => 'description',
  453. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  454. ]);
  455. return $this->fetch('public/edit_content');
  456. }
  457. /**
  458. * 显示编辑资源表单页.
  459. *
  460. * @param int $id
  461. * @return \think\Response
  462. */
  463. public function edit($id)
  464. {
  465. if (!$id) return $this->failed('数据不存在');
  466. $product = ProductModel::get($id);
  467. if (!$product) return Json::fail('数据不存在!');
  468. $field = [
  469. Form::select('cate_id', '产品分类', explode(',', $product->getData('cate_id')))->setOptions(function () {
  470. $list = CategoryModel::getTierList(null, 1);
  471. $menus = [];
  472. foreach ($list as $menu) {
  473. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0];//,'disabled'=>$menu['pid']== 0];
  474. }
  475. return $menus;
  476. })->filterable(1)->multiple(1),
  477. Form::input('store_name', '产品名称', $product->getData('store_name')),
  478. Form::input('store_info', '产品简介', $product->getData('store_info'))->type('textarea'),
  479. Form::input('keyword', '产品关键字', $product->getData('keyword'))->placeholder('多个用英文状态下的逗号隔开'),
  480. Form::input('unit_name', '产品单位', $product->getData('unit_name'))->col(12),
  481. Form::input('bar_code', '产品条码', $product->getData('bar_code'))->col(12),
  482. Form::frameImageOne('image', '产品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'),
  483. 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'),
  484. Form::number('price', '产品售价', $product->getData('price'))->min(0)->col(8),
  485. Form::number('ot_price', '产品市场价', $product->getData('ot_price'))->min(0)->col(8),
  486. Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(8),
  487. Form::number('postage', '邮费', $product->getData('postage'))->min(0)->col(8),
  488. Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(8)->readonly(1),
  489. Form::number('ficti', '虚拟销量', $product->getData('ficti'))->min(0)->precision(0)->col(8),
  490. Form::number('rebate_ratio', '返利积分比例(%)', $product->getData('rebate_ratio'))->min(0)->precision(0)->col(8),
  491. Form::number('stock', '库存', ProductModel::getStock($id) > 0 ? ProductModel::getStock($id) : $product->getData('stock'))->min(0)->precision(0)->col(8),
  492. Form::number('cost', '产品成本价', $product->getData('cost'))->min(0)->col(8),
  493. Form::number('sort', '排序', $product->getData('sort'))->col(8),
  494. Form::radio('is_show', '产品状态', $product->getData('is_show'))->options([['label' => '上架', 'value' => 1], ['label' => '下架', 'value' => 0]])->col(8),
  495. Form::radio('is_hot', '热卖单品', $product->getData('is_hot'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  496. Form::radio('is_benefit', '促销单品', $product->getData('is_benefit'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  497. Form::radio('is_best', '精品推荐', $product->getData('is_best'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  498. Form::radio('is_new', '首发新品', $product->getData('is_new'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  499. Form::radio('is_postage', '是否包邮', $product->getData('is_postage'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  500. Form::radio('is_good', '是否优品推荐', $product->getData('is_good'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  501. Form::radio('is_wholesale', '是否批发商品', $product->getData('is_wholesale'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  502. ];
  503. $form = Form::make_post_form('编辑产品', $field, Url::buildUrl('update', array('id' => $id)), 2);
  504. $this->assign(compact('form'));
  505. return $this->fetch('public/form-builder');
  506. }
  507. /**
  508. * 保存更新的资源
  509. *
  510. * @param $id
  511. */
  512. public function update($id)
  513. {
  514. $data = Util::postMore([
  515. ['cate_id', []],
  516. 'store_name',
  517. 'store_info',
  518. 'keyword',
  519. 'bar_code',
  520. ['unit_name', '件'],
  521. ['image', []],
  522. ['slider_image', []],
  523. ['postage', 0],
  524. ['ot_price', 0],
  525. ['price', 0],
  526. ['sort', 0],
  527. ['stock', 0],
  528. ['temp_id', 0],
  529. ['ficti', 100],
  530. ['rebate_ratio', 0],
  531. ['give_integral', 0],
  532. ['is_show', 0],
  533. ['cost', 0],
  534. ['is_hot', 0],
  535. ['is_benefit', 0],
  536. ['is_best', 0],
  537. ['is_new', 0],
  538. ['mer_use', 0],
  539. ['is_postage', 0],
  540. ['is_good', 0],
  541. ['is_wholesale',0],
  542. ['mer_id',0]
  543. ]);
  544. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  545. $cate_id = $data['cate_id'];
  546. $data['cate_id'] = implode(',', $data['cate_id']);
  547. if (!$data['store_name']) return Json::fail('请输入产品名称');
  548. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  549. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  550. // if(count($data['slider_image'])>8) return Json::fail('轮播图最多5张图');
  551. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入产品售价');
  552. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入产品市场价');
  553. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入库存');
  554. if($data['rebate_ratio']>10) return Json::fail('返利比例不能大于10%');
  555. $data['image'] = $data['image'][0];
  556. $data['slider_image'] = json_encode($data['slider_image']);
  557. ProductModel::edit($data, $id);
  558. StoreProductCate::where('product_id', $id)->delete();
  559. foreach ($cate_id as $cid) {
  560. StoreProductCate::insert(['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()]);
  561. }
  562. return Json::successful('修改成功!');
  563. }
  564. public function attr($id)
  565. {
  566. if (!$id) return $this->failed('数据不存在!');
  567. // $result = StoreProductAttrResult::getResult($id);
  568. $result = StoreProductAttrValue::getStoreProductAttrResult($id);
  569. $image = ProductModel::where('id', $id)->value('image');
  570. $this->assign(compact('id', 'result', 'image'));
  571. return $this->fetch();
  572. }
  573. /**
  574. * 生成属性
  575. * @param int $id
  576. */
  577. public function is_format_attr($id = 0)
  578. {
  579. $data = Util::postMore([
  580. ['attrs', []],
  581. ['items', []]
  582. ]);
  583. $attr = $data['attrs'];
  584. $value = attr_format($attr)[1];
  585. $valueNew = [];
  586. $count = 0;
  587. foreach ($value as $key => $item) {
  588. $detail = $item['detail'];
  589. // sort($item['detail'], SORT_STRING);
  590. $suk = implode(',', $item['detail']);
  591. if ($id) {
  592. $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');
  593. if (!count($sukValue)) {
  594. $sukValue[$suk]['pic'] = '';
  595. $sukValue[$suk]['price'] = 0;
  596. $sukValue[$suk]['integral'] = 0;
  597. $sukValue[$suk]['cost'] = 0;
  598. $sukValue[$suk]['ot_price'] = 0;
  599. $sukValue[$suk]['stock'] = 0;
  600. $sukValue[$suk]['bar_code'] = '';
  601. $sukValue[$suk]['weight'] = 0;
  602. $sukValue[$suk]['volume'] = 0;
  603. $sukValue[$suk]['brokerage'] = 0;
  604. $sukValue[$suk]['brokerage_two'] = 0;
  605. }
  606. } else {
  607. $sukValue[$suk]['pic'] = '';
  608. $sukValue[$suk]['price'] = 0;
  609. $sukValue[$suk]['integral'] = 0;
  610. $sukValue[$suk]['cost'] = 0;
  611. $sukValue[$suk]['ot_price'] = 0;
  612. $sukValue[$suk]['stock'] = 0;
  613. $sukValue[$suk]['bar_code'] = '';
  614. $sukValue[$suk]['weight'] = 0;
  615. $sukValue[$suk]['volume'] = 0;
  616. $sukValue[$suk]['brokerage'] = 0;
  617. $sukValue[$suk]['brokerage_two'] = 0;
  618. }
  619. foreach (array_keys($detail) as $k => $title) {
  620. $header[$k]['title'] = $title;
  621. $header[$k]['align'] = 'center';
  622. $header[$k]['minWidth'] = 130;
  623. }
  624. foreach (array_values($detail) as $k => $v) {
  625. $valueNew[$count]['value' . ($k + 1)] = $v;
  626. $header[$k]['key'] = 'value' . ($k + 1);
  627. }
  628. $valueNew[$count]['detail'] = $detail;
  629. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  630. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  631. $valueNew[$count]['integral'] = $sukValue[$suk]['integral'] ? floatval($sukValue[$suk]['integral']) : 0;
  632. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  633. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  634. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  635. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  636. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  637. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  638. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  639. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  640. $count++;
  641. }
  642. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  643. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
  644. $header[] = ['title' => '积分', 'slot' => 'integral', 'align' => 'center', 'minWidth' => 120];
  645. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
  646. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
  647. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
  648. $header[] = ['title' => '产品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
  649. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 140];
  650. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 140];
  651. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  652. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  653. return Json::successful($info);
  654. }
  655. public function set_attr($id)
  656. {
  657. if (!$id) return $this->failed('产品不存在!');
  658. list($attr, $detail) = Util::postMore([
  659. ['items', []],
  660. ['attrs', []]
  661. ], null, true);
  662. $res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  663. if ($res)
  664. return $this->successful('编辑属性成功!');
  665. else
  666. return $this->failed(StoreProductAttr::getErrorInfo());
  667. }
  668. public function clear_attr($id)
  669. {
  670. if (!$id) return $this->failed('产品不存在!');
  671. if (false !== StoreProductAttr::clearProductAttr($id) && false !== StoreProductAttrResult::clearResult($id))
  672. return $this->successful('清空产品属性成功!');
  673. else
  674. return $this->failed(StoreProductAttr::getErrorInfo('清空产品属性失败!'));
  675. }
  676. /**
  677. * 删除指定资源
  678. *
  679. * @param int $id
  680. * @return \think\Response
  681. */
  682. public function delete($id)
  683. {
  684. if (!$id) return $this->failed('数据不存在');
  685. if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  686. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  687. $data['is_del'] = 0;
  688. if (!ProductModel::edit($data, $id))
  689. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  690. else
  691. return Json::successful('成功恢复产品!');
  692. } else {
  693. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  694. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  695. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  696. if ($res1 || $res2 || $res3) {
  697. return Json::fail(ProductModel::getErrorInfo('该商品已参加活动,无法删除!'));
  698. } else {
  699. $data['is_del'] = 1;
  700. if (!ProductModel::edit($data, $id))
  701. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  702. else
  703. return Json::successful('成功移到回收站!');
  704. }
  705. }
  706. }
  707. /**
  708. * 点赞
  709. * @param $id
  710. * @return mixed|\think\response\Json|void
  711. */
  712. public function collect($id)
  713. {
  714. if (!$id) return $this->failed('数据不存在');
  715. $product = ProductModel::get($id);
  716. if (!$product) return Json::fail('数据不存在!');
  717. $this->assign(StoreProductRelation::getCollect($id));
  718. return $this->fetch();
  719. }
  720. /**
  721. * 收藏
  722. * @param $id
  723. * @return mixed|\think\response\Json|void
  724. */
  725. public function like($id)
  726. {
  727. if (!$id) return $this->failed('数据不存在');
  728. $product = ProductModel::get($id);
  729. if (!$product) return Json::fail('数据不存在!');
  730. $this->assign(StoreProductRelation::getLike($id));
  731. return $this->fetch();
  732. }
  733. /**
  734. * 修改产品价格
  735. */
  736. public function edit_product_price()
  737. {
  738. $data = Util::postMore([
  739. ['id', 0],
  740. ['price', 0],
  741. ]);
  742. if (!$data['id']) return Json::fail('参数错误');
  743. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  744. if ($res) return Json::successful('修改成功');
  745. else return Json::fail('修改失败');
  746. }
  747. /**
  748. * 修改产品库存
  749. *
  750. */
  751. public function edit_product_stock()
  752. {
  753. $data = Util::postMore([
  754. ['id', 0],
  755. ['stock', 0],
  756. ]);
  757. if (!$data['id']) return Json::fail('参数错误');
  758. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  759. if ($res) return Json::successful('修改成功');
  760. else return Json::fail('修改失败');
  761. }
  762. /**
  763. * 检测商品是否开活动
  764. * @param $id
  765. * @throws \think\db\exception\DataNotFoundException
  766. * @throws \think\db\exception\DbException
  767. * @throws \think\db\exception\ModelNotFoundException
  768. */
  769. public function check_activity($id)
  770. {
  771. if ($id != 0) {
  772. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  773. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  774. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  775. if ($res1 || $res2 || $res3) {
  776. return Json::successful('该商品有活动开启,无法删除属性');
  777. } else {
  778. return Json::fail();
  779. }
  780. } else {
  781. return Json::fail();
  782. }
  783. }
  784. }