Goods.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. namespace app\data\controller\shop;
  3. use app\data\model\ShopGoods;
  4. use app\data\model\ShopGoodsStock;
  5. use app\data\service\ExpressService;
  6. use app\data\service\GoodsService;
  7. use app\data\service\UserUpgradeService;
  8. use think\admin\Controller;
  9. use think\admin\extend\CodeExtend;
  10. /**
  11. * 商品数据管理
  12. * Class Goods
  13. * @package app\data\controller\shop
  14. */
  15. class Goods extends Controller
  16. {
  17. /**
  18. * 最大分类等级
  19. * @var integer
  20. */
  21. protected $cateLevel;
  22. /**
  23. * 商品数据管理
  24. * @auth true
  25. * @menu true
  26. * @throws \think\db\exception\DataNotFoundException
  27. * @throws \think\db\exception\DbException
  28. * @throws \think\db\exception\ModelNotFoundException
  29. */
  30. public function index()
  31. {
  32. $this->title = '商品数据管理';
  33. $query = $this->_query(ShopGoods::class);
  34. // 加载对应数据
  35. $this->type = $this->request->get('type', 'index');
  36. if ($this->type === 'index') $query->where(['deleted' => 0]);
  37. elseif ($this->type === 'recycle') $query->where(['deleted' => 1]);
  38. else $this->error("无法加载 {$this->type} 数据列表!");
  39. // 列表排序并显示
  40. $query->like('code,name')->like('marks,cateids', ',');
  41. $query->equal('status,vip_entry,truck_type,rebate_type')->order('sort desc,id desc')->page();
  42. }
  43. /**
  44. * 商品选择器
  45. * @login true
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function select()
  51. {
  52. $query = $this->_query(ShopGoods::mk());
  53. $query->equal('status')->like('code,name,marks')->in('cateids');
  54. $query->where(['deleted' => 0])->order('sort desc,id desc')->page();
  55. }
  56. /**
  57. * 数据列表处理
  58. * @param array $data
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. protected function _page_filter(array &$data)
  64. {
  65. $this->marks = GoodsService::instance()->getMarkData();
  66. $this->cates = GoodsService::instance()->getCateTree('arr2table');
  67. GoodsService::instance()->bindData($data, false);
  68. }
  69. /**
  70. * 添加商品数据
  71. * @auth true
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. public function add()
  77. {
  78. $this->mode = 'add';
  79. $this->title = '添加商品数据';
  80. $this->_form(ShopGoods::mk(), 'form', 'code');
  81. }
  82. /**
  83. * 编辑商品数据
  84. * @auth true
  85. * @throws \think\db\exception\DataNotFoundException
  86. * @throws \think\db\exception\DbException
  87. * @throws \think\db\exception\ModelNotFoundException
  88. */
  89. public function edit()
  90. {
  91. $this->mode = 'edit';
  92. $this->title = '编辑商品数据';
  93. $this->_form(ShopGoods::mk(), 'form', 'code');
  94. }
  95. /**
  96. * 复制编辑商品
  97. * @auth true
  98. * @throws \think\db\exception\DataNotFoundException
  99. * @throws \think\db\exception\DbException
  100. * @throws \think\db\exception\ModelNotFoundException
  101. */
  102. public function copy()
  103. {
  104. $this->mode = 'copy';
  105. $this->title = '复制编辑商品';
  106. $this->_form(ShopGoods::mk(), 'form', 'code');
  107. }
  108. /**
  109. * 表单数据处理
  110. * @param array $data
  111. */
  112. protected function _copy_form_filter(array &$data)
  113. {
  114. if ($this->request->isPost()) {
  115. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  116. }
  117. }
  118. /**
  119. * 表单数据处理
  120. * @param array $data
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. protected function _form_filter(array &$data)
  126. {
  127. if (empty($data['code'])) {
  128. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  129. }
  130. if ($this->request->isGet()) {
  131. $data['marks'] = str2arr($data['marks'] ?? '');
  132. $data['payment'] = str2arr($data['payment'] ?? '');
  133. $data['cateids'] = str2arr($data['cateids'] ?? '');
  134. // 其他表单数据
  135. $this->marks = GoodsService::instance()->getMarkData();
  136. $this->cates = GoodsService::instance()->getCateData();
  137. $this->trucks = ExpressService::instance()->templates();
  138. $this->upgrades = UserUpgradeService::instance()->levels();
  139. $this->payments = $this->app->db->name('BaseUserPayment')->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('type,code,name', 'code');
  140. $this->discounts = $this->app->db->name('BaseUserDiscount')->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('id,name,items', 'id');
  141. // 商品规格处理
  142. $fields = 'goods_sku `sku`,goods_code,goods_spec `key`,price_selling `selling`,price_market `market`,number_virtual `virtual`,number_express `express`,reward_balance `balance`,reward_integral `integral`,status';
  143. $data['data_items'] = json_encode($this->app->db->name('ShopGoodsItem')->where(['goods_code' => $data['code']])->column($fields, 'goods_spec'), JSON_UNESCAPED_UNICODE);
  144. } elseif ($this->request->isPost()) {
  145. if (empty($data['cover'])) $this->error('商品图片不能为空!');
  146. if (empty($data['slider'])) $this->error('轮播图片不能为空!');
  147. if (empty($data['payment'])) $this->error('支付方式不能为空!');
  148. // 商品规格保存
  149. [$data['price_market'], $data['price_selling']] = [0, 0];
  150. [$count, $items] = [0, array_column(json_decode($data['data_items'], true), 0)];
  151. foreach ($items as $item) if ($item['status'] > 0) {
  152. if ($data['price_market'] === 0 || $data['price_market'] > $item['market']) $data['price_market'] = $item['market'];
  153. if ($data['price_selling'] === 0 || $data['price_selling'] > $item['selling']) $data['price_selling'] = $item['selling'];
  154. $count++;
  155. }
  156. if (empty($count)) $this->error('无效的的商品价格信息!');
  157. $data['marks'] = arr2str($data['marks'] ?? []);
  158. $data['payment'] = arr2str($data['payment'] ?? []);
  159. $this->app->db->name('ShopGoodsItem')->where(['goods_code' => $data['code']])->update(['status' => 0]);
  160. foreach ($items as $item) data_save('ShopGoodsItem', [
  161. 'goods_sku' => $item['sku'],
  162. 'goods_spec' => $item['key'],
  163. 'goods_code' => $data['code'],
  164. 'price_market' => $item['market'],
  165. 'price_selling' => $item['selling'],
  166. 'number_virtual' => $item['virtual'],
  167. 'number_express' => $item['express'],
  168. 'reward_balance' => $item['balance'],
  169. 'reward_integral' => $item['integral'],
  170. 'status' => $item['status'] ? 1 : 0,
  171. ], 'goods_spec', [
  172. 'goods_code' => $data['code'],
  173. ]);
  174. }
  175. }
  176. /**
  177. * 表单结果处理
  178. * @param boolean $result
  179. * @throws \think\db\exception\DataNotFoundException
  180. * @throws \think\db\exception\DbException
  181. * @throws \think\db\exception\ModelNotFoundException
  182. */
  183. protected function _form_result(bool $result)
  184. {
  185. if ($result && $this->request->isPost()) {
  186. GoodsService::instance()->stock(input('code'));
  187. $this->success('商品编辑成功!', 'javascript:history.back()');
  188. }
  189. }
  190. /**
  191. * 商品库存入库
  192. * @auth true
  193. * @throws \think\db\exception\DataNotFoundException
  194. * @throws \think\db\exception\DbException
  195. * @throws \think\db\exception\ModelNotFoundException
  196. */
  197. public function stock()
  198. {
  199. $map = $this->_vali(['code.require' => '商品编号不能为空哦!']);
  200. if ($this->request->isGet()) {
  201. $list = ShopGoods::mk()->where($map)->select()->toArray();
  202. if (empty($list)) $this->error('无效的商品数据,请稍候再试!');
  203. [$this->vo] = GoodsService::instance()->bindData($list);
  204. $this->fetch();
  205. } else {
  206. [$data, $post, $batch] = [[], $this->request->post(), CodeExtend::uniqidDate(12, 'B')];
  207. if (isset($post['goods_code']) && is_array($post['goods_code'])) {
  208. foreach (array_keys($post['goods_code']) as $key) {
  209. if ($post['goods_stock'][$key] > 0) $data[] = [
  210. 'batch_no' => $batch,
  211. 'goods_code' => $post['goods_code'][$key],
  212. 'goods_spec' => $post['goods_spec'][$key],
  213. 'goods_stock' => $post['goods_stock'][$key],
  214. ];
  215. }
  216. if (!empty($data)) {
  217. ShopGoodsStock::mk()->insertAll($data);
  218. GoodsService::instance()->stock($map['code']);
  219. $this->success('商品数据入库成功!');
  220. }
  221. }
  222. $this->error('没有需要商品入库的数据!');
  223. }
  224. }
  225. /**
  226. * 商品上下架
  227. * @auth true
  228. * @throws \think\db\exception\DbException
  229. */
  230. public function state()
  231. {
  232. $this->_save(ShopGoods::mk(), $this->_vali([
  233. 'status.in:0,1' => '状态值范围异常!',
  234. 'status.require' => '状态值不能为空!',
  235. ]), 'code');
  236. }
  237. /**
  238. * 删除商品数据
  239. * @auth true
  240. * @throws \think\db\exception\DbException
  241. */
  242. public function remove()
  243. {
  244. $this->_save(ShopGoods::mk(), $this->_vali([
  245. 'deleted.in:0,1' => '状态值范围异常!',
  246. 'deleted.require' => '状态值不能为空!',
  247. ]), 'code');
  248. }
  249. }