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

211 lines
8.2 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. $selectsEmp = 'id,user_id,position,market_id,name,tel,status';
  39. $list = $this->getDataModel($selectsEmp, $where);
  40. return $model->makePaginator(
  41. $list['total'] ?? 0,$list['data'] ?? []
  42. );
  43. }
  44. public function getDataModel($selectsEmp, $params = [], $isPerPage = true)
  45. {
  46. // 获取筛选参数 如果没有时间,则用当天的数据
  47. $startTime = $params['start_time'] ?? request()->input('start_time','');
  48. $endTime = $params['end_time'] ?? request()->input('end_time','');
  49. $status = request()->input('status',false);
  50. $params = [
  51. 'status' => $status !== false ? $status : $params['status'] ?? null,
  52. 'user_id' => $params['user_id'] ?? request()->input('user_id', null),
  53. 'market_id' => $params['market_id'] ?? request()->input('market_id',null),
  54. 'name' => $params['name'] ?? request()->input('name',false),
  55. 'position' => $params['position'] ?? request()->input('position',false),
  56. ];
  57. if(empty($startTime) && empty($endTime)){
  58. return [];
  59. }
  60. $nowDay = date('Y-m-d', time());
  61. if(empty($startTime)){
  62. $startTime = '2020-07-01 00:00:00';
  63. }else{
  64. $startTime = $startTime . ' 00:00:00';
  65. }
  66. if(empty($endTime)){
  67. $endTime = $nowDay . ' 23:59:59';
  68. }else{
  69. $endTime = $endTime . ' 23:59:59';
  70. }
  71. // 获取懒族员工
  72. $employeesData = $this->getEmployeesData($selectsEmp,$params);
  73. $employeesData->orderBy('id','desc');
  74. if($isPerPage){
  75. $employeeList = $employeesData->paginate($this->perPage);
  76. $employeeList = $employeeList->toArray();
  77. }else{
  78. $list = $employeesData->get()->toArray();
  79. $employeeList = [
  80. 'total' => count($list),
  81. 'data' => $list
  82. ];
  83. }
  84. if(!isset($employeeList['data']) || count($employeeList['data']) <= 0 ) {
  85. return $employeeList;
  86. }
  87. foreach ($employeeList['data'] as $key => &$employee) {
  88. // 根据员工信息,查询名下新增绑定的所有服务点 Statistics
  89. $newCsCount = CsInfoModel::where(function ($query) use ($employee) {
  90. $query->where('person_id', '=', $employee['user_id'])
  91. ->orWhere('user_id', '=', $employee['user_id']);
  92. })
  93. ->where('created_at', '>=', strtotime($startTime))
  94. ->where('created_at', '<=', strtotime($endTime))
  95. ->count();
  96. // 查询名下所有的服务点
  97. $allCsInfos = CsInfoModel::where(function ($query) use ($employee) {
  98. $query->where('person_id', '=', $employee['user_id'])
  99. ->orWhere('user_id', '=', $employee['user_id']);
  100. })
  101. ->get()
  102. ->toArray();
  103. // 新增绑定用户数 Statistics
  104. $newBindUserCount = UserRelationBindModel::where('bind_type', '=', 1)
  105. ->whereIn('source_id', array_values(array_column($allCsInfos, 'admin_user_id')))
  106. ->where('created_at', '>=', strtotime($startTime))
  107. ->where('created_at', '<=', strtotime($endTime))
  108. ->count();
  109. // 查询名下所有服务点的所有用户
  110. $allCsBindUsers = UserRelationBindModel::where('bind_type', '=', 1)
  111. ->whereIn('source_id', array_values(array_column($allCsInfos, 'admin_user_id')))
  112. ->get()
  113. ->toArray();
  114. // 平台新增用户数 Statistics
  115. $platNewUsers = OrderMainModel::select('user_id')
  116. ->where('type', '=', OrderMainModel::ORDER_TYPE_ONLINE)
  117. ->whereIn('state', OrderMainModel::ORDER_STATE_FINISH)
  118. ->whereIn('user_id', array_values(array_column($allCsBindUsers, 'user_id')))
  119. ->groupBy('user_id')
  120. ->havingRaw('MIN(created_at)>='.strtotime($startTime).' AND MIN(created_at)<='.strtotime($endTime).'')
  121. ->get()
  122. ->toArray();
  123. $platNewUserCount = count($platNewUsers);
  124. // 所有用户产生的线上订单数 Statistics
  125. $totalOrdersCount = OrderMainModel::where('type', '=', OrderMainModel::ORDER_TYPE_ONLINE)
  126. ->whereIn('state', OrderMainModel::ORDER_STATE_FINISH)
  127. ->whereIn('user_id', array_values(array_column($allCsBindUsers, 'user_id')))
  128. ->where('created_at', '>=', strtotime($startTime))
  129. ->where('created_at', '<=', strtotime($endTime))
  130. ->count();
  131. // 平台新增用户产生的线上订单数 Statistics
  132. $platNewUserOrdersCount =OrderMainModel::where('type', '=', OrderMainModel::ORDER_TYPE_ONLINE)
  133. ->whereIn('state', OrderMainModel::ORDER_STATE_FINISH)
  134. ->whereIn('user_id', array_values(array_column($platNewUsers, 'user_id')))
  135. ->where('created_at', '>=', strtotime($startTime))
  136. ->where('created_at', '<=', strtotime($endTime))
  137. ->count();
  138. $employee['new_cs_count'] = $newCsCount;
  139. $employee['new_bind_user_count'] = $newBindUserCount;
  140. $employee['plat_new_user_count'] = $platNewUserCount;
  141. $employee['bound_order_online_count'] = $totalOrdersCount;
  142. $employee['plat_new_user_order_online_count'] = $platNewUserOrdersCount;
  143. }
  144. return $employeeList;
  145. }
  146. /**
  147. * 获取懒族员工 model
  148. */
  149. public function getEmployeesData($selects, $params = [])
  150. {
  151. $departments = config('role.department_value');
  152. $positions = explode(',', $departments[9]);
  153. $model = Model::select(DB::raw($selects));
  154. if(isset($params['status']) && is_numeric($params['status'])){
  155. $model->where('status',$params['status']);
  156. }
  157. if(isset($params['user_id']) && !empty($params['user_id'])){
  158. $userId = explode(',',$params['user_id']);
  159. $model->whereIn('user_id', $userId);
  160. }
  161. if(isset($params['name']) && $params['name']){
  162. $model->where('name','like','%'.$params['name'].'%');
  163. }
  164. if(isset($params['market_id']) && is_numeric($params['market_id'])){
  165. $model->where('market_id', $params['market_id']);
  166. }
  167. if(!(isset($params['position']) && $params['position'] == 0)){
  168. // $model->whereJsonContains('position', $positions);
  169. $model->where(function ($query) use ($positions) {
  170. foreach($positions as $key => $value){
  171. if($key == 0){
  172. $query->whereJsonContains('position', [$value]);
  173. }else{
  174. $query->orWhere(function ($query) use($value){
  175. $query->whereJsonContains('position', $value);
  176. });
  177. }
  178. }
  179. });
  180. }
  181. return $model;
  182. }
  183. }