Goods.php 20 KB

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