Product.php 16 KB

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