Goods.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. $this->layout=false;
  68. if($this->request->isGet()){
  69. $this->assign('row',[
  70. 'sku'=>[
  71. [
  72. 'amount_ladder'=>[],
  73. 'amount_kill_ladder'=>[],
  74. ]
  75. ],
  76. ]);
  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(['sku'=>function($q){}])->findOrFail($ids);
  86. $this->assign('row',$row);
  87. return $this->fetch('add');
  88. }else{
  89. $this->makeAdd();
  90. }
  91. }
  92. protected function makeVal(){
  93. $data=input('row/a');
  94. $data['logo']=array_filter(explode(',',$data['logo']??''));
  95. $this->validate($data,[
  96. 'name|商品名称'=>['require'],
  97. 'describe|描述'=>['require','max:50'],
  98. 'category_id|商品分类'=>['require'],
  99. 'brand|品牌'=>['require'],
  100. 'is_kill'=>['require'],
  101. 'logo|LOGO'=>['require','array','min:1','max:6'],
  102. ]);
  103. foreach ($data['sku'] as $key=>$item){
  104. $rule=[
  105. 'name|规格名'=>['require'],
  106. 'num_stock|规格库存'=>['require','gt:0'],
  107. 'amount_cost|成本价'=>['require','float','egt:0'],
  108. ];
  109. $this->validate($item,$rule);
  110. #售价
  111. $amountLadderAll=$data['amount_ladder'][$key]??[];
  112. foreach ($amountLadderAll as $amount){
  113. $this->validate($amount,[
  114. 'min|售价区间最小数量'=>['require','gt:0'],
  115. 'max|售价区间最大数量'=>['require','gt:0'],
  116. 'amount|售价区间价格'=>['require','gt:0'],
  117. ]);
  118. }
  119. #秒杀价
  120. if($data['is_kill']) {
  121. $amountKillLadderAll = $data['amount_kill_ladder'][$key] ?? [];
  122. foreach ($amountKillLadderAll as $amount) {
  123. $this->validate($amount, [
  124. 'min|秒杀价区间最小数量' => ['require', 'gt:0'],
  125. 'max|秒杀价区间最大数量' => ['require', 'gt:0'],
  126. 'amount|秒杀价区间价格' => ['require', 'gt:0'],
  127. ]);
  128. }
  129. }
  130. }
  131. foreach ($data['detail'] as $item){
  132. $this->validate($item,[
  133. 'name|规格参数名'=>['require'],
  134. 'value|规格参数值'=>['require'],
  135. ]);
  136. }
  137. foreach ($data['service'] as $item){
  138. $this->validate($item,[
  139. 'name|售后服务参数名'=>['require'],
  140. 'value|售后服务参数值'=>['require'],
  141. ]);
  142. }
  143. return $data;
  144. }
  145. protected function makeAdd(){
  146. $data=$this->makeVal();
  147. if(!empty($data['id'])){
  148. $goods=$this->model->findOrFail($data['id']);
  149. }else{
  150. $goods=new $this->model;
  151. }
  152. Db::startTrans();
  153. foreach ($data as $key=>$value){
  154. if(!in_array($key,$goods::$read) && $goods->hasColumn($key)){
  155. $goods[$key]=$value;
  156. }
  157. }
  158. $goods->allowField(true)->save();
  159. $ids=[];
  160. foreach ($data['sku'] as $key=>$sku){
  161. #售价
  162. $amountLadderAll=array_values($data['amount_ladder'][$key]??[]);
  163. #秒杀价
  164. $amountKillLadderAll=array_values($data['amount_kill_ladder'][$key]??[]);
  165. $sku['amount_ladder']=$amountLadderAll;
  166. $sku['amount_kill_ladder']=$amountKillLadderAll;
  167. if(!empty($sku['id'])){
  168. $SKU=$goods->sku()->find($sku['id']);
  169. $SKU->save($sku);
  170. }else{
  171. $SKU=$goods->sku()->save($sku);
  172. }
  173. $ids[]=$SKU['id'];
  174. }
  175. $skuDel=$goods->sku()->whereNotIn('id',$ids)->select();
  176. foreach ($skuDel as $sku_del){
  177. $sku_del->delete();
  178. }
  179. $ids=[];
  180. foreach ($data['detail'] as $temp){
  181. if(!empty($temp['id'])){
  182. $model=$goods->detail()->find($temp['id']);
  183. $model->save($temp);
  184. }else{
  185. $model=$goods->detail()->save($temp);
  186. }
  187. $ids[]=$model['id'];
  188. }
  189. $goods->detail()->whereNotIn('id',$ids)->delete();
  190. $ids=[];
  191. foreach ($data['service'] as $temp){
  192. if(!empty($temp['id'])){
  193. $model=$goods->service()->find($temp['id']);
  194. $model->save($temp);
  195. }else{
  196. $model=$goods->service()->save($temp);
  197. }
  198. $ids[]=$model['id'];
  199. }
  200. $goods->service()->whereNotIn('id',$ids)->delete();
  201. $goods->setAmount();
  202. Db::commit();
  203. $this->success('成功');
  204. }
  205. public function bind_goods($ids){
  206. if($this->request->isGet()){
  207. $goods=$this->model->find($ids);
  208. $this->assign('row',$goods);
  209. return $this->fetch();
  210. }else{
  211. $data=input('row/a');
  212. foreach ($data as $goods_sku_id=>$info){
  213. $skuIds=array_filter(explode(',',$info['goods_id']));
  214. $sku=GoodsSku::find($goods_sku_id);
  215. $arr=[];
  216. foreach ($skuIds as $skuId){
  217. $bind_sku=GoodsSku::find($skuId);
  218. $arr[$skuId]=[
  219. 'goods_id'=>$ids,
  220. 'bind_goods_id'=>$bind_sku['goods_id'],
  221. ];
  222. }
  223. $sku->bindSku()->sync($arr);
  224. }
  225. $this->success();
  226. }
  227. }
  228. #商品统计
  229. public function goods_statistics($ids){
  230. $row=$this->model->find($ids);
  231. $this->assign('row',$row);
  232. $time=input('time')?:sprintf('%s - %s',date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59'));
  233. $this->assign('time',$time);
  234. $timeArr=explode(' - ',$time);
  235. $this->assign('viewCount',$row->viewFavCart()->typeView()->filterDate($timeArr,'date','date')->sum('num'));
  236. $this->assign('favCount',$row->viewFavCart()->typeFav()->filterDate($timeArr,'date','date')->sum('num'));
  237. $this->assign('cartCount',$row->viewFavCart()->typeCart()->filterDate($timeArr,'date','date')->sum('num'));
  238. #单价
  239. $goodsAmount=OrderInfo::payed()->filterDate($timeArr)
  240. ->where('goods_id',$row['id'])
  241. ->group('goods_id')
  242. ->field('SUM(amount_pay) as amount,SUM(num) as num')
  243. ->find();
  244. $this->assign('goodsAmount',$goodsAmount?bcdiv($goodsAmount['amount'],$goodsAmount['num']):0);
  245. #售后率统计
  246. $goodsNum=OrderInfo::filterDate($timeArr)
  247. ->where('goods_id',$row['id'])
  248. ->sum('num');
  249. $svcNum=Refund::filterDate($timeArr)
  250. ->where('goods_id',$row['id'])
  251. ->filterRefund()
  252. ->sum('num');
  253. $this->assign('svcPer',$goodsNum?bcdiv($svcNum,$goodsNum)*100:0);
  254. return $this->fetch();
  255. }
  256. #采购金额排序
  257. public function goods_buy_rank($ids){
  258. $row=$this->model->find($ids);
  259. //当前是否为关联查询
  260. $this->relationSearch = false;
  261. //设置过滤方法
  262. $this->request->filter(['strip_tags', 'trim']);
  263. if ($this->request->isAjax()) {
  264. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  265. $filter=json_decode(input('filter'),true);
  266. $map=[];
  267. if(!empty($filter['date'])){
  268. $timeArr=explode(' - ',$filter['date']);
  269. $timeArr[0]=strtotime($timeArr[0]);
  270. $timeArr[1]=strtotime($timeArr[1]);
  271. $map['create_time']=['between',$timeArr];
  272. }
  273. $list=OrderInfo::payed()
  274. ->where('goods_id',$row['id'])
  275. ->where($map)
  276. ->group('user_id')
  277. ->field('user_id,SUM(amount_pay) as amount')
  278. ->order('amount','desc')
  279. ->paginate($limit);
  280. if($list->items()) {
  281. $userNames = User::whereIn('id', array_column($list->items(), 'user_id'))->column('nickname', 'id');
  282. }
  283. $index=$list->listRows()*$list->currentPage()-$list->listRows();
  284. foreach ($list as $idx=>$item){
  285. $item['nickname']=$userNames[$item['user_id']]??'-';
  286. $item['rank']=$index+$idx+1;
  287. }
  288. $result = array("total" => $list->total(), "rows" => $list->items());
  289. return json($result);
  290. }
  291. return $this->view->fetch();
  292. }
  293. #订单金额排序
  294. public function order_amount_rank($ids){
  295. $row=$this->model->find($ids);
  296. //当前是否为关联查询
  297. $this->relationSearch = false;
  298. //设置过滤方法
  299. $this->request->filter(['strip_tags', 'trim']);
  300. if ($this->request->isAjax()) {
  301. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  302. $filter=json_decode(input('filter'),true);
  303. $map=[];
  304. if(!empty($filter['date'])){
  305. $timeArr=explode(' - ',$filter['date']);
  306. $timeArr[0]=strtotime($timeArr[0]);
  307. $timeArr[1]=strtotime($timeArr[1]);
  308. $map['create_time']=['between',$timeArr];
  309. }
  310. $list=\app\admin\model\Orders::payed()
  311. ->hasGoods($row['id'])
  312. ->where($map)
  313. ->field('user_id,amount_pay as amount,order_no')
  314. ->order('amount','desc')
  315. ->paginate($limit);
  316. $userNames=User::whereIn('id',array_column($list->items(),'user_id'))->column('nickname','id');
  317. $index=$list->listRows()*$list->currentPage()-$list->listRows();
  318. foreach ($list->items() as $idx=>&$item){
  319. $item['nickname']=$userNames[$item['user_id']]??'-';
  320. $item['rank']=$index+$idx+1;
  321. }
  322. $result = array("total" => $list->total(), "rows" => $list->items());
  323. return json($result);
  324. }
  325. return $this->view->fetch();
  326. }
  327. #订单笔数排名
  328. public function order_num_rank($ids){
  329. $row=$this->model->find($ids);
  330. //当前是否为关联查询
  331. $this->relationSearch = false;
  332. //设置过滤方法
  333. $this->request->filter(['strip_tags', 'trim']);
  334. if ($this->request->isAjax()) {
  335. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  336. $filter=json_decode(input('filter'),true);
  337. $map=[];
  338. if(!empty($filter['date'])){
  339. $timeArr=explode(' - ',$filter['date']);
  340. $timeArr[0]=strtotime($timeArr[0]);
  341. $timeArr[1]=strtotime($timeArr[1]);
  342. $map['create_time']=['between',$timeArr];
  343. }
  344. $list=\app\admin\model\Orders::payed()
  345. ->hasGoods($row['id'])
  346. ->where($map)
  347. ->group('user_id')
  348. ->field('user_id,count(user_id) as num')
  349. ->order('num','desc')
  350. ->paginate($limit);
  351. $userNames=User::whereIn('id',array_column($list->items(),'user_id'))->column('nickname','id');
  352. $index=$list->listRows()*$list->currentPage()-$list->listRows();
  353. foreach ($list as $idx=>$item){
  354. $item['nickname']=$userNames[$item['user_id']]??'-';
  355. $item['rank']=$index+$idx+1;
  356. }
  357. $result = array("total" => $list->total(), "rows" => $list->items());
  358. return json($result);
  359. }
  360. return $this->view->fetch();
  361. }
  362. }