Product.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. if ($productInfo['spec_type'] == 1) {
  177. $result = ProductAttrResult::getResult($id);
  178. foreach ($result['value'] as $k => $v) {
  179. $num = 1;
  180. foreach ($v['detail'] as $dv) {
  181. $result['value'][$k]['value' . $num] = $dv;
  182. $num++;
  183. }
  184. }
  185. $productInfo['items'] = $result['attr'];
  186. $productInfo['attrs'] = $result['value'];
  187. $productInfo['attr'] = ['pic' => '', 'price' => 0, 'cost' => 0, 'ot_price' => 0, 'stock' => 0, 'capacity' => 0, 'bar_code' => '', 'weight' => 0, 'volume' => 0, 'brokerage' => 0];
  188. } else {
  189. $result = ProductAttrValue::where('product_id', $id)->where('type', 0)->find();
  190. $productInfo['items'] = [];
  191. $productInfo['attrs'] = [];
  192. $productInfo['attr'] = [
  193. 'pic' => $result['image'] ?? '',
  194. 'price' => $result['price'] ? floatval($result['price']) : 0,
  195. 'cost' => $result['cost'] ? floatval($result['cost']) : 0,
  196. 'ot_price' => $result['ot_price'] ? floatval($result['ot_price']) : 0,
  197. 'stock' => $result['stock'] ? floatval($result['stock']) : 0,
  198. 'capacity' => $result['capacity'] ? floatval($result['capacity']) : 0,
  199. 'bar_code' => $result['bar_code'] ?? '',
  200. 'weight' => $result['weight'] ? floatval($result['weight']) : 0,
  201. 'volume' => $result['volume'] ? floatval($result['volume']) : 0,
  202. 'brokerage' => $result['brokerage'] ? floatval($result['brokerage']) : 0,
  203. ];
  204. }
  205. $data['productInfo'] = $productInfo;
  206. }
  207. return app('json')->success($data);
  208. }
  209. /**
  210. * 保存新建或编辑
  211. * @param $id
  212. * @return mixed
  213. * @throws \Exception
  214. */
  215. public function save($id)
  216. {
  217. $data = UtilService::getMore([
  218. 'store_name',
  219. 'cate_id',
  220. 'factory_id',
  221. 'code',
  222. 'keyword',
  223. 'store_info',
  224. 'material',
  225. 'craft',
  226. 'packing',
  227. ['unit_name', '件'],
  228. ['postage', 0],
  229. ['image', []],
  230. ['slider_image', []],
  231. ['video_link', ''],
  232. ['spec_type', 0],
  233. ['ficti', 100],
  234. ['sales', 0],
  235. ['sort', 0],
  236. ['is_show', 0],
  237. ['packet', 0],
  238. ['issuing', 0],
  239. ['is_leftover', 0],
  240. ['is_hot', 0],
  241. ['is_new', 0],
  242. ['is_best', 0],
  243. ['is_sub', 0],
  244. ['description', ''],
  245. ['items', []],
  246. ['attrs', []]
  247. ]);
  248. //$data['mer_id'] = $this->merId ?: '';
  249. $detail = $data['attrs'];
  250. $data['price'] = min(array_column($detail, 'price'));
  251. $data['ot_price'] = min(array_column($detail, 'ot_price'));
  252. $data['cost'] = min(array_column($detail, 'cost'));
  253. $attr = $data['items'];
  254. unset($data['items'], $data['video'], $data['attrs']);
  255. if (!$data['cate_id']) return app('json')->fail('请选择商品分类');
  256. if (!$data['factory_id']) return app('json')->fail('请选择供货商家');
  257. if (!$data['store_name']) return app('json')->fail('请输入商品名称');
  258. if (empty($data['image'])) return app('json')->fail('请上传商品图片');
  259. if (count($data['slider_image']) < 1) return app('json')->fail('请上传商品轮播图');
  260. $data['slider_image'] = json_encode($data['slider_image']);
  261. $data['stock'] = array_sum(array_column($detail, 'stock'));
  262. ProductModel::beginTrans();
  263. foreach ($detail as &$item) {
  264. if ($item['brokerage'] > $item['price']) {
  265. return app('json')->fail('佣金不能大于商品售价');
  266. }
  267. }
  268. if ($id) {
  269. unset($data['sales']);
  270. ProductModel::where('id',$id)->save($data);
  271. if ($data['spec_type'] == 0) {
  272. $attr = [
  273. [
  274. 'value' => '规格',
  275. 'detailValue' => '',
  276. 'attrHidden' => '',
  277. 'detail' => ['默认']
  278. ]
  279. ];
  280. $detail[0]['value1'] = '规格';
  281. $detail[0]['detail'] = ['规格' => '默认'];
  282. }
  283. $attr_res = ProductAttr::createProductAttr($attr, $detail, $id);
  284. if ($attr_res) {
  285. ProductModel::commitTrans();
  286. return app('json')->success('修改成功!');
  287. } else {
  288. ProductModel::rollbackTrans();
  289. return app('json')->fail(ProductAttr::getErrorInfo());
  290. }
  291. } else {
  292. $data['add_time'] = time();
  293. $data['code_path'] = '';
  294. $res = ProductModel::create($data);
  295. ProductModel::where('id',$res['id'])->update(['code' => sprintf("%05d", $res['id'])]);
  296. if ($data['spec_type'] == 0) {
  297. $attr = [
  298. [
  299. 'value' => '规格',
  300. 'detailValue' => '',
  301. 'attrHidden' => '',
  302. 'detail' => ['默认']
  303. ]
  304. ];
  305. $detail[0]['value1'] = '规格';
  306. $detail[0]['detail'] = ['规格' => '默认'];
  307. }
  308. $attr_res = ProductAttr::createProductAttr($attr, $detail, $res['id']);
  309. if ($attr_res) {
  310. ProductModel::commitTrans();
  311. return app('json')->success('添加商品成功!');
  312. } else {
  313. ProductModel::rollbackTrans();
  314. return app('json')->fail(ProductAttr::getErrorInfo());
  315. }
  316. }
  317. }
  318. /**
  319. * 删除指定资源
  320. *
  321. * @param int $id
  322. * @return \think\Response
  323. */
  324. public function delete($id)
  325. {
  326. if (!$id) return app('json')->fail('数据不存在');
  327. if (!ProductModel::be(['id' => $id])) return app('json')->fail('商品数据不存在');
  328. if (ProductModel::be(['id' => $id, 'is_del' => 1])) {
  329. $data['is_del'] = 0;
  330. if (!ProductModel::where('id',$id)->save($data))
  331. return app('json')->fail(ProductModel::getErrorInfo('恢复失败,请稍候再试!'));
  332. else
  333. return app('json')->success('成功恢复商品!');
  334. } else {
  335. $data['is_del'] = 1;
  336. $data['is_show'] = 0;
  337. if (!ProductModel::where('id',$id)->save($data))
  338. return app('json')->fail(ProductModel::getErrorInfo('删除失败,请稍候再试!'));
  339. else
  340. return app('json')->success('成功移到回收站!');
  341. }
  342. }
  343. /**
  344. * 修改状态
  345. * @param string $is_show
  346. * @param string $id
  347. * @return mixed
  348. */
  349. public function set_show($is_show = '', $id = '')
  350. {
  351. ($is_show == '' || $id == '') && app('json')->fail('缺少参数');
  352. if (ProductModel::be(['id' => $id, 'is_del' => 1])) return app('json')->fail('商品已删除,不能上架');
  353. $res = ProductModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  354. if ($res) {
  355. return app('json')->success($is_show == 1 ? '上架成功' : '下架成功');
  356. } else {
  357. return app('json')->fail($is_show == 1 ? '上架失败' : '下架失败');
  358. }
  359. }
  360. /**
  361. * 设置批量商品上架
  362. * @return mixed
  363. */
  364. public function product_show()
  365. {
  366. $post = UtilService::getMore([
  367. ['ids', []]
  368. ]);
  369. if (empty($post['ids'])) {
  370. return app('json')->fail('请选择需要上架的商品');
  371. } else {
  372. $res = ProductModel::where('id', 'in', $post['ids'])->update(['is_show' => 1]);
  373. if ($res !== false)
  374. return app('json')->success('上架成功');
  375. else
  376. return app('json')->fail('上架失败');
  377. }
  378. }
  379. /**
  380. * 产品信息
  381. */
  382. public function product_info($id)
  383. {
  384. $info = ProductModel::where('id', $id)->field('id,store_name,image,price,ot_price,cost,postage')->find()->toArray();
  385. $score = Db::name("product_score")->where('product_id', $id)->find();
  386. if($score){
  387. $info['attribute'] = json_decode($score['attribute'], true);
  388. $info['stars'] = $score['stars'];
  389. }
  390. return app('json')->success($info);
  391. }
  392. /**
  393. * 产品评分
  394. */
  395. public function score()
  396. {
  397. $data = UtilService::getMore([
  398. 'product_id',
  399. ['attribute', []],
  400. 'stars'
  401. ]);
  402. $data["attribute"] = json_encode($data["attribute"]);
  403. ProductModel::where('id', $data['product_id'])->update(['stars'=>$data["stars"]]);
  404. $score = Db::name("product_score")->where('product_id', $data['product_id'])->find();
  405. if($score){
  406. $res = Db::name("product_score")->where('product_id', $data['product_id'])->update($data);
  407. }else{
  408. $res = Db::name("product_score")->insert($data);
  409. }
  410. if ($res)
  411. return app('json')->success('产品评分成功');
  412. else
  413. return app('json')->fail('产品评分失败');
  414. }
  415. }