Goods.php 17 KB

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