Patrol.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. ->limit(100)
  113. ->select();
  114. $data = [];
  115. foreach ($lists as $k=>$v){
  116. $addrForm = Db::name('patrol_addr_form')
  117. ->alias('a')
  118. ->join('patrol_task_addr b','b.patrol_form_id = a.id')
  119. ->where('b.id',$v['patrol_addr_form_id'])
  120. ->value('a.title');
  121. $mode = Db::name('patrol_mode')
  122. ->where('id',$v['patrol_mode'])
  123. ->value('name');
  124. $user = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  125. $is_normal = '无异常' ;
  126. if($v['is_normal'] == 1){
  127. $is_normal = '有异常' ;
  128. }
  129. $time = date('Y-m-d H:i',strtotime($v['create_time']));
  130. $content = '员工-'.$user.'完成'.$addrForm.$mode.','.$is_normal;
  131. $data[$k][] = $time;
  132. $data[$k][] = $content;
  133. }
  134. HelpHander::success($data);
  135. }
  136. //异常地点数据总览
  137. public function patrolNormalAddr(){
  138. $mode = input('mode');
  139. if(!in_array($mode,[1,2,3,4])){
  140. HelpHander::error('参数错误');
  141. }
  142. $lists = Db::name('patrol_record')
  143. ->where('org_id',$this->orgId)
  144. ->where('is_normal',1)
  145. ->where('patrol_mode',$mode)
  146. ->order('id desc')
  147. ->limit(100)
  148. ->select();
  149. $data = [];
  150. foreach ($lists as $k=>$v){
  151. $addr = Db::name('address')->where('id',$v['patrol_addr_id'])
  152. ->value('title');
  153. $user = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  154. $is_normal = '正常' ;
  155. if($v['is_normal'] == 1){
  156. $is_normal = '异常' ;
  157. }
  158. $data[$k][] = $addr;
  159. $data[$k][] = $user;
  160. $data[$k][] = $v['create_time'];
  161. $data[$k][] = $is_normal;
  162. $data[$k][] = $v['content'];
  163. }
  164. HelpHander::success($data);
  165. }
  166. //获取今天最新一条巡更记录
  167. public function newestRecord(){
  168. $info = Db::name('patrol_record')
  169. ->alias('a')
  170. ->join('patrol_task t','a.patrol_task_id = t.id')
  171. ->join('address b','a.patrol_addr_id = b.id')
  172. ->join('user c','a.user_id = c.id')
  173. ->where('a.create_yyyymmdd',date('Ymd'))
  174. ->where('a.org_id',$this->orgId)
  175. ->field('a.images,a.create_time,b.title as address,c.real_name as name,t.title as content')
  176. ->order('a.id desc')
  177. ->find();
  178. $img = $info['images']?(explode(',',$info['images']))[0]:'';
  179. if(!$info){
  180. HelpHander::error('错误');
  181. }
  182. $info['img'] = $img;
  183. HelpHander::success($info);
  184. }
  185. // 可视化轨迹展示
  186. public function patrolRecord(){
  187. $lists = Db::name('patrol_record')
  188. ->alias('a')
  189. ->join('patrol_task t','a.patrol_task_id = t.id')
  190. ->join('address b','a.patrol_addr_id = b.id')
  191. ->join('user c','a.user_id = c.id')
  192. // ->where('a.create_yyyymmdd',date('Ymd'))
  193. ->where('a.org_id',$this->orgId)
  194. ->field('a.images,a.create_time,b.title as address,c.real_name as name,t.title as content')
  195. ->order('a.id desc')
  196. ->limit(20)
  197. ->select();
  198. $img = Db::name('patrol_record')
  199. ->alias('a')
  200. ->join('patrol_task t','a.patrol_task_id = t.id')
  201. ->join('address b','a.patrol_addr_id = b.id')
  202. ->field('a.images,b.title as address')
  203. ->where('a.images','<>','')
  204. ->where('a.org_id',$this->orgId)
  205. ->limit(20)
  206. ->order('a.id desc')
  207. ->select();
  208. $imgs = [];
  209. $key = array_rand($img,3);
  210. foreach ($key as $k=>$v){
  211. $images = explode(',',$img[$v]['images']);
  212. $imgs[] = [
  213. 'img'=>$images[0],
  214. 'title' => $img[$v]['address'],
  215. ];
  216. }
  217. $lists = $lists?$lists:[];
  218. // $imgs = [];
  219. $record = [];
  220. foreach ($lists as $k=>$v){
  221. // $img = $v['images']?(explode(',',$v['images']))[0]:'';
  222. // if($img && count($imgs) < 3){
  223. // $imgs[] = [
  224. // 'title' => $v['address'],
  225. // 'img' => $img
  226. // ];
  227. // }
  228. $record[] = [$v['address'],$v['create_time'],$v['name'],$v['content']];
  229. }
  230. HelpHander::success(['imgs' => $imgs,'lists' => $record]);
  231. }
  232. // 巡更任务数据统计 当天及30天异常数量
  233. public function addrRecord(){
  234. //先取30内的巡更地点
  235. $curDay = date('Ymd');
  236. $start = date('Ymd',strtotime(date('Y-m-d')) - 29*24*60*60);
  237. $lists = Db::name('patrol_record')
  238. ->alias('pr')
  239. ->join('address a','a.id = pr.patrol_addr_id')
  240. ->where('pr.org_id',$this->orgId)
  241. ->where('pr.patrol_mode',1)
  242. ->where('pr.create_yyyymmdd','<=',$curDay)
  243. ->where('pr.create_yyyymmdd','>=',$start)
  244. ->field('a.id,a.title,patrol_addr_id')
  245. ->group('pr.patrol_addr_id')
  246. ->select();
  247. //旧代码,查询耗时过长!
  248. // $titles = [];
  249. // $y1 = [];
  250. // $y2 = [];
  251. // foreach ($lists as $k=>$v){
  252. // $titles[] = $v['title'];
  253. // $count1 = Db::name('patrol_record')
  254. // ->where('org_id',$this->orgId)
  255. // ->where('patrol_mode',1)
  256. // ->where('patrol_addr_id',$v['patrol_addr_id'])
  257. // ->where('create_yyyymmdd','=',$curDay)
  258. // ->count();
  259. // $y1[] = $count1;
  260. // $count2 = Db::name('patrol_record')
  261. // ->where('org_id',$this->orgId)
  262. // ->where('patrol_mode',1)
  263. // ->where('is_normal',1)
  264. // ->where('patrol_addr_id',$v['patrol_addr_id'])
  265. // ->where('create_yyyymmdd','<=',$curDay)
  266. // ->where('create_yyyymmdd','>=',$start)
  267. // ->count();
  268. // $y2[] = $count2;
  269. // }
  270. // 获取所有需要的记录,一次性查询
  271. $records = Db::name('patrol_record')
  272. ->where('org_id', $this->orgId)
  273. ->where('patrol_mode', 1)
  274. ->where(function ($query) use ($curDay, $start) {
  275. $query->where('create_yyyymmdd', $curDay)
  276. ->WhereOr(function ($subQuery) use ($start, $curDay) {
  277. $subQuery->where('is_normal', 1)
  278. ->where('create_yyyymmdd', '>=', $start)
  279. ->where('create_yyyymmdd', '<=', $curDay);
  280. });
  281. })
  282. ->select();
  283. $titles = [];
  284. $y1 = [];
  285. $y2 = [];
  286. foreach ($lists as $k => $v) {
  287. $titles[] = $v['title'];
  288. $count1 = 0;
  289. $count2 = 0;
  290. foreach ($records as $record) {
  291. if ($record['patrol_addr_id'] == $v['patrol_addr_id']) {
  292. if ($record['create_yyyymmdd'] == $curDay) {
  293. $count1++;
  294. }
  295. if ($record['is_normal'] == 1 && $record['create_yyyymmdd'] >= $start && $record['create_yyyymmdd'] <= $curDay) {
  296. $count2++;
  297. }
  298. }
  299. }
  300. $y1[] = $count1;
  301. $y2[] = $count2;
  302. }
  303. HelpHander::success(['titles' => $titles,'y1' => $y1,'y2' => $y2]);
  304. }
  305. //巡*图片
  306. public function patrolImg(){
  307. $data = Db::name('patrol_record')
  308. ->field('images')
  309. ->where('images','<>','')
  310. ->where('org_id',$this->orgId)
  311. ->limit(30)
  312. ->order('id desc')
  313. ->select();
  314. foreach ($data as $k=>$v){
  315. $img = explode(',',$v['images']);
  316. if($img){
  317. $data[$k]['images'] = $img[0];
  318. }
  319. }
  320. if(count($data)>3){
  321. $key = array_rand($data,4);
  322. $list[] = $data[$key[0]];
  323. $list[] = $data[$key[1]];
  324. $list[] = $data[$key[2]];
  325. $list[] = $data[$key[3]];
  326. }
  327. $eList = isset($list)?$list:$data;
  328. HelpHander::success($eList);
  329. }
  330. //
  331. public function patrolRecordCount(){
  332. $dlist = [
  333. date('Y-m-d',strtotime('-6 days')),
  334. date('Y-m-d',strtotime('-5 days')),
  335. date('Y-m-d',strtotime('-4 days')),
  336. date('Y-m-d',strtotime('-3 days')),
  337. date('Y-m-d',strtotime('-2 days')),
  338. date('Y-m-d',strtotime('-1 days')),
  339. date('Y-m-d'),
  340. ];
  341. $date = [];
  342. $yCount = [];
  343. $y2Count = [];
  344. $y3Count = [];
  345. $y4Count = [];
  346. foreach ($dlist as $k=>$v){
  347. $tt = date('Ymd',strtotime($v));
  348. $count = Db::name('patrol_record')
  349. ->where('create_yyyymmdd',$tt)
  350. ->where('org_id',$this->orgId)
  351. ->where('patrol_mode',4)
  352. ->count();
  353. $count2 =Db::name('patrol_record')
  354. ->where('create_yyyymmdd',$tt)
  355. ->where('org_id',$this->orgId)
  356. ->where('patrol_mode',2)
  357. ->count();
  358. $count3 =Db::name('patrol_record')
  359. ->where('create_yyyymmdd',$tt)
  360. ->where('patrol_mode',3)
  361. ->where('org_id',$this->orgId)
  362. ->count();
  363. $count4 =Db::name('patrol_record')
  364. ->where('create_yyyymmdd',$tt)
  365. ->where('patrol_mode',1)
  366. ->count();
  367. $date[] = date('m-d',strtotime($v));
  368. $yCount[] = $count;
  369. $y2Count[] = $count2;
  370. $y3Count[] = $count3;
  371. $y4Count[] = $count4;
  372. }
  373. $data = [
  374. 'date'=>$date,
  375. 'count1'=>$yCount,
  376. 'count2'=>$y2Count,
  377. 'count3'=>$y3Count,
  378. 'count4'=>$y4Count,
  379. ];
  380. HelpHander::success($data);
  381. }
  382. }