ShopGoods.php 10.0 KB

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