Goods.php 16 KB

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