param('type'); $id = request()->token['root_org']; $miniprogram = Miniprogram::where('root_id', $id)->findOrEmpty(); if ($miniprogram->isEmpty()) { return json(['code' => 1, 'msg' => '暂未配置小程序']); } if ($type == 'm') { $qrcode = $miniprogram->manager_qr; } else { $qrcode = $miniprogram->employee_qr; } return json(['code' => 0, 'data' => $qrcode, 'msg' => '成功获取二维码']); } public function emplist() { $request = request(); $param = $request->param(); $orgid = (isset($param['orgid']) && $param['orgid']) ? $param['orgid'] : request()->token['root_org']; $condition = []; if (isset($param['verified'])) { $condition[] = ['verified', '=', $param['verified']]; if ($param['verified'] == 0) { $condition[] = ['state', '=', '待审核']; $state = '待审核'; } else { $condition[] = ['state', '=', '在职']; $state = '在职'; } } else { $condition[] = ['state', '=', '在职']; $state = '在职'; } $list = EmployeeLogic::employeelistfront($orgid, $condition, $param['page'], $param['limit'], $state); foreach ($list as $k => $v) { $list[$k]['phone'] = substr_replace($v['phone'], "*****", 3, 5); } $count = EmployeeLogic::count($orgid, $state); return json(['code' => 0, 'data' => $list, 'count' => $count]); } public function empdetail() { $token = request()->token; $detail = Employee::with([ 'company' => function ($query) { $query->withField(['id', 'info', 'name', 'org_type']); }, 'user' => function ($query) { $query->withField(['id', 'phone', 'headimgurl', 'nickname', 'sex']); }, 'org' => function ($query) { $query->withField(['id', 'info', 'name', 'org_type']); } ])->field('id,phone,image_photo,is_manager,is_newbie,media_id,name,org_id,role,root_id,uid,qrcode,wx,position,out_call_phone')->find($token['employee_id']); $detail->out_call_phone = $detail->out_call_phone ? explode(',',$detail->out_call_phone) : []; if ($detail) { return json(['code' => 0, 'data' => $detail, 'msg' => '获取成功']); } else { return json(['code' => 1, 'msg' => '获取失败']); } } public function verifypass() { $request = request(); $empid = $request->param('empid'); if ($empid == request()->token['employee_id']) { return json(['code' => 1, 'msg' => '不可操作']); } $employee = Employee::where([['id', '=', $empid], ['state', '=', '待审核']])->findOrEmpty(); if ($employee->isEmpty()) return json(['code' => 1, 'msg' => '该审核已操作或不存在']); /// $employee->save(['state' => '在职', 'left_to_empid' => NULL, 'verified' => 1]); $companyName = Company::where(['root_id' => $request->token['root_org']])->value('company_name'); event(new Msg($empid, '您的申请已经通过,欢迎来到' . $companyName . '大家庭', 'verifypass')); //审核通过短信通知 $clientType = Miniprogram::where('root_id', $request->token['root_org'])->value('notify'); $urldata = [ 'content' => json_encode([ 'client_type' => $clientType, 'path' => "/pages/index/index", 'query' => "ctp=" . $clientType, 'employee_id' => $empid ]), 'type' => 'join_company' ]; $shortUrl = new ShortUrl(); $shortUrl->save($urldata); // 将ID进行 $query = dec52($shortUrl->id); $shortUrl->uri = $query; $shortUrl->save(); sendSms($employee['phone'], $employee['name'] . "您已成功加入" . $companyName . ",请点击链接访问数字化系统https://wzh.nczyzs.com/open/" . $query); return json(['code' => 0, 'msg' => '通过申请成功']); } public function verifyreject() { $request = request(); $empid = $request->param('empid'); if ($empid == request()->token['employee_id']) { return json(['code' => 1, 'msg' => '不可操作']); } $employee = Employee::where([['id', '=', $empid], ['state', '=', '待审核']])->findOrEmpty(); if ($employee->isEmpty()) return json(['code' => 1, 'msg' => '该审核已操作或不存在']); /// $employee->save(['state' => '驳回', 'grant_id' => 0, 'verified' => 0]); return json(['code' => 0, 'msg' => '驳回申请成功']); } public function orgnodelist() { $request = request(); $orgid = 1; if ($request->param('orgid')) { $orgid = $request->param('orgid'); } $orgids = orgSubIds($orgid); $where = [ ['status', '=', 1], ['id', 'in', $orgids] ]; $keyword = input('keyword',''); if ($keyword) { $where[] = ['name','like','%'.$keyword.'%']; } $allnodes = Org::where($where)->with(['employee' => function ($query) { $query->field('e.id,e.name,e.org_id,user.headimgurl,e.is_manager,e.role')->where('e.state', '在职')->alias('e')->join('user', 'user.id=e.uid'); }])->order('path asc, pid asc')->select()->toArray(); return json(['code' => 0, 'data' => $allnodes]); } public function setormoveleader() { $request = request(); $empid = $request->param('empid'); $employee = Employee::where([['id', '=', $empid], ['root_id', '=', $request->token['root_org']]])->find(); $opName = Employee::where(['id' => $request->token['employee_id']])->value('name'); if ($request->param('action') == 'setleader') { $is_manager = 1; //$grant_id = 1; $role = '领导'; $msg = '设置领导成功'; $orgName = Org::where('id', $employee->org_id)->value('name'); $eventMsg = '您被' . $opName . '添加为' . $orgName . '部负责人身份,请知晓!'; $type = 'setleader'; } else { $is_manager = 0; //$grant_id = 2; $role = '员工'; $msg = '取消领导成功'; $eventMsg = '您的负责人身份被' . $opName . '移除了,请知晓!'; $type = 'removeleader'; } //只能变更自己部门之下的部门员工职位 // if(request()->token['employee_id'] == $empid){ // return json(['code' => 1, 'msg' => '不能变更自己的职位']); // }elseif (request()->token['org_id']==$employee['org_id']) {//同级别,同部门,管理层之间不可以进行职位调整 // if($employee['is_manager']==1) return json(['code' => 1, 'msg' => '同部门同级别管理层之间不可以进行职位调整']); // }else{ // $path = Org::where([['id','=',request()->token['org_id']]])->value('path') ?: 'abc'; // $org_ids = Org::where([['path','like',$path.'%'],['path','<>',$path]])->column('id') ?: ['abc']; // $eid = Employee::where([['id','=',$empid],['org_id','in',$org_ids]])->find(); // if (!$eid) return json(['code' => 1, 'msg' => '只能变更自己部门级别之下的部门员工职位']); // } /// $employee->save(['is_manager' => $is_manager, 'role' => $role]); event(new Msg($empid, $eventMsg, $type)); return json(['code' => 0, 'msg' => $msg]); } public function apiChangeDepartment() { $request = request(); $params = $request->param(); if (!isset($params['empid'])) return false; if (!isset($params['orgid'])) return false; //只能变更自己部门之下的部门员工职位 // if(request()->token['employee_id'] == $params['empid']){ // return json(['code' => 1, 'msg' => '不能变更自己的部门']); // }else{ // $path = Org::where([['id','=',request()->token['org_id']]])->value('path') ?: 'abc'; // $org_ids = Org::where([['path','like',$path.'%'],['path','<>',$path]])->column('id') ?: ['abc']; // if (!in_array($params['orgid'],$org_ids)) { // return json(['code' => 1, 'msg' => '只能变更到自己部门级别之下的部门']); // } // $eid = Employee::where([['id','=',$params['orgid']],['org_id','in',$org_ids]])->find(); // if (!$eid) { // return json(['code' => 1, 'msg' => '只能变更自己部门级别之下的部门员工']); // } // } $newdata = []; $newdata['org_id'] = $params['orgid']; if (isset($params['therole'])) { $newdata['role'] = $params['therole']; } if (isset($params['is_manager'])) { $newdata['is_manager'] = ($params['is_manager'] == 'true') ? 1 : 0; } if (Employee::where('id', $params['empid'])->update($newdata)) { if (request()->token['employee_id'] == $params['empid']) { // 获取用户组织 $org = [$newdata['org_id']]; if ($params['is_manager']) { $org = (new Org())->getChildOrg($newdata['org_id']); cache('org_manager_' . $newdata['org_id'], $org); } // 设置请求信息 $request->employee = Employee::where('id', $params['empid'])->find(); session('employee', $request->employee); $request->org = $org; // 获取用户组织 $org = [$newdata['org_id']]; if ($params['is_manager']) { $org = (new Org())->getChildOrg($newdata['org_id']); cache('org_manager_' . $newdata['org_id'], $org); } // 设置请求信息 $request->employee = Employee::where('id', $params['empid'])->find(); session('employee', $request->employee); $request->org = $org; } return json(['code' => 0, 'msg' => '部门调整成功']); } else { return json(['code' => 1, 'msg' => '部门调整失败']); } } public function apiQuitHandling() { $request = request(); $params = $request->param(); if (!isset($params['empid'])) return false; if (!isset($params['target_employee_id']) || $params['target_employee_id'] == 0) return json(['code' => 1, 'msg' => '必须选择交接人']); if (!Employee::where('id', $params['empid'])->update(['state' => '离职', 'left_to_empid' => $params['target_employee_id'], 'is_manager' => 0, 'verified' => 0])) { return json(['code' => 1, 'msg' => '人员不存在,或离职状态未更改']); } // //$targetArr = EmployeeModel::find($params['target_employee_id'])->toArray(); //handle customers $old_data = Customer::where(['employee_id' => $params['empid']])->select(); foreach ($old_data as $item) { $ori_info_arr = [ //'uid' => $item->uid, 'employee_id' => $item->employee_id, ]; Customer::where(['id' => $item->id])->update([ 'employee_id' => $params['target_employee_id'], //'uid' => (isset($targetArr['uid'])) ? $targetArr['uid'] : NULL, 'changed_ori_info' => json($ori_info_arr) ]); } //handle customer clue $old_clue_data = CustomerClue::where(['employee_id' => $params['empid']])->select(); foreach ($old_clue_data as $item) { $ori_info_arr = [ 'employee_id' => $item->employee_id ]; CustomerClue::where(['id' => $item->id])->update([ 'employee_id' => $params['target_employee_id'], 'changed_ori_info' => json($ori_info_arr) ]); } return json(['code' => 0, 'msg' => '处理离职完成']); } public function targetemplist() { $employeeid = request('empid'); $target_employee_list = EmployeeLogic::getEmpInSameNode($employeeid); return json(['code' => 0, 'data' => $target_employee_list, 'msg' => '目标人员列表']); } public function orgstruc() { $datastr = ''; $empid = request()->token['employee_id']; $employeeAdmin = Employee::find($empid); //if ($employeeAdmin && $employeeAdmin->is_manager == 1 && $employeeAdmin->grant_id == 1) { // $orgids = orgSubIds($employeeAdmin->org_id); // $datastr = OrgLogic::mystruc($orgids); //} $orgids = orgSubIds($employeeAdmin->org_id); $datastr = OrgLogic::mystruc($orgids); return json(['code' => 0, 'data' => $datastr, 'msg' => '所辖部门获取成功']); } /** * 员工五维画像 */ public function employee_icon() { $employee_id = input('employee_id', '', 'intval'); $employee = Employee::with(['org', 'companys', 'user'])->field(['id', 'phone', 'name', 'org_id', 'root_id', 'uid'])->find($employee_id); $root_id = request()->token['root_org']; $last_month_start = date('Y-m-d', strtotime('first day of last month')); $this_month_start = date('Y-m-01', time()); //$all_c_where[] = ['employee_id', '=', $employee_id]; $org_ids = orgSubIds($root_id); $all_c_where[] = ['org_id', 'in', $org_ids]; $all_c_where[] = ['employee_id', '>', 0]; $all_c_where[] = ['is_resource', '=', 0]; $all_c_where[] = ['addtime', '<', $this_month_start]; $all_customer_count = Customer::where($all_c_where)->count(); //本月之前所有客户数 $state_daodian = CustomerVisitLog::changeState('已到店', 'chaos'); $state_liangfang = CustomerVisitLog::changeState('已量房', 'chaos'); $state_daochang = CustomerVisitLog::changeState('已到场', 'chaos'); $state_merge = array_merge($state_daodian, $state_liangfang, $state_daochang); $visit_c_where[] = ['employee_id', '=', $employee_id]; $visit_c_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]]; $visit_c_where[] = ['state', 'in', $state_merge]; $visit_customer_count = CustomerVisitLog::where($visit_c_where)->group('customer_id')->count(); // 上月见面客户数 //见面指数 $visit_percent = $all_customer_count > 0 ? round($visit_customer_count / $all_customer_count * 100) : 0; // 上月收定数 $state_deposit = CustomerVisitLog::changeState('已交定', 'chaos'); $deposit_c_where[] = ['employee_id', '=', $employee_id]; $deposit_c_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]]; $deposit_c_where[] = ['state', 'in', $state_deposit]; $deposit_customer_count = CustomerVisitLog::where($deposit_c_where)->group('customer_id')->count(); // 收定指数 if ($visit_customer_count < $deposit_customer_count) { $visit_customer_count = $deposit_customer_count; } $deposit_percent = $visit_customer_count > 0 ? round($deposit_customer_count / $visit_customer_count * 100) : 0; //上月签单 $state_sign = CustomerVisitLog::changeState('已签单', 'chaos'); $sign_c_where[] = ['employee_id', '=', $employee_id]; $sign_c_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]]; $sign_c_where[] = ['state', 'in', $state_sign]; $sign_customer_count = CustomerVisitLog::where($sign_c_where)->group('customer_id')->count(); // 签单指数 if ($deposit_customer_count < $sign_customer_count) { $deposit_customer_count = $sign_customer_count; } $sign_percent = $deposit_customer_count > 0 ? round($sign_customer_count / $deposit_customer_count * 100) : 0; // 学习指数 $all_class_where[] = ['employee_id', '=', $employee_id]; $all_class_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]]; $all_class_list = TrainClassAssign::where($all_class_where)->select(); $all_class_ids = $all_class_list->column('class_id'); $all_class_count = $all_class_list->count(); $study_class_where[] = ['employee_id', '=', $employee_id]; $study_class_where[] = ['root_id', '=', $root_id]; $study_class_where[] = ['class_id', 'in', $all_class_ids]; $study_class_where[] = ['from', '=', 0]; $study_class_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]]; $study_class_count = TrainDoneLog::where($study_class_where)->count(); $study_percent = $all_class_count > 0 ? round($study_class_count / $all_class_count * 100) : 0; // 员工信息 $return_data['employee'] = !empty($employee) ? $employee->toArray() : []; // 见面指数 $return_data['all_customer_count'] = $all_customer_count; $return_data['visit_customer_count'] = $visit_customer_count; $return_data['visit_percent'] = $visit_percent; // 收定指数 $return_data['deposit_customer_count'] = $deposit_customer_count; $return_data['deposit_percent'] = $deposit_percent; // 签单指数 $return_data['sign_customer_count'] = $sign_customer_count; $return_data['sign_percent'] = $sign_percent; // 学习指数 $return_data['all_class_count'] = $all_class_count; $return_data['study_class_count'] = $study_class_count; $return_data['study_percent'] = $study_percent; // 贡献值 $credits_where[] = ['root_id', '=', $root_id]; $credits_where[] = ['employee_id', '=', $employee_id]; $credits_where[] = ['type', '=', 1]; $credits_where[] = ['del', '=', 0]; $credits_all = Credits::where($credits_where)->sum('credits'); $return_data['credits_all'] = $credits_all; //贡献值排名 $credits_ranking_where[] = ['root_id', '=', $root_id]; $credits_ranking_where[] = ['type', '=', 1]; $credits_ranking_where[] = ['del', '=', 0]; $credits_ranking = Credits::where($credits_ranking_where)->group('employee_id')->order('sum desc')->column('sum(credits) as sum'); $return_data['credits_ranking'] = 0; if ($credits_ranking && $credits_all) { $key = array_search($credits_all, $credits_ranking); $return_data['credits_ranking'] = $key === false ? 0 : $key + 1; } // 贡献指数 $credits_percent = 0; $return_data['credits_top'] = 0; if (!empty($credits_ranking[0])) { $return_data['credits_top'] = $credits_ranking[0]; $credits_percent = $credits_ranking[0] > 0 ? round($credits_all / $credits_ranking[0] * 100) : 0; } $return_data['credits_percent'] = $credits_percent; //执行指数 // 任务 $employee_org_id = $employee['org_id']; $task_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]]; $task_where[] = ['org_ids', '=', $employee_org_id]; $task_where[] = ['root_id', '=', $root_id]; $task_where[] = ['del', '=', 0]; $task_all_list = DailyTasks::where($task_where)->select(); $task_all_count = $task_all_list->count(); // 已完成任务 $task_done_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]]; $task_done_where[] = ['org_ids', '=', $employee_org_id]; $task_done_where[] = ['root_id', '=', $root_id]; $task_done_where[] = ['del', '=', 0]; $task_done_list = DailyTasks::where($task_done_where)->whereRaw("FIND_IN_SET(" . $employee_id . " , employee_ids)")->select(); $task_done_count = $task_done_list->count(); $performance_done_count = 0; $org_id = $employee['org_id']; $w[] = ['start_date', 'between', [$last_month_start, $this_month_start]]; $w[] = ['root_id', '=', $root_id]; $root_performance_ids = PreformanceTasksModel::where($w)->column('id'); $performance_all_count = PreformanceTasksOrg::where([['org_id', '=', $org_id], ['performance_tasks_id', 'in', $root_performance_ids]])->count(); $w[] = ['customer_visit_log_id', '<>', '']; $w[] = ['customer_visit_log_id', 'not null', '']; $tasks = PreformanceTasksModel::with(['preformanceTasksOrgOne' => function ($query) use ($org_id) { $query->where('org_id', $org_id)->visible(['org_id', 'performance_tasks_id', 'ok_deposit', 'ok_money'])->where('del', 0); }])->where($w)->field('*')->select(); foreach ($tasks as $k => $v) { if ($v->preformanceTasksOrgOne) { $vids = explode(',', $v->customer_visit_log_id); $count = CustomerVisitLog::where([['id', 'in', $vids], ['employee_id', '=', $employee_id]])->count(); if ($count == 0) continue; $ok = 0; //部门人数 $employee_count = Employee::where([['org_id', '=', $org_id], ['state', '=', '在职'], ['uid', '>', 0]])->count(); //完成指标 $deposit = $employee_count > 0 ? floor($v->preformanceTasksOrgOne->deposit / $employee_count) : 0; $money = $employee_count > 0 ? floor($v->preformanceTasksOrgOne->money / $employee_count) : 0; if ($v->preformanceTasksOrgOne->is_deposit == 1) { $w1[] = ['id', 'in', $vids]; $w1[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]; $w1[] = ['employee_id', '=', $employee_id]; $my_deposit = CustomerVisitLog::where($w1)->count(); unset($w1); if ($my_deposit >= $deposit) $ok += 1; } else { $ok += 1; } if ($v->preformanceTasksOrgOne->is_money == 1) { $w1[] = ['id', 'in', $vids]; $w1[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]; $w1[] = ['employee_id', '=', $employee_id]; $my_money = CustomerVisitLog::where($w1)->sum('money'); unset($w1); if ($my_money >= $money) $ok += 1; } else { $ok += 1; } if ($ok == 2) $performance_done_count += 1; } } $return_data['task_done_count'] = $task_done_count + $performance_done_count; $return_data['task_all_count'] = $task_all_count + $performance_all_count; $return_data['task_percent'] = $return_data['task_all_count'] > 0 ? round($return_data['task_done_count'] / $return_data['task_all_count'] * 100) : 0; return json(['code' => self::success, 'data' => $return_data]); } /** * 五维画像-量化盘点 */ public function employee_icon_data() { $date = input('month', date('Y-m'), 'trim'); $month = $date . '-01'; $next_month = date('Y-m-d', strtotime("$month next month")); $employee_id = input('employee_id', '', 'intval'); $root_id = request()->token['root_org']; $this_month_start = date('Y-m-01'); $next_month_start = date('Y-m-d', strtotime($this_month_start . ' next month')); $state_daodian = CustomerVisitLog::changeState('已到店', 'chaos'); $state_daiqueren = CustomerVisitLog::changeState('待确认', 'chaos'); $state_daochang = CustomerVisitLog::changeState('已到场', 'chaos'); $state_liangfang = CustomerVisitLog::changeState('已量房', 'chaos'); $state_deposit = CustomerVisitLog::changeState('已交定', 'chaos'); $state_sign = CustomerVisitLog::changeState('已签单', 'chaos'); // 本月待确认 $this_month_customer_daiqueren_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]]; $this_month_customer_daiqueren_where[] = ['employee_id', '=', $employee_id]; $this_month_customer_daiqueren_where[] = ['state', 'in', $state_daiqueren]; $this_month_customer_daiqueren_count = CustomerVisitLog::where($this_month_customer_daiqueren_where)->count(); // 本月到店量 $this_month_customer_daodian_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]]; $this_month_customer_daodian_where[] = ['employee_id', '=', $employee_id]; $this_month_customer_daodian_where[] = ['state', 'in', $state_daodian]; $this_month_customer_daodian_count = CustomerVisitLog::where($this_month_customer_daodian_where)->count(); // 本月到场量 $this_month_customer_daochang_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]]; $this_month_customer_daochang_where[] = ['employee_id', '=', $employee_id]; $this_month_customer_daochang_where[] = ['state', 'in', $state_daochang]; $this_month_customer_daochang_count = CustomerVisitLog::where($this_month_customer_daochang_where)->count(); // 本月量房量 $this_month_customer_liangfang_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]]; $this_month_customer_liangfang_where[] = ['employee_id', '=', $employee_id]; $this_month_customer_liangfang_where[] = ['state', 'in', $state_liangfang]; $this_month_customer_liangfang_count = CustomerVisitLog::where($this_month_customer_liangfang_where)->count(); // 本月交定量 $this_month_customer_deposit_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]]; $this_month_customer_deposit_where[] = ['employee_id', '=', $employee_id]; $this_month_customer_deposit_where[] = ['state', 'in', $state_deposit]; $this_month_customer_deposit_count = CustomerVisitLog::where($this_month_customer_deposit_where)->count(); // 本月签单量 $this_month_customer_sign_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]]; $this_month_customer_sign_where[] = ['employee_id', '=', $employee_id]; $this_month_customer_sign_where[] = ['state', 'in', $state_sign]; $this_month_customer_sign_count = CustomerVisitLog::where($this_month_customer_sign_where)->count(); // 客户本月数据 $data['customer']['this_month'] = [ 'daiqueren' => $this_month_customer_daiqueren_count, 'daodian' => $this_month_customer_daodian_count, 'daochang' => $this_month_customer_daochang_count, 'liangfang' => $this_month_customer_liangfang_count, 'deposit' => $this_month_customer_deposit_count, 'sign' => $this_month_customer_sign_count, ]; // 选择月待确认 $that_month_customer_daiqueren_where[] = ['addtime', 'between', [$month, $next_month]]; $that_month_customer_daiqueren_where[] = ['employee_id', '=', $employee_id]; $that_month_customer_daiqueren_where[] = ['state', 'in', $state_daiqueren]; $that_month_customer_daiqueren_count = CustomerVisitLog::where($that_month_customer_daiqueren_where)->count(); // 选择月到店量 $that_month_customer_daodian_where[] = ['addtime', 'between', [$month, $next_month]]; $that_month_customer_daodian_where[] = ['employee_id', '=', $employee_id]; $that_month_customer_daodian_where[] = ['state', 'in', $state_daodian]; $that_month_customer_daodian_count = CustomerVisitLog::where($that_month_customer_daodian_where)->count(); // 选择月到场量 $that_month_customer_daochang_where[] = ['addtime', 'between', [$month, $next_month]]; $that_month_customer_daochang_where[] = ['employee_id', '=', $employee_id]; $that_month_customer_daochang_where[] = ['state', 'in', $state_daochang]; $that_month_customer_daochang_count = CustomerVisitLog::where($that_month_customer_daochang_where)->count(); // 选择月量房量 $that_month_customer_liangfang_where[] = ['addtime', 'between', [$month, $next_month]]; $that_month_customer_liangfang_where[] = ['employee_id', '=', $employee_id]; $that_month_customer_liangfang_where[] = ['state', 'in', $state_liangfang]; $that_month_customer_liangfang_count = CustomerVisitLog::where($that_month_customer_liangfang_where)->count(); // 选择月交定量 $that_month_customer_deposit_where[] = ['addtime', 'between', [$month, $next_month]]; $that_month_customer_deposit_where[] = ['employee_id', '=', $employee_id]; $that_month_customer_deposit_where[] = ['state', 'in', $state_deposit]; $that_month_customer_deposit_count = CustomerVisitLog::where($that_month_customer_deposit_where)->group('customer_id')->count(); // 选择月签单量 $that_month_customer_sign_where[] = ['addtime', 'between', [$month, $next_month]]; $that_month_customer_sign_where[] = ['employee_id', '=', $employee_id]; $that_month_customer_sign_where[] = ['state', 'in', $state_sign]; $that_month_customer_sign_count = CustomerVisitLog::where($that_month_customer_sign_where)->count(); // 客户当月数据 $data['customer']['that_month'] = [ 'daiqueren' => $that_month_customer_daiqueren_count, 'daodian' => $that_month_customer_daodian_count, 'daochang' => $that_month_customer_daochang_count, 'liangfang' => $that_month_customer_liangfang_count, 'deposit' => $that_month_customer_deposit_count, 'sign' => $that_month_customer_sign_count, ]; // 学习情况 $all_class_where[] = ['employee_id', '=', $employee_id]; $all_class_where[] = ['addtime', 'between', [$month, $next_month]]; $all_class_list = TrainClassAssign::where($all_class_where)->select(); $all_class_ids = $all_class_list->column('class_id'); $all_class_count = $all_class_list->count(); $study_class_where[] = ['employee_id', '=', $employee_id]; $study_class_where[] = ['root_id', '=', $root_id]; $study_class_where[] = ['class_id', 'in', $all_class_ids]; $study_class_where[] = ['from', '=', 0]; $study_class_where[] = ['addtime', 'between', [$month, $next_month]]; $study_class_count = TrainDoneLog::where($study_class_where)->count(); $study_percent = $all_class_count > 0 ? round($study_class_count / $all_class_count * 100) : 0; $data['study']['class'] = [ 'all_count' => $all_class_count, 'done_count' => $study_class_count, 'percent' => $study_percent ]; // 考试情况 $exam_assign_where[] = ['root_id', '=', $root_id]; $exam_assign_where[] = ['starttime', 'between', [$month, $next_month]]; $exam_assign_where[] = ['state', '=', 1]; $exam_assign_list = ExamPaper::where($exam_assign_where)->whereRaw("FIND_IN_SET(" . $employee_id . " , assessment)")->select(); $exam_assign_count = $exam_assign_list->count(); $exam_assign_ids = $exam_assign_list->column('id'); $exam_done_where[] = ['paper_id', 'in', $exam_assign_ids]; $exam_done_where[] = ['employee_id', '=', $employee_id]; $exam_done_where[] = ['addtime', 'between', [$month, $next_month]]; $exam_done_count = ExamEmpResult::where($exam_done_where)->count(); $exam_percent = $exam_assign_count > 0 ? round($exam_done_count / $exam_assign_count * 100) : 0; $data['study']['exam'] = [ 'all_count' => $exam_assign_count, 'done_count' => $exam_done_count, 'percent' => $exam_percent ]; // 学习总结 $sumup_where[] = ['root_id', '=', $root_id]; $sumup_where[] = ['employee_id', '=', $employee_id]; $sumup_where[] = ['class_id', 'in', $all_class_ids]; $sumup_where[] = ['addtime', 'between', [$month, $next_month]]; $sumup_count = TrainSumup::where($sumup_where)->group('class_id')->count(); $sumup_percent = $all_class_count > 0 ? round($sumup_count / $all_class_count * 100) : 0; $data['study']['sumup'] = [ 'all_count' => $all_class_count, 'done_count' => $sumup_count, 'percent' => $sumup_percent ]; // 日报 $uid = Employee::where('id', '=', $employee_id)->value('uid'); $work_day_where[] = ['uid', '=', $uid]; $work_day_where[] = ['type', '=', 'day']; $work_day_where[] = ['addtime', 'between', [$month, $next_month]]; $work_day_count = DailyWork::where($work_day_where)->count(); $work_day_all = date('t', strtotime($month)); $work_day_percent = round($work_day_count / $work_day_all * 100); $data['work']['day'] = [ 'all_count' => $work_day_all, 'done_count' => $work_day_count, 'percent' => $work_day_percent ]; // 周报 $work_week_where[] = ['uid', '=', $uid]; $work_week_where[] = ['type', '=', 'week']; $work_week_where[] = ['addtime', 'between', [$month, $next_month]]; $work_week_count = DailyWork::where($work_week_where)->count(); $work_week_percent = $work_week_count > 3 ? 100 : round($work_week_count / 4 * 100); $data['work']['week'] = [ 'all_count' => 4, 'done_count' => $work_week_count, 'percent' => $work_week_percent ]; // 月报 $work_month_where[] = ['uid', '=', $uid]; $work_month_where[] = ['type', '=', 'month']; $work_month_where[] = ['addtime', 'between', [$month, $next_month]]; $work_month_count = DailyWork::where($work_month_where)->count(); $work_month_percent = $work_month_count > 0 ? 100 : 0; $data['work']['month'] = [ 'all_count' => 1, 'done_count' => $work_month_count, 'percent' => $work_month_percent ]; // 任务 $employee_org_id = Employee::where('id', $employee_id)->value('org_id'); $orgParentIds = orgParentIds($employee_org_id); $task_where[] = ['addtime', 'between', [$month, $next_month]]; $task_where[] = ['org_ids', 'in', $orgParentIds]; $task_where[] = ['root_id', '=', $root_id]; $task_where[] = ['del', '=', 0]; $task_all_list = DailyTasks::where($task_where)->select(); $task_all_count = $task_all_list->count(); // 已完成任务 $task_done_where[] = ['addtime', 'between', [$month, $next_month]]; $task_done_where[] = ['org_ids', 'in', $orgParentIds]; $task_done_where[] = ['root_id', '=', $root_id]; $task_done_where[] = ['del', '=', 0]; $task_done_list = DailyTasks::where($task_done_where)->whereRaw("FIND_IN_SET(" . $employee_id . " , employee_ids)")->select(); $task_done_count = $task_done_list->count(); $task_done_percent = $task_all_count > 0 ? round($task_done_count / $task_all_count * 100) : 0; // 进行中及逾期任务 $task_overdue_count = 0; $task_ing_count = 0; foreach ($task_all_list as $k => $v) { $employee_ids = array_filter(explode(',', $v['employee_ids'])); if (empty($employee_ids)) { if ($v['endtime'] < date('Y-m-d', time())) { //逾期 $task_overdue_count++; } else { $task_ing_count++; } } else { if (!in_array($employee_id, $employee_ids)) { if ($v['endtime'] < date('Y-m-d', time())) { //逾期 $task_overdue_count++; } else { $task_ing_count++; } } } } $task_overdue_percent = $task_all_count > 0 ? round($task_overdue_count / $task_all_count * 100) : 0; $task_ing_percent = $task_all_count > 0 ? round($task_ing_count / $task_all_count * 100) : 0; $data['task'] = [ 'all_count' => $task_all_count, 'done_count' => $task_done_count, 'done_percent' => $task_done_percent, 'overdue_count' => $task_overdue_count, 'overdue_percent' => $task_overdue_percent, 'ing_count' => $task_ing_count, 'ing_percent' => $task_ing_percent ]; //执行指数 $performance_where[] = ['start_date', 'between', [$month, $next_month]]; $performance_where[] = ['root_id', '=', $root_id]; $employee = Employee::find($employee_id); $org_id = $employee['org_id']; $tasks = PreformanceTasksModel::with(['preformanceTasksOrgOne' => function ($query) use ($org_id) { $query->where('org_id', $org_id)->visible(['org_id', 'performance_tasks_id,ok_deposit,ok_money'])->where('del', 0); }])->where($performance_where)->field('*')->select(); $performance_data = []; foreach ($tasks as $k => $v) { if ($v->preformanceTasksOrgOne) { $once_data['title'] = $v['title']; $once_data['is_deposit'] = 0; $once_data['my_deposit'] = 0; $once_data['team_deposit'] = 0; $once_data['all_deposit'] = 0; $once_data['my_deposit_percent'] = 0; $once_data['team_deposit_percent'] = 0; $once_data['is_money'] = 0; $once_data['my_money'] = 0; $once_data['team_money'] = 0; $once_data['all_money'] = 0; $once_data['my_money_percent'] = 0; $once_data['team_money_percent'] = 0; //部门人数 $employee_list = Employee::where([['org_id', '=', $org_id], ['state', '=', '在职'], ['uid', '>', 0]])->select(); $vids = explode(',', $v->customer_visit_log_id); //完成指标 if ($v->preformanceTasksOrgOne->is_deposit == 1) { $once_data['is_deposit'] = 1; $once_data['all_deposit'] = $v->preformanceTasksOrgOne->deposit; $w1[] = ['id', 'in', $vids]; $w1[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]; $w1[] = ['employee_id', '=', $employee_id]; $my_deposit = CustomerVisitLog::where($w1)->count(); unset($w1); $once_data['my_deposit'] = $my_deposit; $once_data['my_deposit_percent'] = $once_data['all_deposit'] > 0 ? round($once_data['my_deposit'] / $once_data['all_deposit'] * 100) : 0; // 团队指标 $w2[] = ['id', 'in', $vids]; $w2[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')]; $w2[] = ['employee_id', 'in', $employee_list->column('id')]; $team_deposit = CustomerVisitLog::where($w2)->count(); unset($w2); $once_data['team_deposit'] = $team_deposit; $once_data['team_deposit_percent'] = $once_data['all_deposit'] > 0 ? round($once_data['team_deposit'] / $once_data['all_deposit'] * 100) : 0; } if ($v->preformanceTasksOrgOne->is_money == 1) { $once_data['is_money'] = 1; $once_data['all_money'] = $v->preformanceTasksOrgOne->money; $w1[] = ['id', 'in', $vids]; $w1[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]; $w1[] = ['employee_id', '=', $employee_id]; $my_money = CustomerVisitLog::where($w1)->sum('money'); unset($w1); $once_data['my_money'] = $my_money; $once_data['my_money_percent'] = $once_data['all_money'] > 0 ? round($once_data['my_money'] / $once_data['all_money'] * 100) : 0; // 团队签单 $w2[] = ['id', 'in', $vids]; $w2[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')]; $w2[] = ['employee_id', 'in', $employee_list->column('id')]; $team_money = CustomerVisitLog::where($w2)->sum('money'); unset($w2); $once_data['team_money'] = $team_money; $once_data['team_money_percent'] = $once_data['all_money'] > 0 ? round($once_data['team_money'] / $once_data['all_money'] * 100) : 0; } $performance_data[] = $once_data; } } $data['performance'] = $performance_data; return json(['code' => self::success, 'data' => $data]); } }