Product.php 15 KB

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