Goods.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. //审核拒绝的情况
  184. if (!empty($data['examine_cause'])){
  185. $updata = [
  186. 'examine_cause'=>$data['examine_cause'],
  187. 'examine'=>2,
  188. 'examine_time'=>date('Y-m-d H:i:s'),
  189. 'examine_adminid'=>AdminService::getUserName()
  190. ];
  191. ShopGoods::update($updata,['id'=>$data['id']]);
  192. $this->success('操作成功1', 'javascript:history.back()');
  193. //$this->success('操作成功');
  194. }
  195. $data['admin_id']=AdminService::getUserId();
  196. // $data['examine']=0;
  197. $good = ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->find();
  198. if($good){
  199. $data['admin_id'] = $good['admin_id'];
  200. }
  201. if (empty($data['cover'])) $this->error('商品图片不能为空!');
  202. // if (empty($data['slider'])) $this->error('轮播图片不能为空!');
  203. // if (empty($data['payment'])) $this->error('支付方式不能为空!');
  204. [$data['price_market'], $data['price_selling']] = [0, 0];
  205. $data['stock_total']=0;
  206. [$count, $items] = [0, array_column(json_decode($data['data_items'], true), 0)];
  207. foreach ($items as $item) if ($item['status'] > 0) {
  208. if ($data['price_market'] === 0 || $data['price_market'] > $item['market']) $data['price_market'] = $item['market'];
  209. if ($data['price_selling'] === 0 || $data['price_selling'] > $item['selling']) $data['price_selling'] = $item['selling'];
  210. $data['stock_total']+=$item['stock'];
  211. $count++;
  212. }
  213. // var_dump($count);die;
  214. if (empty($count)) $this->error('无效的的商品价格信息!');
  215. $data['marks'] = arr2str($data['marks'] ?? []);
  216. // $data['payment'] = arr2str($data['payment'] ?? []);
  217. ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->update(['status' => 0]);
  218. ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->delete();
  219. foreach ($items as $item) data_save(ShopGoodsItem::class, [
  220. 'goods_sku' => $item['sku'],
  221. 'goods_spec' => $item['key'],
  222. 'goods_code' => $data['code'],
  223. 'stock_total' => $item['stock'],
  224. 'price_market' => $item['market'],
  225. 'price_selling' => $item['selling'],
  226. 'status' => $item['status'] ? 1 : 0,
  227. 'admin_id' => $data['admin_id'],
  228. 'show_image' => $data['cover']
  229. ], 'goods_spec', [
  230. 'goods_code' => $data['code'],
  231. ]);
  232. }
  233. }
  234. /**
  235. * 表单结果处理
  236. * @param boolean $result
  237. * @throws \think\db\exception\DataNotFoundException
  238. * @throws \think\db\exception\DbException
  239. * @throws \think\db\exception\ModelNotFoundException
  240. */
  241. protected function _form_result(bool $result)
  242. {
  243. if ($result && $this->request->isPost()) {
  244. // GoodsService::stock(input('code'));
  245. $this->success('商品编辑成功!', 'javascript:history.back()');
  246. }
  247. }
  248. /**
  249. * 商品库存入库
  250. * @auth true
  251. * @throws \think\db\exception\DataNotFoundException
  252. * @throws \think\db\exception\DbException
  253. * @throws \think\db\exception\ModelNotFoundException
  254. */
  255. public function stock()
  256. {
  257. $map = $this->_vali(['code.require' => '商品编号不能为空哦!']);
  258. if ($this->request->isGet()) {
  259. $list = ShopGoods::mk()->where($map)->select()->toArray();
  260. if (empty($list)) $this->error('无效的商品数据,请稍候再试!');
  261. [$this->vo] = GoodsService::bindData($list);
  262. $this->fetch();
  263. } else {
  264. [$data, $post, $batch] = [[], $this->request->post(), CodeExtend::uniqidDate(12, 'B')];
  265. if (isset($post['goods_code']) && is_array($post['goods_code'])) {
  266. foreach (array_keys($post['goods_code']) as $key) {
  267. if ($post['goods_stock'][$key] > 0) $data[] = [
  268. 'batch_no' => $batch,
  269. 'goods_code' => $post['goods_code'][$key],
  270. 'goods_spec' => $post['goods_spec'][$key],
  271. 'goods_stock' => $post['goods_stock'][$key],
  272. ];
  273. }
  274. if (!empty($data)) {
  275. ShopGoodsStock::mk()->insertAll($data);
  276. GoodsService::stock($map['code']);
  277. $this->success('商品数据入库成功!');
  278. }
  279. }
  280. $this->error('没有需要商品入库的数据!');
  281. }
  282. }
  283. /**
  284. * 商品上下架
  285. * @auth true
  286. */
  287. public function state()
  288. {
  289. ShopGoods::mSave($this->_vali([
  290. 'status.in:0,1' => '状态值范围异常!',
  291. 'status.require' => '状态值不能为空!',
  292. ]), 'code');
  293. }
  294. /**
  295. * 删除商品数据
  296. * @auth true
  297. */
  298. public function remove()
  299. {
  300. ShopGoods::mSave($this->_vali([
  301. 'deleted.in:0,1' => '状态值范围异常!',
  302. 'deleted.require' => '状态值不能为空!',
  303. ]), 'code');
  304. }
  305. /**
  306. * 商品数据审核
  307. * @auth true
  308. * @menu true
  309. * @throws \think\db\exception\DataNotFoundException
  310. * @throws \think\db\exception\DbException
  311. * @throws \think\db\exception\ModelNotFoundException
  312. */
  313. public function examine()
  314. {
  315. if(AdminService::getUserId()==10000){
  316. $admin_id = [];
  317. }
  318. else{
  319. $admin_id['admin_id']=AdminService::getUserId();
  320. }
  321. $this->title = '商品数据审核';
  322. $query = ShopGoods::mQuery();
  323. // 加载对应数据
  324. $query->where(['deleted' => 0])->where($admin_id);
  325. // 列表排序并显示
  326. $query->where('examine','in','0,2')->like('code|name#name')->like('marks,cateids', ',');
  327. $list = $query->equal('status,vip_entry,truck_type,rebate_type')->order('sort desc,id desc')->select();
  328. $list->page();
  329. }
  330. protected function _examine_page_filter(&$data)
  331. {
  332. foreach ($data as &$v){
  333. $arr = explode("|", $v['cover']);
  334. $v['cover'] = $arr[0];
  335. $users = SystemUser::mk()->whereIn('id', $v['admin_id'])->column('contact_phone,nickname', 'id');
  336. $v['user'] = $users[$v['admin_id']];
  337. }
  338. }
  339. /**
  340. * 商品审核
  341. * @auth true
  342. * @menu true
  343. * @throws \think\db\exception\DataNotFoundException
  344. * @throws \think\db\exception\DbException
  345. * @throws \think\db\exception\ModelNotFoundException
  346. */
  347. public function examinex()
  348. {
  349. // if ($this->request->isGet()) {
  350. // $query = BasePostageCompany::mk()->where(['deleted' => 0, 'status' => 1]);
  351. // $this->items = $query->order('sort desc,id desc')->select()->toArray();
  352. // }
  353. if(input('examine')==1){
  354. ShopGoods::mSave($this->_vali([
  355. 'examine.in:0,1,2' => '状态值范围异常!',
  356. 'examine.require' => '状态值不能为空!',
  357. 'examine_time' => date('Y-m-d H:i:s'),
  358. 'examine_adminid' => AdminService::getUserName()
  359. ]), 'code');
  360. }else{
  361. ShopGoods::mForm('examinex_form', 'id');
  362. }
  363. }
  364. /**
  365. * 商品审核1
  366. * @auth true
  367. * @menu true
  368. * @throws \think\db\exception\DataNotFoundException
  369. * @throws \think\db\exception\DbException
  370. * @throws \think\db\exception\ModelNotFoundException
  371. */
  372. public function examines()
  373. {
  374. // if ($this->request->isGet()) {
  375. // $query = BasePostageCompany::mk()->where(['deleted' => 0, 'status' => 1]);
  376. // $this->items = $query->order('sort desc,id desc')->select()->toArray();
  377. // }
  378. if(input('examine')==1){
  379. $code = input('code');
  380. ShopGoods::where('code',$code)->update(['examine'=>1]);
  381. $this->success('商品编辑成功!', 'javascript:history.back()');
  382. }else{
  383. ShopGoods::mForm('examinex_form', 'id');
  384. }
  385. }
  386. /**
  387. * 商品编辑
  388. * @param array $data
  389. * @return void
  390. */
  391. public function examinex_edit(){
  392. if (empty($data['code'])) {
  393. $data['code'] = CodeExtend::uniqidNumber(20, 'G');
  394. }
  395. if ($this->request->isGet()) {
  396. $data['marks'] = str2arr($data['marks'] ?? '');
  397. $data['payment'] = str2arr($data['payment'] ?? '');
  398. $data['cateids'] = str2arr($data['cateids'] ?? '');
  399. // 其他表单数据
  400. $this->marks = ShopGoodsMark::items();
  401. $this->cates = ShopGoodsCate::treeTable(true);
  402. $this->trucks = ExpressService::templates();
  403. $this->upgrades = BaseUserUpgrade::items();
  404. $this->payments = BaseUserPayment::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('type,code,name', 'code');
  405. $this->discounts = BaseUserDiscount::mk()->where(['status' => 1, 'deleted' => 0])->order('sort desc,id desc')->column('id,name,items', 'id');
  406. // 商品规格处理
  407. $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';
  408. $data['data_items'] = json_encode(ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->column($fields, 'goods_spec'), JSON_UNESCAPED_UNICODE);
  409. } elseif ($this->request->isPost()) {
  410. $data['admin_id']=AdminService::getUserId();
  411. $data['examine']=0;
  412. if (empty($data['cover'])) $this->error('商品图片不能为空!');
  413. // if (empty($data['slider'])) $this->error('轮播图片不能为空!');
  414. // if (empty($data['payment'])) $this->error('支付方式不能为空!');
  415. [$data['price_market'], $data['price_selling']] = [0, 0];
  416. $data['stock_total']=0;
  417. [$count, $items] = [0, array_column(json_decode($data['data_items'], true), 0)];
  418. foreach ($items as $item) if ($item['status'] > 0) {
  419. if ($data['price_market'] === 0 || $data['price_market'] > $item['market']) $data['price_market'] = $item['market'];
  420. if ($data['price_selling'] === 0 || $data['price_selling'] > $item['selling']) $data['price_selling'] = $item['selling'];
  421. $data['stock_total']+=$item['stock'];
  422. $count++;
  423. }
  424. if (empty($count)) $this->error('无效的的商品价格信息!');
  425. $data['marks'] = arr2str($data['marks'] ?? []);
  426. // $data['payment'] = arr2str($data['payment'] ?? []);
  427. ShopGoodsItem::mk()->where(['goods_code' => $data['code']])->update(['status' => 0]);
  428. foreach ($items as $item) data_save(ShopGoodsItem::class, [
  429. 'goods_sku' => $item['sku'],
  430. 'goods_spec' => $item['key'],
  431. 'goods_code' => $data['code'],
  432. 'stock_total' => $item['stock'],
  433. 'price_market' => $item['market'],
  434. 'price_selling' => $item['selling'],
  435. 'status' => $item['status'] ? 1 : 0,
  436. 'admin_id' => $data['admin_id'],
  437. 'show_image' => $data['cover']
  438. ], 'goods_spec', [
  439. 'goods_code' => $data['code'],
  440. ]);
  441. }
  442. }
  443. public function _examinex_form_filter(array &$data)
  444. {
  445. if ($this->request->isPost()) {
  446. if (empty($data['examine_cause'])) $this->error('不能为空!');
  447. $updata = [
  448. 'examine_cause'=>$data['examine_cause'],
  449. 'examine'=>2,
  450. 'examine_time'=>date('Y-m-d H:i:s'),
  451. 'examine_adminid'=>AdminService::getUserName()
  452. ];
  453. ShopGoods::update($updata,['id'=>$data['id']]);
  454. //$this->success('操作成功1', 'javascript:history.back()');
  455. $this->success('操作成功');
  456. }
  457. }
  458. }