Goods.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. 'show_image' => $data['cover']
  182. ], 'goods_spec', [
  183. 'goods_code' => $data['code'],
  184. ]);
  185. }
  186. }
  187. /**
  188. * 表单结果处理
  189. * @param boolean $result
  190. * @throws \think\db\exception\DataNotFoundException
  191. * @throws \think\db\exception\DbException
  192. * @throws \think\db\exception\ModelNotFoundException
  193. */
  194. protected function _form_result(bool $result)
  195. {
  196. if ($result && $this->request->isPost()) {
  197. // GoodsService::stock(input('code'));
  198. $this->success('商品编辑成功!', 'javascript:history.back()');
  199. }
  200. }
  201. /**
  202. * 商品库存入库
  203. * @auth true
  204. * @throws \think\db\exception\DataNotFoundException
  205. * @throws \think\db\exception\DbException
  206. * @throws \think\db\exception\ModelNotFoundException
  207. */
  208. public function stock()
  209. {
  210. $map = $this->_vali(['code.require' => '商品编号不能为空哦!']);
  211. if ($this->request->isGet()) {
  212. $list = ShopGoods::mk()->where($map)->select()->toArray();
  213. if (empty($list)) $this->error('无效的商品数据,请稍候再试!');
  214. [$this->vo] = GoodsService::bindData($list);
  215. $this->fetch();
  216. } else {
  217. [$data, $post, $batch] = [[], $this->request->post(), CodeExtend::uniqidDate(12, 'B')];
  218. if (isset($post['goods_code']) && is_array($post['goods_code'])) {
  219. foreach (array_keys($post['goods_code']) as $key) {
  220. if ($post['goods_stock'][$key] > 0) $data[] = [
  221. 'batch_no' => $batch,
  222. 'goods_code' => $post['goods_code'][$key],
  223. 'goods_spec' => $post['goods_spec'][$key],
  224. 'goods_stock' => $post['goods_stock'][$key],
  225. ];
  226. }
  227. if (!empty($data)) {
  228. ShopGoodsStock::mk()->insertAll($data);
  229. GoodsService::stock($map['code']);
  230. $this->success('商品数据入库成功!');
  231. }
  232. }
  233. $this->error('没有需要商品入库的数据!');
  234. }
  235. }
  236. /**
  237. * 商品上下架
  238. * @auth true
  239. */
  240. public function state()
  241. {
  242. ShopGoods::mSave($this->_vali([
  243. 'status.in:0,1' => '状态值范围异常!',
  244. 'status.require' => '状态值不能为空!',
  245. ]), 'code');
  246. }
  247. /**
  248. * 删除商品数据
  249. * @auth true
  250. */
  251. public function remove()
  252. {
  253. ShopGoods::mSave($this->_vali([
  254. 'deleted.in:0,1' => '状态值范围异常!',
  255. 'deleted.require' => '状态值不能为空!',
  256. ]), 'code');
  257. }
  258. /**
  259. * 商品数据审核
  260. * @auth true
  261. * @menu true
  262. * @throws \think\db\exception\DataNotFoundException
  263. * @throws \think\db\exception\DbException
  264. * @throws \think\db\exception\ModelNotFoundException
  265. */
  266. public function examine()
  267. {
  268. $this->title = '商品数据审核';
  269. $query = ShopGoods::mQuery();
  270. // 加载对应数据
  271. $query->where(['deleted' => 0]);
  272. // 列表排序并显示
  273. $query->where('examine',0)->like('code|name#name')->like('marks,cateids', ',');
  274. $query->equal('status,vip_entry,truck_type,rebate_type')->order('sort desc,id desc')->page();
  275. }
  276. /**
  277. * 商品审核
  278. * @auth true
  279. * @menu true
  280. * @throws \think\db\exception\DataNotFoundException
  281. * @throws \think\db\exception\DbException
  282. * @throws \think\db\exception\ModelNotFoundException
  283. */
  284. public function examinex()
  285. {
  286. ShopGoods::mSave($this->_vali([
  287. 'examine.in:0,1,2' => '状态值范围异常!',
  288. 'examine.require' => '状态值不能为空!',
  289. ]), 'code');
  290. }
  291. }