Goods.php 14 KB

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