链街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.

173 lines
6.7 KiB

  1. <?php
  2. namespace App\Admin\Repositories\v3;
  3. use App\Models\v3\LanzuEmployees as Model;
  4. use Dcat\Admin\Grid\Model as GridModel;
  5. use Dcat\Admin\Repositories\EloquentRepository;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\LanzuCsInfo as CsInfoModel;
  8. use App\Models\LanzuUserRelationBind as UserRelationBindModel;
  9. use App\Models\ImsCjdcOrderMain as OrderMainModel;
  10. class ReportCommunity extends EloquentRepository
  11. {
  12. protected $params = [];
  13. protected $currentPage = 1;
  14. protected $perPage = 10;
  15. /**
  16. * Model.
  17. *
  18. * @var string
  19. */
  20. protected $eloquentClass = Model::class;
  21. public function __construct($params = [])
  22. {
  23. $this->params = $params;
  24. }
  25. /**
  26. * 获取统计列表数据
  27. */
  28. public function get(GridModel $model)
  29. {
  30. // 获取当前页数
  31. $this->currentPage = $model->getCurrentPage();
  32. // 获取每页显示行数
  33. $this->perPage = $model->getPerPage();
  34. $where = [];
  35. if(!empty($this->params) && isset($this->params['market_id']) && !empty($this->params['market_id'])){
  36. $where = ['market_id'=>$this->params['market_id']];
  37. }
  38. $list = $this->getDataModel($where);
  39. return $model->makePaginator(
  40. $list['total'] ?? 0,$list['data'] ?? []
  41. );
  42. }
  43. public function getDataModel($params = [])
  44. {
  45. // 获取筛选参数 如果没有时间,则用当天的数据
  46. $startTime = $params['start_time'] ?? request()->input('start_time','');
  47. $endTime = $params['end_time'] ?? request()->input('end_time','');
  48. $time = time();
  49. if(empty($startTime)){
  50. $startTime = date('Y-m-d', $time) . '00:00:00';
  51. }else{
  52. $startTime = $startTime . '00:00:00';
  53. }
  54. if(empty($endTime)){
  55. $endTime = date('Y-m-d', $time) . '23:59:59';
  56. }else{
  57. $endTime = $endTime . '23:59:59';
  58. }
  59. // 获取懒族员工
  60. $selectsEmp = 'id,user_id,person_id,market_id,name,phone,status';
  61. $employeesData = $this->getEmployeesData($selectsEmp,$params);
  62. $employeesList = $employeesData->orderBy('id','desc')
  63. ->paginate($this->perPage)
  64. ->map(function ($value) {return (array)$value;})
  65. ->toArray();
  66. // $employeesList = $employeesList->toArray();
  67. foreach ($employeesList as $key => &$employee) {
  68. // 根据员工信息,查询名下新增绑定的所有服务点 Statistics
  69. $newCsCount = CsInfoModel::where(function ($query) use ($employee) {
  70. $query->where('person_id', '=', $employee['user_id'])
  71. ->orWhere('user_id', '=', $employee['user_id']);
  72. })
  73. ->where('created_at', '>=', strtotime($startTime ?? 0))
  74. ->where('created_at', '<=', strtotime($endTime ?? 0))
  75. ->count();
  76. // 查询名下所有的服务点
  77. $allCsInfos = CsInfoModel::where(function ($query) use ($employee) {
  78. $query->where('person_id', '=', $employee['user_id'])
  79. ->orWhere('user_id', '=', $employee['user_id']);
  80. })
  81. ->get()
  82. ->map(function ($value) {return (array)$value;})
  83. ->toArray();
  84. // 新增绑定用户数 Statistics
  85. $newBindUserCount = UserRelationBindModel::where('bind_type', '=', 1)
  86. ->whereIn('source_id', array_values(array_column($allCsInfos, 'admin_user_id')))
  87. ->where('created_at', '>=', strtotime($startTime ?? 0))
  88. ->where('created_at', '<=', strtotime($endTime ?? 0))
  89. ->count();
  90. // 查询名下所有服务点的所有用户
  91. $allCsBindUsers = UserRelationBindModel::where('bind_type', '=', 1)
  92. ->whereIn('source_id', array_values(array_column($allCsInfos, 'admin_user_id')))
  93. ->get()
  94. ->map(function ($value) {return (array)$value;})
  95. ->toArray();
  96. // 平台新增用户数 Statistics
  97. $platNewUsers = OrderMainModel::select('user_id')
  98. ->where('type', '=', 1)
  99. ->whereIn('state', [4,5,10,11])
  100. ->whereIn('user_id', array_values(array_column($allCsBindUsers, 'user_id')))
  101. ->groupBy('user_id')
  102. ->havingRaw('MIN(created_at)>='.strtotime($startTime ?? 0).' AND MIN(created_at)<='.strtotime($endTime ?? 0).'')
  103. ->get()
  104. ->map(function ($value) {return (array)$value;})
  105. ->toArray();
  106. $platNewUserCount = count($platNewUsers);
  107. // 所有用户产生的线上订单数 Statistics
  108. $totalOrdersCount = OrderMainModel::where('type', '=', 1)
  109. ->whereIn('state', [4,5,10,11])
  110. ->whereIn('user_id', array_values(array_column($allCsBindUsers, 'user_id')))
  111. ->where('created_at', '>=', strtotime($startTime ?? 0))
  112. ->where('created_at', '<=', strtotime($endTime ?? 0))
  113. ->count();
  114. // 平台新增用户产生的线上订单数 Statistics
  115. $platNewUserOrdersCount =OrderMainModel::where('type', '=', 1)
  116. ->whereIn('state', [4,5,10,11])
  117. ->whereIn('user_id', array_values(array_column($platNewUsers, 'user_id')))
  118. ->where('created_at', '>=', strtotime($startTime ?? 0))
  119. ->where('created_at', '<=', strtotime($endTime ?? 0))
  120. ->count();
  121. $employee['new_cs_count'] = $newCsCount;
  122. $employee['new_bind_user_count'] = $newBindUserCount;
  123. $employee['plat_new_user_count'] = $platNewUserCount;
  124. $employee['total_order_count'] = $totalOrdersCount;
  125. $employee['plat_new_user_order_count'] = $platNewUserOrdersCount;
  126. }
  127. return $employeesList;
  128. }
  129. /**
  130. * 获取懒族员工 model
  131. */
  132. public function getEmployeesData($selects, $params = ['status' => 1])
  133. {
  134. $model = Model::select(DB::raw($selects));
  135. if(isset($params['status']) && is_numeric($params['status'])){
  136. $model->where('status',$params['status']);
  137. }
  138. if(isset($params['user_id']) && is_numeric($params['user_id'])){
  139. $model->where('user_id', $params['user_id']);
  140. }
  141. if(isset($params['user_id'])){
  142. $model->where('name','like','%'.$params['user_id'].'%');
  143. }
  144. if(isset($params['market_id']) && is_numeric($params['market_id'])){
  145. $model->where('market_id', $params['market_id']);
  146. }
  147. return $model;
  148. }
  149. }