Goods.php 12 KB

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