StoreProductCheck.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. <?php
  2. namespace app\admin\controller\store;
  3. use app\admin\controller\AuthController;
  4. use app\models\store\SystemStockBill;
  5. use app\models\system\SystemStoreStock;
  6. use app\admin\model\store\{StoreDescription,
  7. StoreProductAttrValue,
  8. StoreProductAttr,
  9. StoreProductAttrResult,
  10. StoreCategory as CategoryModel,
  11. StoreProductCate,
  12. StoreProductCheck as ProductModel
  13. };
  14. use crmeb\services\{JsonService,
  15. JsonService as Json,
  16. UtilService as Util,
  17. FormBuilder as Form,
  18. UtilService
  19. };
  20. use crmeb\traits\CurdControllerTrait;
  21. use think\facade\Route as Url;
  22. use app\admin\model\system\{ShippingTemplates, SystemStore};
  23. use think\Model;
  24. /**
  25. * 产品管理
  26. * Class StoreProduct
  27. * @package app\admin\controller\store
  28. */
  29. class StoreProductCheck extends AuthController
  30. {
  31. use CurdControllerTrait;
  32. protected $bindModel = ProductModel::class;
  33. protected $store;
  34. protected $main_where = [];
  35. public function initialize()
  36. {
  37. parent::initialize(); // TODO: Change the autogenerated stub
  38. $store_id = $this->adminInfo['store_id'];
  39. $this->store = SystemStore::where('id', $store_id)->find();
  40. if ($this->store && $this->store['is_triple']) {
  41. $this->main_where['store_id'] = $store_id;
  42. }
  43. }
  44. /**
  45. * 显示资源列表
  46. *
  47. * @return \think\Response
  48. */
  49. public function index()
  50. {
  51. $type = $this->request->param('type', 1);
  52. //获取分类
  53. $this->assign('cate', CategoryModel::getTierList(null, 1));
  54. $checking = ProductModel::getModelObject(['type' => 2])->where($this->main_where)->count();
  55. $checkok = ProductModel::getModelObject(['type' => 3])->where($this->main_where)->count();
  56. $checkno = ProductModel::getModelObject(['type' => 4])->where($this->main_where)->count();
  57. $this->assign('user_store', $this->adminInfo['store_id']);
  58. $this->assign(compact('type', 'checking', 'checkok', 'checkno'));
  59. return $this->fetch();
  60. }
  61. /**
  62. * 异步查找产品
  63. *
  64. * @return void
  65. */
  66. public function product_ist()
  67. {
  68. $where = Util::getMore([
  69. ['page', 1],
  70. ['limit', 20],
  71. ['store_name', ''],
  72. ['cate_id', ''],
  73. ['excel', 0],
  74. ['order', ''],
  75. [['type', 'd'], $this->request->param('type/d')],
  76. ]);
  77. Json::successlayui(ProductModel::ProductList($where));
  78. }
  79. /**
  80. * 设置单个产品上架|下架
  81. *
  82. * @return void
  83. */
  84. public function check($id = '')
  85. {
  86. ($id == '') && Json::fail('缺少参数');
  87. $res = ProductModel::where($this->main_where)->where(['id' => $id])->update(['status' => 0]);
  88. if ($res) {
  89. Json::successful('提交成功');
  90. } else {
  91. Json::fail('提交失败');
  92. }
  93. }
  94. /**
  95. * 设置批量产品上架
  96. *
  97. * @return void
  98. */
  99. public function take_products()
  100. {
  101. $post = Util::postMore([
  102. ['ids', []]
  103. ]);
  104. if (empty($post['ids'])) {
  105. Json::fail('请选择需要提交的产品');
  106. } else {
  107. $res = ProductModel::where($this->main_where)->where('id', 'in', $post['ids'])->update(['status' => 0]);
  108. if ($res)
  109. Json::successful('提交成功');
  110. else
  111. Json::fail('提交失败');
  112. }
  113. }
  114. /**
  115. * 显示创建资源表单页.
  116. *
  117. * @param int $id
  118. * @param int $consumer
  119. * @return \think\Response
  120. * @throws \Exception
  121. */
  122. public function create($id = 0)
  123. {
  124. $this->assign('id', (int)$id);
  125. return $this->fetch();
  126. }
  127. /**
  128. * 获取产品详细信息
  129. * @param int $id
  130. * @throws \think\db\exception\DataNotFoundException
  131. * @throws \think\db\exception\DbException
  132. * @throws \think\db\exception\ModelNotFoundException
  133. */
  134. public function get_product_info($id = 0, $type = 0)
  135. {
  136. $list = CategoryModel::getTierList(null, 1, $type);
  137. $menus = [];
  138. foreach ($list as $menu) {
  139. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0 ? 0 : 1];//,'disabled'=>$menu['pid']== 0];
  140. }
  141. $data['tempList'] = ShippingTemplates::order('sort', 'desc')->field(['id', 'name'])->select()->toArray();
  142. $data['cateList'] = $menus;
  143. $data['productInfo'] = [];
  144. if ($id) {
  145. $productInfo = ProductModel::where($this->main_where)->where('id', $id)->find();
  146. if (!$productInfo) {
  147. return Json::fail('修改的产品不存在');
  148. }
  149. $productInfo['cate_id'] = explode(',', $productInfo['cate_id']);
  150. $productInfo['description'] = htmlspecialchars_decode(StoreDescription::getDescription($id, 8));
  151. $productInfo['slider_image'] = is_string($productInfo['slider_image']) ? json_decode($productInfo['slider_image'], true) : [];
  152. if ($productInfo['spec_type'] == 1) {
  153. $result = StoreProductAttrResult::getResult($id, 8);
  154. foreach ($result['value'] as $k => $v) {
  155. $num = 1;
  156. foreach ($v['detail'] as $dv) {
  157. $result['value'][$k]['value' . $num] = $dv;
  158. $num++;
  159. }
  160. }
  161. $productInfo['items'] = $result['attr'];
  162. $productInfo['attrs'] = $result['value'];
  163. $productInfo['attr'] = ['pic' => '', 'price' => 0, 'cost' => 0, 'ot_price' => 0, 'stock' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0, 'brokerage_two' => 0];
  164. } else {
  165. $result = StoreProductAttrValue::where('product_id', $id)->where('type', 8)->find();
  166. if ($result) {
  167. $single = $result->toArray();
  168. } else {
  169. $single = [];
  170. }
  171. $productInfo['items'] = [];
  172. $productInfo['attrs'] = [];
  173. $productInfo['attr'] = [
  174. 'pic' => $single['image'] ?? '',
  175. 'price' => $single['price'] ?? 0,
  176. 'cost' => $single['cost'] ?? 0,
  177. 'ot_price' => $single['ot_price'] ?? 0,
  178. 'stock' => $single['stock'] ?? 0,
  179. 'bar_code' => $single['bar_code'] ?? '',
  180. 'weight' => $single['weight'] ?? 0,
  181. 'volume' => $single['volume'] ?? 0,
  182. 'brokerage' => $single['brokerage'] ?? 0,
  183. 'brokerage_two' => $single['brokerage_two'] ?? 0,
  184. ];
  185. }
  186. if ($productInfo['activity']) {
  187. $activity = explode(',', $productInfo['activity']);
  188. foreach ($activity as $k => $v) {
  189. if ($v == 1) {
  190. $activity[$k] = '秒杀';
  191. } elseif ($v == 2) {
  192. $activity[$k] = '砍价';
  193. } elseif ($v == 3) {
  194. $activity[$k] = '拼团';
  195. }
  196. }
  197. $productInfo['activity'] = $activity;
  198. } else {
  199. $productInfo['activity'] = ['秒杀', '砍价', '拼团'];
  200. }
  201. $data['productInfo'] = $productInfo;
  202. }
  203. return JsonService::successful($data);
  204. }
  205. /**
  206. * 保存新建的资源
  207. *
  208. *
  209. */
  210. public function save($id)
  211. {
  212. // if (!isset($this->store['is_triple']) || !$this->store['is_triple']) Json::fail('第三方门店专用通道');
  213. $data = Util::postMore([
  214. ['cate_id', []],
  215. 'store_name',
  216. 'store_info',
  217. 'keyword',
  218. ['unit_name', '件'],
  219. ['image', []],
  220. ['slider_image', []],
  221. ['postage', 0],
  222. ['is_sub', 0],
  223. ['sort', 0],
  224. ['sales', 0],
  225. ['ficti', 100],
  226. ['give_integral', 0],
  227. ['max_use_integral', 0],
  228. ['is_show', 0],
  229. ['temp_id', 0],
  230. ['is_hot', 0],
  231. ['is_benefit', 0],
  232. ['is_suit', 0],
  233. ['is_award', 0],
  234. ['is_best', 0],
  235. ['is_new', 0],
  236. ['mer_use', 0],
  237. ['is_postage', 0],
  238. ['is_good', 0],
  239. ['description', ''],
  240. ['spec_type', 0],
  241. ['video_link', ''],
  242. ['items', []],
  243. ['attrs', []],
  244. ['activity', []],
  245. ['is_consumer', 0],
  246. ['bar_code', ''],
  247. ]);
  248. $data['store_id'] = $this->store['id'];
  249. foreach ($data['activity'] as $k => $v) {
  250. if ($v == '秒杀') {
  251. $data['activity'][$k] = 1;
  252. } elseif ($v == '砍价') {
  253. $data['activity'][$k] = 2;
  254. } else {
  255. $data['activity'][$k] = 3;
  256. }
  257. }
  258. $data['activity'] = implode(',', $data['activity']);
  259. $detail = $data['attrs'];
  260. $data['price'] = min(array_column($detail, 'price'));
  261. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  262. $data['cost'] = min(array_column($detail, 'cost'));
  263. $attr = $data['items'];
  264. unset($data['items'], $data['video'], $data['attrs']);
  265. if (count($data['cate_id']) < 1) return Json::fail('请选择产品分类');
  266. $cate_id = $data['cate_id'];
  267. $data['cate_id'] = implode(',', $data['cate_id']);
  268. if (!$data['store_name']) return Json::fail('请输入产品名称');
  269. if (count($data['image']) < 1) return Json::fail('请上传产品图片');
  270. if (count($data['slider_image']) < 1) return Json::fail('请上传产品轮播图');
  271. $data['image'] = $data['image'][0];
  272. $data['slider_image'] = json_encode($data['slider_image']);
  273. $data['stock'] = array_sum(array_column($detail, 'stock'));
  274. ProductModel::beginTrans();
  275. foreach ($detail as &$item) {
  276. if (($item['brokerage'] + $item['brokerage_two']) > $item['price']) {
  277. return Json::fail('一二级返佣相加不能大于商品售价');
  278. }
  279. }
  280. if ($id) {
  281. $info = ProductModel::get($id);
  282. if ($info['status'] != 2) $data['status'] = 0;
  283. unset($data['sales']);
  284. ProductModel::edit($data, $id);
  285. $description = $data['description'];
  286. unset($data['description']);
  287. StoreDescription::saveDescription($description, $id, 8);
  288. if ($data['spec_type'] == 0) {
  289. $attr = [
  290. [
  291. 'value' => '规格',
  292. 'detailValue' => '',
  293. 'attrHidden' => '',
  294. 'detail' => ['默认']
  295. ]
  296. ];
  297. $detail[0]['value1'] = '规格';
  298. $detail[0]['detail'] = ['规格' => '默认'];
  299. }
  300. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $id, 8);
  301. if ($attr_res) {
  302. ProductModel::commitTrans();
  303. return Json::success('修改成功!');
  304. } else {
  305. ProductModel::rollbackTrans();
  306. return Json::fail(StoreProductAttr::getErrorInfo());
  307. }
  308. } else {
  309. $data['add_time'] = time();
  310. $data['code_path'] = '';
  311. $data['status'] = 0;
  312. $res = ProductModel::create($data);
  313. $description = $data['description'];
  314. StoreDescription::saveDescription($description, $res['id'], 8);
  315. if ($data['spec_type'] == 0) {
  316. $attr = [
  317. [
  318. 'value' => '规格',
  319. 'detailValue' => '',
  320. 'attrHidden' => '',
  321. 'detail' => ['默认']
  322. ]
  323. ];
  324. $detail[0]['value1'] = '规格';
  325. $detail[0]['detail'] = ['规格' => '默认'];
  326. }
  327. $attr_res = StoreProductAttr::createProductAttr($attr, $detail, $res['id'], 8);
  328. if ($attr_res) {
  329. ProductModel::commitTrans();
  330. return Json::success('添加产品成功!');
  331. } else {
  332. ProductModel::rollbackTrans();
  333. return Json::fail(StoreProductAttr::getErrorInfo());
  334. }
  335. }
  336. }
  337. public function attr_save()
  338. {
  339. $where = Util::getMore([
  340. ['bar_code', 20],
  341. ['in_stock', 0],
  342. ]);
  343. if ($where['in_stock'] < 1) return app('json')->fail('补货数量不能少于1');
  344. $info = StoreProductAttrValue::alias('a')->join("StoreProduct b", "a.product_id=b.id", "left")->where('a.bar_code', $where['bar_code'])->field('a.price,a.unique,a.stock as in_stock,a.product_id,b.store_name,b.image,a.bar_code')->find();
  345. if (!$info) return app('json')->fail('商品条形码不存在');
  346. $info = $info->toarray();
  347. $info['in_stock'] = $where['in_stock'];
  348. $info['admin_id'] = $this->adminId;
  349. if (SystemStockBill::order_create($info)) {
  350. return Json::successful('入库成功');
  351. } else {
  352. return Json::fail('入库失败');
  353. }
  354. }
  355. /**
  356. * 生成属性
  357. * @param int $id
  358. */
  359. public function is_format_attr($id = 0, $type = 0)
  360. {
  361. $data = Util::postMore([
  362. ['attrs', []],
  363. ['items', []]
  364. ]);
  365. $attr = $data['attrs'];
  366. $value = attr_format($attr)[1];
  367. $valueNew = [];
  368. $count = 0;
  369. foreach ($value as $key => $item) {
  370. $detail = $item['detail'];
  371. sort($item['detail'], SORT_STRING);
  372. $suk = implode(',', $item['detail']);
  373. $types = 1;
  374. if ($id) {
  375. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', 8)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two', 'suk');
  376. if (!count($sukValue)) {
  377. if ($type == 0) $types = 0; //编辑商品时,将没有规格的数据不生成默认值
  378. $sukValue[$suk]['pic'] = '';
  379. $sukValue[$suk]['price'] = 0;
  380. $sukValue[$suk]['cost'] = 0;
  381. $sukValue[$suk]['ot_price'] = 0;
  382. $sukValue[$suk]['stock'] = 0;
  383. $sukValue[$suk]['bar_code'] = '';
  384. $sukValue[$suk]['weight'] = 0;
  385. $sukValue[$suk]['volume'] = 0;
  386. $sukValue[$suk]['brokerage'] = 0;
  387. $sukValue[$suk]['brokerage_two'] = 0;
  388. }
  389. } else {
  390. $sukValue[$suk]['pic'] = '';
  391. $sukValue[$suk]['price'] = 0;
  392. $sukValue[$suk]['cost'] = 0;
  393. $sukValue[$suk]['ot_price'] = 0;
  394. $sukValue[$suk]['stock'] = 0;
  395. $sukValue[$suk]['bar_code'] = '';
  396. $sukValue[$suk]['weight'] = 0;
  397. $sukValue[$suk]['volume'] = 0;
  398. $sukValue[$suk]['brokerage'] = 0;
  399. $sukValue[$suk]['brokerage_two'] = 0;
  400. }
  401. if ($types) { //编辑商品时,将没有规格的数据不生成默认值
  402. foreach (array_keys($detail) as $k => $title) {
  403. $header[$k]['title'] = $title;
  404. $header[$k]['align'] = 'center';
  405. $header[$k]['minWidth'] = 130;
  406. }
  407. foreach (array_values($detail) as $k => $v) {
  408. $valueNew[$count]['value' . ($k + 1)] = $v;
  409. $header[$k]['key'] = 'value' . ($k + 1);
  410. }
  411. $valueNew[$count]['detail'] = $detail;
  412. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  413. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  414. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  415. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  416. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  417. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  418. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ?? 0;
  419. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ?? 0;
  420. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ?? 0;
  421. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ?? 0;
  422. $count++;
  423. }
  424. }
  425. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  426. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 120];
  427. $header[] = ['title' => '成本价', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 140];
  428. $header[] = ['title' => '原价', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 140];
  429. $header[] = ['title' => '库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 140];
  430. $header[] = ['title' => '产品编号', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 140];
  431. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 140];
  432. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 140];
  433. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  434. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  435. return Json::successful($info);
  436. }
  437. public function check_no($id)
  438. {
  439. $info = ProductModel::get($id);
  440. if (!$info || $info['status'] != 0 || $this->adminInfo['store_id'] != 0) {
  441. $this->failed('参数或身份错误');
  442. }
  443. $from = [];
  444. $from['reason'] = Form::textarea('reason', '驳回原因', '')->rows(10);
  445. $form = Form::make_post_form('添加用户通知', $from, Url::buildUrl('save_check_no', array('id' => $id)));
  446. $this->assign(compact('form'));
  447. return $this->fetch('public/form-builder');
  448. }
  449. public function save_check_no($id)
  450. {
  451. $info = ProductModel::get($id);
  452. if (!$info || $info['status'] != 0 || $this->adminInfo['store_id'] != 0) {
  453. Json::fail('参数或身份错误');
  454. }
  455. $data = UtilService::postMore([
  456. ['reason', ''],
  457. ]);
  458. if (ProductModel::where('id', $id)->update(['reason' => $data['reason'], 'status' => 2])) {
  459. JsonService::success('驳回成功');
  460. } else {
  461. JsonService::fail('驳回失败');
  462. }
  463. }
  464. public function check_ok($id)
  465. {
  466. $info = ProductModel::get($id);
  467. if (!$info || $info['status'] != 0 || $this->adminInfo['store_id'] != 0) {
  468. Json::fail('参数或身份错误');
  469. }
  470. $info = $info->toArray();
  471. $description = StoreDescription::getDescription($id, 8);
  472. $cate_id = explode(',', $info['cate_id']);
  473. ProductModel::beginTrans();
  474. $product = \app\admin\model\store\StoreProduct::where('check_id', $id)->find();
  475. try {
  476. if ($product) {
  477. unset($info['id']);
  478. unset($info['add_time']);
  479. \app\admin\model\store\StoreProduct::edit($info, $product['id']);
  480. StoreDescription::saveDescription($description, $product['id'], 0);
  481. StoreProductCate::where('product_id', $product['id'])->delete();
  482. $cateData = [];
  483. foreach ($cate_id as $cid) {
  484. $cateData[] = ['product_id' => $product['id'], 'cate_id' => $cid, 'add_time' => time()];
  485. }
  486. StoreProductCate::insertAll($cateData);
  487. $attr_res = StoreProductAttr::reSaveFromOtherType($id, $product['id'], 8, 0);
  488. if ($attr_res) {
  489. ProductModel::where('id', $id)->update(['status' => 1]);
  490. ProductModel::commitTrans();
  491. Json::success('审核完成');
  492. } else {
  493. ProductModel::rollbackTrans();
  494. Json::fail(StoreProductAttr::getErrorInfo());
  495. }
  496. } else {
  497. //新增
  498. unset($info['id']);
  499. $info['check_id'] = $id;
  500. $info['add_time'] = time();
  501. $res = \app\admin\model\store\StoreProduct::create($info);
  502. StoreDescription::saveDescription($description, $res['id'], 0);
  503. $cateData = [];
  504. foreach ($cate_id as $cid) {
  505. $cateData[] = ['product_id' => $res['id'], 'cate_id' => $cid, 'add_time' => time()];
  506. }
  507. StoreProductCate::insertAll($cateData);
  508. $attr_res = StoreProductAttr::reSaveFromOtherType($id, $res['id'], 8, 0);
  509. if ($attr_res) {
  510. ProductModel::where('id', $id)->update(['status' => 1]);
  511. ProductModel::commitTrans();
  512. Json::success('审核完成');
  513. } else {
  514. ProductModel::rollbackTrans();
  515. Json::fail(StoreProductAttr::getErrorInfo());
  516. }
  517. }
  518. } catch (\Exception $e) {
  519. ProductModel::rollbackTrans();
  520. Json::fail($e->getMessage());
  521. }
  522. }
  523. }