Goods.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. $db = SystemUser::mQuery()->like('nickname#nickname')->db();
  52. if ($db->getOptions('where')) $query->whereRaw("admin_id = {$db->field('id')->buildSql()}");
  53. // 列表排序并显示
  54. $query->like('code|name#name')
  55. ->like('marks', ',')
  56. ->when($cateids,function ($qu) use ($cateids){
  57. $qu->whereRaw("find_in_set(".$cateids.",cateids)");
  58. });
  59. $query->equal('status,vip_entry,truck_type,rebate_type,examine')->order('sort desc,id desc')->page();
  60. }
  61. protected function _index_page_filter(&$data)
  62. {
  63. foreach ($data as &$v){
  64. $arr = explode("|", $v['cover']);
  65. $v['cover'] = $arr[0];
  66. $users = SystemUser::mk()->whereIn('id', $v['admin_id'])->column('contact_phone,nickname', 'id');
  67. $v['user'] = $users[$v['admin_id']];
  68. }
  69. }
  70. /**
  71. * 商品选择器
  72. * @login true
  73. * @throws \think\db\exception\DataNotFoundException
  74. * @throws \think\db\exception\DbException
  75. * @throws \think\db\exception\ModelNotFoundException
  76. */
  77. public function select()
  78. {
  79. $query = ShopGoods::mQuery();
  80. $query->equal('status')->like('code,name,marks')->in('cateids');
  81. $query->where(['deleted' => 0])->order('sort desc,id desc')->page();
  82. }
  83. /**
  84. * 数据列表处理
  85. * @param array $data
  86. * @throws \think\db\exception\DataNotFoundException
  87. * @throws \think\db\exception\DbException
  88. * @throws \think\db\exception\ModelNotFoundException
  89. */
  90. protected function _page_filter(array &$data)
  91. {
  92. $admin_id=AdminService::getUserId();
  93. $this->marks = ShopGoodsMark::items();
  94. $this->cates = ShopGoodsCate::treeTable();
  95. $this->assign('admin_id',$admin_id);
  96. GoodsService::bindData($data, false);
  97. }
  98. /**
  99. * 添加商品数据
  100. * @auth true
  101. */
  102. public function add()
  103. {
  104. $this->mode = 'add';
  105. $data['type'] = 'add';
  106. $this->type = $data['type'];
  107. $this->title = '添加商品数据';
  108. ShopGoods::mForm('form', 'code');
  109. }
  110. /**
  111. * 编辑商品数据
  112. * @auth true
  113. */
  114. public function edit()
  115. {
  116. $data = input();
  117. if(!isset($data['type'])){
  118. $data['type'] = 'edit';
  119. }
  120. $this->type = $data['type'];
  121. $this->mode = 'edit';
  122. $this->title = '编辑商品数据';
  123. ShopGoods::mForm('form', 'code');
  124. }
  125. protected function _edit_form_filter(&$data)
  126. {
  127. // var_dump($data);die;
  128. $arr = explode("|", $data['cover']);
  129. $data['cover_url'] = $arr[0];
  130. // var_dump($data);die;
  131. // foreach ($data['inde3x'] as &$v){
  132. // $arr = explode("|", $v['show_image']);
  133. // $v['show_image'] = $arr[0];
  134. // }
  135. }
  136. /**
  137. * 复制编辑商品
  138. * @auth true
  139. */
  140. public function copy()
  141. {
  142. $this->mode = 'copy';
  143. $this->title = '复制编辑商品';
  144. ShopGoods::mForm('form', 'code');
  145. }
  146. /**
  147. * 表单数据处理
  148. * @param array $data
  149. */
  150. protected function _copy_form_filter(array &$data)
  151. {
  152. if ($this->request->isPost()) {
  153. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  154. }
  155. }
  156. /**
  157. * 表单数据处理
  158. * @param array $data
  159. * @throws \think\db\exception\DataNotFoundException
  160. * @throws \think\db\exception\DbException
  161. * @throws \think\db\exception\ModelNotFoundException
  162. */
  163. protected function _form_filter(array &$data)
  164. {
  165. if (empty($data['code'])) {
  166. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  167. }
  168. if ($this->request->isGet()) {
  169. $data['marks'] = str2arr($data['marks'] ?? '');
  170. $data['payment'] = str2arr($data['payment'] ?? '');
  171. $data['cateids'] = str2arr($data['cateids'] ?? '');
  172. // 其他表单数据
  173. $this->marks = ShopGoodsMark::items();
  174. $this->cates = ShopGoodsCate::treeTable(true);
  175. $this->trucks = ExpressService::templates();
  176. $this->upgrades = BaseUserUpgrade::items();
  177. $this->payments = BaseUserPayment::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('type,code,name', 'code');
  178. $this->discounts = BaseUserDiscount::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('id,name,items', 'id');
  179. // 商品规格处理
  180. $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,show_image';
  181. $data['data_items'] = json_encode(ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->column($fields, 'goods_spec'), JSON_UNESCAPED_UNICODE);
  182. } elseif ($this->request->isPost()) {
  183. $data['admin_id']=AdminService::getUserId();
  184. $data['examine']=0;
  185. $good = ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->find();
  186. if($good){
  187. $data['admin_id'] = $good['admin_id'];
  188. }
  189. if (empty($data['cover'])) $this->error('商品图片不能为空!');
  190. // if (empty($data['slider'])) $this->error('轮播图片不能为空!');
  191. // if (empty($data['payment'])) $this->error('支付方式不能为空!');
  192. [$data['price_market'], $data['price_selling']] = [0, 0];
  193. $data['stock_total']=0;
  194. [$count, $items] = [0, array_column(json_decode($data['data_items'], true), 0)];
  195. foreach ($items as $item) if ($item['status'] > 0) {
  196. if ($data['price_market'] === 0 || $data['price_market'] > $item['market']) $data['price_market'] = $item['market'];
  197. if ($data['price_selling'] === 0 || $data['price_selling'] > $item['selling']) $data['price_selling'] = $item['selling'];
  198. $data['stock_total']+=$item['stock'];
  199. $count++;
  200. }
  201. if (empty($count)) $this->error('无效的的商品价格信息!');
  202. $data['marks'] = arr2str($data['marks'] ?? []);
  203. // $data['payment'] = arr2str($data['payment'] ?? []);
  204. ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->update(['status' => 0]);
  205. foreach ($items as $item) data_save(ShopGoodsItem::class, [
  206. 'goods_sku' => $item['sku'],
  207. 'goods_spec' => $item['key'],
  208. 'goods_code' => $data['code'],
  209. 'stock_total' => $item['stock'],
  210. 'price_market' => $item['market'],
  211. 'price_selling' => $item['selling'],
  212. 'status' => $item['status'] ? 1 : 0,
  213. 'admin_id' => $data['admin_id'],
  214. 'show_image' => $data['cover']
  215. ], 'goods_spec', [
  216. 'goods_code' => $data['code'],
  217. ]);
  218. }
  219. }
  220. /**
  221. * 表单结果处理
  222. * @param boolean $result
  223. * @throws \think\db\exception\DataNotFoundException
  224. * @throws \think\db\exception\DbException
  225. * @throws \think\db\exception\ModelNotFoundException
  226. */
  227. protected function _form_result(bool $result)
  228. {
  229. if ($result && $this->request->isPost()) {
  230. // GoodsService::stock(input('code'));
  231. $this->success('商品编辑成功!', 'javascript:history.back()');
  232. }
  233. }
  234. /**
  235. * 商品库存入库
  236. * @auth true
  237. * @throws \think\db\exception\DataNotFoundException
  238. * @throws \think\db\exception\DbException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. */
  241. public function stock()
  242. {
  243. $map = $this->_vali(['code.require' => '商品编号不能为空哦!']);
  244. if ($this->request->isGet()) {
  245. $list = ShopGoods::mk()->where($map)->select()->toArray();
  246. if (empty($list)) $this->error('无效的商品数据,请稍候再试!');
  247. [$this->vo] = GoodsService::bindData($list);
  248. $this->fetch();
  249. } else {
  250. [$data, $post, $batch] = [[], $this->request->post(), CodeExtend::uniqidDate(12, 'B')];
  251. if (isset($post['goods_code']) && is_array($post['goods_code'])) {
  252. foreach (array_keys($post['goods_code']) as $key) {
  253. if ($post['goods_stock'][$key] > 0) $data[] = [
  254. 'batch_no' => $batch,
  255. 'goods_code' => $post['goods_code'][$key],
  256. 'goods_spec' => $post['goods_spec'][$key],
  257. 'goods_stock' => $post['goods_stock'][$key],
  258. ];
  259. }
  260. if (!empty($data)) {
  261. ShopGoodsStock::mk()->insertAll($data);
  262. GoodsService::stock($map['code']);
  263. $this->success('商品数据入库成功!');
  264. }
  265. }
  266. $this->error('没有需要商品入库的数据!');
  267. }
  268. }
  269. /**
  270. * 商品上下架
  271. * @auth true
  272. */
  273. public function state()
  274. {
  275. ShopGoods::mSave($this->_vali([
  276. 'status.in:0,1' => '状态值范围异常!',
  277. 'status.require' => '状态值不能为空!',
  278. ]), 'code');
  279. }
  280. /**
  281. * 删除商品数据
  282. * @auth true
  283. */
  284. public function remove()
  285. {
  286. ShopGoods::mSave($this->_vali([
  287. 'deleted.in:0,1' => '状态值范围异常!',
  288. 'deleted.require' => '状态值不能为空!',
  289. ]), 'code');
  290. }
  291. /**
  292. * 商品数据审核
  293. * @auth true
  294. * @menu true
  295. * @throws \think\db\exception\DataNotFoundException
  296. * @throws \think\db\exception\DbException
  297. * @throws \think\db\exception\ModelNotFoundException
  298. */
  299. public function examine()
  300. {
  301. if(AdminService::getUserId()==10000){
  302. $admin_id = [];
  303. }
  304. else{
  305. $admin_id['admin_id']=AdminService::getUserId();
  306. }
  307. $this->title = '商品数据审核';
  308. $query = ShopGoods::mQuery();
  309. // 加载对应数据
  310. $query->where(['deleted' => 0])->where($admin_id);
  311. // 列表排序并显示
  312. $query->where('examine','in','0,2')->like('code|name#name')->like('marks,cateids', ',');
  313. $list = $query->equal('status,vip_entry,truck_type,rebate_type')->order('sort desc,id desc')->select();
  314. $list->page();
  315. }
  316. protected function _examine_page_filter(&$data)
  317. {
  318. foreach ($data as &$v){
  319. $arr = explode("|", $v['cover']);
  320. $v['cover'] = $arr[0];
  321. $users = SystemUser::mk()->whereIn('id', $v['admin_id'])->column('contact_phone,nickname', 'id');
  322. $v['user'] = $users[$v['admin_id']];
  323. }
  324. }
  325. /**
  326. * 商品审核
  327. * @auth true
  328. * @menu true
  329. * @throws \think\db\exception\DataNotFoundException
  330. * @throws \think\db\exception\DbException
  331. * @throws \think\db\exception\ModelNotFoundException
  332. */
  333. public function examinex()
  334. {
  335. // if ($this->request->isGet()) {
  336. // $query = BasePostageCompany::mk()->where(['deleted' => 0, 'status' => 1]);
  337. // $this->items = $query->order('sort desc,id desc')->select()->toArray();
  338. // }
  339. if(input('examine')==1){
  340. ShopGoods::mSave($this->_vali([
  341. 'examine.in:0,1,2' => '状态值范围异常!',
  342. 'examine.require' => '状态值不能为空!',
  343. 'examine_time' => date('Y-m-d H:i:s'),
  344. 'examine_adminid' => AdminService::getUserName()
  345. ]), 'code');
  346. }else{
  347. ShopGoods::mForm('examinex_form', 'id');
  348. }
  349. }
  350. /**
  351. * 商品编辑
  352. * @param array $data
  353. * @return void
  354. */
  355. public function examinex_edit(){
  356. if (empty($data['code'])) {
  357. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  358. }
  359. if ($this->request->isGet()) {
  360. $data['marks'] = str2arr($data['marks'] ?? '');
  361. $data['payment'] = str2arr($data['payment'] ?? '');
  362. $data['cateids'] = str2arr($data['cateids'] ?? '');
  363. // 其他表单数据
  364. $this->marks = ShopGoodsMark::items();
  365. $this->cates = ShopGoodsCate::treeTable(true);
  366. $this->trucks = ExpressService::templates();
  367. $this->upgrades = BaseUserUpgrade::items();
  368. $this->payments = BaseUserPayment::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('type,code,name', 'code');
  369. $this->discounts = BaseUserDiscount::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('id,name,items', 'id');
  370. // 商品规格处理
  371. $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,show_image';
  372. $data['data_items'] = json_encode(ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->column($fields, 'goods_spec'), JSON_UNESCAPED_UNICODE);
  373. } elseif ($this->request->isPost()) {
  374. $data['admin_id']=AdminService::getUserId();
  375. $data['examine']=0;
  376. if (empty($data['cover'])) $this->error('商品图片不能为空!');
  377. // if (empty($data['slider'])) $this->error('轮播图片不能为空!');
  378. // if (empty($data['payment'])) $this->error('支付方式不能为空!');
  379. [$data['price_market'], $data['price_selling']] = [0, 0];
  380. $data['stock_total']=0;
  381. [$count, $items] = [0, array_column(json_decode($data['data_items'], true), 0)];
  382. foreach ($items as $item) if ($item['status'] > 0) {
  383. if ($data['price_market'] === 0 || $data['price_market'] > $item['market']) $data['price_market'] = $item['market'];
  384. if ($data['price_selling'] === 0 || $data['price_selling'] > $item['selling']) $data['price_selling'] = $item['selling'];
  385. $data['stock_total']+=$item['stock'];
  386. $count++;
  387. }
  388. if (empty($count)) $this->error('无效的的商品价格信息!');
  389. $data['marks'] = arr2str($data['marks'] ?? []);
  390. // $data['payment'] = arr2str($data['payment'] ?? []);
  391. ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->update(['status' => 0]);
  392. foreach ($items as $item) data_save(ShopGoodsItem::class, [
  393. 'goods_sku' => $item['sku'],
  394. 'goods_spec' => $item['key'],
  395. 'goods_code' => $data['code'],
  396. 'stock_total' => $item['stock'],
  397. 'price_market' => $item['market'],
  398. 'price_selling' => $item['selling'],
  399. 'status' => $item['status'] ? 1 : 0,
  400. 'admin_id' => $data['admin_id'],
  401. 'show_image' => $data['cover']
  402. ], 'goods_spec', [
  403. 'goods_code' => $data['code'],
  404. ]);
  405. }
  406. }
  407. public function _examinex_form_filter(array &$data)
  408. {
  409. if ($this->request->isPost()) {
  410. if (empty($data['examine_cause'])) $this->error('不能为空!');
  411. $updata = [
  412. 'examine_cause'=>$data['examine_cause'],
  413. 'examine'=>2,
  414. 'examine_time'=>date('Y-m-d H:i:s'),
  415. 'examine_adminid'=>AdminService::getUserName()
  416. ];
  417. ShopGoods::update($updata,['id'=>$data['id']]);
  418. $this->success('操作成功');
  419. }
  420. }
  421. }