Goods.php 9.2 KB

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