PatrolTask.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. <?php
  2. namespace app\common\model;
  3. use think\Exception;
  4. use think\Db;
  5. class PatrolTask extends Base {
  6. public function updates($mode){
  7. $post = request()->post();
  8. $validate = new \app\common\validate\PatrolTask();
  9. $result = $validate->check($post,[]);
  10. if(true !== $result){
  11. $this->error = $validate->getError();
  12. return false;
  13. }
  14. $addrs = json_decode($post['addrs'],true);
  15. if(empty($addrs)){
  16. $this->error='未设置地点';
  17. return false;
  18. }
  19. foreach ($addrs as $k=>$v){
  20. if($v['addrId'] <= 0 || count($v['forms']) <= 0){
  21. $this->error='地点设置错误';
  22. return false;
  23. }
  24. }
  25. if(!$post['user_ids']){
  26. $this->error='检查人员不能为空';
  27. return false;
  28. }
  29. if(strtotime($post['start_time']) < time()){
  30. $this->error = '开始时间不能小于当前时间';
  31. return false;
  32. }
  33. if($post['start_time'] >= $post['end_time']){
  34. $this->error = '结束时间请大于开始时间';
  35. return false;
  36. }
  37. if($post['id'] <= 0){
  38. $this->error = '参数错误';
  39. return false;
  40. }
  41. //编辑操作
  42. $curtime = date('Y-m-d H:i:s');
  43. $data = array(
  44. 'title' => $post['title'],
  45. 'update_time' => $curtime,
  46. 'start_time' => $post['start_time'],
  47. 'end_time' => $post['end_time'],
  48. 'create_yyyymm' => date('Ym',strtotime($post['start_time'])),
  49. 'create_yyyy' => date('Y',strtotime($post['start_time'])),
  50. 'create_yyyymmdd' => date('Ymd',strtotime($post['start_time'])),
  51. 'in_order' => $post['in_order'],
  52. 'cate_id' => isset($post['cate_id'])?$post['cate_id']:0,
  53. 'lines' => $post['in_order'] == 1&&$post['lines']?$post['lines']:''
  54. );
  55. Db::startTrans();
  56. try {
  57. //编辑任务
  58. $res = Db::name('patrol_task')->where('id',$post['id'])->update($data);
  59. if(!$res){
  60. exception('编辑任务失败');
  61. }
  62. Db::name('patrol_task_addr')->where('patrol_task_id',$post['id'])->delete();
  63. Db::name('patrol_task_user')->where('patrol_task_id',$post['id'])->delete();
  64. // 重新添加地点及表单
  65. $nforms = [];
  66. foreach ($addrs as $key=>$val){
  67. $pfms = [];
  68. foreach ($val['forms'] as $kk=>$vv){
  69. $pfms[] = $vv['id'];
  70. }
  71. $nforms[] = [
  72. 'patrol_task_id' => $post['id'],
  73. 'address_id' => $val['addrId'],
  74. 'patrol_forms' => implode(',',$pfms),
  75. 'sorts' => $val['sorts'],
  76. ];
  77. }
  78. $fromRet = Db::name('patrol_task_addr')->insertAll($nforms);
  79. if($fromRet != count($nforms)){
  80. \exception('操作失败');
  81. }
  82. // 重新添加用户
  83. $uds = [];
  84. $users = explode(',',$post['user_ids']);
  85. foreach ($users as $key=>$val){
  86. $uds[] = [
  87. 'patrol_task_id' => $post['id'],
  88. 'user_id' => $val
  89. ];
  90. }
  91. $userRet = Db::name('patrol_task_user')->insertAll($uds);
  92. if($userRet != count($uds)){
  93. \exception('操作失败');
  94. }
  95. Db::commit();
  96. return true;
  97. }catch (\Exception $e){
  98. $this->error = $e->getMessage();
  99. Db::rollback();
  100. return false;
  101. }
  102. }
  103. /**
  104. * 格式化时间,获取时间数组
  105. * @param $starttime 开始时间
  106. * @param $endtime 结束时间
  107. * @param $hours 重复小时
  108. * @param $days 执行天数
  109. * @return array
  110. */
  111. private function get_time_arr($starttime,$endtime,$hours,$days){
  112. $onehour = 60*60;
  113. $oneday = 24*60*60;
  114. $start = strtotime($starttime);
  115. $end = strtotime($endtime);
  116. $ctime = $end - $start;
  117. $timearr = array();
  118. $timearr[] = array(
  119. 'start_time' => $starttime,
  120. 'end_time' => $endtime,
  121. );
  122. if($hours > 0){
  123. if($ctime > $hours*$onehour){
  124. $timearr = array(); //置空
  125. $i = $hours;
  126. while(true){
  127. $nend = $start + $i*$onehour;
  128. $nstart = $start + ($i-$hours)*$onehour;
  129. $timearr[] = array(
  130. 'start_time' => date('Y-m-d H:i',$nstart),
  131. 'end_time' => date('Y-m-d H:i',$nend),
  132. );
  133. if($nend >= $end){
  134. break;
  135. }
  136. $i += $hours;
  137. }
  138. }
  139. }
  140. if($days > 0){
  141. $timearray = $timearr;
  142. for ($i=1; $i<=$days;$i++){
  143. foreach ($timearray as $k=>$v){
  144. $timearr[] = array(
  145. 'start_time' => date('Y-m-d H:i',strtotime($v['start_time']) + $i*$oneday),
  146. 'end_time' => date('Y-m-d H:i',strtotime($v['end_time']) + $i*$oneday),
  147. );
  148. }
  149. }
  150. }
  151. return $timearr;
  152. }
  153. //获取巡查计划任务
  154. public function getTaskOne($taskId){
  155. $ret=Db::name('patrol_task')->where('id',$taskId)->find();
  156. //获取计划任务检查组id
  157. $old_addrs=Db::name('patrol_task_addr')
  158. ->where('patrol_task_id',$ret['id'])->select();
  159. foreach ($old_addrs as $k=>$v){
  160. $ids[$k]=$v['patrol_addr_form_id'];
  161. }
  162. $ret['old_addrs']=$ids;
  163. //获取计划任务检查人员
  164. $old_user_ids=Db::name('patrol_task_user')->where('patrol_task_id',$ret['id'])->column('user_id');
  165. $ret['old_user_ids']=$old_user_ids;
  166. return $ret;
  167. }
  168. //api接口
  169. public function lists($page,$size,$type,$userId,$orgId,$patrol_mode,$status){
  170. $ret['list']=[];
  171. if($type == 1){ // 待完成
  172. $map[] = ['t.user_id','=',$userId];
  173. $map[] = ['t.org_id','=',$orgId];
  174. $map[] = ['pt.del','=',0];
  175. $map[] = ['t.type','=',2];
  176. $map[] = ['pt.patrol_mode','=',$patrol_mode];
  177. $ret['list'] = Db::name('task')
  178. ->alias('t')
  179. ->field('pt.id,pt.start_time,pt.end_time,pt.title,pt.patrol_mode,pt.status,pt.is_exception')
  180. ->join('patrol_task pt','t.bus_id = pt.id')
  181. ->where($map)
  182. ->whereIn('pt.status',[0,1])
  183. ->order('pt.start_time desc')
  184. ->page($page,$size)
  185. ->select();
  186. }else if($type == 2){ // 已完成
  187. $where[] = ['pt.org_id','=',$orgId];
  188. $where[] = ['pt.del','=',0];
  189. $where[] = ['pt.patrol_mode','=',$patrol_mode];
  190. $where[] = ['ptu.user_id','=',$userId];
  191. if($status != ''){
  192. $where[] = ['pt.status','=',$status];
  193. }else{
  194. $where[] = ['pt.status','in',[2,3,5]];
  195. }
  196. $ret['list'] = Db::name('patrol_task_user')
  197. ->alias('ptu')
  198. ->field('pt.id,pt.start_time,pt.end_time,pt.title,pt.patrol_mode,pt.status,ptu.user_id,pt.is_exception')
  199. ->join('patrol_task pt','ptu.patrol_task_id=pt.id')
  200. ->where($where)
  201. // ->where('pt.status',$status)
  202. ->order('pt.start_time desc')
  203. ->page($page,$size)
  204. ->select();
  205. foreach ($ret['list'] as $k=>$v){
  206. $user = Db::name('user')
  207. ->alias('u')
  208. ->field('u.real_name,d.title')
  209. ->join('user_dep ud','ud.user_id=u.id')
  210. ->join('dep d','d.id=ud.dep_id')
  211. ->where('u.id',$v['user_id'])
  212. ->find();
  213. $ret['list'][$k]['dep_name'] = $user?$user['title']:"";
  214. $ret['list'][$k]['people_name'] = $user?$user['real_name']:"";
  215. }
  216. }
  217. if($ret['list']){
  218. foreach ($ret['list'] as $k=>$v){
  219. $info = Db::name('patrol_task_delay')
  220. ->where('task_id',$v['id'])
  221. ->where('status',1)
  222. ->order('id desc')
  223. ->find();
  224. if($info){
  225. $ret['list'][$k]['end_time'] = $info['end_time'];
  226. }
  227. }
  228. }
  229. return $ret;
  230. }
  231. public function taskCount($userId,$orgId,$patrol_mode){
  232. $map[] = ['t.user_id','=',$userId];
  233. $map[] = ['t.org_id','=',$orgId];
  234. $map[] = ['pt.del','=',0];
  235. $map[] = ['t.type','=',2];
  236. $map[] = ['pt.patrol_mode','=',$patrol_mode];
  237. $ret= Db::name('task')
  238. ->alias('t')
  239. ->field('pt.id,pt.start_time,pt.end_time,pt.title,pt.patrol_mode,pt.status,pt.is_exception')
  240. ->join('patrol_task pt','t.bus_id = pt.id')
  241. ->where($map)
  242. ->whereIn('pt.status',[0,1])
  243. ->count();
  244. return $ret;
  245. }
  246. public function addrList($taskId,$orgId){
  247. $ret['user_list'] = Db::name('patrol_task_user')
  248. ->alias('ptu')
  249. ->field('u.real_name')
  250. ->join('patrol_task pt','ptu.patrol_task_id=pt.id')
  251. ->join('user u','u.id=ptu.user_id')
  252. ->where('pt.id',$taskId)
  253. ->select();
  254. $task = Db::name('patrol_task')
  255. ->field('interrupt_reson,interrupt_img,in_order')
  256. ->where('id',$taskId)
  257. ->find();
  258. $ret['task']['interrupt_reson'] = isset($task['interrupt_reson'])?$task['interrupt_reson']:'';
  259. $ret['task']['interrupt_img'] = isset($task['interrupt_img'])?$task['interrupt_img']:'';
  260. $ret['task']['in_order'] = isset($task['in_order'])?$task['in_order']:0;
  261. $addrList = Db::name('patrol_task_addr')
  262. ->alias('pta')
  263. ->field('pt.id,pt.start_time,pt.end_time,pt.status,pta.id as patrol_addr_form_id,a.title,pta.address_id as patrol_addr_id,a.sn as device_sn')
  264. ->join('patrol_task pt','pta.patrol_task_id = pt.id')
  265. ->join('address a','pta.address_id = a.id')
  266. ->where('pt.id',$taskId)
  267. ->select();
  268. $addrList = $addrList?$addrList:[];
  269. foreach ($addrList as $k=>$v){
  270. $isNormal= Db::name('patrol_record')
  271. ->field('is_normal')
  272. ->where('org_id',$orgId)
  273. ->where('patrol_task_id',$v['id'])
  274. ->where('patrol_addr_id',$v['patrol_addr_id'])
  275. ->find();
  276. $addrList[$k]['is_normal'] = $isNormal?$isNormal['is_normal']:0;
  277. $addrList[$k]['address_status'] = $isNormal?1:0;
  278. if($isNormal){
  279. $addrList[$k]['type'] = 1;
  280. }else{
  281. $addrList[$k]['type'] = 0;
  282. }
  283. $addrList[$k]['addrCode'] = get_qrcode_str('address',$v['patrol_addr_id']);
  284. $formList = $this->forms($v['patrol_addr_form_id']);
  285. $addrList[$k]['formList'] = $formList?$formList:[];
  286. }
  287. $ret['addr_list'] = $addrList;
  288. return $ret;
  289. }
  290. public function taskDeatils($taskId,$formId,$orgId){
  291. $ret['details'] = Db::name('patrol_task')
  292. ->field('id,title,create_time,start_time,end_time,patrol_form,in_order')
  293. ->where('id',$taskId)
  294. ->find();
  295. $patrolRecord = Db::name('patrol_record')
  296. ->field('id as recordId,images,create_time,content,check_json,user_id,time,wc_time,min,max,order_id')
  297. ->where('org_id',$orgId)
  298. ->where('patrol_task_id',$taskId)
  299. ->where('patrol_addr_form_id',$formId)
  300. ->find();
  301. $jsonArray=json_decode($patrolRecord['check_json'],true);
  302. $jsonArray = $jsonArray?$jsonArray:[];
  303. foreach ($jsonArray as $k=>$v){
  304. foreach ($v['forms'] as $kk=>$vv){
  305. if(!empty($vv['remark'])){
  306. $remark=$vv['remark'];
  307. }else{
  308. $remark='';
  309. }
  310. $v['forms'][$kk]['title'] = $vv['title'];
  311. $v['forms'][$kk]['remark'] = $remark;
  312. }
  313. $jsonArray[$k] = $v;
  314. }
  315. $ret['forms']=$jsonArray;
  316. unset($patrolRecord['check_json']);
  317. $ret['details']['images']=$patrolRecord?$patrolRecord['images']:'';
  318. $ret['details']['content']=$patrolRecord?$patrolRecord['content']:'';
  319. $ret['details']['user_id']=$patrolRecord?$patrolRecord['user_id']:'';
  320. $ret['details']['order_id']=$patrolRecord?$patrolRecord['order_id']:0;
  321. $ret['details']['recordId']=$patrolRecord?$patrolRecord['recordId']:0;
  322. $ret['details']['real_name']= Db::name('user')->where('id',$patrolRecord['user_id'])->value('real_name');
  323. $ret['details']['time']=$patrolRecord?$patrolRecord['time']:0;
  324. $ret['details']['wc_time']=$patrolRecord?$patrolRecord['wc_time']:0;
  325. $ret['details']['min']=$patrolRecord?$patrolRecord['min']:0;
  326. $ret['details']['max']=$patrolRecord?$patrolRecord['max']:0;
  327. $ret['details']['create_time']=$patrolRecord?$patrolRecord['create_time']:"";
  328. $user = Db::name('patrol_task_user')
  329. ->alias('ptu')
  330. ->field('u.real_name')
  331. ->join('patrol_task pt','ptu.patrol_task_id=pt.id')
  332. ->join('user u','u.id=ptu.user_id')
  333. ->where('pt.id',$taskId)
  334. ->select();
  335. $ret['users']=$user?$user:'';
  336. return $ret;
  337. }
  338. public function forms($patrolAddrFormId){
  339. $forms = Db::name('patrol_task_addr')->where('id',$patrolAddrFormId)->value('patrol_forms');
  340. $ret = [];
  341. if($forms){
  342. $patrolForm = Db::name('patrol_addr_form')
  343. ->where('id','in',$forms)
  344. ->field('id,title,patrol_form')
  345. ->select();
  346. $patrolForm = $patrolForm?$patrolForm:[];
  347. foreach ($patrolForm as $k=>$v){
  348. $pfs = $v['patrol_form']?explode(',',$v['patrol_form']):[];
  349. $pfret = Db::name('patrol_form')
  350. ->whereIn('id',$pfs)
  351. ->select();
  352. foreach ($pfret as $kk=>$vv){
  353. $pfret[$kk]['remark'] = $vv['remark']?$vv['remark']:'';
  354. }
  355. $patrolForm[$k]['forms'] = $pfret;
  356. unset($patrolForm[$k]['patrol_form']);
  357. }
  358. $ret = $patrolForm;
  359. }
  360. return $ret;
  361. }
  362. public function updateStatus($patrolTaskId,$interruptReson,$interruptImg){
  363. if(!$interruptReson) {
  364. $this->error = '中断原因不能为空';
  365. return false;
  366. }
  367. $data=[
  368. 'status'=>5,
  369. 'interrupt_reson'=>$interruptReson,
  370. 'interrupt_img'=>$interruptImg,
  371. ];
  372. $update = Db::name('patrol_task')
  373. ->where('id',$patrolTaskId)
  374. ->update($data);
  375. return $update;
  376. }
  377. /**
  378. * 巡更人员工作量统计
  379. *
  380. * @author wst
  381. * @date 2021/4/13 9:10
  382. */
  383. public function WorkerPatrol($orgId,$search,$model=1){
  384. $org_id = $orgId;
  385. $query = Db::name('patrol_record');
  386. if(!empty($search['start'])){
  387. $search['start'] = date('Ymd',strtotime($search['start']));
  388. $query->where('patrol_record.create_yyyymmdd','>=', $search['start']);
  389. }
  390. if(!empty($search['end'])){
  391. $search['end'] = date('Ymd',strtotime($search['end']));
  392. $query->where('patrol_record.create_yyyymmdd','<=', $search['end']);
  393. }
  394. $query->where('patrol_record.patrol_mode', $model);
  395. $query->where('patrol_record.org_id',$org_id);
  396. $query->group('patrol_record.user_id');
  397. $query->join('user','user.id = patrol_record.user_id');
  398. $query->field('patrol_record.id,patrol_record.user_id,user.nickname');
  399. $list =$query->select();
  400. $list = $list?$list:array();
  401. foreach ($list as $k=>$v){
  402. $query1 = Db::name('patrol_record');
  403. $query1->where('org_id',$org_id);
  404. $query1->where('user_id',$v['user_id']);
  405. if(!empty($search['start'])){
  406. $search['start'] = date('Ymd',strtotime($search['start']));
  407. $query1->where('create_yyyymmdd','>=', $search['start']);
  408. }
  409. if(!empty($search['end'])){
  410. $search['end'] = date('Ymd',strtotime($search['end']));
  411. $query1->where('create_yyyymmdd','<=', $search['end']);
  412. }
  413. $query1->where('patrol_mode', $model);
  414. $count = $query1->count();
  415. $list[$k]['count'] = $count?$count:0;
  416. }
  417. return $list;
  418. }
  419. public function get_list_by_time($orgId,$mode,$start,$end){
  420. $map[] = ['org_id','=',$orgId];
  421. $map[] = ['del','=',0];
  422. $map[] = ['patrol_mode','=',$mode];
  423. $map[] = ['start_time','<',$end];
  424. $map[] = ['end_time','>=',$start];
  425. $list = $this->field('id,title,start_time,end_time,status')
  426. ->where($map)
  427. ->select();
  428. $list = $list?$list->toArray():[];
  429. foreach ($list as $k=>$v){
  430. $userList = Db::name('patrol_task_user')
  431. ->alias('a')
  432. ->field('u.id,u.real_name')
  433. ->join('user u','u.id = a.user_id')
  434. ->where('a.patrol_task_id',$v['id'])
  435. ->column('real_name');
  436. $list[$k]['users'] = $userList?implode(',',$userList):'';
  437. }
  438. return $list?$list:array();
  439. }
  440. //分组获取任务计划最近12个月份
  441. public function get_task_month($org_id,$mode){
  442. $list = $this
  443. ->field('create_yyyymm')
  444. ->group('create_yyyymm')
  445. ->where('org_id',$org_id)
  446. ->where('del',0)
  447. ->where('patrol_mode',$mode)
  448. ->order('create_yyyymm','desc')
  449. ->limit(12)
  450. ->select();
  451. $list = $list?$list->toArray():array();
  452. foreach ($list as $k=>$v){
  453. $list[$k]['create_yyyymm'] = date('Y-m',strtotime($v['create_yyyymm'].'01'));
  454. }
  455. return $list;
  456. }
  457. //复制任务
  458. public function plan_data($post,$org){ //TODO::已复制过的月份是否可以继续复制
  459. $from = strtotime($post['from'].'-01');
  460. $to = strtotime($post['to'].'-01');
  461. $max = strtotime(date('Y-m-d', strtotime($post['to'].'-01 +1 month')));
  462. $cha = $to - $from;
  463. $map[] = ['del','=',0];
  464. $map[] = ['org_id','=',$org];
  465. $map[] = ['patrol_mode','=',$post['mode']];
  466. $map[] = ['create_yyyymm','=',date('Ym',strtotime($post['from'].'-01'))];
  467. $list = $this->where($map)->select();
  468. if(!$list){
  469. $this->error = '要复制的月份下没有任务';
  470. return false;
  471. }
  472. $list = $list->toArray();
  473. $this->startTrans();
  474. try{
  475. $uuid = '';
  476. while (true) {
  477. $uuid = new_guid();
  478. $ret = $this
  479. ->where('uuid',$uuid)
  480. ->find();
  481. if(!$ret){
  482. break;
  483. }
  484. }
  485. foreach ($list as $k=>$v){
  486. $start_time = strtotime($v['start_time'])+$cha;
  487. if($start_time >= $max){
  488. continue;
  489. }
  490. $v['start_time'] = date('Y-m-d H:i:s',$start_time);
  491. $v['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time'])+$cha);
  492. $v['uuid'] = $uuid;
  493. $ret = $this->copy_one($v);
  494. if(!$ret){
  495. \exception('复制失败');
  496. }
  497. }
  498. $this->commit();
  499. return true;
  500. }catch (Exception $e){
  501. $this->db->trans_rollback();
  502. $this->error = $e->getMessage();
  503. return false;
  504. }
  505. }
  506. //复制单个任务
  507. private function copy_one($data){
  508. $id = $data['id'];
  509. unset($data['id']);
  510. $data['status'] = 0;
  511. $data['create_time'] = date('Y-m-d H:i:s');
  512. $data['create_yyyymm'] = date('Ym',strtotime($data['start_time']));
  513. $data['create_yyyy'] = date('Y',strtotime($data['start_time']));
  514. $data['create_yyyymmdd'] = date('Ymd',strtotime($data['start_time']));
  515. $addrs = $this->get_task_addr($id);
  516. $userids = $this->get_task_user($id);
  517. //添加任务
  518. $taskid = $this->insertGetId($data);
  519. if(!$taskid){
  520. return false;
  521. }
  522. //添加检查地点
  523. $addr = array();
  524. foreach ($addrs as $k=>$v){
  525. $addr[] = array(
  526. 'patrol_task_id' => $taskid,
  527. 'patrol_addr_form_id' => $v
  528. );
  529. }
  530. $res = Db::name('patrol_task_addr')->insertAll($addr);
  531. if(!$res){
  532. return false;
  533. }
  534. //添加检查人员
  535. $user = array();
  536. foreach ($userids as $k=>$v){
  537. $user[] = array(
  538. 'patrol_task_id' => $taskid,
  539. 'user_id' => $v
  540. );
  541. }
  542. $res = Db::name('patrol_task_user')->insertAll($user);
  543. if(!$res){
  544. return false;
  545. }
  546. return true;
  547. }
  548. //获取任务检查地点
  549. public function get_task_addr($taskid){
  550. $addrlist = Db::name('patrol_task_addr')
  551. ->where('patrol_task_id',$taskid)
  552. ->column('patrol_addr_form_id');
  553. return $addrlist;
  554. }
  555. //获取任务检查人员
  556. public function get_task_user($taskid){
  557. $userlist = Db::name('patrol_task_user')
  558. ->where('patrol_task_id',$taskid)
  559. ->column('user_id');
  560. return $userlist;
  561. }
  562. //定时处理超时任务
  563. public function timer_action(){
  564. $curTime = date('Y-m-d H:i:s');
  565. $map[] = ['del','=',0];
  566. $map[] = ['status','in',[0,1]];
  567. $map[] = ['end_time','<',$curTime];
  568. $this->where($map)
  569. ->update(['status'=>3]);
  570. $ids = Db::name('task')
  571. ->alias('a')
  572. ->join('patrol_task b','a.bus_id=b.id')
  573. ->where('b.end_time','<',$curTime)
  574. ->where('a.type',2)
  575. ->column('a.id');
  576. if(!empty($ids)){
  577. Db::name('task')
  578. ->where('id','in',$ids)
  579. ->delete();
  580. }
  581. }
  582. public function addPatrolTask(){
  583. $curTime = date('Y-m-d H:i:s',time()+8*60*60);
  584. $map[] = ['a.del','=',0];
  585. $map[] = ['a.status','=',0];
  586. $map[] = ['a.start_time','<',$curTime];
  587. $ids = $this
  588. ->alias('a')
  589. ->join('patrol_task_user b','b.patrol_task_id=a.id')
  590. ->where($map)
  591. ->whereIn('a.patrol_mode',[1,2,3])
  592. ->field('a.*,b.user_id')
  593. ->select();
  594. if(!empty($ids)){
  595. $ids = $ids->toArray();
  596. $a = [];
  597. foreach ($ids as $k=>$v){
  598. $check = Db::name('task')
  599. ->where('type',2)
  600. ->where('org_id',$v['org_id'])
  601. ->where('bus_id',$v['id'])
  602. ->where('user_id',$v['user_id'])
  603. ->find();
  604. if(empty($check)){
  605. $a[]= [
  606. 'org_id'=>$v['org_id'],
  607. 'type'=>2,
  608. 'start_time'=>$v['start_time'],
  609. 'create_time'=>getTime(),
  610. 'bus_id'=>$v['id'],
  611. 'user_id'=>$v['user_id']
  612. ];
  613. }
  614. }
  615. if(!empty($a)){
  616. Db::name('task')
  617. ->insertAll($a);
  618. }
  619. }
  620. //巡检类型
  621. $curTime2 = date('Y-m-d H:i:s',time()+10*60*60);
  622. $map2[] = ['a.del','=',0];
  623. $map2[] = ['a.status','=',0];
  624. $map2[] = ['a.start_time','<',$curTime2];
  625. $ids2 = $this
  626. ->alias('a')
  627. ->join('patrol_task_user b','b.patrol_task_id=a.id')
  628. ->where($map2)
  629. ->where('a.patrol_mode',4)
  630. ->field('a.*,b.user_id')
  631. ->select();
  632. if(!empty($ids2)){
  633. $ids2 = $ids2->toArray();
  634. $a2 = [];
  635. foreach ($ids2 as $k=>$v){
  636. $check2 = Db::name('task')
  637. ->where('type',2)
  638. ->where('org_id',$v['org_id'])
  639. ->where('bus_id',$v['id'])
  640. ->where('user_id',$v['user_id'])
  641. ->find();
  642. if(empty($check2)){
  643. $a2[]= [
  644. 'org_id'=>$v['org_id'],
  645. 'type'=>2,
  646. 'start_time'=>$v['start_time'],
  647. 'create_time'=>getTime(),
  648. 'bus_id'=>$v['id'],
  649. 'user_id'=>$v['user_id']
  650. ];
  651. }
  652. }
  653. if(!empty($a2)){
  654. Db::name('task')
  655. ->insertAll($a2);
  656. }
  657. }
  658. }
  659. }