Goods.php 19 KB

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