Goods.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. // 列表排序并显示
  49. $query->like('code|name#name,cateids')->like('marks', ',');
  50. $query->equal('status,vip_entry,truck_type,rebate_type')->order('sort desc,id desc')->page();
  51. }
  52. protected function _index_page_filter(&$data)
  53. {
  54. //dump($data);die;
  55. }
  56. /**
  57. * 商品选择器
  58. * @login true
  59. * @throws \think\db\exception\DataNotFoundException
  60. * @throws \think\db\exception\DbException
  61. * @throws \think\db\exception\ModelNotFoundException
  62. */
  63. public function select()
  64. {
  65. $query = ShopGoods::mQuery();
  66. $query->equal('status')->like('code,name,marks')->in('cateids');
  67. $query->where(['deleted' => 0])->order('sort desc,id desc')->page();
  68. }
  69. /**
  70. * 数据列表处理
  71. * @param array $data
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\DbException
  74. * @throws \think\db\exception\ModelNotFoundException
  75. */
  76. protected function _page_filter(array &$data)
  77. {
  78. $admin_id=AdminService::getUserId();
  79. $this->marks = ShopGoodsMark::items();
  80. $this->cates = ShopGoodsCate::treeTable();
  81. $this->assign('admin_id',$admin_id);
  82. GoodsService::bindData($data, false);
  83. }
  84. /**
  85. * 添加商品数据
  86. * @auth true
  87. */
  88. public function add()
  89. {
  90. $this->mode = 'add';
  91. $this->title = '添加商品数据';
  92. ShopGoods::mForm('form', 'code');
  93. }
  94. /**
  95. * 编辑商品数据
  96. * @auth true
  97. */
  98. public function edit()
  99. {
  100. $this->mode = 'edit';
  101. $this->title = '编辑商品数据';
  102. ShopGoods::mForm('form', 'code');
  103. }
  104. /**
  105. * 复制编辑商品
  106. * @auth true
  107. */
  108. public function copy()
  109. {
  110. $this->mode = 'copy';
  111. $this->title = '复制编辑商品';
  112. ShopGoods::mForm('form', 'code');
  113. }
  114. /**
  115. * 表单数据处理
  116. * @param array $data
  117. */
  118. protected function _copy_form_filter(array &$data)
  119. {
  120. if ($this->request->isPost()) {
  121. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  122. }
  123. }
  124. /**
  125. * 表单数据处理
  126. * @param array $data
  127. * @throws \think\db\exception\DataNotFoundException
  128. * @throws \think\db\exception\DbException
  129. * @throws \think\db\exception\ModelNotFoundException
  130. */
  131. protected function _form_filter(array &$data)
  132. {
  133. if (empty($data['code'])) {
  134. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  135. }
  136. if ($this->request->isGet()) {
  137. $data['marks'] = str2arr($data['marks'] ?? '');
  138. $data['payment'] = str2arr($data['payment'] ?? '');
  139. $data['cateids'] = str2arr($data['cateids'] ?? '');
  140. // 其他表单数据
  141. $this->marks = ShopGoodsMark::items();
  142. $this->cates = ShopGoodsCate::treeTable(true);
  143. $this->trucks = ExpressService::templates();
  144. $this->upgrades = BaseUserUpgrade::items();
  145. $this->payments = BaseUserPayment::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('type,code,name', 'code');
  146. $this->discounts = BaseUserDiscount::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('id,name,items', 'id');
  147. // 商品规格处理
  148. $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';
  149. $data['data_items'] = json_encode(ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->column($fields, 'goods_spec'), JSON_UNESCAPED_UNICODE);
  150. } elseif ($this->request->isPost()) {
  151. $data['admin_id']=AdminService::getUserId();
  152. $data['examine']=0;
  153. if (empty($data['cover'])) $this->error('商品图片不能为空!');
  154. if (empty($data['slider'])) $this->error('轮播图片不能为空!');
  155. // if (empty($data['payment'])) $this->error('支付方式不能为空!');
  156. [$data['price_market'], $data['price_selling']] = [0, 0];
  157. $data['stock_total']=10;
  158. [$count, $items] = [0, array_column(json_decode($data['data_items'], true), 0)];
  159. foreach ($items as $item) if ($item['status'] > 0) {
  160. if ($data['price_market'] === 0 || $data['price_market'] > $item['market']) $data['price_market'] = $item['market'];
  161. if ($data['price_selling'] === 0 || $data['price_selling'] > $item['selling']) $data['price_selling'] = $item['selling'];
  162. $data['stock_total']+=$item['stock'];
  163. $count++;
  164. }
  165. if (empty($count)) $this->error('无效的的商品价格信息!');
  166. $data['marks'] = arr2str($data['marks'] ?? []);
  167. // $data['payment'] = arr2str($data['payment'] ?? []);
  168. ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->update(['status' => 0]);
  169. foreach ($items as $item) data_save(ShopGoodsItem::class, [
  170. 'goods_sku' => $item['sku'],
  171. 'goods_spec' => $item['key'],
  172. 'goods_code' => $data['code'],
  173. 'stock_total' => $item['stock'],
  174. 'price_market' => $item['market'],
  175. 'price_selling' => $item['selling'],
  176. 'number_virtual' => $item['virtual'],
  177. 'number_express' => $item['express'],
  178. 'reward_balance' => $item['balance'],
  179. 'reward_integral' => $item['integral'],
  180. 'status' => $item['status'] ? 1 : 0,
  181. 'admin_id' => $data['admin_id']
  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. }