|
|
<?php
namespace App\Admin\Repositories;
use App\Models\v3\Store as Model;use Dcat\Admin\Grid\Model as GridModel;use Dcat\Admin\Repositories\EloquentRepository;use App\Models\StoreAccount as StoreAccountModel;use App\Models\FinancialRecord0 as FinancialRecordModel0;use App\Models\FinancialRecord1 as FinancialRecordModel1;use App\Models\FinancialRecord2 as FinancialRecordModel2;use App\Models\FinancialRecord3 as FinancialRecordModel3;use App\Models\FinancialRecord4 as FinancialRecordModel4;use Dcat\Admin\Layout\Row;use Illuminate\Support\Facades\DB;use App\Admin\Common\Type;
class StoreUserReport extends EloquentRepository{ /** * Model. * * @var string */ protected $eloquentClass = Model::class;
/** * 获取统计列表数据 * 旧表流水数据未洗到新表,所以统计需要查询旧表和新表数据 */ public function get(GridModel $model) { $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 用全匹配文字方式查询新用户 旧表 */ $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'); /* 根据流水查询 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') ->whereRaw('user_type ='.Type::USER_TYPE_STORE) ->whereRaw('source_type = '.Type::SOURCE_TYPE_ORDER) ->whereRaw('money_type = '.Type::MONEY_TYPE_STORE_PLAT_NEW_USER) ->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') ->whereRaw('user_type ='.Type::USER_TYPE_STORE) ->whereRaw('source_type = '.Type::SOURCE_TYPE_ORDER) ->whereRaw('money_type = '.Type::MONEY_TYPE_STORE_PLAT_NEW_USER) ->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') ->whereRaw('user_type ='.Type::USER_TYPE_STORE) ->whereRaw('source_type = '.Type::SOURCE_TYPE_ORDER) ->whereRaw('money_type = '.Type::MONEY_TYPE_STORE_PLAT_NEW_USER) ->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') ->whereRaw('user_type ='.Type::USER_TYPE_STORE) ->whereRaw('source_type = '.Type::SOURCE_TYPE_ORDER) ->whereRaw('money_type = '.Type::MONEY_TYPE_STORE_PLAT_NEW_USER) ->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') ->whereRaw('user_type ='.Type::USER_TYPE_STORE) ->whereRaw('source_type = '.Type::SOURCE_TYPE_ORDER) ->whereRaw('money_type = '.Type::MONEY_TYPE_STORE_PLAT_NEW_USER) ->whereRaw('f4.created_at > 1600444800') ->groupBy('store_id','store_name','market_id');
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); $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();
$query = $model->makePaginator($query['total'],$query['data']); return $query; }}
|