Goods.php 11 KB

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