post(); $validate = new \app\common\validate\QualityTask(); $result = $validate->check($post,[]); if(true !== $result){ $this->error = $validate->getError(); return false; } // $addrs = json_decode($post['addrs'],true); // if(empty($addrs)){ // $this->error='未设置检查内容'; // return false; // } // foreach ($addrs as $k=>$v){ // if($v['addrId'] <= 0){ // $this->error='检查内容设置错误'; // return false; // } // } $addrs = json_decode($post['group'],true); if(empty($addrs)){ $this->error='未配置检查内容'; return false; } foreach ($addrs as $k=>$v){ if(empty($v['forms'])){ $this->error='未选择检查项'; return false; } if($v['type']>0 && empty($v['buss'])){ $this->error='未选择关联项'; return false; } } if(!$post['user_ids']){ $this->error='检查人员不能为空'; return false; } if(strtotime($post['start_time']) < time()){ $this->error = '开始时间不能小于当前时间'; return false; } if($post['start_time'] >= $post['end_time']){ $this->error = '结束时间请大于开始时间'; return false; } if($post['id'] <= 0){ $this->error = '参数错误'; return false; } //编辑操作 $curtime = date('Y-m-d H:i:s'); $data = array( 'title' => $post['title'], 'update_time' => $curtime, 'start_time' => $post['start_time'], 'end_time' => $post['end_time'], 'create_yyyymm' => date('Ym',strtotime($post['start_time'])), 'create_yyyy' => date('Y',strtotime($post['start_time'])), 'create_yyyymmdd' => date('Ymd',strtotime($post['start_time'])), // 'in_order' => $post['in_order'], // 'lines' => $post['in_order'] == 1&&$post['lines']?$post['lines']:'' ); Db::startTrans(); try { //编辑任务 $res = Db::name('quality_task')->where('id',$post['id'])->update($data); if(!$res){ exception('编辑任务失败'); } Db::name('quality_task_addr')->where('quality_task_id',$post['id'])->delete(); Db::name('quality_task_user')->where('quality_task_id',$post['id'])->delete(); // 重新添加地点及表单 $nforms = []; foreach ($addrs as $key=>$val){ if(!empty($val['addrId'])){ $addrId = $val['addrId']; $ud = [ 'title' => $v['title'], 'forms' => !empty($val['forms'])?implode(',',array_column($val['forms'],'id')):'', 'busids' => !empty($val['buss'])?implode(',',array_column($val['buss'],'id')):'', 'type' => $val['type'], ]; Db::name('quality_group') ->where('id',$addrId) ->update($ud); }else{ $addrId = Db::name('quality_group')->insertGetId([ 'title' => $v['title'], 'org_id' => cur_org_id(), 'forms' => !empty($val['forms'])?implode(',',array_column($val['forms'],'id')):'', 'busids' => !empty($val['buss'])?implode(',',array_column($val['buss'],'id')):'', 'type' => $val['type'], 'enable' => 1, 'del' => 0, 'create_time' => getTime() ]); } $nforms[] = [ 'quality_task_id' => $post['id'], 'quality_group_id' => $addrId, //'sorts' => $val['sorts'], ]; } $fromRet = Db::name('quality_task_addr')->insertAll($nforms); if($fromRet != count($nforms)){ \exception('操作失败'); } // 重新添加用户 $uds = []; $users = explode(',',$post['user_ids']); foreach ($users as $key=>$val){ $uds[] = [ 'quality_task_id' => $post['id'], 'user_id' => $val ]; } $userRet = Db::name('quality_task_user')->insertAll($uds); if($userRet != count($uds)){ \exception('操作失败'); } Db::commit(); return true; }catch (\Exception $e){ $this->error = $e->getMessage(); Db::rollback(); return false; } } /** * 格式化时间,获取时间数组 * @param $starttime 开始时间 * @param $endtime 结束时间 * @param $hours 重复小时 * @param $days 执行天数 * @return array */ private function get_time_arr($starttime,$endtime,$hours,$days){ $onehour = 60*60; $oneday = 24*60*60; $start = strtotime($starttime); $end = strtotime($endtime); $ctime = $end - $start; $timearr = array(); $timearr[] = array( 'start_time' => $starttime, 'end_time' => $endtime, ); if($hours > 0){ if($ctime > $hours*$onehour){ $timearr = array(); //置空 $i = $hours; while(true){ $nend = $start + $i*$onehour; $nstart = $start + ($i-$hours)*$onehour; $timearr[] = array( 'start_time' => date('Y-m-d H:i',$nstart), 'end_time' => date('Y-m-d H:i',$nend), ); if($nend >= $end){ break; } $i += $hours; } } } if($days > 0){ $timearray = $timearr; for ($i=1; $i<=$days;$i++){ foreach ($timearray as $k=>$v){ $timearr[] = array( 'start_time' => date('Y-m-d H:i',strtotime($v['start_time']) + $i*$oneday), 'end_time' => date('Y-m-d H:i',strtotime($v['end_time']) + $i*$oneday), ); } } } return $timearr; } //获取巡查计划任务 public function getTaskOne($taskId){ $ret=Db::name('quality_task')->where('id',$taskId)->find(); //获取计划任务检查组id $old_addrs=Db::name('quality_task_addr') ->where('quality_task_id',$ret['id'])->select(); foreach ($old_addrs as $k=>$v){ $ids[$k]=$v['quality_addr_form_id']; } $ret['old_addrs']=$ids; //获取计划任务检查人员 $old_user_ids=Db::name('quality_task_user')->where('quality_task_id',$ret['id'])->column('user_id'); $ret['old_user_ids']=$old_user_ids; return $ret; } //api接口 public function lists($page,$size,$type,$status,$userId,$orgId){ if($type == 1){ // 待完成 $map[] = ['t.user_id','=',$userId]; $map[] = ['t.org_id','=',$orgId]; $map[] = ['pt.del','=',0]; $map[] = ['t.type','=',5]; $lists = Db::name('task') ->alias('t') ->field('pt.id,pt.start_time,pt.end_time,pt.title,pt.status') ->join('quality_task pt','t.bus_id = pt.id') ->where($map) ->whereIn('pt.status',[0,1]) ->order('pt.start_time desc,pt.id desc') ->page($page,$size) ->select(); }else if($type == 2){ // 已完成 $where[] = ['pt.org_id','=',$orgId]; $where[] = ['pt.del','=',0]; $where[] = ['ptu.user_id','=',$userId]; if($status == 1){ $where[] = ['pt.status','=',2]; }else if($status == 2){ $where[] = ['pt.status','=',3]; } $lists = Db::name('quality_task_user') ->alias('ptu') ->field('pt.id,pt.start_time,pt.end_time,pt.title,pt.status,ptu.user_id') ->join('quality_task pt','ptu.quality_task_id=pt.id') ->where($where) ->whereIn('pt.status',[2,3]) ->order('pt.start_time desc,pt.id desc') ->page($page,$size) ->select(); } $lists = $lists?$lists:[]; foreach ($lists as $k=>$v){ $lists[$k]['start_time'] = date('Y-m-d H:i',strtotime($v['start_time'])); $lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time'])); } return $lists; } public function info($id,$userId,$orgId){ $info = Db::name('quality_task') ->where('del',0) ->where('id',$id) ->where('org_id',$orgId) ->field('id,title,status,start_time,end_time') ->find(); if(!$info){ HelpHander::error('记录不存在'); } $users = Db::name('quality_task_user') ->alias('a') ->join('user b','a.user_id = b.id') ->where('a.quality_task_id',$id) ->column('b.real_name'); $info['users'] = $users?implode(',',$users):''; $info['start_time'] = date('Y-m-d H:i',strtotime($info['start_time'])); $info['end_time'] = date('Y-m-d H:i',strtotime($info['end_time'])); $group = Db::name('quality_task_addr') ->alias('a') ->join('quality_group b','a.quality_group_id = b.id') ->where('a.quality_task_id',$id) ->field('a.id,b.title,b.type,b.forms,b.busids') ->select(); $group = $group?$group:[]; foreach ($group as $k=>$v){ $forms = Db::name('quality_cate') ->where('id','in',$v['forms']) ->field('id,title') ->order('id asc') ->select(); foreach ($forms as $kk=>$vv){ $forms[$kk]['type'] = 0; } $group[$k]['forms'] = $forms?$forms:[]; if($v['type'] > 0){ $busids = explode(',',$v['busids']); $bus = (new \app\common\model\QualityGroup())->getListByType($v['type'],$orgId); $nbus = []; foreach ($bus as $kk=>$vv){ if(in_array($vv['id'],$busids)){ $nbus[] = $vv; } } if($nbus){ $group[$k]['buss'] = $nbus; } } unset($group[$k]['busids']); $group[$k]['status'] = 0; $record = Db::name('quality_record') ->where('quality_addr_id',$v['id']) ->field('id') ->find(); if($record){ $group[$k]['status'] = 1; } } $info['group'] = $group; return $info; } public function taskCount($userId,$orgId,$patrol_mode){ $map[] = ['t.user_id','=',$userId]; $map[] = ['t.org_id','=',$orgId]; $map[] = ['pt.del','=',0]; $map[] = ['t.type','=',2]; // $map[] = ['pt.patrol_mode','=',$patrol_mode]; $ret= Db::name('task') ->alias('t') ->field('pt.id,pt.start_time,pt.end_time,pt.title,pt.status,pt.is_exception') ->join('quality_task pt','t.bus_id = pt.id') ->where($map) ->whereIn('pt.status',[0,1]) ->count(); return $ret; } public function addrList($taskId,$orgId){ $ret['user_list'] = Db::name('quality_task_user') ->alias('ptu') ->field('u.real_name') ->join('quality_task pt','ptu.quality_task_id=pt.id') ->join('user u','u.id=ptu.user_id') ->where('pt.id',$taskId) ->select(); $task = Db::name('quality_task') // ->field('interrupt_reson,interrupt_img,in_order') ->where('id',$taskId) ->find(); $ret['task']['interrupt_reson'] = isset($task['interrupt_reson'])?$task['interrupt_reson']:''; $ret['task']['interrupt_img'] = isset($task['interrupt_img'])?$task['interrupt_img']:''; $ret['task']['in_order'] = isset($task['in_order'])?$task['in_order']:0; $addrList = Db::name('quality_task_addr') ->alias('pta') ->field('pt.id,pt.start_time,pt.end_time,pt.status,pta.id as quality_addr_form_id,a.title,pta.address_id as quality_addr_id,a.sn as device_sn') ->join('quality_task pt','pta.quality_task_id = pt.id') ->join('address a','pta.address_id = a.id') ->where('pt.id',$taskId) ->select(); $addrList = $addrList?$addrList:[]; foreach ($addrList as $k=>$v){ // $isNormal= Db::name('quality_record') // ->field('is_normal') // ->where('org_id',$orgId) // ->where('quality_task_id',$v['id']) // ->where('quality_addr_id',$v['quality_addr_id']) // ->find(); // $addrList[$k]['is_normal'] = $isNormal?$isNormal['is_normal']:0; // $addrList[$k]['address_status'] = $isNormal?1:0; // if($isNormal){ // $addrList[$k]['type'] = 1; // }else{ // $addrList[$k]['type'] = 0; // } $addrList[$k]['addrCode'] = get_qrcode_str('address',$v['patrol_addr_id']); } $ret['addr_list'] = $addrList; return $ret; } public function taskDeatils($taskId,$formId,$orgId){ $ret['details'] = Db::name('quality_task') ->field('id,title,create_time,end_time,quality_form,in_order') ->where('id',$taskId) ->find(); $patrolRecord = Db::name('quality_record') ->field('images,content,check_json,user_id') ->where('org_id',$orgId) ->where('quality_task_id',$taskId) ->where('patrol_addr_form_id',$formId) ->find(); $jsonArray=json_decode($patrolRecord['check_json'],true); $jsonArray = $jsonArray?$jsonArray:[]; foreach ($jsonArray as $k=>$v){ foreach ($v['forms'] as $kk=>$vv){ if(!empty($vv['remark'])){ $remark=$vv['remark']; }else{ $remark=''; } $v['forms'][$kk]['title'] = $vv['title']; $v['forms'][$kk]['remark'] = $remark; } $jsonArray[$k] = $v; } $ret['forms']=$jsonArray; unset($patrolRecord['check_json']); $ret['details']['images']=$patrolRecord?$patrolRecord['images']:''; $ret['details']['content']=$patrolRecord?$patrolRecord['content']:''; $ret['details']['user_id']=$patrolRecord?$patrolRecord['user_id']:''; $ret['details']['time']=$patrolRecord?$patrolRecord['time']:0; $ret['details']['wc_time']=$patrolRecord?$patrolRecord['wc_time']:0; $ret['details']['min']=$patrolRecord?$patrolRecord['min']:0; $ret['details']['max']=$patrolRecord?$patrolRecord['max']:0; $user = Db::name('patrol_task_user') ->alias('ptu') ->field('u.real_name') ->join('patrol_task pt','ptu.patrol_task_id=pt.id') ->join('user u','u.id=ptu.user_id') ->where('pt.id',$taskId) ->select(); $ret['users']=$user?$user:''; return $ret; } public function forms($patrolAddrFormId){ $forms = Db::name('patrol_task_addr')->where('id',$patrolAddrFormId)->value('patrol_forms'); $ret = []; if($forms){ $patrolForm = Db::name('patrol_addr_form') ->where('id','in',$forms) ->field('id,title,patrol_form') ->select(); $patrolForm = $patrolForm?$patrolForm:[]; foreach ($patrolForm as $k=>$v){ $pfs = $v['patrol_form']?explode(',',$v['patrol_form']):[]; $pfret = Db::name('patrol_form') ->whereIn('id',$pfs) ->select(); $patrolForm[$k]['forms'] = $pfret; unset($patrolForm[$k]['patrol_form']); } $ret = $patrolForm; } return $ret; } public function updateStatus($patrolTaskId,$interruptReson,$interruptImg){ if(!$interruptReson) { $this->error = '中断原因不能为空'; return false; } $data=[ 'status'=>5, 'interrupt_reson'=>$interruptReson, 'interrupt_img'=>$interruptImg, ]; $update = Db::name('patrol_task') ->where('id',$patrolTaskId) ->update($data); return $update; } /** * 巡更人员工作量统计 * * @author wst * @date 2021/4/13 9:10 */ public function WorkerPatrol($orgId,$search,$model=1){ $org_id = $orgId; $query = Db::name('patrol_record'); if(!empty($search['start'])){ $search['start'] = date('Ymd',strtotime($search['start'])); $query->where('patrol_record.create_yyyymmdd','>=', $search['start']); } if(!empty($search['end'])){ $search['end'] = date('Ymd',strtotime($search['end'])); $query->where('patrol_record.create_yyyymmdd','<=', $search['end']); } $query->where('patrol_record.patrol_mode', $model); $query->where('patrol_record.org_id',$org_id); $query->group('patrol_record.user_id'); $query->join('user','user.id = patrol_record.user_id'); $query->field('patrol_record.id,patrol_record.user_id,user.nickname'); $list =$query->select(); $list = $list?$list:array(); foreach ($list as $k=>$v){ $query1 = Db::name('patrol_record'); $query1->where('org_id',$org_id); $query1->where('user_id',$v['user_id']); if(!empty($search['start'])){ $search['start'] = date('Ymd',strtotime($search['start'])); $query1->where('create_yyyymmdd','>=', $search['start']); } if(!empty($search['end'])){ $search['end'] = date('Ymd',strtotime($search['end'])); $query1->where('create_yyyymmdd','<=', $search['end']); } // $query1->where('patrol_mode', $model); $count = $query1->count(); $list[$k]['count'] = $count?$count:0; } return $list; } public function get_list_by_time($orgId,$mode,$start,$end){ $map[] = ['org_id','=',$orgId]; $map[] = ['del','=',0]; // $map[] = ['patrol_mode','=',$mode]; $map[] = ['start_time','<',$end]; $map[] = ['end_time','>=',$start]; $list = $this->field('id,title,start_time,end_time,status') ->where($map) ->select(); $list = $list?$list->toArray():[]; foreach ($list as $k=>$v){ $userList = Db::name('quality_task_user') ->alias('a') ->field('u.id,u.real_name') ->join('user u','u.id = a.user_id') ->where('a.quality_task_id',$v['id']) ->column('real_name'); $list[$k]['users'] = $userList?implode(',',$userList):''; } return $list?$list:array(); } //分组获取任务计划最近12个月份 public function get_task_month($org_id,$mode){ $list = $this ->field('create_yyyymm') ->group('create_yyyymm') ->where('org_id',$org_id) ->where('del',0) // ->where('patrol_mode',$mode) ->order('create_yyyymm','desc') ->limit(12) ->select(); $list = $list?$list->toArray():array(); foreach ($list as $k=>$v){ $list[$k]['create_yyyymm'] = date('Y-m',strtotime($v['create_yyyymm'].'01')); } return $list; } //复制任务 public function plan_data($post,$org){ //TODO::已复制过的月份是否可以继续复制 $from = strtotime($post['from'].'-01'); $to = strtotime($post['to'].'-01'); $max = strtotime(date('Y-m-d', strtotime($post['to'].'-01 +1 month'))); $cha = $to - $from; $map[] = ['del','=',0]; $map[] = ['org_id','=',$org]; // $map[] = ['patrol_mode','=',$post['mode']]; $map[] = ['create_yyyymm','=',date('Ym',strtotime($post['from'].'-01'))]; $list = $this->where($map)->select(); if(!$list){ $this->error = '要复制的月份下没有任务'; return false; } $list = $list->toArray(); $this->startTrans(); try{ $uuid = ''; while (true) { $uuid = new_guid(); $ret = $this ->where('uuid',$uuid) ->find(); if(!$ret){ break; } } foreach ($list as $k=>$v){ $start_time = strtotime($v['start_time'])+$cha; if($start_time >= $max){ continue; } $v['start_time'] = date('Y-m-d H:i:s',$start_time); $v['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time'])+$cha); $v['uuid'] = $uuid; $ret = $this->copy_one($v); if(!$ret){ \exception('复制失败'); } } $this->commit(); return true; }catch (Exception $e){ $this->db->trans_rollback(); $this->error = $e->getMessage(); return false; } } //复制单个任务 private function copy_one($data){ $id = $data['id']; unset($data['id']); $data['status'] = 0; $data['create_time'] = date('Y-m-d H:i:s'); $data['create_yyyymm'] = date('Ym',strtotime($data['start_time'])); $data['create_yyyy'] = date('Y',strtotime($data['start_time'])); $data['create_yyyymmdd'] = date('Ymd',strtotime($data['start_time'])); $addrs = $this->get_task_addr($id); $userids = $this->get_task_user($id); //添加任务 $taskid = $this->insertGetId($data); if(!$taskid){ return false; } //添加检查地点 $addr = array(); foreach ($addrs as $k=>$v){ $addr[] = array( 'quality_task_id' => $taskid, 'quality_addr_form_id' => $v ); } $res = Db::name('quality_task_addr')->insertAll($addr); if(!$res){ return false; } //添加检查人员 $user = array(); foreach ($userids as $k=>$v){ $user[] = array( 'patrol_task_id' => $taskid, 'user_id' => $v ); } $res = Db::name('quality_task_user')->insertAll($user); if(!$res){ return false; } return true; } //获取任务检查地点 public function get_task_addr($taskid){ $addrlist = Db::name('quality_task_addr') ->where('quality_task_id',$taskid) ->column('quality_addr_form_id'); return $addrlist; } //获取任务检查人员 public function get_task_user($taskid){ $userlist = Db::name('quality_task_user') ->where('quality_task_id',$taskid) ->column('user_id'); return $userlist; } //定时处理超时任务 public function timerAction(){ $curTime = date('Y-m-d H:i:s'); $map[] = ['del','=',0]; $map[] = ['status','in',[0,1]]; $map[] = ['end_time','<',$curTime]; $this->where($map) ->update(['status'=>3]); $ids = Db::name('task') ->alias('a') ->join('quality_task b','a.bus_id=b.id') ->where('b.end_time','<',$curTime) ->where('a.type',5) ->column('a.id'); if(!empty($ids)){ Db::name('task') ->where('id','in',$ids) ->delete(); } } public function addPatrolTask(){ $curTime = date('Y-m-d H:i:s',time()+8*60*60); $map[] = ['a.del','=',0]; $map[] = ['a.status','=',0]; $map[] = ['a.start_time','<',$curTime]; $ids = $this ->alias('a') ->join('quality_task_user b','b.quality_task_id=a.id') ->where($map) ->field('a.*,b.user_id') ->select(); if(!empty($ids)){ $ids = $ids->toArray(); $a = []; foreach ($ids as $k=>$v){ $check = Db::name('task') ->where('type',5) ->where('org_id',$v['org_id']) ->where('bus_id',$v['id']) ->where('user_id',$v['user_id']) ->find(); if(empty($check)){ $a[]= [ 'org_id'=>$v['org_id'], 'type'=>5, 'start_time'=>$v['start_time'], 'create_time'=>getTime(), 'bus_id'=>$v['id'], 'user_id'=>$v['user_id'] ]; } } if(!empty($a)){ Db::name('task') ->insertAll($a); } } } }