CockpitStatistics.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. use think\Exception;
  5. class CockpitStatistics extends Auth
  6. {
  7. public function index(){
  8. return $this->fetch();
  9. }
  10. public function todoCompletion(){
  11. $mode = input('mode',1);
  12. $month = date('Ym');
  13. //总工单
  14. $totalCount = Db::name('todo')
  15. ->where('del',0)
  16. ->where('org_id',$this->orgId)
  17. ->where('create_yyyymm',$month)
  18. ->where('work_type_mode',$mode)
  19. ->count();
  20. //完成的工单
  21. $count1 = Db::name('todo')
  22. ->where('del',0)
  23. ->where('org_id',$this->orgId)
  24. ->where('create_yyyymm',$month)
  25. ->where('work_type_mode',$mode)
  26. ->where('todo_mode',3)
  27. ->count();
  28. //主动完成工单
  29. $count2 = Db::name('todo')
  30. ->alias('t')
  31. ->join('orders o','o.id=t.order_id')
  32. ->where('o.del',0)
  33. ->where('o.source_type',3)
  34. ->where('o.create_yyyymm',$month)
  35. ->where('o.org_id',$this->orgId)
  36. ->where('t.del',0)
  37. ->where('t.org_id',$this->orgId)
  38. ->where('t.create_yyyymm',$month)
  39. ->where('t.work_type_mode',$mode)
  40. ->where('t.todo_mode',3)
  41. ->count();
  42. //被动完成的工单
  43. $count3 = Db::name('todo')
  44. ->alias('t')
  45. ->join('orders o','o.id=t.order_id')
  46. ->where('o.del',0)
  47. ->whereIn('o.source_type',[1,2])
  48. ->where('o.create_yyyymm',$month)
  49. ->where('o.org_id',$this->orgId)
  50. ->where('t.del',0)
  51. ->where('t.org_id',$this->orgId)
  52. ->where('t.create_yyyymm',$month)
  53. ->where('t.work_type_mode',$mode)
  54. ->where('t.todo_mode',3)
  55. ->count();
  56. $bl1 = $bl3 = $bl5 = 0;
  57. $bl2 = $bl4 = $bl6 = 100;
  58. if($totalCount > 0){
  59. $remain = $totalCount-$count1;
  60. $bl1 = round($count1/$totalCount*100,2);
  61. $bl2 = round($remain/$totalCount*100,2);
  62. $remain2 = $totalCount-$count2;
  63. $bl3 = round($count2/$totalCount*100,2);
  64. $bl4 = round($remain2/$totalCount*100,2);
  65. $remain3 = $totalCount-$count3;
  66. $bl5 = round($count3/$totalCount*100,2);
  67. $bl6 = round($remain3/$totalCount*100,2);
  68. }
  69. $data =[
  70. 'count1'=>['title'=>'总完成率','bl1'=>$bl1,'bl2'=>$bl2],
  71. 'count2'=>['title'=>'主动完成率','bl1'=>$bl3,'bl2'=>$bl4],
  72. 'count3'=>['title'=>'被动完成率','bl1'=>$bl5,'bl2'=>$bl6],
  73. 'title'=>['总完成率','主动完成率','被动完成率'],
  74. ];
  75. $this->success('','',$data);
  76. }
  77. public function todoResponseTime(){
  78. $mode = input('mode',1);
  79. $month = date('Ym');
  80. $lastMonth = date('Ym',strtotime("-1 month"));
  81. $todo = Db::name('todo')
  82. ->field('xy_time')
  83. ->where('del',0)
  84. ->where('org_id',$this->orgId)
  85. ->where('create_yyyymm',$month)
  86. ->where('work_type_mode',$mode)
  87. ->select();
  88. $nums = 0;
  89. foreach ($todo as $k=>$v){
  90. $nums +=$v['xy_time'];
  91. }
  92. $count = round($nums/60/60,1);
  93. $lastTodo = Db::name('todo')
  94. ->field('xy_time')
  95. ->where('del',0)
  96. ->where('org_id',$this->orgId)
  97. ->where('create_yyyymm',$lastMonth)
  98. ->where('work_type_mode',$mode)
  99. ->select();
  100. $nums2 = 0;
  101. foreach ($lastTodo as $k=>$v){
  102. $nums2 +=$v['xy_time'];
  103. }
  104. $count2 = round($nums2/60/60,1);
  105. $data = [
  106. 'count'=>$count,
  107. 'count2'=>$count-$count2,
  108. ];
  109. $this->success('','',$data);
  110. }
  111. public function repairTodo(){
  112. $list = Db::name('order_type')
  113. ->field('id,title as name')
  114. ->where('org_id',$this->orgId)
  115. ->where('parent_id',0)
  116. ->where('del',0)
  117. ->where('enable',1)
  118. ->limit(5)
  119. ->select();
  120. foreach ($list as $k=>$v){
  121. $repair = Db::name('order_type')
  122. ->field('id')
  123. ->where('org_id',$this->orgId)
  124. ->where('parent_id',$v['id'])
  125. ->where('del',0)
  126. ->where('enable',1)
  127. ->select();
  128. $ids = [];
  129. foreach ($repair as $kk=>$vv){
  130. $ids[] = $vv['id'];
  131. }
  132. $todo = Db::name('todo')
  133. ->alias('t')
  134. ->field('t.wc_time')
  135. ->join('order_repair or','or.order_id=t.order_id')
  136. ->whereIn('or.type_id',$ids)
  137. ->where('t.del',0)
  138. ->where('t.org_id',$this->orgId)
  139. ->where('t.create_yyyymm',date('Ym'))
  140. ->select();
  141. $nums = 0;
  142. foreach ($todo as $key=>$val){
  143. $nums +=$val['wc_time'];
  144. }
  145. $list[$k]['value'] = round($nums/60/60,1);
  146. }
  147. $this->success('','',$list);
  148. }
  149. public function cleanTypeTask(){
  150. $list = Db::name('clean_type')
  151. ->field('id,title')
  152. ->where('parent_id',0)
  153. ->where('org_id',$this->orgId)
  154. ->where('del',0)
  155. ->where('enable',1)
  156. ->limit(5)
  157. ->select();
  158. $title = [];
  159. foreach ($list as $k=>$v){
  160. $title[] = $v['title'];
  161. $type = Db::name('clean_type')
  162. ->where('parent_id',$v['id'])
  163. ->where('org_id',$this->orgId)
  164. ->where('del',0)
  165. ->where('enable',1)
  166. ->column('id');
  167. $form = Db::name('clean_form')
  168. ->where('org_id',$this->orgId)
  169. ->where('del',0)
  170. ->where('enable',1)
  171. ->whereIn('type_id',$type)
  172. ->column('id');
  173. // $task = Db::name('clean_task')
  174. // ->alias('ct')
  175. // ->join('clean_task_form ctf','ctf.task_id=ct.id')
  176. // ->whereIn('ctf.form_id',$form)
  177. // ->where('ct.org_id',$this->orgId)
  178. // ->where('ct.del',0)
  179. // ->count();
  180. $taskCount = Db::name('clean_task')
  181. ->alias('ct')
  182. ->join('clean_task_form ctf','ctf.task_id=ct.id')
  183. ->whereIn('ctf.form_id',$form)
  184. ->where('ct.org_id',$this->orgId)
  185. ->where('ct.del',0)
  186. ->whereIn('ct.status',[2,3])
  187. ->count();
  188. $list[$k]['value'] =$taskCount;
  189. }
  190. $data = [
  191. 'list'=>$list,
  192. 'title'=>$title,
  193. ];
  194. $this->success('','',$data);
  195. }
  196. public function mateGoods(){
  197. $where[] = ['create_time','>=',date('Y-m').'-01 00:00:00'];
  198. $where[] = ['create_time','<=',date('Y-m').'-31 00:00:00'];
  199. $list = Db::name('mate_goods')
  200. ->field('id,title')
  201. ->where('org_id',$this->orgId)
  202. ->where('del',0)
  203. ->where('enable',1)
  204. ->limit(6)
  205. ->select();
  206. $title = $count = [];
  207. foreach ($list as $k=>$v){
  208. $title[]= $v['title'];
  209. $nums = Db::name('todo_mate_item')
  210. ->where('items_id',$v['id'])
  211. ->where($where)
  212. ->sum('total');
  213. $count[] = $nums;
  214. }
  215. $data = [
  216. 'list'=>$count,
  217. 'title'=>$title,
  218. ];
  219. $this->success('','',$data);
  220. }
  221. public function orgUser(){
  222. $list = [
  223. ['id'=>3,'name'=>'客户'],
  224. ['id'=>4,'name'=>'综合'],
  225. ['id'=>5,'name'=>'保安'],
  226. ['id'=>6,'name'=>'运送'],
  227. ['id'=>7,'name'=>'维修'],
  228. ['id'=>8,'name'=>'保洁'],
  229. ['id'=>9,'name'=>'调度'],
  230. ['id'=>10,'name'=>'管理层'],
  231. ];
  232. foreach ($list as $k=>$v){
  233. $roles = Db::name('roles')
  234. ->where('org_id',$this->orgId)
  235. ->where('enable',1)
  236. ->where('del',0)
  237. ->where('parent_id',$v['id'])
  238. ->column('id');
  239. $user = Db::name('user')
  240. ->alias('u')
  241. ->join('user_org uo','uo.user_id=u.id')
  242. ->join('user_roles ur','ur.user_id=uo.user_id')
  243. ->whereIn('ur.roles_id',$roles)
  244. ->where('uo.org_id',$this->orgId)
  245. ->where('u.enable',1)
  246. ->where('u.del',0)
  247. ->count();
  248. $list[$k]['value'] = $user;
  249. }
  250. $this->success('','',$list);
  251. }
  252. public function wasteCount(){
  253. $waste = Db::name('waste_record')
  254. ->where('org_id',$this->orgId)
  255. ->where('create_yyyymm',date('Ym'))
  256. ->where('status',2)
  257. ->where('del',0)
  258. ->column('weight');
  259. $nums = 0;
  260. foreach ($waste as $k=>$v){
  261. $nums +=$v;
  262. }
  263. $count = round($nums/1000,2);
  264. $type = 0;
  265. $record = Db::name('waste_record')->where('org_id',$this->orgId)->where('del',0)->find();
  266. if(!$record){
  267. $type = 1; //功能未应用
  268. }
  269. $sdate = date('Y-m-d H:i:s',strtotime("-3 day"));
  270. $edate = date('Y-m-d H:i:s');
  271. $record3 = Db::name('waste_record')
  272. ->where('org_id',$this->orgId)
  273. ->where('del',0)
  274. ->where('create_time','>',$sdate)
  275. ->where('create_time','<',$edate)
  276. ->order('id desc')
  277. ->find();
  278. if($record3){
  279. $type = 3; //绿色
  280. }
  281. $record2 = Db::name('waste_record')
  282. ->where('org_id',$this->orgId)
  283. ->where('del',0)
  284. ->where('create_time','>',$sdate)
  285. ->order('id desc')
  286. ->find();
  287. if(!$record3 && !$record2){
  288. $type = 2; //红色
  289. }
  290. $data = [
  291. 'count' =>$count,
  292. 'type' =>$type,
  293. ];
  294. $this->success('','',$data);
  295. }
  296. public function dailyDataCount(){
  297. $sDate = date('Ymd',strtotime( "-3 month"));
  298. $eDate = date('Ymd');
  299. $count = Db::name('daily_record')
  300. ->where('org_id',$this->orgId)
  301. ->where('create_yyyymmdd','>',$sDate)
  302. ->where('create_yyyymmdd','<=',$eDate)
  303. ->count();
  304. $count2 = Db::name('comment')
  305. ->where('org_id',$this->orgId)
  306. ->where('type',0)
  307. ->where('create_yyyymmdd','>',$sDate)
  308. ->where('create_yyyymmdd','<=',$eDate)
  309. ->count();
  310. $count3 = Db::name('comment')
  311. ->where('org_id',$this->orgId)
  312. ->where('type',0)
  313. ->where('create_yyyymm',date('Ym'))
  314. ->count();
  315. $count4 = Db::name('comment')
  316. ->where('org_id',$this->orgId)
  317. ->where('type',0)
  318. ->where('create_yyyymm',date('Ym'))
  319. ->avg('score');
  320. $bl = '100%';
  321. if($count > 0){
  322. $bl = round($count2/$count*100,1).'%';
  323. }
  324. $data = [
  325. 'bl'=>$bl,
  326. 'count2'=>$count2.'/'.$count,
  327. 'count3'=>$count3,
  328. 'count4'=>round($count4,1),
  329. ];
  330. $this->success('','',$data);
  331. }
  332. }