StoreProduct.php 33 KB

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