User.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <?php
  2. namespace app\api\controller\screen;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. class User extends Index
  6. {
  7. // 项目大屏--员工数据分析
  8. public function userCate(){
  9. $lists = model('WorkTypeMode')->getWorkerUserStatistics($this->orgId);
  10. $data = [];
  11. foreach ($lists as $v){
  12. $data[] = [
  13. 'name' => $v['name'],
  14. 'value' => $v['count']
  15. ];
  16. }
  17. HelpHander::success($data);
  18. }
  19. // 项目大屏--员工好评度排名
  20. public function userPraise(){
  21. $allUids = model('WorkTypeMode')->getWorkerUserIds($this->orgId);
  22. $users = [];
  23. if($allUids){
  24. $users = Db::name('user')
  25. ->where('id','in',$allUids)
  26. ->order('score desc,id asc')
  27. ->field('id,real_name')
  28. ->limit(10)
  29. ->select();
  30. $users = $users?$users:[];
  31. }
  32. $data = [];
  33. $host = config("app.app_host");
  34. foreach ($users as $k=>$v){
  35. if($k == 0){
  36. $top = '<img src="'.$host.'/img/org-top1.png" width="35" style="margin-top:5px">';
  37. }else if($k == 1){
  38. $top = '<img src="'.$host.'/img/org-top2.png" width="35" style="margin-top:5px">';
  39. }else if($k == 2){
  40. $top = '<img src="'.$host.'/img/org-top3.png" width="35" style="margin-top:5px">';
  41. }else{
  42. $top = 'TOP'.($k+1);
  43. }
  44. $data[] = [$top,$v['real_name']];
  45. }
  46. HelpHander::success($data);
  47. }
  48. // 项目大屏--员工实时在岗情况
  49. public function userPosition(){
  50. $lists = Db::name('todo')
  51. ->where('todo_mode',3)
  52. ->where('org_id',$this->orgId)
  53. ->where('create_yyyymmdd',date('Ymd'))
  54. ->field('count(*) as count,to_user_id')
  55. ->group('to_user_id')
  56. ->distinct(true)
  57. ->order('count desc,to_user_id asc')
  58. ->select();
  59. $data = [];
  60. foreach ($lists as $k=>$v){
  61. $addr = Db::name('convey_plan_record')
  62. ->alias('cpr')
  63. ->join('address a','a.id = cpr.addr_id')
  64. ->where('user_id',$v['to_user_id'])
  65. ->value('title');
  66. $username = Db::name('user')->where('id',$v['to_user_id'])->value('real_name');
  67. $data[] = [$username?$username:'',$v['count'],$addr?$addr:''];
  68. }
  69. HelpHander::success($data);
  70. }
  71. public function userDep(){
  72. $depList = Db::name('dep')->where('enable',1)->where('org_id',$this->orgId)->where('del',0)->limit(6)->order('id desc')->select();
  73. $data = [];
  74. foreach ($depList as $v){
  75. $data[] = [
  76. 'name' => $v['title'],
  77. 'value' => Db::name('user_dep')
  78. ->alias('ud')
  79. ->join('user u','u.id=ud.user_id')
  80. ->where('ud.dep_id',$v['id'])
  81. ->where('u.del',0)
  82. ->count(),
  83. ];
  84. }
  85. HelpHander::success($data);
  86. }
  87. //新项目大屏---在岗人员情况
  88. public function onGuardUserData(){
  89. $type = input('type','');
  90. $pid = 0;
  91. if($type == 1){
  92. $pid = 7;
  93. }elseif ($type == 2){
  94. $pid = 8;
  95. }elseif ($type == 3){
  96. $pid = 6;
  97. }elseif ($type == 4){
  98. $pid = 5;
  99. }elseif ($type == 5){
  100. $pid = 9;
  101. }elseif ($type == 6){
  102. $pid = 11;
  103. }
  104. $rolesId = Db::name('roles')
  105. ->where('parent_id',$pid)
  106. ->where('del',0)
  107. ->where('type',1)
  108. ->where('enable',1)
  109. ->where('org_id',$this->orgId)
  110. ->column('id');
  111. $userCount = Db::name('user')
  112. ->alias('u')
  113. ->field('u.id,u.real_name,ur.roles_id')
  114. ->join('user_roles ur','ur.user_id=u.id')
  115. ->where('u.del',0)
  116. ->where('u.enable',1)
  117. ->whereIn('ur.roles_id',$rolesId)
  118. ->count();
  119. $rolesUser = Db::name('user')
  120. ->alias('u')
  121. ->field('u.id,u.real_name,ur.roles_id')
  122. ->join('user_roles ur','ur.user_id=u.id')
  123. ->where('u.del',0)
  124. ->where('u.enable',1)
  125. ->where('u.work',1)
  126. ->whereIn('ur.roles_id',$rolesId)
  127. ->select();
  128. foreach ($rolesUser as $k=>$v){
  129. $roles = Db::name('roles')->where('id',$v['roles_id'])->find();
  130. $rolesUser[$k]['roles_name'] = Db::name('roles')->where('id',$roles['parent_id'])->value('name');
  131. $rolesUser[$k]['dep_name'] = Db::name('user_dep')
  132. ->alias('ud')
  133. ->join('dep d','d.id=ud.dep_id')
  134. ->where('ud.user_id',$v['id'])
  135. ->where('d.org_id',$this->orgId)
  136. ->value('d.title');
  137. }
  138. $list = [];
  139. foreach ($rolesUser as $k=>$v){
  140. $list[$k][] = $v['roles_name'];
  141. $list[$k][] = $v['dep_name'];
  142. $list[$k][] = $v['real_name'];
  143. }
  144. $data = [
  145. 'userCount'=>$userCount,
  146. 'rolesCount'=>count($rolesUser),
  147. 'list'=>$list
  148. ];
  149. HelpHander::success($data);
  150. }
  151. //人员在岗数据总览
  152. public function orgUserCount(){
  153. $userCount = Db::name('user')
  154. ->alias('u')
  155. ->join('user_org uo','u.id=uo.user_id')
  156. ->where('uo.org_id',$this->orgId)
  157. ->where('u.type',0)
  158. ->where('enable',1)
  159. ->where('del',0)
  160. ->count();
  161. $userPostCount = Db::name('user')
  162. ->alias('u')
  163. ->join('user_org uo','u.id=uo.user_id')
  164. ->where('uo.org_id',$this->orgId)
  165. ->where('u.work',1)
  166. ->where('u.type',0)
  167. ->where('del',0)
  168. ->where('enable',1)
  169. ->count();
  170. $data = [
  171. ['name'=>'总人数','value'=>$userCount],
  172. ['name'=>'在岗人员','value'=>$userPostCount]
  173. ];
  174. HelpHander::success($data);
  175. }
  176. public function rolesUserData(){
  177. $data = [
  178. ['pid'=>8,'name'=>'保洁','value'=>0],
  179. ['pid'=>7,'name'=>'维修','value'=>0],
  180. ['pid'=>5,'name'=>'保安','value'=>0],
  181. ['pid'=>6,'name'=>'运送','value'=>0],
  182. ['pid'=>11,'name'=>'陪护','value'=>0],
  183. ];
  184. foreach ($data as $k=>$v){
  185. $data[$k]['pid'] = Db::name('roles')
  186. ->where('parent_id',$v['pid'])
  187. ->where('del',0)
  188. ->where('type',1)
  189. ->where('enable',1)
  190. ->where('org_id',$this->orgId)
  191. ->column('id');
  192. }
  193. foreach ($data as $k=>$v){
  194. $data[$k]['value'] = Db::name('user')
  195. ->alias('u')
  196. ->join('user_roles ur','ur.user_id=u.id')
  197. ->whereIn('ur.roles_id',$v['pid'])
  198. ->where('u.del',0)
  199. ->where('u.enable',1)
  200. ->count();
  201. unset($data[$k]['pid']);
  202. }
  203. HelpHander::success($data);
  204. }
  205. public function bjUserCountData(){
  206. //保洁
  207. $bjRolesId = Db::name('roles')
  208. ->where('parent_id',8)
  209. ->where('del',0)
  210. ->where('type',1)
  211. ->where('enable',1)
  212. ->where('org_id',$this->orgId)
  213. ->column('id');
  214. $bjCount = Db::name('user')
  215. ->alias('u')
  216. ->join('user_roles ur','ur.user_id=u.id')
  217. ->whereIn('ur.roles_id',$bjRolesId)
  218. ->where('u.del',0)
  219. ->where('u.enable',1)
  220. ->count();
  221. $bjWorkCount = Db::name('user')
  222. ->alias('u')
  223. ->join('user_roles ur','ur.user_id=u.id')
  224. ->whereIn('ur.roles_id',$bjRolesId)
  225. ->where('u.del',0)
  226. ->where('u.enable',1)
  227. ->where('u.work',1)
  228. ->count();
  229. $bl = $bl2 = 0;
  230. if($bjCount > 0){
  231. $bl = round($bjWorkCount/$bjCount*100,1);
  232. $bl2 = 100 - $bl;
  233. }
  234. $data = [
  235. 'count'=>['bjCount'=>$bjCount,'bjWorkCount'=>$bjWorkCount],
  236. 'count2'=>[['value'=>$bl],['value'=>$bl2]],
  237. 'bl'=>$bl
  238. ];
  239. HelpHander::success($data);
  240. }
  241. public function wxUserCountData(){
  242. //维修
  243. $rolesId = Db::name('roles')
  244. ->where('parent_id',7)
  245. ->where('del',0)
  246. ->where('type',1)
  247. ->where('enable',1)
  248. ->where('org_id',$this->orgId)
  249. ->column('id');
  250. $count = Db::name('user')
  251. ->alias('u')
  252. ->join('user_roles ur','ur.user_id=u.id')
  253. ->whereIn('ur.roles_id',$rolesId)
  254. ->where('u.del',0)
  255. ->where('u.enable',1)
  256. ->count();
  257. $workCount = Db::name('user')
  258. ->alias('u')
  259. ->join('user_roles ur','ur.user_id=u.id')
  260. ->whereIn('ur.roles_id',$rolesId)
  261. ->where('u.del',0)
  262. ->where('u.enable',1)
  263. ->where('u.work',1)
  264. ->count();
  265. $bl = $bl2 = 0;
  266. if($count > 0){
  267. $bl = round($workCount/$count*100,1);
  268. $bl2 = 100 - $bl;
  269. }
  270. $data = [
  271. 'count'=>['count'=>$count,'workCount'=>$workCount],
  272. 'count2'=>[['value'=>$bl],['value'=>$bl2]],
  273. 'bl'=>$bl
  274. ];
  275. HelpHander::success($data);
  276. }
  277. public function baUserCountData(){
  278. //保安
  279. $rolesId = Db::name('roles')
  280. ->where('parent_id',5)
  281. ->where('del',0)
  282. ->where('type',1)
  283. ->where('enable',1)
  284. ->where('org_id',$this->orgId)
  285. ->column('id');
  286. $count = Db::name('user')
  287. ->alias('u')
  288. ->join('user_roles ur','ur.user_id=u.id')
  289. ->whereIn('ur.roles_id',$rolesId)
  290. ->where('u.del',0)
  291. ->where('u.enable',1)
  292. ->count();
  293. $workCount = Db::name('user')
  294. ->alias('u')
  295. ->join('user_roles ur','ur.user_id=u.id')
  296. ->whereIn('ur.roles_id',$rolesId)
  297. ->where('u.del',0)
  298. ->where('u.enable',1)
  299. ->where('u.work',1)
  300. ->count();
  301. $bl = $bl2 = 0;
  302. if($count > 0){
  303. $bl = round($workCount/$count*100,1);
  304. $bl2 = 100 - $bl;
  305. }
  306. $data = [
  307. 'count'=>['count'=>$count,'workCount'=>$workCount],
  308. 'count2'=>[['value'=>$bl],['value'=>$bl2]],
  309. 'bl'=>$bl
  310. ];
  311. HelpHander::success($data);
  312. }
  313. public function phUserCountData(){
  314. //陪护
  315. $rolesId = Db::name('roles')
  316. ->where('parent_id',11)
  317. ->where('del',0)
  318. ->where('type',1)
  319. ->where('enable',1)
  320. ->where('org_id',$this->orgId)
  321. ->column('id');
  322. $count = Db::name('user')
  323. ->alias('u')
  324. ->join('user_roles ur','ur.user_id=u.id')
  325. ->whereIn('ur.roles_id',$rolesId)
  326. ->where('u.del',0)
  327. ->where('u.enable',1)
  328. ->count();
  329. $workCount = Db::name('user')
  330. ->alias('u')
  331. ->join('user_roles ur','ur.user_id=u.id')
  332. ->whereIn('ur.roles_id',$rolesId)
  333. ->where('u.del',0)
  334. ->where('u.enable',1)
  335. ->where('u.work',1)
  336. ->count();
  337. $bl = $bl2 = 0;
  338. if($count > 0){
  339. $bl = round($workCount/$count*100,1);
  340. $bl2 = 100 - $bl;
  341. }
  342. $workUserIds = Db::name('user')
  343. ->alias('u')
  344. ->join('user_roles ur','ur.user_id=u.id')
  345. ->whereIn('ur.roles_id',$rolesId)
  346. ->where('u.del',0)
  347. ->where('u.enable',1)
  348. ->where('u.work',1)
  349. ->column('u.id');
  350. $isWorkCount = Db::name('ph_todo')->whereIn('user_id',$workUserIds)->where('org_id',$this->orgId)->where('status',1)->group('user_id')->count();
  351. $noWorkCount = $workCount - $isWorkCount;
  352. if($noWorkCount < 1 ){
  353. $noWorkCount = 0;
  354. }
  355. $data = [
  356. 'count'=>['count'=>$count,'workCount'=>$workCount,'noWorkCount'=>$noWorkCount],
  357. 'count2'=>[['value'=>$bl],['value'=>$bl2]],
  358. 'bl'=>$bl
  359. ];
  360. HelpHander::success($data);
  361. }
  362. public function ysUserCountData(){
  363. //运送
  364. $rolesId = Db::name('roles')
  365. ->where('parent_id',6)
  366. ->where('del',0)
  367. ->where('type',1)
  368. ->where('enable',1)
  369. ->where('org_id',$this->orgId)
  370. ->column('id');
  371. $count = Db::name('user')
  372. ->alias('u')
  373. ->join('user_roles ur','ur.user_id=u.id')
  374. ->whereIn('ur.roles_id',$rolesId)
  375. ->where('u.del',0)
  376. ->where('u.enable',1)
  377. ->count();
  378. $workCount = Db::name('user')
  379. ->alias('u')
  380. ->join('user_roles ur','ur.user_id=u.id')
  381. ->whereIn('ur.roles_id',$rolesId)
  382. ->where('u.del',0)
  383. ->where('u.enable',1)
  384. ->where('u.work',1)
  385. ->count();
  386. $bl = $bl2 = 0;
  387. if($count > 0){
  388. $bl = round($workCount/$count*100,1);
  389. $bl2 = 100 - $bl;
  390. }
  391. $workUserIds = Db::name('user')
  392. ->alias('u')
  393. ->join('user_roles ur','ur.user_id=u.id')
  394. ->whereIn('ur.roles_id',$rolesId)
  395. ->where('u.del',0)
  396. ->where('u.enable',1)
  397. ->where('u.work',1)
  398. ->column('u.id');
  399. $isWorkCount = Db::name('todo')->whereIn('to_user_id',$workUserIds)->where('org_id',$this->orgId)->where('todo_mode',2)->where('work_type_mode',3)->group('to_user_id')->where('del',0)->count();
  400. $noWorkCount = $workCount - $isWorkCount;
  401. if($noWorkCount < 1 ){
  402. $noWorkCount = 0;
  403. }
  404. $data = [
  405. 'count'=>['count'=>$count,'workCount'=>$workCount,'noWorkCount'=>$noWorkCount],
  406. 'count2'=>[['value'=>$bl],['value'=>$bl2]],
  407. 'bl'=>$bl
  408. ];
  409. HelpHander::success($data);
  410. }
  411. public function userCount(){
  412. $userCount = Db::name('user')
  413. ->alias('u')
  414. ->join('user_org uo','uo.user_id=u.id')
  415. ->join('org o','o.id=uo.org_id')
  416. ->where('u.del',0)
  417. ->where('u.type','in',[0,2])
  418. ->where('o.enable',1)
  419. ->where('o.del',0)
  420. ->where('o.id',$this->orgId)
  421. ->count();
  422. HelpHander::success($userCount);
  423. }
  424. // 项目大屏--满意度排名
  425. public function userCommentScoreList(){
  426. $user = Db::name('comment')
  427. ->field('user_id')
  428. ->where('org_id',$this->orgId)
  429. ->group('user_id')
  430. ->select();
  431. foreach ($user as $k=>$v){
  432. $user[$k]['score'] = Db::name('comment')
  433. ->where('org_id',$this->orgId)
  434. ->where('user_id',$v['user_id'])
  435. ->sum('score');
  436. $user[$k]['name'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  437. $user[$k]['dep'] = Db::name('user_dep')
  438. ->alias('ud')
  439. ->join('dep d','d.id=ud.dep_id')
  440. ->where('ud.user_id',$v['user_id'])
  441. ->value('title');
  442. $rolesId = Db::name('user_roles')
  443. ->alias('ur')
  444. ->join('roles r','r.id=ur.roles_id')
  445. ->where('ur.user_id',$v['user_id'])
  446. ->value('r.parent_id');
  447. $user[$k]['roles'] = $rolesId ? Db::name('roles')->where('id',$rolesId)->value('name'):'';
  448. $user[$k]['task'] = Db::name('comment')
  449. ->where('org_id',$this->orgId)
  450. ->where('user_id',$v['user_id'])
  451. ->count();
  452. }
  453. $list = list_sort_by($user,'score','desc');
  454. $data = [];
  455. $host = config("app.app_host");
  456. foreach ($list as $k=>$v){
  457. if(isset($k) && $k==0){
  458. $top = '<img src="'.$host.'/img/org-top1.png" width="30" style="margin-top:5px" >';
  459. }elseif (isset($k) && $k==1){
  460. $top = '<img src="'.$host.'/img/org-top2.png" width="30" style="margin-top:5px" >';
  461. }elseif (isset($k) && $k==2){
  462. $top = '<img src="'.$host.'/img/org-top3.png" width="30" style="margin-top:5px">';
  463. }else{
  464. $top = 'TOP'.($k+1);
  465. }
  466. $data[] = [$top,$v['name'],$v['dep'],$v['roles'],$v['task'],$v['score']];
  467. }
  468. HelpHander::success($data);
  469. }
  470. }