123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352 |
- <?php
- namespace app\api\controller\screen;
- use app\hander\HelpHander;
- use app\api\controller\screen\Index;
- use think\Db;
- class Patrol extends Index
- {
- public function patrolNums(){
- $mode = input('mode');
- if(!in_array($mode,[1,2,3,4])){
- HelpHander::error('参数错误');
- }
- if($mode == 1){
- $c = 3;
- }elseif ($mode == 2){
- $c = 4;
- }elseif ($mode == 3){
- $c = 5;
- }elseif ($mode == 4){
- $c = 6;
- }
- $addr = Db::name('address')
- ->where('del',0)
- ->where('enable',1)
- ->where('org_id',$this->orgId)
- ->where('','exp',Db::raw("FIND_IN_SET($c,types)"))
- ->count('id');
- $user = Db::name('patrol_task_user')
- ->alias('ptu')
- ->join('patrol_task pt','pt.id=ptu.patrol_task_id')
- ->where('pt.del',0)
- ->where('pt.org_id',$this->orgId)
- ->where('pt.patrol_mode',$mode)
- ->group('ptu.user_id')
- ->count();
- $task = Db::name('patrol_task')->where('org_id',$this->orgId)->where('del',0)->where('patrol_mode',$mode)->count();
- $data = [
- 'addrCount'=>$addr,
- 'userCount'=>$user,
- 'taskCount'=>$task,
- ];
- HelpHander::success($data);
- }
- //最新的一条巡更内容
- public function patrolPath(){
- $mode = input('mode');
- if(!in_array($mode,[1,2,3,4])){
- HelpHander::error('参数错误');
- }
- $record = Db::name('patrol_record')
- ->alias('pr')
- ->field('pr.*')
- ->join('patrol_task pt','pt.id=pr.patrol_task_id')
- ->where('pr.org_id',$this->orgId)
- ->where('pt.patrol_mode',$mode)
- ->where('pt.del',0)
- ->where('pt.status',1)
- ->order('pr.id desc')
- ->find();
- if(isset($record) && !empty($record)){
- $record['addr'] = Db::name('address')->where('id',$record['patrol_addr_id'])->value('title');
- $record['user'] = Db::name('user')->where('id',$record['user_id'])->value('real_name');
- $record['task'] = Db::name('patrol_task')->where('id',$record['patrol_task_id'])->value('title');
- }
- $addrList = Db::name('patrol_task_addr')
- ->alias('pta')
- ->field('a.id,a.title')
- ->join('patrol_addr_form paf','paf.id=pta.patrol_form_id')
- ->join('address a','a.id=paf.patrol_addr_id')
- ->where('pta.patrol_task_id',$record['patrol_task_id'])
- ->where('a.del',0)
- ->select();
- foreach ($addrList as $k=>$v){
- $addrList[$k]['nums'] = Db::name('patrol_record')
- ->where('org_id',$this->orgId)
- ->where('patrol_addr_id',$v['id'])
- ->count();
- $active = 0;
- if($v['id'] == $record['patrol_addr_id']){
- $active = 1;
- }
- $addrList[$k]['active'] = $active;
- $times = Db::name('patrol_record')
- ->field('create_time')
- ->where('org_id',$this->orgId)
- ->where('patrol_addr_id',$v['id'])
- ->select();
- $eDate = '无';
- foreach ($times as $kk=>$vv){
- $date = isset($times[1]['create_time'])&&!empty($times[1]['create_time'])?$times[1]['create_time']:'';
- $eDate = !empty($date)?date('H:i',strtotime($date)):'无';
- }
- $addrList[$k]['time'] = $eDate;
- }
- $data = [
- 'record' => $record,
- 'addrList' => $addrList,
- ];
- HelpHander::success($data);
- }
- //实时信息总览
- public function patrolRealTime(){
- $mode = input('mode');
- if(!in_array($mode,[1,2,3,4])){
- HelpHander::error('参数错误');
- }
- $lists = Db::name('patrol_record')
- ->field('create_time,user_id,patrol_addr_form_id,patrol_mode,is_normal')
- ->where('patrol_mode',$mode)
- ->where('org_id',$this->orgId)
- ->order('id desc')
- ->select();
- $data = [];
- foreach ($lists as $k=>$v){
- $addrForm = Db::name('patrol_addr_form')
- ->alias('a')
- ->join('patrol_task_addr b','b.patrol_form_id = a.id')
- ->where('b.id',$v['patrol_addr_form_id'])
- ->value('a.title');
- $mode = Db::name('patrol_mode')
- ->where('id',$v['patrol_mode'])
- ->value('name');
- $user = Db::name('user')->where('id',$v['user_id'])->value('real_name');
- $is_normal = '无异常' ;
- if($v['is_normal'] == 1){
- $is_normal = '有异常' ;
- }
- $time = date('Y-m-d H:i',strtotime($v['create_time']));
- $content = '员工-'.$user.'完成'.$addrForm.$mode.','.$is_normal;
- $data[$k][] = $time;
- $data[$k][] = $content;
- }
- HelpHander::success($data);
- }
- //异常地点数据总览
- public function patrolNormalAddr(){
- $mode = input('mode');
- if(!in_array($mode,[1,2,3,4])){
- HelpHander::error('参数错误');
- }
- $lists = Db::name('patrol_record')
- ->where('org_id',$this->orgId)
- ->where('is_normal',1)
- ->where('patrol_mode',$mode)
- ->order('id desc')
- ->select();
- $data = [];
- foreach ($lists as $k=>$v){
- $addr = Db::name('address')->where('id',$v['patrol_addr_id'])
- ->value('title');
- $user = Db::name('user')->where('id',$v['user_id'])->value('real_name');
- $is_normal = '正常' ;
- if($v['is_normal'] == 1){
- $is_normal = '异常' ;
- }
- $data[$k][] = $addr;
- $data[$k][] = $user;
- $data[$k][] = $v['create_time'];
- $data[$k][] = $is_normal;
- $data[$k][] = $v['content'];
- }
- HelpHander::success($data);
- }
- //获取今天最新一条巡更记录
- public function newestRecord(){
- $info = Db::name('patrol_record')
- ->alias('a')
- ->join('patrol_task t','a.patrol_task_id = t.id')
- ->join('address b','a.patrol_addr_id = b.id')
- ->join('user c','a.user_id = c.id')
- ->where('a.create_yyyymmdd',date('Ymd'))
- ->where('a.org_id',$this->orgId)
- ->field('a.images,a.create_time,b.title as address,c.real_name as name,t.title as content')
- ->order('a.id desc')
- ->find();
- $img = $info['images']?(explode(',',$info['images']))[0]:'';
- if(!$info){
- HelpHander::error('错误');
- }
- $info['img'] = $img;
- HelpHander::success($info);
- }
- // 可视化轨迹展示
- public function patrolRecord(){
- $lists = Db::name('patrol_record')
- ->alias('a')
- ->join('patrol_task t','a.patrol_task_id = t.id')
- ->join('address b','a.patrol_addr_id = b.id')
- ->join('user c','a.user_id = c.id')
- // ->where('a.create_yyyymmdd',date('Ymd'))
- ->where('a.org_id',$this->orgId)
- ->field('a.images,a.create_time,b.title as address,c.real_name as name,a.content')
- ->order('a.id desc')
- ->limit(20)
- ->select();
- $lists = $lists?$lists:[];
- $imgs = [];
- $record = [];
- foreach ($lists as $k=>$v){
- $img = $v['images']?(explode(',',$v['images']))[0]:'';
- if($img && count($imgs) < 3){
- $imgs[] = [
- 'title' => $v['address'],
- 'img' => $img
- ];
- }
- $record[] = [$v['address'],$v['create_time'],$v['name'],$v['content']];
- }
- HelpHander::success(['imgs' => $imgs,'lists' => $record]);
- }
- // 巡更任务数据统计 当天及30天异常数量
- public function addrRecord(){
- //先取30内的巡更地点
- $curDay = date('Ymd');
- $start = date('Ymd',strtotime(date('Y-m-d')) - 29*24*60*60);
- $lists = Db::name('patrol_record')
- ->alias('pr')
- ->join('address a','a.id = pr.patrol_addr_id')
- ->where('pr.org_id',$this->orgId)
- ->where('pr.patrol_mode',1)
- ->where('pr.create_yyyymmdd','<=',$curDay)
- ->where('pr.create_yyyymmdd','>=',$start)
- ->field('a.id,a.title')
- ->group('pr.patrol_addr_id')
- ->distinct(true)
- ->select();
- $titles = [];
- $y1 = [];
- $y2 = [];
- foreach ($lists as $k=>$v){
- $titles[] = $v['title'];
- $count1 = Db::name('patrol_record')
- ->where('org_id',$this->orgId)
- ->where('patrol_mode',1)
- ->where('create_yyyymmdd','=',$curDay)
- ->count();
- $y1[] = $count1;
- $count2 = Db::name('patrol_record')
- ->where('org_id',$this->orgId)
- ->where('patrol_mode',1)
- ->where('is_normal',1)
- ->where('create_yyyymmdd','<=',$curDay)
- ->where('create_yyyymmdd','>=',$start)
- ->count();
- $y2[] = $count2;
- }
- HelpHander::success(['titles' => $titles,'y1' => $y1,'y2' => $y2]);
- }
- //巡*图片
- public function patrolImg(){
- $data = Db::name('patrol_record')
- ->field('images')
- ->where('images','<>','')
- ->where('org_id',$this->orgId)
- ->limit(30)
- ->order('id desc')
- ->select();
- foreach ($data as $k=>$v){
- $img = explode(',',$v['images']);
- if($img){
- $data[$k]['images'] = $img[0];
- }
- }
- if(count($data)>3){
- $key = array_rand($data,4);
- $list[] = $data[$key[0]];
- $list[] = $data[$key[1]];
- $list[] = $data[$key[2]];
- $list[] = $data[$key[3]];
- }
- $eList = isset($list)?$list:$data;
- HelpHander::success($eList);
- }
- //
- public function patrolRecordCount(){
- $dlist = [
- date('Y-m-d',strtotime('-6 days')),
- date('Y-m-d',strtotime('-5 days')),
- date('Y-m-d',strtotime('-4 days')),
- date('Y-m-d',strtotime('-3 days')),
- date('Y-m-d',strtotime('-2 days')),
- date('Y-m-d',strtotime('-1 days')),
- date('Y-m-d'),
- ];
- $date = [];
- $yCount = [];
- $y2Count = [];
- $y3Count = [];
- $y4Count = [];
- foreach ($dlist as $k=>$v){
- $tt = date('Ymd',strtotime($v));
- $count = Db::name('patrol_record')
- ->where('create_yyyymmdd',$tt)
- ->where('org_id',$this->orgId)
- ->where('patrol_mode',4)
- ->count();
- $count2 =Db::name('patrol_record')
- ->where('create_yyyymmdd',$tt)
- ->where('org_id',$this->orgId)
- ->where('patrol_mode',2)
- ->count();
- $count3 =Db::name('patrol_record')
- ->where('create_yyyymmdd',$tt)
- ->where('patrol_mode',3)
- ->where('org_id',$this->orgId)
- ->count();
- $count4 =Db::name('patrol_record')
- ->where('create_yyyymmdd',$tt)
- ->where('patrol_mode',1)
- ->count();
- $date[] = date('m-d',strtotime($v));
- $yCount[] = $count;
- $y2Count[] = $count2;
- $y3Count[] = $count3;
- $y4Count[] = $count4;
- }
- $data = [
- 'date'=>$date,
- 'count1'=>$yCount,
- 'count2'=>$y2Count,
- 'count3'=>$y3Count,
- 'count4'=>$y4Count,
- ];
- HelpHander::success($data);
- }
- }
|