Goods.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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 think\admin\Controller;
  14. use think\admin\extend\CodeExtend;
  15. use think\admin\service\AdminService;
  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. if(AdminService::getUserId()==10000){
  35. $admin_id = [];
  36. }
  37. else{
  38. $admin_id['admin_id']=AdminService::getUserId();
  39. }
  40. $query = ShopGoods::mQuery();
  41. // 加载对应数据
  42. $this->type = $this->request->get('type', 'index');
  43. if ($this->type === 'index') $query->where($admin_id)->where(['deleted' => 0]);
  44. elseif ($this->type === 'recycle') $query->where(['deleted' => 1]);
  45. else $this->error("无法加载 {$this->type} 数据列表!");
  46. // 列表排序并显示
  47. $query->like('code|name#name')->like('marks,cateids', ',');
  48. $query->equal('status,vip_entry,truck_type,rebate_type')->order('sort desc,id desc')->page();
  49. }
  50. /**
  51. * 商品选择器
  52. * @login true
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function select()
  58. {
  59. $query = ShopGoods::mQuery();
  60. $query->equal('status')->like('code,name,marks')->in('cateids');
  61. $query->where(['deleted' => 0])->order('sort desc,id desc')->page();
  62. }
  63. /**
  64. * 数据列表处理
  65. * @param array $data
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. protected function _page_filter(array &$data)
  71. {
  72. $this->marks = ShopGoodsMark::items();
  73. $this->cates = ShopGoodsCate::treeTable();
  74. GoodsService::bindData($data, false);
  75. }
  76. /**
  77. * 添加商品数据
  78. * @auth true
  79. */
  80. public function add()
  81. {
  82. $this->mode = 'add';
  83. $this->title = '添加商品数据';
  84. ShopGoods::mForm('form', 'code');
  85. }
  86. /**
  87. * 编辑商品数据
  88. * @auth true
  89. */
  90. public function edit()
  91. {
  92. $this->mode = 'edit';
  93. $this->title = '编辑商品数据';
  94. ShopGoods::mForm('form', 'code');
  95. }
  96. /**
  97. * 复制编辑商品
  98. * @auth true
  99. */
  100. public function copy()
  101. {
  102. $this->mode = 'copy';
  103. $this->title = '复制编辑商品';
  104. ShopGoods::mForm('form', 'code');
  105. }
  106. /**
  107. * 表单数据处理
  108. * @param array $data
  109. */
  110. protected function _copy_form_filter(array &$data)
  111. {
  112. if ($this->request->isPost()) {
  113. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  114. }
  115. }
  116. /**
  117. * 表单数据处理
  118. * @param array $data
  119. * @throws \think\db\exception\DataNotFoundException
  120. * @throws \think\db\exception\DbException
  121. * @throws \think\db\exception\ModelNotFoundException
  122. */
  123. protected function _form_filter(array &$data)
  124. {
  125. if (empty($data['code'])) {
  126. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  127. }
  128. if ($this->request->isGet()) {
  129. $data['marks'] = str2arr($data['marks'] ?? '');
  130. $data['payment'] = str2arr($data['payment'] ?? '');
  131. $data['cateids'] = str2arr($data['cateids'] ?? '');
  132. // 其他表单数据
  133. $this->marks = ShopGoodsMark::items();
  134. $this->cates = ShopGoodsCate::treeTable(true);
  135. $this->trucks = ExpressService::templates();
  136. $this->upgrades = BaseUserUpgrade::items();
  137. $this->payments = BaseUserPayment::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('type,code,name', 'code');
  138. $this->discounts = BaseUserDiscount::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('id,name,items', 'id');
  139. // 商品规格处理
  140. $fields = 'goods_sku `sku`,goods_code,goods_spec `key`,stock_total `stock`,price_selling `selling`,price_market `market`,number_virtual `virtual`,number_express `express`,reward_balance `balance`,reward_integral `integral`,status';
  141. $data['data_items'] = json_encode(ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->column($fields, 'goods_spec'), JSON_UNESCAPED_UNICODE);
  142. } elseif ($this->request->isPost()) {
  143. $data['admin_id']=AdminService::getUserId();
  144. $data['examine']=0;
  145. if (empty($data['cover'])) $this->error('商品图片不能为空!');
  146. if (empty($data['slider'])) $this->error('轮播图片不能为空!');
  147. // if (empty($data['payment'])) $this->error('支付方式不能为空!');
  148. [$data['price_market'], $data['price_selling']] = [0, 0];
  149. $data['stock_total']=10;
  150. [$count, $items] = [0, array_column(json_decode($data['data_items'], true), 0)];
  151. foreach ($items as $item) if ($item['status'] > 0) {
  152. if ($data['price_market'] === 0 || $data['price_market'] > $item['market']) $data['price_market'] = $item['market'];
  153. if ($data['price_selling'] === 0 || $data['price_selling'] > $item['selling']) $data['price_selling'] = $item['selling'];
  154. $data['stock_total']+=$item['stock'];
  155. $count++;
  156. }
  157. if (empty($count)) $this->error('无效的的商品价格信息!');
  158. $data['marks'] = arr2str($data['marks'] ?? []);
  159. // $data['payment'] = arr2str($data['payment'] ?? []);
  160. ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->update(['status' => 0]);
  161. foreach ($items as $item) data_save(ShopGoodsItem::class, [
  162. 'goods_sku' => $item['sku'],
  163. 'goods_spec' => $item['key'],
  164. 'goods_code' => $data['code'],
  165. 'stock_total' => $item['stock'],
  166. 'price_market' => $item['market'],
  167. 'price_selling' => $item['selling'],
  168. 'number_virtual' => $item['virtual'],
  169. 'number_express' => $item['express'],
  170. 'reward_balance' => $item['balance'],
  171. 'reward_integral' => $item['integral'],
  172. 'status' => $item['status'] ? 1 : 0,
  173. 'admin_id' => $data['admin_id']
  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::stock(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 = ShopGoods::mk()->where($map)->select()->toArray();
  205. if (empty($list)) $this->error('无效的商品数据,请稍候再试!');
  206. [$this->vo] = GoodsService::bindData($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. ShopGoodsStock::mk()->insertAll($data);
  221. GoodsService::stock($map['code']);
  222. $this->success('商品数据入库成功!');
  223. }
  224. }
  225. $this->error('没有需要商品入库的数据!');
  226. }
  227. }
  228. /**
  229. * 商品上下架
  230. * @auth true
  231. */
  232. public function state()
  233. {
  234. ShopGoods::mSave($this->_vali([
  235. 'status.in:0,1' => '状态值范围异常!',
  236. 'status.require' => '状态值不能为空!',
  237. ]), 'code');
  238. }
  239. /**
  240. * 删除商品数据
  241. * @auth true
  242. */
  243. public function remove()
  244. {
  245. ShopGoods::mSave($this->_vali([
  246. 'deleted.in:0,1' => '状态值范围异常!',
  247. 'deleted.require' => '状态值不能为空!',
  248. ]), 'code');
  249. }
  250. /**
  251. * 商品数据审核
  252. * @auth true
  253. * @menu true
  254. * @throws \think\db\exception\DataNotFoundException
  255. * @throws \think\db\exception\DbException
  256. * @throws \think\db\exception\ModelNotFoundException
  257. */
  258. public function examine()
  259. {
  260. $this->title = '商品数据审核';
  261. $query = ShopGoods::mQuery();
  262. // 加载对应数据
  263. $query->where(['deleted' => 0]);
  264. // 列表排序并显示
  265. $query->where('examine',0)->like('code|name#name')->like('marks,cateids', ',');
  266. $query->equal('status,vip_entry,truck_type,rebate_type')->order('sort desc,id desc')->page();
  267. }
  268. /**
  269. * 商品审核
  270. * @auth true
  271. * @menu true
  272. * @throws \think\db\exception\DataNotFoundException
  273. * @throws \think\db\exception\DbException
  274. * @throws \think\db\exception\ModelNotFoundException
  275. */
  276. public function examinex()
  277. {
  278. ShopGoods::mSave($this->_vali([
  279. 'examine.in:0,1,2' => '状态值范围异常!',
  280. 'examine.require' => '状态值不能为空!',
  281. ]), 'code');
  282. }
  283. }