Product.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. namespace app\system\controller\v1;
  3. use app\BaseController;
  4. use app\model\system\ProductRule;
  5. use app\model\system\ProductAttr;
  6. use app\model\system\ProductAttrValue;
  7. use app\model\system\ProductAttrResult;
  8. use app\model\system\Product as ProductModel;
  9. use app\model\system\Category;
  10. use library\services\UtilService;
  11. use think\facade\Db;
  12. class Product extends BaseController
  13. {
  14. /**
  15. * 显示资源列表头部
  16. *
  17. * @return \think\Response
  18. */
  19. public function type_header()
  20. {
  21. //出售中商品
  22. $onsale = ProductModel::where('is_del', 0)->where('is_show', 1)->count();
  23. //仓库中商品
  24. $forsale = ProductModel::where('is_del', 0)->where('is_show', 0)->count();
  25. //已经售馨产品
  26. $outofstock = (new ProductModel)->getWhere(['type' => 4])->count();
  27. //警戒库存
  28. $policeforce = (new ProductModel)->getWhere(['type' => 5])->count();
  29. //回收站
  30. $recycle = ProductModel::where('is_del', 1)->count();
  31. $list = [
  32. ['type' => 1, 'name' => '出售中产品', 'count' => $onsale],
  33. ['type' => 2, 'name' => '仓库中产品', 'count' => $forsale],
  34. ['type' => 4, 'name' => '已经售馨产品', 'count' => $outofstock],
  35. ['type' => 5, 'name' => '警戒库存', 'count' => $policeforce],
  36. ['type' => 6, 'name' => '产品回收站', 'count' => $recycle],
  37. ];
  38. return app('json')->success(compact('list'));
  39. }
  40. /**
  41. * 显示资源列表
  42. * @return mixed
  43. */
  44. public function index()
  45. {
  46. $where = UtilService::getMore([
  47. ['page', 1],
  48. ['limit', 20],
  49. ['store_name', ''],
  50. ['cate_id', ''],
  51. ['factory_id', ''],
  52. ['label', ''],
  53. ['excel', 0],
  54. ['type', 1]
  55. ]);
  56. // if($this->merId){
  57. // $where['mer_id'] = $this->merId;
  58. // }
  59. return app('json')->success(ProductModel::ProductList($where));
  60. }
  61. /**
  62. * 获取规则属性模板
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function get_rule()
  68. {
  69. return app('json')->success(ProductRule::field(['rule_name', 'rule_value'])->select()->each(function ($item) {
  70. $item['rule_value'] = json_decode($item['rule_value'], true);
  71. })->toArray());
  72. }
  73. /**
  74. * 生成属性
  75. * @param int $id
  76. */
  77. public function is_format_attr($id)
  78. {
  79. $data = UtilService::getMore([
  80. ['attrs', []],
  81. ['items', []]
  82. ]);
  83. $attr = $data['attrs'];
  84. $value = attr_format($attr)[1];
  85. $valueNew = [];
  86. $count = 0;
  87. foreach ($value as $key => $item) {
  88. $detail = $item['detail'];
  89. sort($item['detail'], SORT_STRING);
  90. $suk = implode(',', $item['detail']);
  91. if ($id) {
  92. $sukValue = ProductAttrValue::where('product_id', $id)->where('type', 0)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,capacity,image as pic,weight,volume,brokerage', 'suk');
  93. if (!count($sukValue)) {
  94. $sukValue[$suk]['pic'] = '';
  95. $sukValue[$suk]['price'] = 0;
  96. $sukValue[$suk]['cost'] = 0;
  97. $sukValue[$suk]['ot_price'] = 0;
  98. $sukValue[$suk]['stock'] = 0;
  99. $sukValue[$suk]['capacity'] = 0;
  100. $sukValue[$suk]['bar_code'] = '';
  101. $sukValue[$suk]['weight'] = 0;
  102. $sukValue[$suk]['volume'] = 0;
  103. $sukValue[$suk]['brokerage'] = 0;
  104. }
  105. } else {
  106. $sukValue[$suk]['pic'] = '';
  107. $sukValue[$suk]['price'] = 0;
  108. $sukValue[$suk]['cost'] = 0;
  109. $sukValue[$suk]['ot_price'] = 0;
  110. $sukValue[$suk]['stock'] = 0;
  111. $sukValue[$suk]['capacity'] = 0;
  112. $sukValue[$suk]['bar_code'] = '';
  113. $sukValue[$suk]['weight'] = 0;
  114. $sukValue[$suk]['volume'] = 0;
  115. $sukValue[$suk]['brokerage'] = 0;
  116. }
  117. foreach (array_keys($detail) as $k => $title) {
  118. $header[$k]['title'] = $title;
  119. $header[$k]['align'] = 'center';
  120. $header[$k]['minWidth'] = 120;
  121. }
  122. foreach (array_values($detail) as $k => $v) {
  123. $valueNew[$count]['value' . ($k + 1)] = $v;
  124. $header[$k]['key'] = 'value' . ($k + 1);
  125. }
  126. $valueNew[$count]['detail'] = $detail;
  127. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  128. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  129. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  130. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  131. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  132. $valueNew[$count]['capacity'] = $sukValue[$suk]['capacity'] ? intval($sukValue[$suk]['capacity']) : 0;
  133. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  134. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  135. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  136. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  137. $count++;
  138. }
  139. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 80];
  140. $header[] = ['title' => '条形码', 'slot' => 'bar_code', 'align' => 'center', 'minWidth' => 120];
  141. $header[] = ['title' => '售价', 'slot' => 'price', 'align' => 'center', 'minWidth' => 95];
  142. $header[] = ['title' => '出厂价(含所有辅材)', 'slot' => 'cost', 'align' => 'center', 'minWidth' => 95];
  143. $header[] = ['title' => '代发价(含运费)', 'slot' => 'ot_price', 'align' => 'center', 'minWidth' => 95];
  144. $header[] = ['title' => '现货库存', 'slot' => 'stock', 'align' => 'center', 'minWidth' => 95];
  145. $header[] = ['title' => '单日产能', 'slot' => 'capacity', 'align' => 'center', 'minWidth' => 95];
  146. $header[] = ['title' => '重量(KG)', 'slot' => 'weight', 'align' => 'center', 'minWidth' => 95];
  147. $header[] = ['title' => '体积(m³)', 'slot' => 'volume', 'align' => 'center', 'minWidth' => 95];
  148. $header[] = ['title' => '操作', 'slot' => 'action', 'align' => 'center', 'minWidth' => 70];
  149. $info = ['attr' => $attr, 'value' => $valueNew, 'header' => $header];
  150. return app('json')->success(compact('info'));
  151. }
  152. /**
  153. * 获取商品详细信息
  154. * @param int $id
  155. * @throws \think\db\exception\DataNotFoundException
  156. * @throws \think\db\exception\DbException
  157. * @throws \think\db\exception\ModelNotFoundException
  158. */
  159. public function get_product_info($id = 0)
  160. {
  161. //$mer_id = $this->merId ?: '';
  162. $list = Category::getTierList(null, 1);
  163. $menus = [];
  164. foreach ($list as $menu) {
  165. $menus[] = ['value' => $menu['id'], 'label' => $menu['html'] . $menu['cate_name'], 'disabled' => $menu['pid'] == 0 ? 0 : 1];//'disabled'=>$menu['pid']== 0];
  166. }
  167. $data['cateList'] = $menus;
  168. $data['productInfo'] = [];
  169. if ($id) {
  170. $productInfo = ProductModel::get($id);
  171. if (!$productInfo) {
  172. return app('json')->fail('修改的商品不存在');
  173. }
  174. $productInfo['postage'] = floatval($productInfo['postage']);
  175. $productInfo['slider_image'] = is_string($productInfo['slider_image']) ? json_decode($productInfo['slider_image'], true) : [];
  176. $productInfo['detail_image'] = is_string($productInfo['detail_image']) ? json_decode($productInfo['detail_image'], true) : [];
  177. if ($productInfo['spec_type'] == 1) {
  178. $result = ProductAttrResult::getResult($id);
  179. foreach ($result['value'] as $k => $v) {
  180. $num = 1;
  181. foreach ($v['detail'] as $dv) {
  182. $result['value'][$k]['value' . $num] = $dv;
  183. $num++;
  184. }
  185. }
  186. $productInfo['items'] = $result['attr'];
  187. $productInfo['attrs'] = $result['value'];
  188. $productInfo['attr'] = ['pic' => '', 'price' => 0, 'cost' => 0, 'ot_price' => 0, 'stock' => 0, 'capacity' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0];
  189. } else {
  190. $result = ProductAttrValue::where('product_id', $id)->where('type', 0)->find();
  191. $productInfo['items'] = [];
  192. $productInfo['attrs'] = [];
  193. $productInfo['attr'] = [
  194. 'pic' => $result['image'] ?? '',
  195. 'price' => $result['price'] ? floatval($result['price']) : 0,
  196. 'cost' => $result['cost'] ? floatval($result['cost']) : 0,
  197. 'ot_price' => $result['ot_price'] ? floatval($result['ot_price']) : 0,
  198. 'stock' => $result['stock'] ? floatval($result['stock']) : 0,
  199. 'capacity' => $result['capacity'] ? floatval($result['capacity']) : 0,
  200. 'bar_code' => $result['bar_code'] ?? '',
  201. 'weight' => $result['weight'] ? floatval($result['weight']) : 0,
  202. 'volume' => $result['volume'] ? floatval($result['volume']) : 0,
  203. 'brokerage' => $result['brokerage'] ? floatval($result['brokerage']) : 0,
  204. ];
  205. }
  206. $data['productInfo'] = $productInfo;
  207. }
  208. return app('json')->success($data);
  209. }
  210. /**
  211. * 保存新建或编辑
  212. * @param $id
  213. * @return mixed
  214. * @throws \Exception
  215. */
  216. public function save($id)
  217. {
  218. $data = UtilService::getMore([
  219. 'store_name',
  220. 'cate_id',
  221. 'factory_id',
  222. 'code',
  223. 'keyword',
  224. 'store_info',
  225. 'material',
  226. 'craft',
  227. 'packing',
  228. ['unit_name', '件'],
  229. ['postage', 0],
  230. ['image', []],
  231. ['slider_image', []],
  232. ['video_link', ''],
  233. ['detail_image', []],
  234. ['spec_type', 0],
  235. ['ficti', 100],
  236. ['sales', 0],
  237. ['sort', 0],
  238. 'label',
  239. ['is_show', 0],
  240. ['packet', 0],
  241. ['issuing', 0],
  242. ['is_leftover', 0],
  243. ['is_hot', 0],
  244. ['is_new', 0],
  245. ['is_best', 0],
  246. ['is_sub', 0],
  247. ['description', ''],
  248. ['items', []],
  249. ['attrs', []]
  250. ]);
  251. //$data['mer_id'] = $this->merId ?: '';
  252. $detail = $data['attrs'];
  253. $data['price'] = min(array_column($detail, 'price'));
  254. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  255. $data['cost'] = min(array_column($detail, 'cost'));
  256. $attr = $data['items'];
  257. unset($data['items'], $data['video'], $data['attrs']);
  258. if (!$data['cate_id']) return app('json')->fail('请选择商品分类');
  259. if (!$data['factory_id']) return app('json')->fail('请选择供货商家');
  260. if (!$data['store_name']) return app('json')->fail('请输入商品名称');
  261. if (empty($data['image'])) return app('json')->fail('请上传商品图片');
  262. if (count($data['slider_image']) < 1) return app('json')->fail('请上传商品轮播图');
  263. $data['slider_image'] = json_encode($data['slider_image']);
  264. $data['detail_image'] = json_encode($data['detail_image']);
  265. $data['stock'] = array_sum(array_column($detail, 'stock'));
  266. ProductModel::beginTrans();
  267. foreach ($detail as &$item) {
  268. if ($item['brokerage'] > $item['price']) {
  269. return app('json')->fail('佣金不能大于商品售价');
  270. }
  271. }
  272. if ($id) {
  273. unset($data['sales']);
  274. ProductModel::where('id',$id)->save($data);
  275. if ($data['spec_type'] == 0) {
  276. $attr = [
  277. [
  278. 'value' => '规格',
  279. 'detailValue' => '',
  280. 'attrHidden' => '',
  281. 'detail' => ['默认']
  282. ]
  283. ];
  284. $detail[0]['value1'] = '规格';
  285. $detail[0]['detail'] = ['规格' => '默认'];
  286. }
  287. $attr_res = ProductAttr::createProductAttr($attr, $detail, $id);
  288. if ($attr_res) {
  289. ProductModel::commitTrans();
  290. return app('json')->success('修改成功!');
  291. } else {
  292. ProductModel::rollbackTrans();
  293. return app('json')->fail(ProductAttr::getErrorInfo());
  294. }
  295. } else {
  296. $data['add_time'] = time();
  297. $data['code_path'] = '';
  298. $res = ProductModel::create($data);
  299. ProductModel::where('id',$res['id'])->update(['code' => sprintf("%05d", $res['id'])]);
  300. if ($data['spec_type'] == 0) {
  301. $attr = [
  302. [
  303. 'value' => '规格',
  304. 'detailValue' => '',
  305. 'attrHidden' => '',
  306. 'detail' => ['默认']
  307. ]
  308. ];
  309. $detail[0]['value1'] = '规格';
  310. $detail[0]['detail'] = ['规格' => '默认'];
  311. }
  312. $attr_res = ProductAttr::createProductAttr($attr, $detail, $res['id']);
  313. if ($attr_res) {
  314. ProductModel::commitTrans();
  315. return app('json')->success('添加商品成功!');
  316. } else {
  317. ProductModel::rollbackTrans();
  318. return app('json')->fail(ProductAttr::getErrorInfo());
  319. }
  320. }
  321. }
  322. /**
  323. * 删除指定资源
  324. *
  325. * @param int $id
  326. * @return \think\Response
  327. */
  328. public function delete($id)
  329. {
  330. if (!$id) return app('json')->fail('数据不存在');
  331. if (!ProductModel::be(['id' => $id])) return app('json')->fail('商品数据不存在');
  332. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  333. $data['is_del'] = 0;
  334. if (!ProductModel::where('id',$id)->save($data))
  335. return app('json')->fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  336. else
  337. return app('json')->success('成功恢复商品!');
  338. } else {
  339. $data['is_del'] = 1;
  340. $data['is_show'] = 0;
  341. if (!ProductModel::where('id',$id)->save($data))
  342. return app('json')->fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  343. else
  344. return app('json')->success('成功移到回收站!');
  345. }
  346. }
  347. /**
  348. * 修改状态
  349. * @param string $is_show
  350. * @param string $id
  351. * @return mixed
  352. */
  353. public function set_show($is_show = '', $id = '')
  354. {
  355. ($is_show == '' || $id == '') && app('json')->fail('缺少参数');
  356. if (ProductModel::be(['id' => $id, 'is_del' => 1])) return app('json')->fail('商品已删除,不能上架');
  357. $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  358. if ($res) {
  359. return app('json')->success($is_show == 1 ? '上架成功' : '下架成功');
  360. } else {
  361. return app('json')->fail($is_show == 1 ? '上架失败' : '下架失败');
  362. }
  363. }
  364. /**
  365. * 设置批量商品上架
  366. * @return mixed
  367. */
  368. public function product_show()
  369. {
  370. $post = UtilService::getMore([
  371. ['ids', []]
  372. ]);
  373. if (empty($post['ids'])) {
  374. return app('json')->fail('请选择需要上架的商品');
  375. } else {
  376. $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
  377. if ($res !== false)
  378. return app('json')->success('上架成功');
  379. else
  380. return app('json')->fail('上架失败');
  381. }
  382. }
  383. /**
  384. * 产品信息
  385. */
  386. public function product_info($id)
  387. {
  388. $info = ProductModel::where('id', $id)->field('id,store_name,image,price,ot_price,cost,postage')->find()->toArray();
  389. $score = Db::name("product_score")->where('product_id', $id)->find();
  390. if($score){
  391. $info['attribute'] = json_decode($score['attribute'], true);
  392. $info['stars'] = $score['stars'];
  393. }
  394. return app('json')->success($info);
  395. }
  396. /**
  397. * 产品评分
  398. */
  399. public function score()
  400. {
  401. $data = UtilService::getMore([
  402. 'product_id',
  403. ['attribute', []],
  404. 'stars'
  405. ]);
  406. $data["attribute"] = json_encode($data["attribute"]);
  407. ProductModel::where('id', $data['product_id'])->update(['stars'=>$data["stars"]]);
  408. $score = Db::name("product_score")->where('product_id', $data['product_id'])->find();
  409. if($score){
  410. $res = Db::name("product_score")->where('product_id', $data['product_id'])->update($data);
  411. }else{
  412. $res = Db::name("product_score")->insert($data);
  413. }
  414. if ($res)
  415. return app('json')->success('产品评分成功');
  416. else
  417. return app('json')->fail('产品评分失败');
  418. }
  419. }