Goods.php 11 KB

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