Goods.php 9.3 KB

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