Goods.php 9.3 KB

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