Goods.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\GoodsSize;
  4. use app\admin\model\OrderInfo;
  5. use app\admin\model\User;
  6. use app\common\controller\Backend;
  7. use app\common\model\GoodsBind;
  8. use app\common\model\GoodsDetail;
  9. use app\common\model\GoodsService;
  10. use app\common\model\GoodsSku;
  11. use app\common\model\Refund;
  12. use think\Db;
  13. use think\db\Query;
  14. use Yansongda\Supports\Arr;
  15. /**
  16. * 商品列管理
  17. *
  18. * @icon fa fa-circle-o
  19. */
  20. class Goods extends Backend
  21. {
  22. /**
  23. * Goods模型对象
  24. * @var \app\admin\model\Goods
  25. */
  26. protected $model = null;
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = new \app\admin\model\Goods;
  31. $this->assign('status',\app\admin\model\Goods::getStatus());
  32. $this->assign('category',\app\common\model\Category::mall());
  33. }
  34. public function import()
  35. {
  36. parent::import();
  37. }
  38. /**
  39. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  40. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  41. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  42. */
  43. /**
  44. * 查看
  45. */
  46. public function index()
  47. {
  48. //当前是否为关联查询
  49. $this->relationSearch = true;
  50. //设置过滤方法
  51. $this->request->filter(['strip_tags', 'trim']);
  52. if ($this->request->isAjax()) {
  53. //如果发送的来源是Selectpage,则转发到Selectpage
  54. if ($this->request->request('keyField')) {
  55. return $this->selectpage();
  56. }
  57. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  58. $list = $this->model
  59. ->with(['category','sku'=>function(Query $query){
  60. $query->where('num_stock',0);
  61. }])
  62. ->where($where)
  63. ->order($sort, $order)
  64. ->paginate($limit);
  65. foreach ($list as $row) {
  66. }
  67. $result = array("total" => $list->total(), "rows" => $list->items());
  68. return json($result);
  69. }
  70. return $this->view->fetch();
  71. }
  72. public function add()
  73. {
  74. $this->layout=false;
  75. if($this->request->isGet()){
  76. $this->assign('row',null);
  77. return $this->fetch();
  78. }else{
  79. $this->makeAdd();
  80. }
  81. }
  82. public function edit($ids = null)
  83. {
  84. if($this->request->isGet()){
  85. $row=$this->model->with(['service','size','sku','detail'])->findOrFail($ids)->toArray();
  86. foreach ($row['sku'] as &$_sku){
  87. $_sku['detail']=[];
  88. $_sku['specValues']=$_sku['name'];
  89. foreach (explode(',',$_sku['name']) as $idx=>$name){
  90. $_sku["sku-{$idx}"]=$name;
  91. }
  92. foreach ($row['detail']??[] as $d){
  93. if($_sku['name']==$d['sku_name']){
  94. $_sku['detail'][]=$d;
  95. }
  96. }
  97. }
  98. //return json($row);
  99. $this->assign('row',$row);
  100. return $this->fetch('add');
  101. }else{
  102. $this->makeAdd();
  103. }
  104. }
  105. protected function makeVal(){
  106. $data=$this->request->post();
  107. $data['logo']=array_filter($data['logo']);
  108. $this->validate($data,[
  109. 'name|商品名称'=>['require'],
  110. 'describe|描述'=>['require','max:50'],
  111. 'category_id|商品分类'=>['require'],
  112. 'brand|品牌'=>['require'],
  113. 'is_kill'=>['require'],
  114. 'logo|LOGO'=>['require','array','min:1','max:6'],
  115. ]);
  116. if(empty($data['size'])){
  117. $this->error('请填写规格');
  118. }
  119. if(empty($data['sku'])){
  120. $this->error('请填写参数');
  121. }
  122. foreach ($data['size'] as $key=>$item){
  123. $this->validate($item,[
  124. 'type|规格名称'=>['require']
  125. ]);
  126. }
  127. foreach ($data['sku'] as $key=>$item){
  128. $this->validate($item,[
  129. 'amount_cost|成本价'=>['require','float','egt:0'],
  130. 'num_stock|库存'=>['require','float','egt:0'],
  131. 'num_stock_kill|秒杀库存'=>['require','float','egt:0'],
  132. ]);
  133. if(empty($item['amount_ladder'])){
  134. $this->error('请填写售价区间');
  135. }
  136. foreach ($item['amount_ladder'] as $ladder){
  137. $this->validate($ladder,[
  138. 'min|售价数量区间'=>['require','float','gt:0'],
  139. 'max|售价数量区间'=>['require','float','gt:0'],
  140. 'amount|售价区间价格'=>['require','float','gt:0'],
  141. ]);
  142. }
  143. foreach ($item['detail'] as $d){
  144. $this->validate($d,[
  145. 'name|规格介绍参数名'=>['require','max:20'],
  146. 'value|规格介绍参数值'=>['require','max:20'],
  147. ]);
  148. }
  149. if($data['is_kill']) {
  150. if(empty($item['amount_ladder'])){
  151. $this->error('请填写秒杀价区间');
  152. }
  153. foreach ($item['amount_kill_ladder'] as $ladder) {
  154. $this->validate($ladder, [
  155. 'min|秒杀价数量区间' => ['require', 'float', 'egt:0'],
  156. 'max|秒杀价数量区间' => ['require', 'float', 'egt:0'],
  157. 'amount|秒杀价区间价格' => ['require', 'float', 'egt:0'],
  158. ]);
  159. }
  160. }
  161. }
  162. foreach ($data['service'] as $item){
  163. $this->validate($item,[
  164. 'name|售后服务参数名'=>['require','max:20'],
  165. 'value|售后服务参数值'=>['require','max:20'],
  166. ]);
  167. }
  168. return $data;
  169. }
  170. protected function makeAdd(){
  171. $data=$this->makeVal();
  172. if(!empty($data['id'])){
  173. $goods=$this->model->findOrFail($data['id']);
  174. }else{
  175. $goods=new $this->model;
  176. }
  177. //dd($data);
  178. Db::startTrans();
  179. foreach ($data as $key=>$value){
  180. if(!in_array($key,$goods::$read) && $goods->hasColumn($key)){
  181. $goods[$key]=$value;
  182. }
  183. }
  184. $goods->allowField(true)->save();
  185. $sizeId=[];
  186. foreach ($data['size'] as $size){
  187. $size['goods_id']=$goods['id'];
  188. if(!empty($size['id'])){
  189. $model=GoodsSize::get($size['id']);
  190. $model['names']=$size['names'];
  191. $model['type']=$size['type'];
  192. $model->save();
  193. }else{
  194. $model=GoodsSize::create($size);
  195. }
  196. $sizeId[]=$model['id'];
  197. }
  198. $goods->size()->whereNotIn('id',$sizeId)->delete();
  199. $skuId=[];
  200. $detailId=[];
  201. foreach ($data['sku'] as $key=>$_sku){
  202. $sku=Arr::only($_sku,['amount_cost','amount_kill_ladder','amount_ladder','num_stock','num_stock_kill','temp_ids']);
  203. $sku['name']=$_sku['specValues'];
  204. $sku['goods_id']=$goods['id'];
  205. if(!empty($sku['id'])){
  206. $model=GoodsSku::get($sku['id']);
  207. $model->allowField(true)->save($sku);
  208. }else{
  209. $model=GoodsSku::create($sku);
  210. }
  211. $skuId[]=$model['id'];
  212. foreach ($_sku['detail'] as $detail){
  213. $detail['sku_name']=$sku['name'];
  214. $detail['temp_ids']=$sku['temp_ids'];
  215. if(!empty($detail['id'])){
  216. $model=GoodsDetail::get($detail['id']);
  217. $model->allowField(true)->save($detail);
  218. }else{
  219. $detail['goods_id']=$goods['id'];
  220. $model=GoodsDetail::create($detail);
  221. }
  222. $detailId[]=$model['id'];
  223. }
  224. }
  225. $goods->sku()->whereNotIn('id',$skuId)->chunk(10000,function ($sku){
  226. foreach ($sku as $item){
  227. $item->delete();
  228. }
  229. });
  230. $goods->detail()->whereNotIn('id',$detailId)->delete();
  231. $serviceId=[];
  232. foreach ($data['service'] as $service){
  233. if(!empty($service['id'])){
  234. $model=GoodsService::get($service['id']);
  235. $model->allowField(true)->save($service);
  236. }else{
  237. $service['goods_id']=$goods['id'];
  238. $model=GoodsService::create($service);
  239. }
  240. $serviceId[]=$model['id'];
  241. }
  242. $goods->service()->whereNotIn('id',$serviceId)->delete();
  243. $goods->setAmount();
  244. Db::commit();
  245. $this->success('成功');
  246. }
  247. public function bind_goods($ids){
  248. if($this->request->isGet()){
  249. $goods=$this->model->find($ids);
  250. $this->assign('row',$goods);
  251. return $this->fetch();
  252. }else{
  253. $data=input('row/a');
  254. foreach ($data as $goods_sku_id=>$info){
  255. $skuIds=array_filter(explode(',',$info['goods_id']));
  256. $sku=GoodsSku::find($goods_sku_id);
  257. $arr=[];
  258. foreach ($skuIds as $skuId){
  259. $bind_sku=GoodsSku::find($skuId);
  260. $arr[$skuId]=[
  261. 'goods_id'=>$ids,
  262. 'bind_goods_id'=>$bind_sku['goods_id'],
  263. ];
  264. }
  265. $sku->bindSku()->sync($arr);
  266. }
  267. $this->success();
  268. }
  269. }
  270. #商品统计
  271. public function goods_statistics($ids){
  272. $row=$this->model->find($ids);
  273. $this->assign('row',$row);
  274. $time=input('time')?:sprintf('%s - %s',date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59'));
  275. $this->assign('time',$time);
  276. $timeArr=explode(' - ',$time);
  277. $this->assign('viewCount',$row->viewFavCart()->typeView()->filterDate($timeArr,'date','date')->sum('num'));
  278. $this->assign('favCount',$row->viewFavCart()->typeFav()->filterDate($timeArr,'date','date')->sum('num'));
  279. $this->assign('cartCount',$row->viewFavCart()->typeCart()->filterDate($timeArr,'date','date')->sum('num'));
  280. #单价
  281. $goodsAmount=OrderInfo::payed()->filterDate($timeArr)
  282. ->where('goods_id',$row['id'])
  283. ->group('goods_id')
  284. ->field('SUM(amount_pay) as amount,SUM(num) as num')
  285. ->find();
  286. $this->assign('goodsAmount',$goodsAmount?bcdiv($goodsAmount['amount'],$goodsAmount['num']):0);
  287. #售后率统计
  288. $goodsNum=OrderInfo::filterDate($timeArr)
  289. ->where('goods_id',$row['id'])
  290. ->sum('num');
  291. $svcNum=Refund::filterDate($timeArr)
  292. ->where('goods_id',$row['id'])
  293. ->filterRefund()
  294. ->sum('num');
  295. $this->assign('svcPer',$goodsNum?bcdiv($svcNum,$goodsNum)*100:0);
  296. return $this->fetch();
  297. }
  298. #采购金额排序
  299. public function goods_buy_rank($ids){
  300. $row=$this->model->find($ids);
  301. //当前是否为关联查询
  302. $this->relationSearch = false;
  303. //设置过滤方法
  304. $this->request->filter(['strip_tags', 'trim']);
  305. if ($this->request->isAjax()) {
  306. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  307. $filter=json_decode(input('filter'),true);
  308. $map=[];
  309. if(!empty($filter['date'])){
  310. $timeArr=explode(' - ',$filter['date']);
  311. $timeArr[0]=strtotime($timeArr[0]);
  312. $timeArr[1]=strtotime($timeArr[1]);
  313. $map['create_time']=['between',$timeArr];
  314. }
  315. $list=OrderInfo::payed()
  316. ->where('goods_id',$row['id'])
  317. ->where($map)
  318. ->group('user_id')
  319. ->field('user_id,SUM(amount_pay) as amount')
  320. ->order('amount','desc')
  321. ->paginate($limit);
  322. if($list->items()) {
  323. $userNames = User::whereIn('id', array_column($list->items(), 'user_id'))->column('nickname', 'id');
  324. }
  325. $index=$list->listRows()*$list->currentPage()-$list->listRows();
  326. foreach ($list as $idx=>$item){
  327. $item['nickname']=$userNames[$item['user_id']]??'-';
  328. $item['rank']=$index+$idx+1;
  329. }
  330. $result = array("total" => $list->total(), "rows" => $list->items());
  331. return json($result);
  332. }
  333. return $this->view->fetch();
  334. }
  335. #订单金额排序
  336. public function order_amount_rank($ids){
  337. $row=$this->model->find($ids);
  338. //当前是否为关联查询
  339. $this->relationSearch = false;
  340. //设置过滤方法
  341. $this->request->filter(['strip_tags', 'trim']);
  342. if ($this->request->isAjax()) {
  343. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  344. $filter=json_decode(input('filter'),true);
  345. $map=[];
  346. if(!empty($filter['date'])){
  347. $timeArr=explode(' - ',$filter['date']);
  348. $timeArr[0]=strtotime($timeArr[0]);
  349. $timeArr[1]=strtotime($timeArr[1]);
  350. $map['create_time']=['between',$timeArr];
  351. }
  352. $list=\app\admin\model\Orders::payed()
  353. ->hasGoods($row['id'])
  354. ->where($map)
  355. ->field('user_id,amount_pay as amount,order_no')
  356. ->order('amount','desc')
  357. ->paginate($limit);
  358. $userNames=User::whereIn('id',array_column($list->items(),'user_id'))->column('nickname','id');
  359. $index=$list->listRows()*$list->currentPage()-$list->listRows();
  360. foreach ($list->items() as $idx=>&$item){
  361. $item['nickname']=$userNames[$item['user_id']]??'-';
  362. $item['rank']=$index+$idx+1;
  363. }
  364. $result = array("total" => $list->total(), "rows" => $list->items());
  365. return json($result);
  366. }
  367. return $this->view->fetch();
  368. }
  369. #订单笔数排名
  370. public function order_num_rank($ids){
  371. $row=$this->model->find($ids);
  372. //当前是否为关联查询
  373. $this->relationSearch = false;
  374. //设置过滤方法
  375. $this->request->filter(['strip_tags', 'trim']);
  376. if ($this->request->isAjax()) {
  377. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  378. $filter=json_decode(input('filter'),true);
  379. $map=[];
  380. if(!empty($filter['date'])){
  381. $timeArr=explode(' - ',$filter['date']);
  382. $timeArr[0]=strtotime($timeArr[0]);
  383. $timeArr[1]=strtotime($timeArr[1]);
  384. $map['create_time']=['between',$timeArr];
  385. }
  386. $list=\app\admin\model\Orders::payed()
  387. ->hasGoods($row['id'])
  388. ->where($map)
  389. ->group('user_id')
  390. ->field('user_id,count(user_id) as num')
  391. ->order('num','desc')
  392. ->paginate($limit);
  393. $userNames=User::whereIn('id',array_column($list->items(),'user_id'))->column('nickname','id');
  394. $index=$list->listRows()*$list->currentPage()-$list->listRows();
  395. foreach ($list as $idx=>$item){
  396. $item['nickname']=$userNames[$item['user_id']]??'-';
  397. $item['rank']=$index+$idx+1;
  398. }
  399. $result = array("total" => $list->total(), "rows" => $list->items());
  400. return json($result);
  401. }
  402. return $this->view->fetch();
  403. }
  404. }