request->only(['date', 'type', 'page', 'screened']); $token = $this->request->token; //选取时间月初和月末时间 $start_time = $param['date'] . '-01 00:00:00'; $end_time = date('Y-m-d H:i:s', strtotime('+1 months', strtotime($start_time))); $where = [['state', '=', '在职'], ['uid', '<>', 0]]; if ($param['screened'] == 'company') { $where[] = ['root_id', '=', $token['root_org']]; } else { $where[] = ['root_id', '=', $token['root_org']]; $org_id = orgSubIds($token['org_id']); $where[] = ['org_id', 'in', $org_id]; } $searchObj = Employee::with(['user' => function ($query) { $query->field('id, headimgurl'); }])->field('id,uid,name')->where($where); if ($param['type'] == 'ding') { $chaos = Customer::changeState('签单', 'chaos'); $searchObj->withCount([ 'customer' => function ($query) use ($start_time, $end_time, $chaos) { $query->where([['addtime', 'between', [$start_time, $end_time]], ['state', 'in', $chaos]]); } ]); $field = 'customer_count'; $gdp = 'userdingGdp'; $totalType = 'total_customer'; } elseif ($param['type'] == 'ke') { $searchObj->withCount([ 'clue' => function ($query) use ($start_time, $end_time) { $query->where([['addtime', 'between', [$start_time, $end_time]]]); } ]); $field = 'clue_count'; $gdp = 'userFanGdp'; $totalType = 'total_clue'; } elseif ($param['type'] == 'xuefen') { $searchObj->withSum(['studycredit' => function ($query) use ($start_time, $end_time) { $query->where([['addtime', 'between', [$start_time, $end_time]]]); }], 'credits'); $field = 'studycredit_sum'; $gdp = 'usersdyctGdp'; $totalType = 'total_studycredit'; }else { $searchObj->withSum(['credits' => function ($query) use ($start_time, $end_time) { $query->where([['addtime', 'between', [$start_time, $end_time]], ['type', '=', 1]]); }], 'credits'); $field = 'credits_sum'; $gdp = 'userdcreditsGdp'; $totalType = 'total_credits'; } $data = $searchObj->cache(60)->select()->toArray(); array_multisort(array_column($data, $field), SORT_DESC, $data); // 统计总数 $count = array_sum(array_column($data, $field)); // 获取我的数据与排名 $found_arr = array_column($data, 'id'); $key = array_search($token['employee_id'], $found_arr); $myRanking = $data[$key]; $myRanking['ranking'] = $key + 1; // 获取我的占比 $myRanking[$gdp] = $count == 0 ? 0 : floor($myRanking[$field] / $count * 100); // 总人数 $myRanking['count'] = count($data); $myRanking[$totalType] = $count; $data = array_slice($data, ($param['page'] - 1) * 10, 10); if ($param['type'] !== 'ke' && $param['type'] !== 'ding') { if($param['type']== 'xuefen'){ foreach ($data as $k => $v) { $data[$k]['studycredit_max'] = $data[$k]['studycredit_sum']; } }else{ foreach ($data as $k => $v) { $data[$k]['credits_max'] = $data[$k]['credits_sum']; } } } $companyName = Org::where(['id' => $token['root_org']])->value('name'); return json(['code' => self::success, 'msg' => '获取成功', 'company_ranking_list' => $data, 'my_ranking' => $myRanking, 'company' => $companyName]); } }