123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- <?php
- namespace app\api\controller;
- use app\model\Customer;
- use app\model\CustomerClue;
- use app\model\Employee;
- use app\model\Org;
- use app\model\User;
- class Company extends Base
- {
- /*
- * 公司员工列表
- * screened:company企业,team团队
- */
- public function employee()
- {
- $param = $this->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]);
- }
- }
|