链街Dcat后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
8.0 KiB

  1. <?php
  2. namespace App\Admin\Repositories;
  3. use App\Models\v3\Store as Model;
  4. use Dcat\Admin\Grid\Model as GridModel;
  5. use Dcat\Admin\Repositories\EloquentRepository;
  6. use App\Models\StoreAccount as StoreAccountModel;
  7. use App\Models\FinancialRecord0 as FinancialRecordModel0;
  8. use App\Models\FinancialRecord1 as FinancialRecordModel1;
  9. use App\Models\FinancialRecord2 as FinancialRecordModel2;
  10. use App\Models\FinancialRecord3 as FinancialRecordModel3;
  11. use App\Models\FinancialRecord4 as FinancialRecordModel4;
  12. use Dcat\Admin\Layout\Row;
  13. use Illuminate\Support\Facades\DB;
  14. use App\Admin\Common\Type;
  15. class StoreUserReport extends EloquentRepository
  16. {
  17. /**
  18. * Model.
  19. *
  20. * @var string
  21. */
  22. protected $eloquentClass = Model::class;
  23. /**
  24. * 获取统计列表数据
  25. * 旧表流水数据未洗到新表,所以统计需要查询旧表和新表数据
  26. */
  27. public function get(GridModel $model)
  28. {
  29. $this->setSort($model);
  30. $this->setPaginate($model);
  31. // 获取每页显示行数
  32. $perPage = $model->getPerPage();
  33. $startTime = $params['start_time'] ?? request()->input('start_time','');
  34. $endTime = $params['end_time'] ?? request()->input('end_time','');
  35. $marketId = $params['market_id'] ?? request()->input('market_id',0);
  36. $storeIds = $params['store_id'] ?? request()->input('store_id',[]);
  37. /* 根据流水查询 2020-08-18 用全匹配文字方式查询新用户 旧表 */
  38. $storeAccount = StoreAccountModel::join(Model::$tableName.' as store',StoreAccountModel::$tableName.'.store_id','store.id')
  39. ->select('store_id','store.name as store_name','store.market_id',DB::raw("count(distinct ".StoreAccountModel::$tableName.".id) as new_user_total"))
  40. ->whereRaw("note = '新用户下单成功,平台奖励'")
  41. ->groupBy('store_id');
  42. // dd($storeAccount->get()->toArray());
  43. /* 根据流水查询 2020-10-29 用类型字段查询新用户 新表 */
  44. $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'))
  45. ->join(FinancialRecordModel0::$tableName.' as f0',Model::$tableName.'.user_id','=','f0.user_id','left')
  46. ->whereRaw(Model::$tableName.'.user_id MOD 5 = 0')
  47. ->whereRaw('user_type ='.Type::USER_TYPE_STORE)
  48. // ->where('source_type',Type::SOURCE_TYPE_ORDER)
  49. // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
  50. ->whereRaw('f0.created_at > 1600444800')
  51. ->groupBy('store_id','store_name','market_id');
  52. $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'))
  53. ->join(FinancialRecordModel1::$tableName.' as f1',Model::$tableName.'.user_id','=','f1.user_id','left')
  54. ->whereRaw(Model::$tableName.'.user_id MOD 5 = 1')
  55. ->whereRaw('user_type ='.Type::USER_TYPE_STORE)
  56. // ->where('source_type',Type::SOURCE_TYPE_ORDER)
  57. // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
  58. ->whereRaw('f1.created_at > 1600444800')
  59. ->groupBy('store_id','store_name','market_id');
  60. $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'))
  61. ->join(FinancialRecordModel2::$tableName.' as f2',Model::$tableName.'.user_id','=','f2.user_id','left')
  62. ->whereRaw(Model::$tableName.'.user_id MOD 5 = 2')
  63. ->whereRaw('user_type ='.Type::USER_TYPE_STORE)
  64. // ->where('source_type',Type::SOURCE_TYPE_ORDER)
  65. // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
  66. ->whereRaw('f2.created_at > 1600444800')
  67. ->groupBy('store_id','store_name','market_id');
  68. $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'))
  69. ->join(FinancialRecordModel3::$tableName.' as f3',Model::$tableName.'.user_id','=','f3.user_id','left')
  70. ->whereRaw(Model::$tableName.'.user_id MOD 5 = 3')
  71. ->whereRaw('user_type ='.Type::USER_TYPE_STORE)
  72. // ->where('source_type',Type::SOURCE_TYPE_ORDER)
  73. // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
  74. ->whereRaw('f3.created_at > 1600444800')
  75. ->groupBy('store_id','store_name','market_id');
  76. $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'))
  77. ->join(FinancialRecordModel4::$tableName.' as f4',Model::$tableName.'.user_id','=','f4.user_id','left')
  78. ->whereRaw(Model::$tableName.'.user_id MOD 5 = 4')
  79. ->whereRaw('user_type ='.Type::USER_TYPE_STORE)
  80. // ->where('source_type',Type::SOURCE_TYPE_ORDER)
  81. // ->where('money_type',Type::MONEY_TYPE_STORE_PLAT_NEW_USER)
  82. ->whereRaw('f4.created_at > 1600444800')
  83. ->groupBy('store_id','store_name','market_id');
  84. if($startTime){
  85. $startTime = $startTime.' 00:00:00';
  86. $storeAccount->whereRaw(StoreAccountModel::$tableName.".time >= '".$startTime."'");
  87. $financial0->whereRaw('f0.created_at >='.strtotime($startTime));
  88. $financial1->whereRaw('f1.created_at >='.strtotime($startTime));
  89. $financial2->whereRaw('f2.created_at >='.strtotime($startTime));
  90. $financial3->whereRaw('f3.created_at >='.strtotime($startTime));
  91. $financial4->whereRaw('f4.created_at >='.strtotime($startTime));
  92. }
  93. if($endTime){
  94. $endTime = $endTime.' 23:59:59';
  95. $storeAccount->whereRaw(StoreAccountModel::$tableName.".time <='".$endTime."'");
  96. $financial0->whereRaw('f0.created_at <='.strtotime($endTime));
  97. $financial1->whereRaw('f1.created_at <='.strtotime($endTime));
  98. $financial2->whereRaw('f2.created_at <='.strtotime($endTime));
  99. $financial3->whereRaw('f3.created_at <='.strtotime($endTime));
  100. $financial4->whereRaw('f4.created_at <='.strtotime($endTime));
  101. }
  102. if($marketId){
  103. $storeAccount->whereRaw('store.market_id ='.$marketId);
  104. $financial0->whereRaw(Model::$tableName.'.market_id ='.$marketId);
  105. $financial1->whereRaw(Model::$tableName.'.market_id ='.$marketId);
  106. $financial2->whereRaw(Model::$tableName.'.market_id ='.$marketId);
  107. $financial3->whereRaw(Model::$tableName.'.market_id ='.$marketId);
  108. $financial4->whereRaw(Model::$tableName.'.market_id ='.$marketId);
  109. }
  110. if($storeIds){
  111. $storeStr = implode(',',$storeIds);//dd($storeArr);
  112. $storeAccount->whereRaw('store.id in('. $storeStr.')');
  113. $financial0->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
  114. $financial1->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
  115. $financial2->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
  116. $financial3->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
  117. $financial4->whereRaw(Model::$tableName.'.id in('. $storeStr.')');
  118. }
  119. $unionAll = $storeAccount->union($financial0)->union($financial1)->union($financial2)->union($financial3)->union($financial4);
  120. $query = DB::table(Db::raw("({$unionAll->toSql()}) as a1"))
  121. ->select('a1.store_id','a1.store_name','a1.market_id',DB::raw('SUM(a1.new_user_total) as new_total'))
  122. ->groupBy('market_id','store_id','store_name')
  123. ->orderBy('new_total','desc')->paginate($perPage);
  124. $query = $query->toArray();
  125. $query = $model->makePaginator($query['total'],$query['data']);
  126. return $query;
  127. }
  128. }