123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393 |
- <?php
- namespace app\admin\controller;
- use app\admin\model\OrderInfo;
- use app\admin\model\User;
- use app\common\controller\Backend;
- use app\common\model\GoodsBind;
- use app\common\model\GoodsSku;
- use app\common\model\Refund;
- use think\Db;
- /**
- * 商品列管理
- *
- * @icon fa fa-circle-o
- */
- class Goods extends Backend
- {
- /**
- * Goods模型对象
- * @var \app\admin\model\Goods
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\Goods;
- $this->assign('status',\app\admin\model\Goods::getStatus());
- $this->assign('category',\app\common\model\Category::mall());
- }
- public function import()
- {
- parent::import();
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- ->with(['category'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- public function add()
- {
- $this->layout=false;
- if($this->request->isGet()){
- $this->assign('row',[
- 'sku'=>[
- [
- 'amount_ladder'=>[],
- 'amount_kill_ladder'=>[],
- ]
- ],
- ]);
- return $this->fetch();
- }else{
- $this->makeAdd();
- }
- }
- public function edit($ids = null)
- {
- if($this->request->isGet()){
- $row=$this->model->with(['sku'=>function($q){}])->findOrFail($ids);
- $this->assign('row',$row);
- return $this->fetch('add');
- }else{
- $this->makeAdd();
- }
- }
- protected function makeVal(){
- $data=input('row/a');
- $data['logo']=array_filter(explode(',',$data['logo']??''));
- $this->validate($data,[
- 'name|商品名称'=>['require'],
- 'describe|描述'=>['require','max:50'],
- 'category_id|商品分类'=>['require'],
- 'brand|品牌'=>['require'],
- 'is_kill'=>['require'],
- 'logo|LOGO'=>['require','array','min:1','max:6'],
- ]);
- foreach ($data['sku'] as $key=>$item){
- $rule=[
- 'name|规格名'=>['require'],
- 'num_stock|规格库存'=>['require','gt:0'],
- 'amount_cost|成本价'=>['require','float','egt:0'],
- ];
- $this->validate($item,$rule);
- #售价
- $amountLadderAll=$data['amount_ladder'][$key]??[];
- foreach ($amountLadderAll as $amount){
- $this->validate($amount,[
- 'min|售价区间最小数量'=>['require','gt:0'],
- 'max|售价区间最大数量'=>['require','gt:0'],
- 'amount|售价区间价格'=>['require','gt:0'],
- ]);
- }
- #秒杀价
- if($data['is_kill']) {
- $amountKillLadderAll = $data['amount_kill_ladder'][$key] ?? [];
- foreach ($amountKillLadderAll as $amount) {
- $this->validate($amount, [
- 'min|秒杀价区间最小数量' => ['require', 'gt:0'],
- 'max|秒杀价区间最大数量' => ['require', 'gt:0'],
- 'amount|秒杀价区间价格' => ['require', 'gt:0'],
- ]);
- }
- }
- }
- foreach ($data['detail'] as $item){
- $this->validate($item,[
- 'name|规格参数名'=>['require'],
- 'value|规格参数值'=>['require'],
- ]);
- }
- foreach ($data['service'] as $item){
- $this->validate($item,[
- 'name|售后服务参数名'=>['require'],
- 'value|售后服务参数值'=>['require'],
- ]);
- }
- return $data;
- }
- protected function makeAdd(){
- $data=$this->makeVal();
- if(!empty($data['id'])){
- $goods=$this->model->findOrFail($data['id']);
- }else{
- $goods=new $this->model;
- }
- Db::startTrans();
- foreach ($data as $key=>$value){
- if(!in_array($key,$goods::$read) && $goods->hasColumn($key)){
- $goods[$key]=$value;
- }
- }
- $goods->allowField(true)->save();
- $ids=[];
- foreach ($data['sku'] as $key=>$sku){
- #售价
- $amountLadderAll=array_values($data['amount_ladder'][$key]??[]);
- #秒杀价
- $amountKillLadderAll=array_values($data['amount_kill_ladder'][$key]??[]);
- $sku['amount_ladder']=$amountLadderAll;
- $sku['amount_kill_ladder']=$amountKillLadderAll;
- if(!empty($sku['id'])){
- $SKU=$goods->sku()->find($sku['id']);
- $SKU->save($sku);
- }else{
- $SKU=$goods->sku()->save($sku);
- }
- $ids[]=$SKU['id'];
- }
- $skuDel=$goods->sku()->whereNotIn('id',$ids)->select();
- foreach ($skuDel as $sku_del){
- $sku_del->delete();
- }
- $ids=[];
- foreach ($data['detail'] as $temp){
- if(!empty($temp['id'])){
- $model=$goods->detail()->find($temp['id']);
- $model->save($temp);
- }else{
- $model=$goods->detail()->save($temp);
- }
- $ids[]=$model['id'];
- }
- $goods->detail()->whereNotIn('id',$ids)->delete();
- $ids=[];
- foreach ($data['service'] as $temp){
- if(!empty($temp['id'])){
- $model=$goods->service()->find($temp['id']);
- $model->save($temp);
- }else{
- $model=$goods->service()->save($temp);
- }
- $ids[]=$model['id'];
- }
- $goods->service()->whereNotIn('id',$ids)->delete();
- $goods->setAmount();
- Db::commit();
- $this->success('成功');
- }
- public function bind_goods($ids){
- if($this->request->isGet()){
- $goods=$this->model->find($ids);
- $this->assign('row',$goods);
- return $this->fetch();
- }else{
- $data=input('row/a');
- foreach ($data as $goods_sku_id=>$info){
- $skuIds=array_filter(explode(',',$info['goods_id']));
- $sku=GoodsSku::find($goods_sku_id);
- $arr=[];
- foreach ($skuIds as $skuId){
- $bind_sku=GoodsSku::find($skuId);
- $arr[$skuId]=[
- 'goods_id'=>$ids,
- 'bind_goods_id'=>$bind_sku['goods_id'],
- ];
- }
- $sku->bindSku()->sync($arr);
- }
- $this->success();
- }
- }
- #商品统计
- public function goods_statistics($ids){
- $row=$this->model->find($ids);
- $this->assign('row',$row);
- $time=input('time')?:sprintf('%s - %s',date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59'));
- $this->assign('time',$time);
- $timeArr=explode(' - ',$time);
- $this->assign('viewCount',$row->viewFavCart()->typeView()->filterDate($timeArr,'date','date')->sum('num'));
- $this->assign('favCount',$row->viewFavCart()->typeFav()->filterDate($timeArr,'date','date')->sum('num'));
- $this->assign('cartCount',$row->viewFavCart()->typeCart()->filterDate($timeArr,'date','date')->sum('num'));
- #单价
- $goodsAmount=OrderInfo::payed()->filterDate($timeArr)
- ->where('goods_id',$row['id'])
- ->group('goods_id')
- ->field('SUM(amount_pay) as amount,SUM(num) as num')
- ->find();
- $this->assign('goodsAmount',$goodsAmount?bcdiv($goodsAmount['amount'],$goodsAmount['num']):0);
- #售后率统计
- $goodsNum=OrderInfo::filterDate($timeArr)
- ->where('goods_id',$row['id'])
- ->sum('num');
- $svcNum=Refund::filterDate($timeArr)
- ->where('goods_id',$row['id'])
- ->filterRefund()
- ->sum('num');
- $this->assign('svcPer',$goodsNum?bcdiv($svcNum,$goodsNum)*100:0);
- return $this->fetch();
- }
- #采购金额排序
- public function goods_buy_rank($ids){
- $row=$this->model->find($ids);
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $filter=json_decode(input('filter'),true);
- $map=[];
- if(!empty($filter['date'])){
- $timeArr=explode(' - ',$filter['date']);
- $timeArr[0]=strtotime($timeArr[0]);
- $timeArr[1]=strtotime($timeArr[1]);
- $map['create_time']=['between',$timeArr];
- }
- $list=OrderInfo::payed()
- ->where('goods_id',$row['id'])
- ->where($map)
- ->group('user_id')
- ->field('user_id,SUM(amount_pay) as amount')
- ->order('amount','desc')
- ->paginate($limit);
- if($list->items()) {
- $userNames = User::whereIn('id', array_column($list->items(), 'user_id'))->column('nickname', 'id');
- }
- $index=$list->listRows()*$list->currentPage()-$list->listRows();
- foreach ($list as $idx=>$item){
- $item['nickname']=$userNames[$item['user_id']]??'-';
- $item['rank']=$index+$idx+1;
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- #订单金额排序
- public function order_amount_rank($ids){
- $row=$this->model->find($ids);
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $filter=json_decode(input('filter'),true);
- $map=[];
- if(!empty($filter['date'])){
- $timeArr=explode(' - ',$filter['date']);
- $timeArr[0]=strtotime($timeArr[0]);
- $timeArr[1]=strtotime($timeArr[1]);
- $map['create_time']=['between',$timeArr];
- }
- $list=\app\admin\model\Orders::payed()
- ->hasGoods($row['id'])
- ->where($map)
- ->field('user_id,amount_pay as amount,order_no')
- ->order('amount','desc')
- ->paginate($limit);
- $userNames=User::whereIn('id',array_column($list->items(),'user_id'))->column('nickname','id');
- $index=$list->listRows()*$list->currentPage()-$list->listRows();
- foreach ($list->items() as $idx=>&$item){
- $item['nickname']=$userNames[$item['user_id']]??'-';
- $item['rank']=$index+$idx+1;
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- #订单笔数排名
- public function order_num_rank($ids){
- $row=$this->model->find($ids);
- //当前是否为关联查询
- $this->relationSearch = false;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $filter=json_decode(input('filter'),true);
- $map=[];
- if(!empty($filter['date'])){
- $timeArr=explode(' - ',$filter['date']);
- $timeArr[0]=strtotime($timeArr[0]);
- $timeArr[1]=strtotime($timeArr[1]);
- $map['create_time']=['between',$timeArr];
- }
- $list=\app\admin\model\Orders::payed()
- ->hasGoods($row['id'])
- ->where($map)
- ->group('user_id')
- ->field('user_id,count(user_id) as num')
- ->order('num','desc')
- ->paginate($limit);
- $userNames=User::whereIn('id',array_column($list->items(),'user_id'))->column('nickname','id');
- $index=$list->listRows()*$list->currentPage()-$list->listRows();
- foreach ($list as $idx=>$item){
- $item['nickname']=$userNames[$item['user_id']]??'-';
- $item['rank']=$index+$idx+1;
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- }
|