post(); $validate = new \app\common\validate\GreenTask(); $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 || $v['formId'] <= 0){ $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['end_time'])), 'create_yyyymmdd' => date('Ymd',strtotime($post['start_time'])), ); Db::startTrans(); try { //编辑任务 $res = Db::name('green_task')->where('id',$post['id'])->update($data); if(!$res){ exception('编辑任务失败'); } Db::name('green_task_addr')->where('green_task_id',$post['id'])->delete(); Db::name('green_task_user')->where('green_task_id',$post['id'])->delete(); // 重新添加地点及表单 $nforms = []; foreach ($addrs as $key=>$val){ $nforms[] = [ 'green_task_id' => $post['id'], 'green_addr_id' => $val['addrId'], 'green_form_id' => $val['formId'] ]; } $fromRet = Db::name('green_task_addr')->insertAll($nforms); if($fromRet != count($nforms)){ \exception('操作失败'); } // 重新添加用户 $uds = []; $users = explode(',',$post['user_ids']); foreach ($users as $key=>$val){ $uds[] = [ 'green_task_id' => $post['id'], 'user_id' => $val ]; } $userRet = Db::name('green_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('green_task')->where('id',$taskId)->find(); //获取计划任务检查组id $old_addrs = Db::name('green_task_addr') ->where('green_task_id',$ret['id']) ->column('green_form_id'); $ret['old_addrs'] = $old_addrs; //获取计划任务检查人员 $old_user_ids=Db::name('green_task_user')->where('green_task_id',$ret['id'])->column('user_id'); $ret['old_user_ids'] = $old_user_ids; return $ret; } //api接口 public function lists($page,$size,$type,$userId,$orgId){ $ret['list']=[]; if($type == 1){ // 待完成 $map[] = ['t.user_id','=',$userId]; $map[] = ['t.org_id','=',$orgId]; $map[] = ['gt.del','=',0]; $map[] = ['t.type','=',6]; $ret['list'] = Db::name('task') ->alias('t') ->field('gt.id,gt.start_time,gt.end_time,gt.title,gt.status,gt.is_exception') ->join('green_task gt','t.bus_id = gt.id') ->where($map) ->whereIn('gt.status',[0,1]) ->order('gt.start_time desc') ->page($page,$size) ->select(); foreach ($ret['list'] as $k=>$v){ $people_name = Db::name('green_task_user') ->alias('gtu') ->join('user u','u.id=gtu.user_id') ->where('gtu.green_task_id',$v['id']) ->column('u.real_name'); $ret['list'][$k]['peoples'] = $people_name?implode(',',$people_name):''; } }else if($type == 2){ // 已完成 $where[] = ['gt.org_id','=',$orgId]; $where[] = ['gt.del','=',0]; $where[] = ['ptu.user_id','=',$userId]; $ret['list'] = Db::name('green_task_user') ->alias('gtu') ->field('gt.id,gt.start_time,gt.end_time,gt.title,gt.status,gtu.user_id,gt.is_exception') ->join('green_task gt','gtu.green_task_id=gt.id') ->where($where) ->whereIn('gt.status',[2,3,5]) ->order('gt.start_time desc') ->page($page,$size) ->select(); foreach ($ret['list'] as $k=>$v){ $user = Db::name('user') ->alias('u') ->field('u.real_name,d.title') ->join('user_dep ud','ud.user_id=u.id') ->join('dep d','d.id=ud.dep_id') ->where('u.id',$v['user_id']) ->find(); $ret['list'][$k]['dep_name'] = $user?$user['title']:""; $ret['list'][$k]['people_name'] = $user?$user['real_name']:""; } } return $ret; } public function addrList($taskId,$orgId,$userId){ $ret['user_list'] = Db::name('green_task_user') ->alias('gtu') ->field('u.real_name') ->join('green_task gt','gtu.green_task_id=gt.id') ->join('user u','u.id=gtu.user_id') ->where('gt.id',$taskId) ->select(); $task = Db::name('green_task') ->field('interrupt_reson,interrupt_img') ->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']:''; $addrList = Db::name('green_task_addr') ->alias('gta') ->field('gt.id,gt.start_time,gt.end_time,gt.status,gta.id as green_task_addr_id,ga.title,gta.green_addr_id') ->join('green_task gt','gta.green_task_id = gt.id') ->join('green_addr ga','gta.green_addr_id = ga.id') ->where('gt.id',$taskId) ->select(); $addrList = $addrList?$addrList:[]; foreach ($addrList as $k=>$v){ $record = Db::name('green_task_record') ->where('org_id',$orgId) ->where('green_task_id',$v['id']) ->where('green_addr_id',$v['green_addr_id']) ->where('user_id',$userId) ->where('green_task_addr_id',$v['green_task_addr_id']) ->find(); $addrList[$k]['status'] = $record?1:0; } $ret['addr_list'] = $addrList; return $ret; } public function taskDetails($taskId,$greenTaskAddrId,$orgId){ $ret['details'] = Db::name('green_task') ->field('id,title,create_time,end_time,green_form') ->where('id',$taskId) ->find(); $greenRecord = Db::name('green_task_record') ->field('create_time,images,content,check_json,user_id') ->where('org_id',$orgId) ->where('green_task_id',$taskId) ->where('green_task_addr_id',$greenTaskAddrId) ->find(); $jsonArray=json_decode($greenRecord['check_json'],true); $jsonArray = $jsonArray?$jsonArray:[]; foreach ($jsonArray as $k=>$v){ if(!empty($v['remark'])){ $remark=$v['remark']; }else{ $remark=''; } $jsonArray[$k]['title'] = $v['title']; $jsonArray[$k]['remark'] = $remark; } $ret['forms']=$jsonArray; unset($greenRecord['check_json']); $ret['details']['images']=$greenRecord?$greenRecord['images']:''; $ret['details']['content']=$greenRecord?$greenRecord['content']:''; $ret['details']['user_id']=$greenRecord?$greenRecord['user_id']:''; $ret['details']['done_time']=$greenRecord?$greenRecord['create_time']:''; $user = Db::name('green_task_user') ->alias('gtu') ->field('u.real_name') ->join('green_task gt','gtu.green_task_id=gt.id') ->join('user u','u.id=gtu.user_id') ->where('gt.id',$taskId) ->select(); $ret['users']=$user?$user:''; $ret['addr'] = Db::name('green_task_addr') ->alias('gta') ->field('ga.id,ga.title,ga.img,ga.content') ->join('green_addr ga','ga.id=gta.green_addr_id') ->where('gta.id',$greenTaskAddrId) ->find(); return $ret; } public function forms($greenTaskAddrId){ $greenForm = Db::name('green_addr_form') ->alias('a') ->join('green_task_addr b','b.green_form_id = a.id') ->where('b.id',$greenTaskAddrId) ->value('a.green_form'); $greenForm = $greenForm?explode(',',$greenForm):[]; $ret = Db::name('green_form') ->whereIn('id',$greenForm) ->select(); return $ret; } public function updateStatus($greenTaskId,$interruptReson,$interruptImg){ if(!$interruptReson) { $this->error = '中断原因不能为空'; return false; } $data=[ 'status'=>5, 'interrupt_reson'=>$interruptReson, 'interrupt_img'=>$interruptImg, ]; $update = Db::name('green_task') ->where('id',$greenTaskId) ->update($data); return $update; } public function WorkerGreen($orgId,$search){ $org_id = $orgId; $query = Db::name('green_task_record'); if(!empty($search['start'])){ $search['start'] = date('Ymd',strtotime($search['start'])); $query->where('green_task_record.create_yyyymmdd','>=', $search['start']); } if(!empty($search['end'])){ $search['end'] = date('Ymd',strtotime($search['end'])); $query->where('green_task_record.create_yyyymmdd','<=', $search['end']); } $query->where('green_task_record.org_id',$org_id); $query->group('green_task_record.user_id'); $query->join('user','user.id = green_record.user_id'); $query->field('green_task_record.id,green_task_record.user_id,user.nickname'); $list =$query->select(); $list = $list?$list:array(); foreach ($list as $k=>$v){ $query1 = Db::name('green_task_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']); } $count = $query1->count(); $list[$k]['count'] = $count?$count:0; } return $list; } public function get_list_by_time($orgId,$start,$end){ $map[] = ['org_id','=',$orgId]; $map[] = ['del','=',0]; $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('green_task_user') ->alias('a') ->field('u.id,u.real_name') ->join('user u','u.id = a.user_id') ->where('a.green_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){ $list = $this ->field('create_yyyymm') ->group('create_yyyymm') ->where('org_id',$org_id) ->where('del',0) ->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[] = ['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( 'green_task_id' => $taskid, 'green_addr_form_id' => $v ); } $res = Db::name('green_task_addr')->insertAll($addr); if(!$res){ return false; } //添加检查人员 $user = array(); foreach ($userids as $k=>$v){ $user[] = array( 'green_task_id' => $taskid, 'user_id' => $v ); } $res = Db::name('green_task_user')->insertAll($user); if(!$res){ return false; } return true; } //获取任务检查地点 public function get_task_addr($taskid){ $addrlist = Db::name('green_task_addr') ->where('green_task_id',$taskid) ->column('green_addr_id'); return $addrlist; } //获取任务检查人员 public function get_task_user($taskid){ $userlist = Db::name('green_task_user') ->where('green_task_id',$taskid) ->column('user_id'); return $userlist; } //定时处理超时任务 public function timer_action(){ $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('green_task b','a.bus_id=b.id') ->where('b.end_time','<',$curTime) ->where('a.type',6) ->column('a.id'); if(!empty($ids)){ Db::name('task') ->where('id','in',$ids) ->delete(); } } public function addGreenTask(){ $curTime = date('Y-m-d H:i:s',time()+12*60*60); $map[] = ['a.del','=',0]; $map[] = ['a.status','=',0]; $map[] = ['a.start_time','<',$curTime]; $ids = $this ->alias('a') ->join('green_task_user b','b.green_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',6) ->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'=>6, '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); } } } }