ShopGoods.php 9.9 KB

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