123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889 |
- <?php
- namespace app\api\controller;
- use app\event\Msg;
- use app\model\Credits;
- use app\model\Customer;
- use app\model\CustomerClue;
- use app\model\CustomerVisitLog;
- use app\model\Employee;
- use app\model\ExamEmpResult;
- use app\model\ExamPaper;
- use app\model\DailyWork;
- use app\model\DailyTasks;
- use app\model\Org;
- use app\model\TrainClassAssign;
- use app\model\TrainDoneLog;
- use app\model\TrainSumup;
- use app\model\User;
- use app\model\Miniprogram;
- use app\logics\EmployeeLogic;
- use app\logics\OrgLogic;
- use app\model\Company;
- use think\facade\Request;
- use app\model\PreformanceTasks as PreformanceTasksModel;
- use app\model\PreformanceTasksOrg;
- use app\model\ShortUrl;
- class OrgEmployee extends Base
- {
- /**
- * 员工邀请
- */
- public function invite()
- {
- $type = request()->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]);
- }
- }
|