StoreProduct.php 41 KB

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