Product.php 17 KB

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