1
0

OrgEmployee.php 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. <?php
  2. namespace app\api\controller;
  3. use app\event\Msg;
  4. use app\model\Credits;
  5. use app\model\Customer;
  6. use app\model\CustomerClue;
  7. use app\model\CustomerVisitLog;
  8. use app\model\Employee;
  9. use app\model\ExamEmpResult;
  10. use app\model\ExamPaper;
  11. use app\model\DailyWork;
  12. use app\model\DailyTasks;
  13. use app\model\Org;
  14. use app\model\TrainClassAssign;
  15. use app\model\TrainDoneLog;
  16. use app\model\TrainSumup;
  17. use app\model\User;
  18. use app\model\Miniprogram;
  19. use app\logics\EmployeeLogic;
  20. use app\logics\OrgLogic;
  21. use app\model\Company;
  22. use think\facade\Request;
  23. use app\model\PreformanceTasks as PreformanceTasksModel;
  24. use app\model\PreformanceTasksOrg;
  25. use app\model\ShortUrl;
  26. class OrgEmployee extends Base
  27. {
  28. /**
  29. * 员工邀请
  30. */
  31. public function invite()
  32. {
  33. $type = request()->param('type');
  34. $id = request()->token['root_org'];
  35. $miniprogram = Miniprogram::where('root_id', $id)->findOrEmpty();
  36. if ($miniprogram->isEmpty()) {
  37. return json(['code' => 1, 'msg' => '暂未配置小程序']);
  38. }
  39. if ($type == 'm') {
  40. $qrcode = $miniprogram->manager_qr;
  41. } else {
  42. $qrcode = $miniprogram->employee_qr;
  43. }
  44. return json(['code' => 0, 'data' => $qrcode, 'msg' => '成功获取二维码']);
  45. }
  46. public function emplist()
  47. {
  48. $request = request();
  49. $param = $request->param();
  50. $orgid = (isset($param['orgid']) && $param['orgid']) ? $param['orgid'] : request()->token['root_org'];
  51. $condition = [];
  52. if (isset($param['verified'])) {
  53. $condition[] = ['verified', '=', $param['verified']];
  54. if ($param['verified'] == 0) {
  55. $condition[] = ['state', '=', '待审核'];
  56. $state = '待审核';
  57. } else {
  58. $condition[] = ['state', '=', '在职'];
  59. $state = '在职';
  60. }
  61. } else {
  62. $condition[] = ['state', '=', '在职'];
  63. $state = '在职';
  64. }
  65. $list = EmployeeLogic::employeelistfront($orgid, $condition, $param['page'], $param['limit'], $state);
  66. foreach ($list as $k => $v) {
  67. $list[$k]['phone'] = substr_replace($v['phone'], "*****", 3, 5);
  68. }
  69. $count = EmployeeLogic::count($orgid, $state);
  70. return json(['code' => 0, 'data' => $list, 'count' => $count]);
  71. }
  72. public function empdetail()
  73. {
  74. $token = request()->token;
  75. $detail = Employee::with([
  76. 'company' => function ($query) {
  77. $query->withField(['id', 'info', 'name', 'org_type']);
  78. }, 'user' => function ($query) {
  79. $query->withField(['id', 'phone', 'headimgurl', 'nickname', 'sex']);
  80. }, 'org' => function ($query) {
  81. $query->withField(['id', 'info', 'name', 'org_type']);
  82. }
  83. ])->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']);
  84. $detail->out_call_phone = $detail->out_call_phone ? explode(',',$detail->out_call_phone) : [];
  85. if ($detail) {
  86. return json(['code' => 0, 'data' => $detail, 'msg' => '获取成功']);
  87. } else {
  88. return json(['code' => 1, 'msg' => '获取失败']);
  89. }
  90. }
  91. public function verifypass()
  92. {
  93. $request = request();
  94. $empid = $request->param('empid');
  95. if ($empid == request()->token['employee_id']) {
  96. return json(['code' => 1, 'msg' => '不可操作']);
  97. }
  98. $employee = Employee::where([['id', '=', $empid], ['state', '=', '待审核']])->findOrEmpty();
  99. if ($employee->isEmpty()) return json(['code' => 1, 'msg' => '该审核已操作或不存在']);
  100. ///
  101. $employee->save(['state' => '在职', 'left_to_empid' => NULL, 'verified' => 1]);
  102. $companyName = Company::where(['root_id' => $request->token['root_org']])->value('company_name');
  103. event(new Msg($empid, '您的申请已经通过,欢迎来到' . $companyName . '大家庭', 'verifypass'));
  104. //审核通过短信通知
  105. $clientType = Miniprogram::where('root_id', $request->token['root_org'])->value('notify');
  106. $urldata = [
  107. 'content' => json_encode([
  108. 'client_type' => $clientType,
  109. 'path' => "/pages/index/index",
  110. 'query' => "ctp=" . $clientType,
  111. 'employee_id' => $empid
  112. ]),
  113. 'type' => 'join_company'
  114. ];
  115. $shortUrl = new ShortUrl();
  116. $shortUrl->save($urldata);
  117. // 将ID进行
  118. $query = dec52($shortUrl->id);
  119. $shortUrl->uri = $query;
  120. $shortUrl->save();
  121. sendSms($employee['phone'], $employee['name'] . "您已成功加入" . $companyName . ",请点击链接访问数字化系统https://wzh.nczyzs.com/open/" . $query);
  122. return json(['code' => 0, 'msg' => '通过申请成功']);
  123. }
  124. public function verifyreject()
  125. {
  126. $request = request();
  127. $empid = $request->param('empid');
  128. if ($empid == request()->token['employee_id']) {
  129. return json(['code' => 1, 'msg' => '不可操作']);
  130. }
  131. $employee = Employee::where([['id', '=', $empid], ['state', '=', '待审核']])->findOrEmpty();
  132. if ($employee->isEmpty()) return json(['code' => 1, 'msg' => '该审核已操作或不存在']);
  133. ///
  134. $employee->save(['state' => '驳回', 'grant_id' => 0, 'verified' => 0]);
  135. return json(['code' => 0, 'msg' => '驳回申请成功']);
  136. }
  137. public function orgnodelist()
  138. {
  139. $request = request();
  140. $orgid = 1;
  141. if ($request->param('orgid')) {
  142. $orgid = $request->param('orgid');
  143. }
  144. $orgids = orgSubIds($orgid);
  145. $where = [
  146. ['status', '=', 1],
  147. ['id', 'in', $orgids]
  148. ];
  149. $keyword = input('keyword','');
  150. if ($keyword) {
  151. $where[] = ['name','like','%'.$keyword.'%'];
  152. }
  153. $allnodes = Org::where($where)->with(['employee' => function ($query) {
  154. $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');
  155. }])->order('path asc, pid asc')->select()->toArray();
  156. return json(['code' => 0, 'data' => $allnodes]);
  157. }
  158. public function setormoveleader()
  159. {
  160. $request = request();
  161. $empid = $request->param('empid');
  162. $employee = Employee::where([['id', '=', $empid], ['root_id', '=', $request->token['root_org']]])->find();
  163. $opName = Employee::where(['id' => $request->token['employee_id']])->value('name');
  164. if ($request->param('action') == 'setleader') {
  165. $is_manager = 1;
  166. //$grant_id = 1;
  167. $role = '领导';
  168. $msg = '设置领导成功';
  169. $orgName = Org::where('id', $employee->org_id)->value('name');
  170. $eventMsg = '您被' . $opName . '添加为' . $orgName . '部负责人身份,请知晓!';
  171. $type = 'setleader';
  172. } else {
  173. $is_manager = 0;
  174. //$grant_id = 2;
  175. $role = '员工';
  176. $msg = '取消领导成功';
  177. $eventMsg = '您的负责人身份被' . $opName . '移除了,请知晓!';
  178. $type = 'removeleader';
  179. }
  180. //只能变更自己部门之下的部门员工职位
  181. // if(request()->token['employee_id'] == $empid){
  182. // return json(['code' => 1, 'msg' => '不能变更自己的职位']);
  183. // }elseif (request()->token['org_id']==$employee['org_id']) {//同级别,同部门,管理层之间不可以进行职位调整
  184. // if($employee['is_manager']==1) return json(['code' => 1, 'msg' => '同部门同级别管理层之间不可以进行职位调整']);
  185. // }else{
  186. // $path = Org::where([['id','=',request()->token['org_id']]])->value('path') ?: 'abc';
  187. // $org_ids = Org::where([['path','like',$path.'%'],['path','<>',$path]])->column('id') ?: ['abc'];
  188. // $eid = Employee::where([['id','=',$empid],['org_id','in',$org_ids]])->find();
  189. // if (!$eid) return json(['code' => 1, 'msg' => '只能变更自己部门级别之下的部门员工职位']);
  190. // }
  191. ///
  192. $employee->save(['is_manager' => $is_manager, 'role' => $role]);
  193. event(new Msg($empid, $eventMsg, $type));
  194. return json(['code' => 0, 'msg' => $msg]);
  195. }
  196. public function apiChangeDepartment()
  197. {
  198. $request = request();
  199. $params = $request->param();
  200. if (!isset($params['empid'])) return false;
  201. if (!isset($params['orgid'])) return false;
  202. //只能变更自己部门之下的部门员工职位
  203. // if(request()->token['employee_id'] == $params['empid']){
  204. // return json(['code' => 1, 'msg' => '不能变更自己的部门']);
  205. // }else{
  206. // $path = Org::where([['id','=',request()->token['org_id']]])->value('path') ?: 'abc';
  207. // $org_ids = Org::where([['path','like',$path.'%'],['path','<>',$path]])->column('id') ?: ['abc'];
  208. // if (!in_array($params['orgid'],$org_ids)) {
  209. // return json(['code' => 1, 'msg' => '只能变更到自己部门级别之下的部门']);
  210. // }
  211. // $eid = Employee::where([['id','=',$params['orgid']],['org_id','in',$org_ids]])->find();
  212. // if (!$eid) {
  213. // return json(['code' => 1, 'msg' => '只能变更自己部门级别之下的部门员工']);
  214. // }
  215. // }
  216. $newdata = [];
  217. $newdata['org_id'] = $params['orgid'];
  218. if (isset($params['therole'])) {
  219. $newdata['role'] = $params['therole'];
  220. }
  221. if (isset($params['is_manager'])) {
  222. $newdata['is_manager'] = ($params['is_manager'] == 'true') ? 1 : 0;
  223. }
  224. if (Employee::where('id', $params['empid'])->update($newdata)) {
  225. if (request()->token['employee_id'] == $params['empid']) {
  226. // 获取用户组织
  227. $org = [$newdata['org_id']];
  228. if ($params['is_manager']) {
  229. $org = (new Org())->getChildOrg($newdata['org_id']);
  230. cache('org_manager_' . $newdata['org_id'], $org);
  231. }
  232. // 设置请求信息
  233. $request->employee = Employee::where('id', $params['empid'])->find();
  234. session('employee', $request->employee);
  235. $request->org = $org; // 获取用户组织
  236. $org = [$newdata['org_id']];
  237. if ($params['is_manager']) {
  238. $org = (new Org())->getChildOrg($newdata['org_id']);
  239. cache('org_manager_' . $newdata['org_id'], $org);
  240. }
  241. // 设置请求信息
  242. $request->employee = Employee::where('id', $params['empid'])->find();
  243. session('employee', $request->employee);
  244. $request->org = $org;
  245. }
  246. return json(['code' => 0, 'msg' => '部门调整成功']);
  247. } else {
  248. return json(['code' => 1, 'msg' => '部门调整失败']);
  249. }
  250. }
  251. public function apiQuitHandling()
  252. {
  253. $request = request();
  254. $params = $request->param();
  255. if (!isset($params['empid'])) return false;
  256. if (!isset($params['target_employee_id']) || $params['target_employee_id'] == 0) return json(['code' => 1, 'msg' => '必须选择交接人']);
  257. if (!Employee::where('id', $params['empid'])->update(['state' => '离职', 'left_to_empid' => $params['target_employee_id'], 'is_manager' => 0, 'verified' => 0])) {
  258. return json(['code' => 1, 'msg' => '人员不存在,或离职状态未更改']);
  259. }
  260. //
  261. //$targetArr = EmployeeModel::find($params['target_employee_id'])->toArray();
  262. //handle customers
  263. $old_data = Customer::where(['employee_id' => $params['empid']])->select();
  264. foreach ($old_data as $item) {
  265. $ori_info_arr = [
  266. //'uid' => $item->uid,
  267. 'employee_id' => $item->employee_id,
  268. ];
  269. Customer::where(['id' => $item->id])->update([
  270. 'employee_id' => $params['target_employee_id'],
  271. //'uid' => (isset($targetArr['uid'])) ? $targetArr['uid'] : NULL,
  272. 'changed_ori_info' => json($ori_info_arr)
  273. ]);
  274. }
  275. //handle customer clue
  276. $old_clue_data = CustomerClue::where(['employee_id' => $params['empid']])->select();
  277. foreach ($old_clue_data as $item) {
  278. $ori_info_arr = [
  279. 'employee_id' => $item->employee_id
  280. ];
  281. CustomerClue::where(['id' => $item->id])->update([
  282. 'employee_id' => $params['target_employee_id'],
  283. 'changed_ori_info' => json($ori_info_arr)
  284. ]);
  285. }
  286. return json(['code' => 0, 'msg' => '处理离职完成']);
  287. }
  288. public function targetemplist()
  289. {
  290. $employeeid = request('empid');
  291. $target_employee_list = EmployeeLogic::getEmpInSameNode($employeeid);
  292. return json(['code' => 0, 'data' => $target_employee_list, 'msg' => '目标人员列表']);
  293. }
  294. public function orgstruc()
  295. {
  296. $datastr = '';
  297. $empid = request()->token['employee_id'];
  298. $employeeAdmin = Employee::find($empid);
  299. //if ($employeeAdmin && $employeeAdmin->is_manager == 1 && $employeeAdmin->grant_id == 1) {
  300. // $orgids = orgSubIds($employeeAdmin->org_id);
  301. // $datastr = OrgLogic::mystruc($orgids);
  302. //}
  303. $orgids = orgSubIds($employeeAdmin->org_id);
  304. $datastr = OrgLogic::mystruc($orgids);
  305. return json(['code' => 0, 'data' => $datastr, 'msg' => '所辖部门获取成功']);
  306. }
  307. /**
  308. * 员工五维画像
  309. */
  310. public function employee_icon()
  311. {
  312. $employee_id = input('employee_id', '', 'intval');
  313. $employee = Employee::with(['org', 'companys', 'user'])->field(['id', 'phone', 'name', 'org_id', 'root_id', 'uid'])->find($employee_id);
  314. $root_id = request()->token['root_org'];
  315. $last_month_start = date('Y-m-d', strtotime('first day of last month'));
  316. $this_month_start = date('Y-m-01', time());
  317. //$all_c_where[] = ['employee_id', '=', $employee_id];
  318. $org_ids = orgSubIds($root_id);
  319. $all_c_where[] = ['org_id', 'in', $org_ids];
  320. $all_c_where[] = ['employee_id', '>', 0];
  321. $all_c_where[] = ['is_resource', '=', 0];
  322. $all_c_where[] = ['addtime', '<', $this_month_start];
  323. $all_customer_count = Customer::where($all_c_where)->count(); //本月之前所有客户数
  324. $state_daodian = CustomerVisitLog::changeState('已到店', 'chaos');
  325. $state_liangfang = CustomerVisitLog::changeState('已量房', 'chaos');
  326. $state_daochang = CustomerVisitLog::changeState('已到场', 'chaos');
  327. $state_merge = array_merge($state_daodian, $state_liangfang, $state_daochang);
  328. $visit_c_where[] = ['employee_id', '=', $employee_id];
  329. $visit_c_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]];
  330. $visit_c_where[] = ['state', 'in', $state_merge];
  331. $visit_customer_count = CustomerVisitLog::where($visit_c_where)->group('customer_id')->count(); // 上月见面客户数
  332. //见面指数
  333. $visit_percent = $all_customer_count > 0 ? round($visit_customer_count / $all_customer_count * 100) : 0;
  334. // 上月收定数
  335. $state_deposit = CustomerVisitLog::changeState('已交定', 'chaos');
  336. $deposit_c_where[] = ['employee_id', '=', $employee_id];
  337. $deposit_c_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]];
  338. $deposit_c_where[] = ['state', 'in', $state_deposit];
  339. $deposit_customer_count = CustomerVisitLog::where($deposit_c_where)->group('customer_id')->count();
  340. // 收定指数
  341. if ($visit_customer_count < $deposit_customer_count) {
  342. $visit_customer_count = $deposit_customer_count;
  343. }
  344. $deposit_percent = $visit_customer_count > 0 ? round($deposit_customer_count / $visit_customer_count * 100) : 0;
  345. //上月签单
  346. $state_sign = CustomerVisitLog::changeState('已签单', 'chaos');
  347. $sign_c_where[] = ['employee_id', '=', $employee_id];
  348. $sign_c_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]];
  349. $sign_c_where[] = ['state', 'in', $state_sign];
  350. $sign_customer_count = CustomerVisitLog::where($sign_c_where)->group('customer_id')->count();
  351. // 签单指数
  352. if ($deposit_customer_count < $sign_customer_count) {
  353. $deposit_customer_count = $sign_customer_count;
  354. }
  355. $sign_percent = $deposit_customer_count > 0 ? round($sign_customer_count / $deposit_customer_count * 100) : 0;
  356. // 学习指数
  357. $all_class_where[] = ['employee_id', '=', $employee_id];
  358. $all_class_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]];
  359. $all_class_list = TrainClassAssign::where($all_class_where)->select();
  360. $all_class_ids = $all_class_list->column('class_id');
  361. $all_class_count = $all_class_list->count();
  362. $study_class_where[] = ['employee_id', '=', $employee_id];
  363. $study_class_where[] = ['root_id', '=', $root_id];
  364. $study_class_where[] = ['class_id', 'in', $all_class_ids];
  365. $study_class_where[] = ['from', '=', 0];
  366. $study_class_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]];
  367. $study_class_count = TrainDoneLog::where($study_class_where)->count();
  368. $study_percent = $all_class_count > 0 ? round($study_class_count / $all_class_count * 100) : 0;
  369. // 员工信息
  370. $return_data['employee'] = !empty($employee) ? $employee->toArray() : [];
  371. // 见面指数
  372. $return_data['all_customer_count'] = $all_customer_count;
  373. $return_data['visit_customer_count'] = $visit_customer_count;
  374. $return_data['visit_percent'] = $visit_percent;
  375. // 收定指数
  376. $return_data['deposit_customer_count'] = $deposit_customer_count;
  377. $return_data['deposit_percent'] = $deposit_percent;
  378. // 签单指数
  379. $return_data['sign_customer_count'] = $sign_customer_count;
  380. $return_data['sign_percent'] = $sign_percent;
  381. // 学习指数
  382. $return_data['all_class_count'] = $all_class_count;
  383. $return_data['study_class_count'] = $study_class_count;
  384. $return_data['study_percent'] = $study_percent;
  385. // 贡献值
  386. $credits_where[] = ['root_id', '=', $root_id];
  387. $credits_where[] = ['employee_id', '=', $employee_id];
  388. $credits_where[] = ['type', '=', 1];
  389. $credits_where[] = ['del', '=', 0];
  390. $credits_all = Credits::where($credits_where)->sum('credits');
  391. $return_data['credits_all'] = $credits_all;
  392. //贡献值排名
  393. $credits_ranking_where[] = ['root_id', '=', $root_id];
  394. $credits_ranking_where[] = ['type', '=', 1];
  395. $credits_ranking_where[] = ['del', '=', 0];
  396. $credits_ranking = Credits::where($credits_ranking_where)->group('employee_id')->order('sum desc')->column('sum(credits) as sum');
  397. $return_data['credits_ranking'] = 0;
  398. if ($credits_ranking && $credits_all) {
  399. $key = array_search($credits_all, $credits_ranking);
  400. $return_data['credits_ranking'] = $key === false ? 0 : $key + 1;
  401. }
  402. // 贡献指数
  403. $credits_percent = 0;
  404. $return_data['credits_top'] = 0;
  405. if (!empty($credits_ranking[0])) {
  406. $return_data['credits_top'] = $credits_ranking[0];
  407. $credits_percent = $credits_ranking[0] > 0 ? round($credits_all / $credits_ranking[0] * 100) : 0;
  408. }
  409. $return_data['credits_percent'] = $credits_percent;
  410. //执行指数
  411. // 任务
  412. $employee_org_id = $employee['org_id'];
  413. $task_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]];
  414. $task_where[] = ['org_ids', '=', $employee_org_id];
  415. $task_where[] = ['root_id', '=', $root_id];
  416. $task_where[] = ['del', '=', 0];
  417. $task_all_list = DailyTasks::where($task_where)->select();
  418. $task_all_count = $task_all_list->count();
  419. // 已完成任务
  420. $task_done_where[] = ['addtime', 'between', [$last_month_start, $this_month_start]];
  421. $task_done_where[] = ['org_ids', '=', $employee_org_id];
  422. $task_done_where[] = ['root_id', '=', $root_id];
  423. $task_done_where[] = ['del', '=', 0];
  424. $task_done_list = DailyTasks::where($task_done_where)->whereRaw("FIND_IN_SET(" . $employee_id . " , employee_ids)")->select();
  425. $task_done_count = $task_done_list->count();
  426. $performance_done_count = 0;
  427. $org_id = $employee['org_id'];
  428. $w[] = ['start_date', 'between', [$last_month_start, $this_month_start]];
  429. $w[] = ['root_id', '=', $root_id];
  430. $root_performance_ids = PreformanceTasksModel::where($w)->column('id');
  431. $performance_all_count = PreformanceTasksOrg::where([['org_id', '=', $org_id], ['performance_tasks_id', 'in', $root_performance_ids]])->count();
  432. $w[] = ['customer_visit_log_id', '<>', ''];
  433. $w[] = ['customer_visit_log_id', 'not null', ''];
  434. $tasks = PreformanceTasksModel::with(['preformanceTasksOrgOne' => function ($query) use ($org_id) {
  435. $query->where('org_id', $org_id)->visible(['org_id', 'performance_tasks_id', 'ok_deposit', 'ok_money'])->where('del', 0);
  436. }])->where($w)->field('*')->select();
  437. foreach ($tasks as $k => $v) {
  438. if ($v->preformanceTasksOrgOne) {
  439. $vids = explode(',', $v->customer_visit_log_id);
  440. $count = CustomerVisitLog::where([['id', 'in', $vids], ['employee_id', '=', $employee_id]])->count();
  441. if ($count == 0) continue;
  442. $ok = 0;
  443. //部门人数
  444. $employee_count = Employee::where([['org_id', '=', $org_id], ['state', '=', '在职'], ['uid', '>', 0]])->count();
  445. //完成指标
  446. $deposit = $employee_count > 0 ? floor($v->preformanceTasksOrgOne->deposit / $employee_count) : 0;
  447. $money = $employee_count > 0 ? floor($v->preformanceTasksOrgOne->money / $employee_count) : 0;
  448. if ($v->preformanceTasksOrgOne->is_deposit == 1) {
  449. $w1[] = ['id', 'in', $vids];
  450. $w1[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
  451. $w1[] = ['employee_id', '=', $employee_id];
  452. $my_deposit = CustomerVisitLog::where($w1)->count();
  453. unset($w1);
  454. if ($my_deposit >= $deposit) $ok += 1;
  455. } else {
  456. $ok += 1;
  457. }
  458. if ($v->preformanceTasksOrgOne->is_money == 1) {
  459. $w1[] = ['id', 'in', $vids];
  460. $w1[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
  461. $w1[] = ['employee_id', '=', $employee_id];
  462. $my_money = CustomerVisitLog::where($w1)->sum('money');
  463. unset($w1);
  464. if ($my_money >= $money) $ok += 1;
  465. } else {
  466. $ok += 1;
  467. }
  468. if ($ok == 2) $performance_done_count += 1;
  469. }
  470. }
  471. $return_data['task_done_count'] = $task_done_count + $performance_done_count;
  472. $return_data['task_all_count'] = $task_all_count + $performance_all_count;
  473. $return_data['task_percent'] = $return_data['task_all_count'] > 0 ? round($return_data['task_done_count'] / $return_data['task_all_count'] * 100) : 0;
  474. return json(['code' => self::success, 'data' => $return_data]);
  475. }
  476. /**
  477. * 五维画像-量化盘点
  478. */
  479. public function employee_icon_data()
  480. {
  481. $date = input('month', date('Y-m'), 'trim');
  482. $month = $date . '-01';
  483. $next_month = date('Y-m-d', strtotime("$month next month"));
  484. $employee_id = input('employee_id', '', 'intval');
  485. $root_id = request()->token['root_org'];
  486. $this_month_start = date('Y-m-01');
  487. $next_month_start = date('Y-m-d', strtotime($this_month_start . ' next month'));
  488. $state_daodian = CustomerVisitLog::changeState('已到店', 'chaos');
  489. $state_daiqueren = CustomerVisitLog::changeState('待确认', 'chaos');
  490. $state_daochang = CustomerVisitLog::changeState('已到场', 'chaos');
  491. $state_liangfang = CustomerVisitLog::changeState('已量房', 'chaos');
  492. $state_deposit = CustomerVisitLog::changeState('已交定', 'chaos');
  493. $state_sign = CustomerVisitLog::changeState('已签单', 'chaos');
  494. // 本月待确认
  495. $this_month_customer_daiqueren_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]];
  496. $this_month_customer_daiqueren_where[] = ['employee_id', '=', $employee_id];
  497. $this_month_customer_daiqueren_where[] = ['state', 'in', $state_daiqueren];
  498. $this_month_customer_daiqueren_count = CustomerVisitLog::where($this_month_customer_daiqueren_where)->count();
  499. // 本月到店量
  500. $this_month_customer_daodian_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]];
  501. $this_month_customer_daodian_where[] = ['employee_id', '=', $employee_id];
  502. $this_month_customer_daodian_where[] = ['state', 'in', $state_daodian];
  503. $this_month_customer_daodian_count = CustomerVisitLog::where($this_month_customer_daodian_where)->count();
  504. // 本月到场量
  505. $this_month_customer_daochang_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]];
  506. $this_month_customer_daochang_where[] = ['employee_id', '=', $employee_id];
  507. $this_month_customer_daochang_where[] = ['state', 'in', $state_daochang];
  508. $this_month_customer_daochang_count = CustomerVisitLog::where($this_month_customer_daochang_where)->count();
  509. // 本月量房量
  510. $this_month_customer_liangfang_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]];
  511. $this_month_customer_liangfang_where[] = ['employee_id', '=', $employee_id];
  512. $this_month_customer_liangfang_where[] = ['state', 'in', $state_liangfang];
  513. $this_month_customer_liangfang_count = CustomerVisitLog::where($this_month_customer_liangfang_where)->count();
  514. // 本月交定量
  515. $this_month_customer_deposit_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]];
  516. $this_month_customer_deposit_where[] = ['employee_id', '=', $employee_id];
  517. $this_month_customer_deposit_where[] = ['state', 'in', $state_deposit];
  518. $this_month_customer_deposit_count = CustomerVisitLog::where($this_month_customer_deposit_where)->count();
  519. // 本月签单量
  520. $this_month_customer_sign_where[] = ['addtime', 'between', [$this_month_start, $next_month_start]];
  521. $this_month_customer_sign_where[] = ['employee_id', '=', $employee_id];
  522. $this_month_customer_sign_where[] = ['state', 'in', $state_sign];
  523. $this_month_customer_sign_count = CustomerVisitLog::where($this_month_customer_sign_where)->count();
  524. // 客户本月数据
  525. $data['customer']['this_month'] = [
  526. 'daiqueren' => $this_month_customer_daiqueren_count,
  527. 'daodian' => $this_month_customer_daodian_count,
  528. 'daochang' => $this_month_customer_daochang_count,
  529. 'liangfang' => $this_month_customer_liangfang_count,
  530. 'deposit' => $this_month_customer_deposit_count,
  531. 'sign' => $this_month_customer_sign_count,
  532. ];
  533. // 选择月待确认
  534. $that_month_customer_daiqueren_where[] = ['addtime', 'between', [$month, $next_month]];
  535. $that_month_customer_daiqueren_where[] = ['employee_id', '=', $employee_id];
  536. $that_month_customer_daiqueren_where[] = ['state', 'in', $state_daiqueren];
  537. $that_month_customer_daiqueren_count = CustomerVisitLog::where($that_month_customer_daiqueren_where)->count();
  538. // 选择月到店量
  539. $that_month_customer_daodian_where[] = ['addtime', 'between', [$month, $next_month]];
  540. $that_month_customer_daodian_where[] = ['employee_id', '=', $employee_id];
  541. $that_month_customer_daodian_where[] = ['state', 'in', $state_daodian];
  542. $that_month_customer_daodian_count = CustomerVisitLog::where($that_month_customer_daodian_where)->count();
  543. // 选择月到场量
  544. $that_month_customer_daochang_where[] = ['addtime', 'between', [$month, $next_month]];
  545. $that_month_customer_daochang_where[] = ['employee_id', '=', $employee_id];
  546. $that_month_customer_daochang_where[] = ['state', 'in', $state_daochang];
  547. $that_month_customer_daochang_count = CustomerVisitLog::where($that_month_customer_daochang_where)->count();
  548. // 选择月量房量
  549. $that_month_customer_liangfang_where[] = ['addtime', 'between', [$month, $next_month]];
  550. $that_month_customer_liangfang_where[] = ['employee_id', '=', $employee_id];
  551. $that_month_customer_liangfang_where[] = ['state', 'in', $state_liangfang];
  552. $that_month_customer_liangfang_count = CustomerVisitLog::where($that_month_customer_liangfang_where)->count();
  553. // 选择月交定量
  554. $that_month_customer_deposit_where[] = ['addtime', 'between', [$month, $next_month]];
  555. $that_month_customer_deposit_where[] = ['employee_id', '=', $employee_id];
  556. $that_month_customer_deposit_where[] = ['state', 'in', $state_deposit];
  557. $that_month_customer_deposit_count = CustomerVisitLog::where($that_month_customer_deposit_where)->group('customer_id')->count();
  558. // 选择月签单量
  559. $that_month_customer_sign_where[] = ['addtime', 'between', [$month, $next_month]];
  560. $that_month_customer_sign_where[] = ['employee_id', '=', $employee_id];
  561. $that_month_customer_sign_where[] = ['state', 'in', $state_sign];
  562. $that_month_customer_sign_count = CustomerVisitLog::where($that_month_customer_sign_where)->count();
  563. // 客户当月数据
  564. $data['customer']['that_month'] = [
  565. 'daiqueren' => $that_month_customer_daiqueren_count,
  566. 'daodian' => $that_month_customer_daodian_count,
  567. 'daochang' => $that_month_customer_daochang_count,
  568. 'liangfang' => $that_month_customer_liangfang_count,
  569. 'deposit' => $that_month_customer_deposit_count,
  570. 'sign' => $that_month_customer_sign_count,
  571. ];
  572. // 学习情况
  573. $all_class_where[] = ['employee_id', '=', $employee_id];
  574. $all_class_where[] = ['addtime', 'between', [$month, $next_month]];
  575. $all_class_list = TrainClassAssign::where($all_class_where)->select();
  576. $all_class_ids = $all_class_list->column('class_id');
  577. $all_class_count = $all_class_list->count();
  578. $study_class_where[] = ['employee_id', '=', $employee_id];
  579. $study_class_where[] = ['root_id', '=', $root_id];
  580. $study_class_where[] = ['class_id', 'in', $all_class_ids];
  581. $study_class_where[] = ['from', '=', 0];
  582. $study_class_where[] = ['addtime', 'between', [$month, $next_month]];
  583. $study_class_count = TrainDoneLog::where($study_class_where)->count();
  584. $study_percent = $all_class_count > 0 ? round($study_class_count / $all_class_count * 100) : 0;
  585. $data['study']['class'] = [
  586. 'all_count' => $all_class_count,
  587. 'done_count' => $study_class_count,
  588. 'percent' => $study_percent
  589. ];
  590. // 考试情况
  591. $exam_assign_where[] = ['root_id', '=', $root_id];
  592. $exam_assign_where[] = ['starttime', 'between', [$month, $next_month]];
  593. $exam_assign_where[] = ['state', '=', 1];
  594. $exam_assign_list = ExamPaper::where($exam_assign_where)->whereRaw("FIND_IN_SET(" . $employee_id . " , assessment)")->select();
  595. $exam_assign_count = $exam_assign_list->count();
  596. $exam_assign_ids = $exam_assign_list->column('id');
  597. $exam_done_where[] = ['paper_id', 'in', $exam_assign_ids];
  598. $exam_done_where[] = ['employee_id', '=', $employee_id];
  599. $exam_done_where[] = ['addtime', 'between', [$month, $next_month]];
  600. $exam_done_count = ExamEmpResult::where($exam_done_where)->count();
  601. $exam_percent = $exam_assign_count > 0 ? round($exam_done_count / $exam_assign_count * 100) : 0;
  602. $data['study']['exam'] = [
  603. 'all_count' => $exam_assign_count,
  604. 'done_count' => $exam_done_count,
  605. 'percent' => $exam_percent
  606. ];
  607. // 学习总结
  608. $sumup_where[] = ['root_id', '=', $root_id];
  609. $sumup_where[] = ['employee_id', '=', $employee_id];
  610. $sumup_where[] = ['class_id', 'in', $all_class_ids];
  611. $sumup_where[] = ['addtime', 'between', [$month, $next_month]];
  612. $sumup_count = TrainSumup::where($sumup_where)->group('class_id')->count();
  613. $sumup_percent = $all_class_count > 0 ? round($sumup_count / $all_class_count * 100) : 0;
  614. $data['study']['sumup'] = [
  615. 'all_count' => $all_class_count,
  616. 'done_count' => $sumup_count,
  617. 'percent' => $sumup_percent
  618. ];
  619. // 日报
  620. $uid = Employee::where('id', '=', $employee_id)->value('uid');
  621. $work_day_where[] = ['uid', '=', $uid];
  622. $work_day_where[] = ['type', '=', 'day'];
  623. $work_day_where[] = ['addtime', 'between', [$month, $next_month]];
  624. $work_day_count = DailyWork::where($work_day_where)->count();
  625. $work_day_all = date('t', strtotime($month));
  626. $work_day_percent = round($work_day_count / $work_day_all * 100);
  627. $data['work']['day'] = [
  628. 'all_count' => $work_day_all,
  629. 'done_count' => $work_day_count,
  630. 'percent' => $work_day_percent
  631. ];
  632. // 周报
  633. $work_week_where[] = ['uid', '=', $uid];
  634. $work_week_where[] = ['type', '=', 'week'];
  635. $work_week_where[] = ['addtime', 'between', [$month, $next_month]];
  636. $work_week_count = DailyWork::where($work_week_where)->count();
  637. $work_week_percent = $work_week_count > 3 ? 100 : round($work_week_count / 4 * 100);
  638. $data['work']['week'] = [
  639. 'all_count' => 4,
  640. 'done_count' => $work_week_count,
  641. 'percent' => $work_week_percent
  642. ];
  643. // 月报
  644. $work_month_where[] = ['uid', '=', $uid];
  645. $work_month_where[] = ['type', '=', 'month'];
  646. $work_month_where[] = ['addtime', 'between', [$month, $next_month]];
  647. $work_month_count = DailyWork::where($work_month_where)->count();
  648. $work_month_percent = $work_month_count > 0 ? 100 : 0;
  649. $data['work']['month'] = [
  650. 'all_count' => 1,
  651. 'done_count' => $work_month_count,
  652. 'percent' => $work_month_percent
  653. ];
  654. // 任务
  655. $employee_org_id = Employee::where('id', $employee_id)->value('org_id');
  656. $orgParentIds = orgParentIds($employee_org_id);
  657. $task_where[] = ['addtime', 'between', [$month, $next_month]];
  658. $task_where[] = ['org_ids', 'in', $orgParentIds];
  659. $task_where[] = ['root_id', '=', $root_id];
  660. $task_where[] = ['del', '=', 0];
  661. $task_all_list = DailyTasks::where($task_where)->select();
  662. $task_all_count = $task_all_list->count();
  663. // 已完成任务
  664. $task_done_where[] = ['addtime', 'between', [$month, $next_month]];
  665. $task_done_where[] = ['org_ids', 'in', $orgParentIds];
  666. $task_done_where[] = ['root_id', '=', $root_id];
  667. $task_done_where[] = ['del', '=', 0];
  668. $task_done_list = DailyTasks::where($task_done_where)->whereRaw("FIND_IN_SET(" . $employee_id . " , employee_ids)")->select();
  669. $task_done_count = $task_done_list->count();
  670. $task_done_percent = $task_all_count > 0 ? round($task_done_count / $task_all_count * 100) : 0;
  671. // 进行中及逾期任务
  672. $task_overdue_count = 0;
  673. $task_ing_count = 0;
  674. foreach ($task_all_list as $k => $v) {
  675. $employee_ids = array_filter(explode(',', $v['employee_ids']));
  676. if (empty($employee_ids)) {
  677. if ($v['endtime'] < date('Y-m-d', time())) {
  678. //逾期
  679. $task_overdue_count++;
  680. } else {
  681. $task_ing_count++;
  682. }
  683. } else {
  684. if (!in_array($employee_id, $employee_ids)) {
  685. if ($v['endtime'] < date('Y-m-d', time())) {
  686. //逾期
  687. $task_overdue_count++;
  688. } else {
  689. $task_ing_count++;
  690. }
  691. }
  692. }
  693. }
  694. $task_overdue_percent = $task_all_count > 0 ? round($task_overdue_count / $task_all_count * 100) : 0;
  695. $task_ing_percent = $task_all_count > 0 ? round($task_ing_count / $task_all_count * 100) : 0;
  696. $data['task'] = [
  697. 'all_count' => $task_all_count,
  698. 'done_count' => $task_done_count,
  699. 'done_percent' => $task_done_percent,
  700. 'overdue_count' => $task_overdue_count,
  701. 'overdue_percent' => $task_overdue_percent,
  702. 'ing_count' => $task_ing_count,
  703. 'ing_percent' => $task_ing_percent
  704. ];
  705. //执行指数
  706. $performance_where[] = ['start_date', 'between', [$month, $next_month]];
  707. $performance_where[] = ['root_id', '=', $root_id];
  708. $employee = Employee::find($employee_id);
  709. $org_id = $employee['org_id'];
  710. $tasks = PreformanceTasksModel::with(['preformanceTasksOrgOne' => function ($query) use ($org_id) {
  711. $query->where('org_id', $org_id)->visible(['org_id', 'performance_tasks_id,ok_deposit,ok_money'])->where('del', 0);
  712. }])->where($performance_where)->field('*')->select();
  713. $performance_data = [];
  714. foreach ($tasks as $k => $v) {
  715. if ($v->preformanceTasksOrgOne) {
  716. $once_data['title'] = $v['title'];
  717. $once_data['is_deposit'] = 0;
  718. $once_data['my_deposit'] = 0;
  719. $once_data['team_deposit'] = 0;
  720. $once_data['all_deposit'] = 0;
  721. $once_data['my_deposit_percent'] = 0;
  722. $once_data['team_deposit_percent'] = 0;
  723. $once_data['is_money'] = 0;
  724. $once_data['my_money'] = 0;
  725. $once_data['team_money'] = 0;
  726. $once_data['all_money'] = 0;
  727. $once_data['my_money_percent'] = 0;
  728. $once_data['team_money_percent'] = 0;
  729. //部门人数
  730. $employee_list = Employee::where([['org_id', '=', $org_id], ['state', '=', '在职'], ['uid', '>', 0]])->select();
  731. $vids = explode(',', $v->customer_visit_log_id);
  732. //完成指标
  733. if ($v->preformanceTasksOrgOne->is_deposit == 1) {
  734. $once_data['is_deposit'] = 1;
  735. $once_data['all_deposit'] = $v->preformanceTasksOrgOne->deposit;
  736. $w1[] = ['id', 'in', $vids];
  737. $w1[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
  738. $w1[] = ['employee_id', '=', $employee_id];
  739. $my_deposit = CustomerVisitLog::where($w1)->count();
  740. unset($w1);
  741. $once_data['my_deposit'] = $my_deposit;
  742. $once_data['my_deposit_percent'] = $once_data['all_deposit'] > 0 ? round($once_data['my_deposit'] / $once_data['all_deposit'] * 100) : 0;
  743. // 团队指标
  744. $w2[] = ['id', 'in', $vids];
  745. $w2[] = ['state', 'in', CustomerVisitLog::changeState('已交定', 'chaos')];
  746. $w2[] = ['employee_id', 'in', $employee_list->column('id')];
  747. $team_deposit = CustomerVisitLog::where($w2)->count();
  748. unset($w2);
  749. $once_data['team_deposit'] = $team_deposit;
  750. $once_data['team_deposit_percent'] = $once_data['all_deposit'] > 0 ? round($once_data['team_deposit'] / $once_data['all_deposit'] * 100) : 0;
  751. }
  752. if ($v->preformanceTasksOrgOne->is_money == 1) {
  753. $once_data['is_money'] = 1;
  754. $once_data['all_money'] = $v->preformanceTasksOrgOne->money;
  755. $w1[] = ['id', 'in', $vids];
  756. $w1[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
  757. $w1[] = ['employee_id', '=', $employee_id];
  758. $my_money = CustomerVisitLog::where($w1)->sum('money');
  759. unset($w1);
  760. $once_data['my_money'] = $my_money;
  761. $once_data['my_money_percent'] = $once_data['all_money'] > 0 ? round($once_data['my_money'] / $once_data['all_money'] * 100) : 0;
  762. // 团队签单
  763. $w2[] = ['id', 'in', $vids];
  764. $w2[] = ['state', 'in', CustomerVisitLog::changeState('已签单', 'chaos')];
  765. $w2[] = ['employee_id', 'in', $employee_list->column('id')];
  766. $team_money = CustomerVisitLog::where($w2)->sum('money');
  767. unset($w2);
  768. $once_data['team_money'] = $team_money;
  769. $once_data['team_money_percent'] = $once_data['all_money'] > 0 ? round($once_data['team_money'] / $once_data['all_money'] * 100) : 0;
  770. }
  771. $performance_data[] = $once_data;
  772. }
  773. }
  774. $data['performance'] = $performance_data;
  775. return json(['code' => self::success, 'data' => $data]);
  776. }
  777. }