From 71ae2db337f5e1289898d293c9e47a05aeffebb1 Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Fri, 30 Oct 2020 11:38:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=97=E9=93=BA=E6=96=B0=E5=A2=9E=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=80=BB=E6=95=B0=E7=BB=9F=E8=AE=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/StoreUserReportController.php | 48 ++++----- app/Admin/Repositories/StoreUserReport.php | 100 +++++++++++------- resources/lang/zh-CN/store-user-report.php | 1 + 3 files changed, 85 insertions(+), 64 deletions(-) diff --git a/app/Admin/Controllers/StoreUserReportController.php b/app/Admin/Controllers/StoreUserReportController.php index 8358b25..d8b835e 100644 --- a/app/Admin/Controllers/StoreUserReportController.php +++ b/app/Admin/Controllers/StoreUserReportController.php @@ -7,7 +7,9 @@ use Dcat\Admin\Form; use Dcat\Admin\Grid; use Dcat\Admin\Show; use Dcat\Admin\Controllers\AdminController; -use App\Models\ImsCjdcMarket as marketModel; +use App\Models\v3\Market as MarketModel; +use App\Models\StoreAccount as StoreAccountModel; +use App\Models\v3\Store as StoreModel; class StoreUserReportController extends AdminController { @@ -24,37 +26,29 @@ class StoreUserReportController extends AdminController $grid->disableEditButton(); $grid->disableQuickEditButton(); $grid->disableViewButton(); + $grid->disableActions(); + $grid->disableRowSelector(); + + $marketList = MarketModel::getMarketArray(); + $storeList = StoreModel::getStoreArray(); $grid->store_id; - $grid->market_id->display(function ($markrtId) { - $market = marketModel::select('id', 'name')->find($markrtId); - if (!$market) { - return '数据错误'; - } - return $market->name; + $grid->market_id->display(function ($marketId) use($marketList){ + return $marketList[$marketId] ?? ''; }); - $grid->store_name; - $grid->new_user_total; + $grid->store_name->width('25%'); + $grid->new_total; + + $grid->filter(function (Grid\Filter $filter) use($marketList,$storeList){ + $filter->panel(); - $grid->filter(function (Grid\Filter $filter) { - $marketList = []; - $list = marketModel::select('id', 'name')->get()->toArray(); - foreach ($list as $value) { - $marketList[$value['id']] = $value['name']; - } - $filter->in('market_id')->multipleSelect($marketList); - $filter->like('store.name', '店铺名称'); - $filter->whereBetween('time', function ($q) { - $start = $this->input['start'] ?? null; - $end = $this->input['end'] ?? null; - if ($start !== null) { - $q->whereRaw("ims_cjdc_store_account.time >= ?", $start); - } - if ($end !== null) { - $q->whereRaw("ims_cjdc_store_account.time <= ?", $end); - } - })->datetime(); + $filter->equal('start_time','开始时间')->date()->width(2); + $filter->equal('end_time','结束时间')->date()->width(2); + $filter->equal('market_id')->select($marketList)->width(3); + $filter->in('store_id','店铺名称')->multipleSelect($storeList)->width(4); }); + // 每页10条 + $grid->paginate(10); }); } diff --git a/app/Admin/Repositories/StoreUserReport.php b/app/Admin/Repositories/StoreUserReport.php index 0b9c6d0..ce51045 100644 --- a/app/Admin/Repositories/StoreUserReport.php +++ b/app/Admin/Repositories/StoreUserReport.php @@ -32,80 +32,106 @@ class StoreUserReport extends EloquentRepository { $this->setSort($model); $this->setPaginate($model); + // 获取每页显示行数 + $perPage = $model->getPerPage(); + $startTime = $params['start_time'] ?? request()->input('start_time',''); + $endTime = $params['end_time'] ?? request()->input('end_time',''); + $marketId = $params['market_id'] ?? request()->input('market_id',0); + $storeIds = $params['store_id'] ?? request()->input('store_id',[]); /* 根据流水查询 2020-08-18 用全匹配文字方式查询新用户 旧表 */ - // $storeAccountModel = new StoreAccountModel(); - // $query = StoreAccountModel::join(Model::$tableName.' as store',StoreAccountModel::$tableName.'.store_id','store.id') - // ->select('store_id','store.market_id','store.name as store_name',DB::raw("count(distinct ".StoreAccountModel::$tableName.".id) as new_user_total")) - // ->whereRaw("note = '新用户下单成功,平台奖励'") - // ->groupBy('store_id') - // ->orderBy('store.market_id','desc') - // ->orderBY('new_user_total','desc') - // ->orderBY('store_id','desc'); - // $l = $query->get()->toArray(); - // dd($l); + $storeAccount = StoreAccountModel::join(Model::$tableName.' as store',StoreAccountModel::$tableName.'.store_id','store.id') + ->select('store_id','store.name as store_name','store.market_id',DB::raw("count(distinct ".StoreAccountModel::$tableName.".id) as new_user_total")) + ->whereRaw("note = '新用户下单成功,平台奖励'") + ->groupBy('store_id'); + // dd($storeAccount->get()->toArray()); + /* 根据流水查询 2020-10-29 用类型字段查询新用户 新表 */ $financial0 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f0.id) as new_user_total')) ->join(FinancialRecordModel0::$tableName.' as f0',Model::$tableName.'.user_id','=','f0.user_id','left') ->whereRaw(Model::$tableName.'.user_id MOD 5 = 0') - ->where('user_type',Type::USER_TYPE_STORE) + ->whereRaw('user_type ='.Type::USER_TYPE_STORE) // ->where('source_type',Type::SOURCE_TYPE_ORDER) // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER) - ->where('f0.created_at','>','1600444800') + ->whereRaw('f0.created_at > 1600444800') ->groupBy('store_id','store_name','market_id'); $financial1 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f1.id) as new_user_total')) ->join(FinancialRecordModel1::$tableName.' as f1',Model::$tableName.'.user_id','=','f1.user_id','left') ->whereRaw(Model::$tableName.'.user_id MOD 5 = 1') - ->where('user_type',Type::USER_TYPE_STORE) + ->whereRaw('user_type ='.Type::USER_TYPE_STORE) // ->where('source_type',Type::SOURCE_TYPE_ORDER) // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER) - ->where('f1.created_at','>','1600444800') + ->whereRaw('f1.created_at > 1600444800') ->groupBy('store_id','store_name','market_id'); $financial2 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f2.id) as new_user_total')) ->join(FinancialRecordModel2::$tableName.' as f2',Model::$tableName.'.user_id','=','f2.user_id','left') ->whereRaw(Model::$tableName.'.user_id MOD 5 = 2') - ->where('user_type',Type::USER_TYPE_STORE) + ->whereRaw('user_type ='.Type::USER_TYPE_STORE) // ->where('source_type',Type::SOURCE_TYPE_ORDER) // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER) - ->where('f2.created_at','>','1600444800') + ->whereRaw('f2.created_at > 1600444800') ->groupBy('store_id','store_name','market_id'); $financial3 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f3.id) as new_user_total')) ->join(FinancialRecordModel3::$tableName.' as f3',Model::$tableName.'.user_id','=','f3.user_id','left') ->whereRaw(Model::$tableName.'.user_id MOD 5 = 3') - ->where('user_type',Type::USER_TYPE_STORE) + ->whereRaw('user_type ='.Type::USER_TYPE_STORE) // ->where('source_type',Type::SOURCE_TYPE_ORDER) // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER) - ->where('f3.created_at','>','1600444800') + ->whereRaw('f3.created_at > 1600444800') ->groupBy('store_id','store_name','market_id'); $financial4 = Model::select(Model::$tableName.'.id as store_id',Model::$tableName.'.name as store_name','market_id',DB::raw('count(distinct f4.id) as new_user_total')) ->join(FinancialRecordModel4::$tableName.' as f4',Model::$tableName.'.user_id','=','f4.user_id','left') ->whereRaw(Model::$tableName.'.user_id MOD 5 = 4') - ->where('user_type',Type::USER_TYPE_STORE) + ->whereRaw('user_type ='.Type::USER_TYPE_STORE) // ->where('source_type',Type::SOURCE_TYPE_ORDER) // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER) - ->where('f4.created_at','>','1600444800') - ->groupBy('store_id','store_name','market_id') - ->union($financial0) - ->union($financial1) - ->union($financial2) - ->union($financial3); - - $query = $financial4->get()->toArray(); - - dd($query); + ->whereRaw('f4.created_at > 1600444800') + ->groupBy('store_id','store_name','market_id'); - $model->getQueries()->unique()->each(function ($value) use (&$query) { - if ($value['method'] == 'paginate') { - $value['arguments'][1] = $this->getGridColumns(); - } elseif ($value['method'] == 'get') { - $value['arguments'] = [$this->getGridColumns()]; - } - $query = call_user_func_array([$query, $value['method']], $value['arguments'] ?? []); - }); + if($startTime){ + $startTime = $startTime.' 00:00:00'; + $storeAccount->whereRaw(StoreAccountModel::$tableName.".time >= '".$startTime."'"); + $financial0->whereRaw('f0.created_at >='.strtotime($startTime)); + $financial1->whereRaw('f1.created_at >='.strtotime($startTime)); + $financial2->whereRaw('f2.created_at >='.strtotime($startTime)); + $financial3->whereRaw('f3.created_at >='.strtotime($startTime)); + $financial4->whereRaw('f4.created_at >='.strtotime($startTime)); + } + if($endTime){ + $endTime = $endTime.' 23:59:59'; + $storeAccount->whereRaw(StoreAccountModel::$tableName.".time <='".$endTime."'"); + $financial0->whereRaw('f0.created_at <='.strtotime($endTime)); + $financial1->whereRaw('f1.created_at <='.strtotime($endTime)); + $financial2->whereRaw('f2.created_at <='.strtotime($endTime)); + $financial3->whereRaw('f3.created_at <='.strtotime($endTime)); + $financial4->whereRaw('f4.created_at <='.strtotime($endTime)); + } + if($marketId){ + $storeAccount->whereRaw('store.market_id ='.$marketId); + $financial0->whereRaw(Model::$tableName.'.market_id ='.$marketId); + $financial1->whereRaw(Model::$tableName.'.market_id ='.$marketId); + $financial2->whereRaw(Model::$tableName.'.market_id ='.$marketId); + $financial3->whereRaw(Model::$tableName.'.market_id ='.$marketId); + $financial4->whereRaw(Model::$tableName.'.market_id ='.$marketId); + } + if($storeIds){ + $storeStr = implode(',',$storeIds);//dd($storeArr); + $storeAccount->whereRaw('store.id in('. $storeStr.')'); + $financial0->whereRaw(Model::$tableName.'.id in('. $storeStr.')'); + $financial1->whereRaw(Model::$tableName.'.id in('. $storeStr.')'); + $financial2->whereRaw(Model::$tableName.'.id in('. $storeStr.')'); + $financial3->whereRaw(Model::$tableName.'.id in('. $storeStr.')'); + $financial4->whereRaw(Model::$tableName.'.id in('. $storeStr.')'); + } + $unionAll = $storeAccount->union($financial0)->union($financial1)->union($financial2)->union($financial3)->union($financial4); + $query = DB::table(Db::raw("({$unionAll->toSql()}) as a1")) + ->select('a1.store_id','a1.store_name','a1.market_id',DB::raw('SUM(a1.new_user_total) as new_total')) + ->groupBy('market_id','store_id','store_name') + ->orderBy('new_total','desc')->paginate($perPage); $query = $query->toArray(); diff --git a/resources/lang/zh-CN/store-user-report.php b/resources/lang/zh-CN/store-user-report.php index 5bb0d36..a09f452 100644 --- a/resources/lang/zh-CN/store-user-report.php +++ b/resources/lang/zh-CN/store-user-report.php @@ -9,6 +9,7 @@ return [ 'market_id' => '所属市场', 'store_name' => '商家名称', 'new_user_total' => '新增用户总数', + 'new_total' => '新增用户总数', 'mm_user_id' => '所属市场经理', 'time' => '时间', ],