0
0

Patrol.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. namespace app\api\controller\screen;
  3. use app\hander\HelpHander;
  4. use app\api\controller\screen\Index;
  5. use think\Db;
  6. class Patrol extends Index
  7. {
  8. public function patrolNums(){
  9. $mode = input('mode');
  10. if(!in_array($mode,[1,2,3,4])){
  11. HelpHander::error('参数错误');
  12. }
  13. if($mode == 1){
  14. $c = 3;
  15. }elseif ($mode == 2){
  16. $c = 4;
  17. }elseif ($mode == 3){
  18. $c = 5;
  19. }elseif ($mode == 4){
  20. $c = 6;
  21. }
  22. $addr = Db::name('address')
  23. ->where('del',0)
  24. ->where('enable',1)
  25. ->where('org_id',$this->orgId)
  26. ->where('','exp',Db::raw("FIND_IN_SET($c,types)"))
  27. ->count('id');
  28. $user = Db::name('patrol_task_user')
  29. ->alias('ptu')
  30. ->join('patrol_task pt','pt.id=ptu.patrol_task_id')
  31. ->where('pt.del',0)
  32. ->where('pt.org_id',$this->orgId)
  33. ->where('pt.patrol_mode',$mode)
  34. ->group('ptu.user_id')
  35. ->count();
  36. $task = Db::name('patrol_task')->where('org_id',$this->orgId)->where('del',0)->where('patrol_mode',$mode)->count();
  37. $data = [
  38. 'addrCount'=>$addr,
  39. 'userCount'=>$user,
  40. 'taskCount'=>$task,
  41. ];
  42. HelpHander::success($data);
  43. }
  44. //最新的一条巡更内容
  45. public function patrolPath(){
  46. $mode = input('mode');
  47. if(!in_array($mode,[1,2,3,4])){
  48. HelpHander::error('参数错误');
  49. }
  50. $record = Db::name('patrol_record')
  51. ->alias('pr')
  52. ->field('pr.*')
  53. ->join('patrol_task pt','pt.id=pr.patrol_task_id')
  54. ->where('pr.org_id',$this->orgId)
  55. ->where('pt.patrol_mode',$mode)
  56. ->where('pt.del',0)
  57. ->where('pt.status',1)
  58. ->order('pr.id desc')
  59. ->find();
  60. if(isset($record) && !empty($record)){
  61. $record['addr'] = Db::name('address')->where('id',$record['patrol_addr_id'])->value('title');
  62. $record['user'] = Db::name('user')->where('id',$record['user_id'])->value('real_name');
  63. $record['task'] = Db::name('patrol_task')->where('id',$record['patrol_task_id'])->value('title');
  64. }
  65. $addrList = Db::name('patrol_task_addr')
  66. ->alias('pta')
  67. ->field('a.id,a.title')
  68. ->join('patrol_addr_form paf','paf.id=pta.patrol_form_id')
  69. ->join('address a','a.id=paf.patrol_addr_id')
  70. ->where('pta.patrol_task_id',$record['patrol_task_id'])
  71. ->where('a.del',0)
  72. ->select();
  73. foreach ($addrList as $k=>$v){
  74. $addrList[$k]['nums'] = Db::name('patrol_record')
  75. ->where('org_id',$this->orgId)
  76. ->where('patrol_addr_id',$v['id'])
  77. ->count();
  78. $active = 0;
  79. if($v['id'] == $record['patrol_addr_id']){
  80. $active = 1;
  81. }
  82. $addrList[$k]['active'] = $active;
  83. $times = Db::name('patrol_record')
  84. ->field('create_time')
  85. ->where('org_id',$this->orgId)
  86. ->where('patrol_addr_id',$v['id'])
  87. ->select();
  88. $eDate = '无';
  89. foreach ($times as $kk=>$vv){
  90. $date = isset($times[1]['create_time'])&&!empty($times[1]['create_time'])?$times[1]['create_time']:'';
  91. $eDate = !empty($date)?date('H:i',strtotime($date)):'无';
  92. }
  93. $addrList[$k]['time'] = $eDate;
  94. }
  95. $data = [
  96. 'record' => $record,
  97. 'addrList' => $addrList,
  98. ];
  99. HelpHander::success($data);
  100. }
  101. //实时信息总览
  102. public function patrolRealTime(){
  103. $mode = input('mode');
  104. if(!in_array($mode,[1,2,3,4])){
  105. HelpHander::error('参数错误');
  106. }
  107. $lists = Db::name('patrol_record')
  108. ->field('create_time,user_id,patrol_addr_form_id,patrol_mode,is_normal')
  109. ->where('patrol_mode',$mode)
  110. ->where('org_id',$this->orgId)
  111. ->order('id desc')
  112. ->select();
  113. $data = [];
  114. foreach ($lists as $k=>$v){
  115. $addrForm = Db::name('patrol_addr_form')
  116. ->alias('a')
  117. ->join('patrol_task_addr b','b.patrol_form_id = a.id')
  118. ->where('b.id',$v['patrol_addr_form_id'])
  119. ->value('a.title');
  120. $mode = Db::name('patrol_mode')
  121. ->where('id',$v['patrol_mode'])
  122. ->value('name');
  123. $user = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  124. $is_normal = '无异常' ;
  125. if($v['is_normal'] == 1){
  126. $is_normal = '有异常' ;
  127. }
  128. $time = date('Y-m-d H:i',strtotime($v['create_time']));
  129. $content = '员工-'.$user.'完成'.$addrForm.$mode.','.$is_normal;
  130. $data[$k][] = $time;
  131. $data[$k][] = $content;
  132. }
  133. HelpHander::success($data);
  134. }
  135. //异常地点数据总览
  136. public function patrolNormalAddr(){
  137. $mode = input('mode');
  138. if(!in_array($mode,[1,2,3,4])){
  139. HelpHander::error('参数错误');
  140. }
  141. $lists = Db::name('patrol_record')
  142. ->where('org_id',$this->orgId)
  143. ->where('is_normal',1)
  144. ->where('patrol_mode',$mode)
  145. ->order('id desc')
  146. ->select();
  147. $data = [];
  148. foreach ($lists as $k=>$v){
  149. $addr = Db::name('address')->where('id',$v['patrol_addr_id'])
  150. ->value('title');
  151. $user = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  152. $is_normal = '正常' ;
  153. if($v['is_normal'] == 1){
  154. $is_normal = '异常' ;
  155. }
  156. $data[$k][] = $addr;
  157. $data[$k][] = $user;
  158. $data[$k][] = $v['create_time'];
  159. $data[$k][] = $is_normal;
  160. $data[$k][] = $v['content'];
  161. }
  162. HelpHander::success($data);
  163. }
  164. //获取今天最新一条巡更记录
  165. public function newestRecord(){
  166. $info = Db::name('patrol_record')
  167. ->alias('a')
  168. ->join('patrol_task t','a.patrol_task_id = t.id')
  169. ->join('address b','a.patrol_addr_id = b.id')
  170. ->join('user c','a.user_id = c.id')
  171. ->where('a.create_yyyymmdd',date('Ymd'))
  172. ->where('a.org_id',$this->orgId)
  173. ->field('a.images,a.create_time,b.title as address,c.real_name as name,t.title as content')
  174. ->order('a.id desc')
  175. ->find();
  176. $img = $info['images']?(explode(',',$info['images']))[0]:'';
  177. if(!$info){
  178. HelpHander::error('错误');
  179. }
  180. $info['img'] = $img;
  181. HelpHander::success($info);
  182. }
  183. // 可视化轨迹展示
  184. public function patrolRecord(){
  185. $lists = Db::name('patrol_record')
  186. ->alias('a')
  187. ->join('patrol_task t','a.patrol_task_id = t.id')
  188. ->join('address b','a.patrol_addr_id = b.id')
  189. ->join('user c','a.user_id = c.id')
  190. // ->where('a.create_yyyymmdd',date('Ymd'))
  191. ->where('a.org_id',$this->orgId)
  192. ->field('a.images,a.create_time,b.title as address,c.real_name as name,a.content')
  193. ->order('a.id desc')
  194. ->limit(20)
  195. ->select();
  196. $lists = $lists?$lists:[];
  197. $imgs = [];
  198. $record = [];
  199. foreach ($lists as $k=>$v){
  200. $img = $v['images']?(explode(',',$v['images']))[0]:'';
  201. if($img && count($imgs) < 3){
  202. $imgs[] = [
  203. 'title' => $v['address'],
  204. 'img' => $img
  205. ];
  206. }
  207. $record[] = [$v['address'],$v['create_time'],$v['name'],$v['content']];
  208. }
  209. HelpHander::success(['imgs' => $imgs,'lists' => $record]);
  210. }
  211. // 巡更任务数据统计 当天及30天异常数量
  212. public function addrRecord(){
  213. //先取30内的巡更地点
  214. $curDay = date('Ymd');
  215. $start = date('Ymd',strtotime(date('Y-m-d')) - 29*24*60*60);
  216. $lists = Db::name('patrol_record')
  217. ->alias('pr')
  218. ->join('address a','a.id = pr.patrol_addr_id')
  219. ->where('pr.org_id',$this->orgId)
  220. ->where('pr.patrol_mode',1)
  221. ->where('pr.create_yyyymmdd','<=',$curDay)
  222. ->where('pr.create_yyyymmdd','>=',$start)
  223. ->field('a.id,a.title')
  224. ->group('pr.patrol_addr_id')
  225. ->distinct(true)
  226. ->select();
  227. $titles = [];
  228. $y1 = [];
  229. $y2 = [];
  230. foreach ($lists as $k=>$v){
  231. $titles[] = $v['title'];
  232. $count1 = Db::name('patrol_record')
  233. ->where('org_id',$this->orgId)
  234. ->where('patrol_mode',1)
  235. ->where('create_yyyymmdd','=',$curDay)
  236. ->count();
  237. $y1[] = $count1;
  238. $count2 = Db::name('patrol_record')
  239. ->where('org_id',$this->orgId)
  240. ->where('patrol_mode',1)
  241. ->where('is_normal',1)
  242. ->where('create_yyyymmdd','<=',$curDay)
  243. ->where('create_yyyymmdd','>=',$start)
  244. ->count();
  245. $y2[] = $count2;
  246. }
  247. HelpHander::success(['titles' => $titles,'y1' => $y1,'y2' => $y2]);
  248. }
  249. //巡*图片
  250. public function patrolImg(){
  251. $data = Db::name('patrol_record')
  252. ->field('images')
  253. ->where('images','<>','')
  254. ->where('org_id',$this->orgId)
  255. ->limit(30)
  256. ->order('id desc')
  257. ->select();
  258. foreach ($data as $k=>$v){
  259. $img = explode(',',$v['images']);
  260. if($img){
  261. $data[$k]['images'] = $img[0];
  262. }
  263. }
  264. if(count($data)>3){
  265. $key = array_rand($data,4);
  266. $list[] = $data[$key[0]];
  267. $list[] = $data[$key[1]];
  268. $list[] = $data[$key[2]];
  269. $list[] = $data[$key[3]];
  270. }
  271. $eList = isset($list)?$list:$data;
  272. HelpHander::success($eList);
  273. }
  274. //
  275. public function patrolRecordCount(){
  276. $dlist = [
  277. date('Y-m-d',strtotime('-6 days')),
  278. date('Y-m-d',strtotime('-5 days')),
  279. date('Y-m-d',strtotime('-4 days')),
  280. date('Y-m-d',strtotime('-3 days')),
  281. date('Y-m-d',strtotime('-2 days')),
  282. date('Y-m-d',strtotime('-1 days')),
  283. date('Y-m-d'),
  284. ];
  285. $date = [];
  286. $yCount = [];
  287. $y2Count = [];
  288. $y3Count = [];
  289. $y4Count = [];
  290. foreach ($dlist as $k=>$v){
  291. $tt = date('Ymd',strtotime($v));
  292. $count = Db::name('patrol_record')
  293. ->where('create_yyyymmdd',$tt)
  294. ->where('org_id',$this->orgId)
  295. ->where('patrol_mode',4)
  296. ->count();
  297. $count2 =Db::name('patrol_record')
  298. ->where('create_yyyymmdd',$tt)
  299. ->where('org_id',$this->orgId)
  300. ->where('patrol_mode',2)
  301. ->count();
  302. $count3 =Db::name('patrol_record')
  303. ->where('create_yyyymmdd',$tt)
  304. ->where('patrol_mode',3)
  305. ->where('org_id',$this->orgId)
  306. ->count();
  307. $count4 =Db::name('patrol_record')
  308. ->where('create_yyyymmdd',$tt)
  309. ->where('patrol_mode',1)
  310. ->count();
  311. $date[] = date('m-d',strtotime($v));
  312. $yCount[] = $count;
  313. $y2Count[] = $count2;
  314. $y3Count[] = $count3;
  315. $y4Count[] = $count4;
  316. }
  317. $data = [
  318. 'date'=>$date,
  319. 'count1'=>$yCount,
  320. 'count2'=>$y2Count,
  321. 'count3'=>$y3Count,
  322. 'count4'=>$y4Count,
  323. ];
  324. HelpHander::success($data);
  325. }
  326. }