Product.php 17 KB

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