StoreProduct.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. $agentLevel = \app\admin\model\system\SystemUserAgentLevel::where('is_del', 0)->order('grade asc')->field(['id', 'grade', 'name as level_name'])->select()->toArray();
  274. $level_discount = [];
  275. $num = 0;
  276. foreach ($agentLevel as $k => $v){
  277. $num = $num+1;
  278. $level_discount_lst = StoreProductLevel::where('product_id', $id)->where('level_id', $v['id'])->where('type', 2)->find();
  279. if (!empty($level_discount_lst)){
  280. $level_discount_lst = $level_discount_lst ->toArray();
  281. $level_discount[] = ['grade'=>$v['grade'],'level_name' => $v['level_name'], 'discount' => $level_discount_lst['discount']];
  282. }else{
  283. $level_discount[] = ['grade'=>$v['grade'],'level_name' => $v['level_name'], 'discount' => ''];
  284. }
  285. }
  286. $data['productInfo']['level_discount'] = $level_discount;
  287. }
  288. // $data['level_set'] = 1;
  289. return JsonService::successful($data);
  290. }
  291. /**
  292. * 保存新建的资源
  293. *
  294. *
  295. */
  296. public function save($id)
  297. {
  298. $data = Util::postMore([
  299. ['cate_id', []],
  300. 'store_name',
  301. 'store_info',
  302. 'keyword',
  303. ['unit_name', '件'],
  304. ['image', []],
  305. ['slider_image', []],
  306. ['postage', 0],
  307. ['is_sub', 0],
  308. ['sort', 0],
  309. ['sales', 0],
  310. ['ficti', 100],
  311. ['rebate_ratio', 0],
  312. ['give_integral', 0],
  313. ['is_show', 0],
  314. ['temp_id', 0],
  315. ['is_hot', 0],
  316. ['is_benefit', 0],
  317. ['is_best', 0],
  318. ['is_new', 0],
  319. ['is_wholesale', 0],
  320. ['mer_use', 0],
  321. ['is_postage', 0],
  322. ['is_good', 0],
  323. ['dealer_visible_grade', 0],
  324. // ['description', ''],
  325. ['spec_type', 0],
  326. ['video_link', ''],
  327. ['items', []],
  328. ['attrs', []],
  329. ['activity', []],
  330. ['store_type', 1],
  331. ['mer_id',0],
  332. // ['level_set', 0],
  333. ['level_radio', []],
  334. ['level_discount', []],
  335. ], $this->request, false, true);
  336. // $admin_id=$this->adminId;
  337. // $mer_id = SystemAdmin::where('id',$admin_id)->value('mer_id');
  338. // if ($data['mer_id']>0){
  339. // $data['mer_id'] = $mer_id;
  340. $data['is_pick']=SystemStore::where('id',$data['mer_id'])->value('is_pick');
  341. // var_dump($data);die();
  342. // }
  343. // $level_set = $data['level_set'];
  344. $level_radio = $data['level_radio'];
  345. $level_discount = $data['level_discount'];
  346. // unset($data['level_set']);
  347. unset($data['level_radio']);
  348. unset($data['level_discount']);
  349. foreach ($data['activity'] as $k => $v) {
  350. if ($v == '秒杀') {
  351. $data['activity'][$k] = 1;
  352. } elseif ($v == '砍价') {
  353. $data['activity'][$k] = 2;
  354. } else {
  355. $data['activity'][$k] = 3;
  356. }
  357. }
  358. $data['description'] = $this->request->post('description', '');
  359. $data['activity'] = implode(',', $data['activity']);
  360. $detail = $data['attrs'];
  361. $data['price'] = min(array_column($detail, 'price'));
  362. $data['min_integral'] = min(array_column($detail, 'integral'));
  363. $data['max_integral'] = max(array_column($detail, 'integral'));
  364. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  365. $data['cost'] = min(array_column($detail, 'cost'));
  366. $attr = $data['items'];
  367. unset($data['items'], $data['video'], $data['attrs']);
  368. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  369. if (!in_array($data['store_type'], [1, 2])) return Json::fail('请选择正确的商品类型');
  370. $cate_id = $data['cate_id'];
  371. $data['cate_id'] = implode(',', $data['cate_id']);
  372. if (!$data['store_name']) return Json::fail('请输入产品名称');
  373. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  374. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  375. if ($data['rebate_ratio']>10) return Json::fail('返利比例不能大于10%');
  376. $data['image'] = $data['image'][0];
  377. $data['slider_image'] = json_encode($data['slider_image']);
  378. $data['stock'] = array_sum(array_column($detail, 'stock'));
  379. ProductModel::beginTrans();
  380. foreach ($detail as &$item) {
  381. if (($item['brokerage'] + $item['brokerage_two']) > $item['price']) {
  382. return Json::fail('一二级返佣相加不能大于商品售价');
  383. }
  384. }
  385. if ($id) {
  386. unset($data['sales']);
  387. ProductModel::edit($data, $id);
  388. $description = $data['description'];
  389. unset($data['description']);
  390. StoreDescription::saveDescription($description, $id);
  391. StoreProductCate::where('product_id', $id)->delete();
  392. $cateData = [];
  393. foreach ($cate_id as $cid) {
  394. $cateData[] = ['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()];
  395. }
  396. StoreProductCate::insertAll($cateData);
  397. if ($data['spec_type'] == 0) {
  398. $attr = [
  399. [
  400. 'value' => '规格',
  401. 'detailValue' => '',
  402. 'attrHidden' => '',
  403. 'detail' => ['默认']
  404. ]
  405. ];
  406. $detail[0]['value1'] = '规格';
  407. $detail[0]['detail'] = ['规格' => '默认'];
  408. }
  409. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  410. // if ($level_set>0){
  411. StoreProductLevel::where('product_id',$id)->where('type',1)->delete();
  412. foreach ($level_radio as $k => $v) {
  413. $level_id = SystemUserLevel::where('grade',$v['grade'])->value('id');
  414. StoreProductLevel::create(['product_id' => $id, 'type' => 1, 'level_id' => $level_id, 'rebate_ratio' => $v['ratio'],'create_time'=>time()]);
  415. }
  416. // 未完待续
  417. // }
  418. // 处理代理等级折扣
  419. StoreProductLevel::where('product_id',$id)->where('type',2)->delete();
  420. foreach ($level_discount as $k => $v) {
  421. $level_id = \app\admin\model\system\SystemUserAgentLevel::where('grade',$v['grade'])->value('id');
  422. StoreProductLevel::create(['product_id' => $id, 'type' => 2, 'level_id' => $level_id, 'discount' => $v['discount'],'create_time'=>time()]);
  423. }
  424. if ($attr_res) {
  425. ProductModel::commitTrans();
  426. return Json::success('修改成功!');
  427. } else {
  428. ProductModel::rollbackTrans();
  429. return Json::fail(StoreProductAttr::getErrorInfo());
  430. }
  431. } else {
  432. $data['add_time'] = time();
  433. $data['code_path'] = '';
  434. $res = ProductModel::create($data);
  435. $id = $res['id'];
  436. $description = $data['description'];
  437. StoreDescription::saveDescription($description, $res['id']);
  438. $cateData = [];
  439. foreach ($cate_id as $cid) {
  440. $cateData[] = ['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()];
  441. }
  442. StoreProductCate::insertAll($cateData);
  443. if ($data['spec_type'] == 0) {
  444. $attr = [
  445. [
  446. 'value' => '规格',
  447. 'detailValue' => '',
  448. 'attrHidden' => '',
  449. 'detail' => ['默认']
  450. ]
  451. ];
  452. $detail[0]['value1'] = '规格';
  453. $detail[0]['detail'] = ['规格' => '默认'];
  454. }
  455. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $res['id']);
  456. StoreProductLevel::where('product_id',$id)->where('type',1)->delete();
  457. foreach ($level_radio as $k => $v) {
  458. $level_id = SystemUserLevel::where('grade',$v['grade'])->value('id');
  459. StoreProductLevel::create(['product_id' => $id, 'type' => 1, 'level_id' => $level_id, 'ratio' => $v['ratio'],'create_time'=>time()]);
  460. }
  461. // 处理代理等级折扣
  462. StoreProductLevel::where('product_id',$id)->where('type',2)->delete();
  463. foreach ($level_discount as $k => $v) {
  464. $level_id = \app\admin\model\system\SystemUserAgentLevel::where('grade',$v['grade'])->value('id');
  465. StoreProductLevel::create(['product_id' => $id, 'type' => 2, 'level_id' => $level_id, 'discount' => $v['discount'],'create_time'=>time()]);
  466. }
  467. if ($attr_res) {
  468. ProductModel::commitTrans();
  469. return Json::success('添加产品成功!');
  470. } else {
  471. ProductModel::rollbackTrans();
  472. return Json::fail(StoreProductAttr::getErrorInfo());
  473. }
  474. }
  475. }
  476. public function edit_content($id)
  477. {
  478. if (!$id) return $this->failed('数据不存在');
  479. $product = ProductModel::get($id);
  480. if (!$product) return Json::fail('数据不存在!');
  481. $this->assign([
  482. 'content' => $product->description,
  483. 'field' => 'description',
  484. 'action' => Url::buildUrl('change_field', ['id' => $id, 'field' => 'description'])
  485. ]);
  486. return $this->fetch('public/edit_content');
  487. }
  488. /**
  489. * 显示编辑资源表单页.
  490. *
  491. * @param int $id
  492. * @return \think\Response
  493. */
  494. public function edit($id)
  495. {
  496. if (!$id) return $this->failed('数据不存在');
  497. $product = ProductModel::get($id);
  498. if (!$product) return Json::fail('数据不存在!');
  499. $field = [
  500. Form::select('cate_id', '产品分类', explode(',', $product->getData('cate_id')))->setOptions(function () {
  501. $list = CategoryModel::getTierList(null, 1);
  502. $menus = [];
  503. foreach ($list as $menu) {
  504. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0];//,'disabled'=>$menu['pid']== 0];
  505. }
  506. return $menus;
  507. })->filterable(1)->multiple(1),
  508. Form::input('store_name', '产品名称', $product->getData('store_name')),
  509. Form::input('store_info', '产品简介', $product->getData('store_info'))->type('textarea'),
  510. Form::input('keyword', '产品关键字', $product->getData('keyword'))->placeholder('多个用英文状态下的逗号隔开'),
  511. Form::input('unit_name', '产品单位', $product->getData('unit_name'))->col(12),
  512. Form::input('bar_code', '产品条码', $product->getData('bar_code'))->col(12),
  513. Form::frameImageOne('image', '产品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px'),
  514. 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'),
  515. Form::number('price', '产品售价', $product->getData('price'))->min(0)->col(8),
  516. Form::number('ot_price', '产品市场价', $product->getData('ot_price'))->min(0)->col(8),
  517. Form::number('give_integral', '赠送积分', $product->getData('give_integral'))->min(0)->col(8),
  518. Form::number('postage', '邮费', $product->getData('postage'))->min(0)->col(8),
  519. Form::number('sales', '销量', $product->getData('sales'))->min(0)->precision(0)->col(8)->readonly(1),
  520. Form::number('ficti', '虚拟销量', $product->getData('ficti'))->min(0)->precision(0)->col(8),
  521. Form::number('rebate_ratio', '返利积分比例(%)', $product->getData('rebate_ratio'))->min(0)->precision(0)->col(8),
  522. Form::number('stock', '库存', ProductModel::getStock($id) > 0 ? ProductModel::getStock($id) : $product->getData('stock'))->min(0)->precision(0)->col(8),
  523. Form::number('cost', '产品成本价', $product->getData('cost'))->min(0)->col(8),
  524. Form::number('sort', '排序', $product->getData('sort'))->col(8),
  525. Form::radio('is_show', '产品状态', $product->getData('is_show'))->options([['label' => '上架', 'value' => 1], ['label' => '下架', 'value' => 0]])->col(8),
  526. Form::radio('is_hot', '热卖单品', $product->getData('is_hot'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  527. Form::radio('is_benefit', '促销单品', $product->getData('is_benefit'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  528. Form::radio('is_best', '精品推荐', $product->getData('is_best'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  529. Form::radio('is_new', '首发新品', $product->getData('is_new'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  530. Form::radio('is_postage', '是否包邮', $product->getData('is_postage'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  531. Form::radio('is_good', '是否优品推荐', $product->getData('is_good'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  532. Form::radio('is_wholesale', '是否批发商品', $product->getData('is_wholesale'))->options([['label' => '是', 'value' => 1], ['label' => '否', 'value' => 0]])->col(8),
  533. ];
  534. $form = Form::make_post_form('编辑产品', $field, Url::buildUrl('update', array('id' => $id)), 2);
  535. $this->assign(compact('form'));
  536. return $this->fetch('public/form-builder');
  537. }
  538. /**
  539. * 保存更新的资源
  540. *
  541. * @param $id
  542. */
  543. public function update($id)
  544. {
  545. $data = Util::postMore([
  546. ['cate_id', []],
  547. 'store_name',
  548. 'store_info',
  549. 'keyword',
  550. 'bar_code',
  551. ['unit_name', '件'],
  552. ['image', []],
  553. ['slider_image', []],
  554. ['postage', 0],
  555. ['ot_price', 0],
  556. ['price', 0],
  557. ['sort', 0],
  558. ['stock', 0],
  559. ['temp_id', 0],
  560. ['ficti', 100],
  561. ['rebate_ratio', 0],
  562. ['give_integral', 0],
  563. ['is_show', 0],
  564. ['cost', 0],
  565. ['is_hot', 0],
  566. ['is_benefit', 0],
  567. ['is_best', 0],
  568. ['is_new', 0],
  569. ['mer_use', 0],
  570. ['is_postage', 0],
  571. ['is_good', 0],
  572. ['dealer_visible_grade', 0],
  573. ['is_wholesale',0],
  574. ['mer_id',0]
  575. ]);
  576. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  577. $cate_id = $data['cate_id'];
  578. $data['cate_id'] = implode(',', $data['cate_id']);
  579. if (!$data['store_name']) return Json::fail('请输入产品名称');
  580. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  581. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  582. // if(count($data['slider_image'])>8) return Json::fail('轮播图最多5张图');
  583. if ($data['price'] == '' || $data['price'] < 0) return Json::fail('请输入产品售价');
  584. if ($data['ot_price'] == '' || $data['ot_price'] < 0) return Json::fail('请输入产品市场价');
  585. if ($data['stock'] == '' || $data['stock'] < 0) return Json::fail('请输入库存');
  586. if($data['rebate_ratio']>10) return Json::fail('返利比例不能大于10%');
  587. $data['image'] = $data['image'][0];
  588. $data['slider_image'] = json_encode($data['slider_image']);
  589. ProductModel::edit($data, $id);
  590. StoreProductCate::where('product_id', $id)->delete();
  591. foreach ($cate_id as $cid) {
  592. StoreProductCate::insert(['product_id' => $id, 'cate_id' => $cid, 'add_time' => time()]);
  593. }
  594. return Json::successful('修改成功!');
  595. }
  596. public function attr($id)
  597. {
  598. if (!$id) return $this->failed('数据不存在!');
  599. // $result = StoreProductAttrResult::getResult($id);
  600. $result = StoreProductAttrValue::getStoreProductAttrResult($id);
  601. $image = ProductModel::where('id', $id)->value('image');
  602. $this->assign(compact('id', 'result', 'image'));
  603. return $this->fetch();
  604. }
  605. /**
  606. * 生成属性
  607. * @param int $id
  608. */
  609. public function is_format_attr($id = 0)
  610. {
  611. $data = Util::postMore([
  612. ['attrs', []],
  613. ['items', []]
  614. ]);
  615. $attr = $data['attrs'];
  616. $value = attr_format($attr)[1];
  617. $valueNew = [];
  618. $count = 0;
  619. foreach ($value as $key => $item) {
  620. $detail = $item['detail'];
  621. // sort($item['detail'], SORT_STRING);
  622. $suk = implode(',', $item['detail']);
  623. if ($id) {
  624. $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');
  625. if (!count($sukValue)) {
  626. $sukValue[$suk]['pic'] = '';
  627. $sukValue[$suk]['price'] = 0;
  628. $sukValue[$suk]['integral'] = 0;
  629. $sukValue[$suk]['cost'] = 0;
  630. $sukValue[$suk]['ot_price'] = 0;
  631. $sukValue[$suk]['stock'] = 0;
  632. $sukValue[$suk]['bar_code'] = '';
  633. $sukValue[$suk]['weight'] = 0;
  634. $sukValue[$suk]['volume'] = 0;
  635. $sukValue[$suk]['brokerage'] = 0;
  636. $sukValue[$suk]['brokerage_two'] = 0;
  637. }
  638. } else {
  639. $sukValue[$suk]['pic'] = '';
  640. $sukValue[$suk]['price'] = 0;
  641. $sukValue[$suk]['integral'] = 0;
  642. $sukValue[$suk]['cost'] = 0;
  643. $sukValue[$suk]['ot_price'] = 0;
  644. $sukValue[$suk]['stock'] = 0;
  645. $sukValue[$suk]['bar_code'] = '';
  646. $sukValue[$suk]['weight'] = 0;
  647. $sukValue[$suk]['volume'] = 0;
  648. $sukValue[$suk]['brokerage'] = 0;
  649. $sukValue[$suk]['brokerage_two'] = 0;
  650. }
  651. foreach (array_keys($detail) as $k => $title) {
  652. $header[$k]['title'] = $title;
  653. $header[$k]['align'] = 'center';
  654. $header[$k]['minWidth'] = 130;
  655. }
  656. foreach (array_values($detail) as $k => $v) {
  657. $valueNew[$count]['value' . ($k + 1)] = $v;
  658. $header[$k]['key'] = 'value' . ($k + 1);
  659. }
  660. $valueNew[$count]['detail'] = $detail;
  661. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  662. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  663. $valueNew[$count]['integral'] = $sukValue[$suk]['integral'] ? floatval($sukValue[$suk]['integral']) : 0;
  664. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  665. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  666. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  667. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  668. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  669. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  670. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  671. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  672. $count++;
  673. }
  674. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  675. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
  676. $header[] = ['title' => '积分', 'slot' => 'integral', 'align' => 'center', 'minWidth' => 120];
  677. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
  678. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
  679. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
  680. $header[] = ['title' => '产品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
  681. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 140];
  682. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 140];
  683. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  684. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  685. return Json::successful($info);
  686. }
  687. public function set_attr($id)
  688. {
  689. if (!$id) return $this->failed('产品不存在!');
  690. list($attr, $detail) = Util::postMore([
  691. ['items', []],
  692. ['attrs', []]
  693. ], null, true);
  694. $res = StoreProductAttr::createProductAttr($attr, $detail, $id);
  695. if ($res)
  696. return $this->successful('编辑属性成功!');
  697. else
  698. return $this->failed(StoreProductAttr::getErrorInfo());
  699. }
  700. public function clear_attr($id)
  701. {
  702. if (!$id) return $this->failed('产品不存在!');
  703. if (false !== StoreProductAttr::clearProductAttr($id) && false !== StoreProductAttrResult::clearResult($id))
  704. return $this->successful('清空产品属性成功!');
  705. else
  706. return $this->failed(StoreProductAttr::getErrorInfo('清空产品属性失败!'));
  707. }
  708. /**
  709. * 删除指定资源
  710. *
  711. * @param int $id
  712. * @return \think\Response
  713. */
  714. public function delete($id)
  715. {
  716. if (!$id) return $this->failed('数据不存在');
  717. if (!ProductModel::be(['id' => $id])) return $this->failed('产品数据不存在');
  718. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  719. $data['is_del'] = 0;
  720. if (!ProductModel::edit($data, $id))
  721. return Json::fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  722. else
  723. return Json::successful('成功恢复产品!');
  724. } else {
  725. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  726. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  727. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  728. if ($res1 || $res2 || $res3) {
  729. return Json::fail(ProductModel::getErrorInfo('该商品已参加活动,无法删除!'));
  730. } else {
  731. $data['is_del'] = 1;
  732. if (!ProductModel::edit($data, $id))
  733. return Json::fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  734. else
  735. return Json::successful('成功移到回收站!');
  736. }
  737. }
  738. }
  739. /**
  740. * 点赞
  741. * @param $id
  742. * @return mixed|\think\response\Json|void
  743. */
  744. public function collect($id)
  745. {
  746. if (!$id) return $this->failed('数据不存在');
  747. $product = ProductModel::get($id);
  748. if (!$product) return Json::fail('数据不存在!');
  749. $this->assign(StoreProductRelation::getCollect($id));
  750. return $this->fetch();
  751. }
  752. /**
  753. * 收藏
  754. * @param $id
  755. * @return mixed|\think\response\Json|void
  756. */
  757. public function like($id)
  758. {
  759. if (!$id) return $this->failed('数据不存在');
  760. $product = ProductModel::get($id);
  761. if (!$product) return Json::fail('数据不存在!');
  762. $this->assign(StoreProductRelation::getLike($id));
  763. return $this->fetch();
  764. }
  765. /**
  766. * 修改产品价格
  767. */
  768. public function edit_product_price()
  769. {
  770. $data = Util::postMore([
  771. ['id', 0],
  772. ['price', 0],
  773. ]);
  774. if (!$data['id']) return Json::fail('参数错误');
  775. $res = ProductModel::edit(['price' => $data['price']], $data['id']);
  776. if ($res) return Json::successful('修改成功');
  777. else return Json::fail('修改失败');
  778. }
  779. /**
  780. * 修改产品库存
  781. *
  782. */
  783. public function edit_product_stock()
  784. {
  785. $data = Util::postMore([
  786. ['id', 0],
  787. ['stock', 0],
  788. ]);
  789. if (!$data['id']) return Json::fail('参数错误');
  790. $res = ProductModel::edit(['stock' => $data['stock']], $data['id']);
  791. if ($res) return Json::successful('修改成功');
  792. else return Json::fail('修改失败');
  793. }
  794. /**
  795. * 检测商品是否开活动
  796. * @param $id
  797. * @throws \think\db\exception\DataNotFoundException
  798. * @throws \think\db\exception\DbException
  799. * @throws \think\db\exception\ModelNotFoundException
  800. */
  801. public function check_activity($id)
  802. {
  803. if ($id != 0) {
  804. $res1 = StoreSeckill::where('product_id', $id)->where('is_del', 0)->find();
  805. $res2 = StoreBargain::where('product_id', $id)->where('is_del', 0)->find();
  806. $res3 = StoreCombination::where('product_id', $id)->where('is_del', 0)->find();
  807. if ($res1 || $res2 || $res3) {
  808. return Json::successful('该商品有活动开启,无法删除属性');
  809. } else {
  810. return Json::fail();
  811. }
  812. } else {
  813. return Json::fail();
  814. }
  815. }
  816. }