PatrolTask.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  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,end_time,patrol_form,in_order')
  293. ->where('id',$taskId)
  294. ->find();
  295. $patrolRecord = Db::name('patrol_record')
  296. ->field('id as recordId,images,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. $user = Db::name('patrol_task_user')
  328. ->alias('ptu')
  329. ->field('u.real_name')
  330. ->join('patrol_task pt','ptu.patrol_task_id=pt.id')
  331. ->join('user u','u.id=ptu.user_id')
  332. ->where('pt.id',$taskId)
  333. ->select();
  334. $ret['users']=$user?$user:'';
  335. return $ret;
  336. }
  337. public function forms($patrolAddrFormId){
  338. $forms = Db::name('patrol_task_addr')->where('id',$patrolAddrFormId)->value('patrol_forms');
  339. $ret = [];
  340. if($forms){
  341. $patrolForm = Db::name('patrol_addr_form')
  342. ->where('id','in',$forms)
  343. ->field('id,title,patrol_form')
  344. ->select();
  345. $patrolForm = $patrolForm?$patrolForm:[];
  346. foreach ($patrolForm as $k=>$v){
  347. $pfs = $v['patrol_form']?explode(',',$v['patrol_form']):[];
  348. $pfret = Db::name('patrol_form')
  349. ->whereIn('id',$pfs)
  350. ->select();
  351. $patrolForm[$k]['forms'] = $pfret;
  352. unset($patrolForm[$k]['patrol_form']);
  353. }
  354. $ret = $patrolForm;
  355. }
  356. return $ret;
  357. }
  358. public function updateStatus($patrolTaskId,$interruptReson,$interruptImg){
  359. if(!$interruptReson) {
  360. $this->error = '中断原因不能为空';
  361. return false;
  362. }
  363. $data=[
  364. 'status'=>5,
  365. 'interrupt_reson'=>$interruptReson,
  366. 'interrupt_img'=>$interruptImg,
  367. ];
  368. $update = Db::name('patrol_task')
  369. ->where('id',$patrolTaskId)
  370. ->update($data);
  371. return $update;
  372. }
  373. /**
  374. * 巡更人员工作量统计
  375. *
  376. * @author wst
  377. * @date 2021/4/13 9:10
  378. */
  379. public function WorkerPatrol($orgId,$search,$model=1){
  380. $org_id = $orgId;
  381. $query = Db::name('patrol_record');
  382. if(!empty($search['start'])){
  383. $search['start'] = date('Ymd',strtotime($search['start']));
  384. $query->where('patrol_record.create_yyyymmdd','>=', $search['start']);
  385. }
  386. if(!empty($search['end'])){
  387. $search['end'] = date('Ymd',strtotime($search['end']));
  388. $query->where('patrol_record.create_yyyymmdd','<=', $search['end']);
  389. }
  390. $query->where('patrol_record.patrol_mode', $model);
  391. $query->where('patrol_record.org_id',$org_id);
  392. $query->group('patrol_record.user_id');
  393. $query->join('user','user.id = patrol_record.user_id');
  394. $query->field('patrol_record.id,patrol_record.user_id,user.nickname');
  395. $list =$query->select();
  396. $list = $list?$list:array();
  397. foreach ($list as $k=>$v){
  398. $query1 = Db::name('patrol_record');
  399. $query1->where('org_id',$org_id);
  400. $query1->where('user_id',$v['user_id']);
  401. if(!empty($search['start'])){
  402. $search['start'] = date('Ymd',strtotime($search['start']));
  403. $query1->where('create_yyyymmdd','>=', $search['start']);
  404. }
  405. if(!empty($search['end'])){
  406. $search['end'] = date('Ymd',strtotime($search['end']));
  407. $query1->where('create_yyyymmdd','<=', $search['end']);
  408. }
  409. $query1->where('patrol_mode', $model);
  410. $count = $query1->count();
  411. $list[$k]['count'] = $count?$count:0;
  412. }
  413. return $list;
  414. }
  415. public function get_list_by_time($orgId,$mode,$start,$end){
  416. $map[] = ['org_id','=',$orgId];
  417. $map[] = ['del','=',0];
  418. $map[] = ['patrol_mode','=',$mode];
  419. $map[] = ['start_time','<',$end];
  420. $map[] = ['end_time','>=',$start];
  421. $list = $this->field('id,title,start_time,end_time,status')
  422. ->where($map)
  423. ->select();
  424. $list = $list?$list->toArray():[];
  425. foreach ($list as $k=>$v){
  426. $userList = Db::name('patrol_task_user')
  427. ->alias('a')
  428. ->field('u.id,u.real_name')
  429. ->join('user u','u.id = a.user_id')
  430. ->where('a.patrol_task_id',$v['id'])
  431. ->column('real_name');
  432. $list[$k]['users'] = $userList?implode(',',$userList):'';
  433. }
  434. return $list?$list:array();
  435. }
  436. //分组获取任务计划最近12个月份
  437. public function get_task_month($org_id,$mode){
  438. $list = $this
  439. ->field('create_yyyymm')
  440. ->group('create_yyyymm')
  441. ->where('org_id',$org_id)
  442. ->where('del',0)
  443. ->where('patrol_mode',$mode)
  444. ->order('create_yyyymm','desc')
  445. ->limit(12)
  446. ->select();
  447. $list = $list?$list->toArray():array();
  448. foreach ($list as $k=>$v){
  449. $list[$k]['create_yyyymm'] = date('Y-m',strtotime($v['create_yyyymm'].'01'));
  450. }
  451. return $list;
  452. }
  453. //复制任务
  454. public function plan_data($post,$org){ //TODO::已复制过的月份是否可以继续复制
  455. $from = strtotime($post['from'].'-01');
  456. $to = strtotime($post['to'].'-01');
  457. $max = strtotime(date('Y-m-d', strtotime($post['to'].'-01 +1 month')));
  458. $cha = $to - $from;
  459. $map[] = ['del','=',0];
  460. $map[] = ['org_id','=',$org];
  461. $map[] = ['patrol_mode','=',$post['mode']];
  462. $map[] = ['create_yyyymm','=',date('Ym',strtotime($post['from'].'-01'))];
  463. $list = $this->where($map)->select();
  464. if(!$list){
  465. $this->error = '要复制的月份下没有任务';
  466. return false;
  467. }
  468. $list = $list->toArray();
  469. $this->startTrans();
  470. try{
  471. $uuid = '';
  472. while (true) {
  473. $uuid = new_guid();
  474. $ret = $this
  475. ->where('uuid',$uuid)
  476. ->find();
  477. if(!$ret){
  478. break;
  479. }
  480. }
  481. foreach ($list as $k=>$v){
  482. $start_time = strtotime($v['start_time'])+$cha;
  483. if($start_time >= $max){
  484. continue;
  485. }
  486. $v['start_time'] = date('Y-m-d H:i:s',$start_time);
  487. $v['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time'])+$cha);
  488. $v['uuid'] = $uuid;
  489. $ret = $this->copy_one($v);
  490. if(!$ret){
  491. \exception('复制失败');
  492. }
  493. }
  494. $this->commit();
  495. return true;
  496. }catch (Exception $e){
  497. $this->db->trans_rollback();
  498. $this->error = $e->getMessage();
  499. return false;
  500. }
  501. }
  502. //复制单个任务
  503. private function copy_one($data){
  504. $id = $data['id'];
  505. unset($data['id']);
  506. $data['status'] = 0;
  507. $data['create_time'] = date('Y-m-d H:i:s');
  508. $data['create_yyyymm'] = date('Ym',strtotime($data['start_time']));
  509. $data['create_yyyy'] = date('Y',strtotime($data['start_time']));
  510. $data['create_yyyymmdd'] = date('Ymd',strtotime($data['start_time']));
  511. $addrs = $this->get_task_addr($id);
  512. $userids = $this->get_task_user($id);
  513. //添加任务
  514. $taskid = $this->insertGetId($data);
  515. if(!$taskid){
  516. return false;
  517. }
  518. //添加检查地点
  519. $addr = array();
  520. foreach ($addrs as $k=>$v){
  521. $addr[] = array(
  522. 'patrol_task_id' => $taskid,
  523. 'patrol_addr_form_id' => $v
  524. );
  525. }
  526. $res = Db::name('patrol_task_addr')->insertAll($addr);
  527. if(!$res){
  528. return false;
  529. }
  530. //添加检查人员
  531. $user = array();
  532. foreach ($userids as $k=>$v){
  533. $user[] = array(
  534. 'patrol_task_id' => $taskid,
  535. 'user_id' => $v
  536. );
  537. }
  538. $res = Db::name('patrol_task_user')->insertAll($user);
  539. if(!$res){
  540. return false;
  541. }
  542. return true;
  543. }
  544. //获取任务检查地点
  545. public function get_task_addr($taskid){
  546. $addrlist = Db::name('patrol_task_addr')
  547. ->where('patrol_task_id',$taskid)
  548. ->column('patrol_addr_form_id');
  549. return $addrlist;
  550. }
  551. //获取任务检查人员
  552. public function get_task_user($taskid){
  553. $userlist = Db::name('patrol_task_user')
  554. ->where('patrol_task_id',$taskid)
  555. ->column('user_id');
  556. return $userlist;
  557. }
  558. //定时处理超时任务
  559. public function timer_action(){
  560. $curTime = date('Y-m-d H:i:s');
  561. $map[] = ['del','=',0];
  562. $map[] = ['status','in',[0,1]];
  563. $map[] = ['end_time','<',$curTime];
  564. $this->where($map)
  565. ->update(['status'=>3]);
  566. $ids = Db::name('task')
  567. ->alias('a')
  568. ->join('patrol_task b','a.bus_id=b.id')
  569. ->where('b.end_time','<',$curTime)
  570. ->where('a.type',2)
  571. ->column('a.id');
  572. if(!empty($ids)){
  573. Db::name('task')
  574. ->where('id','in',$ids)
  575. ->delete();
  576. }
  577. }
  578. public function addPatrolTask(){
  579. $curTime = date('Y-m-d H:i:s',time()+8*60*60);
  580. $map[] = ['a.del','=',0];
  581. $map[] = ['a.status','=',0];
  582. $map[] = ['a.start_time','<',$curTime];
  583. $ids = $this
  584. ->alias('a')
  585. ->join('patrol_task_user b','b.patrol_task_id=a.id')
  586. ->where($map)
  587. ->whereIn('a.patrol_mode',[1,2,3])
  588. ->field('a.*,b.user_id')
  589. ->select();
  590. if(!empty($ids)){
  591. $ids = $ids->toArray();
  592. $a = [];
  593. foreach ($ids as $k=>$v){
  594. $check = Db::name('task')
  595. ->where('type',2)
  596. ->where('org_id',$v['org_id'])
  597. ->where('bus_id',$v['id'])
  598. ->where('user_id',$v['user_id'])
  599. ->find();
  600. if(empty($check)){
  601. $a[]= [
  602. 'org_id'=>$v['org_id'],
  603. 'type'=>2,
  604. 'start_time'=>$v['start_time'],
  605. 'create_time'=>getTime(),
  606. 'bus_id'=>$v['id'],
  607. 'user_id'=>$v['user_id']
  608. ];
  609. }
  610. }
  611. if(!empty($a)){
  612. Db::name('task')
  613. ->insertAll($a);
  614. }
  615. }
  616. //巡检类型
  617. $curTime2 = date('Y-m-d H:i:s',time()+10*60);
  618. $map2[] = ['a.del','=',0];
  619. $map2[] = ['a.status','=',0];
  620. $map2[] = ['a.start_time','<',$curTime2];
  621. $ids2 = $this
  622. ->alias('a')
  623. ->join('patrol_task_user b','b.patrol_task_id=a.id')
  624. ->where($map2)
  625. ->where('a.patrol_mode',4)
  626. ->field('a.*,b.user_id')
  627. ->select();
  628. if(!empty($ids2)){
  629. $ids2 = $ids2->toArray();
  630. $a2 = [];
  631. foreach ($ids2 as $k=>$v){
  632. $check2 = Db::name('task')
  633. ->where('type',2)
  634. ->where('org_id',$v['org_id'])
  635. ->where('bus_id',$v['id'])
  636. ->where('user_id',$v['user_id'])
  637. ->find();
  638. if(empty($check2)){
  639. $a2[]= [
  640. 'org_id'=>$v['org_id'],
  641. 'type'=>2,
  642. 'start_time'=>$v['start_time'],
  643. 'create_time'=>getTime(),
  644. 'bus_id'=>$v['id'],
  645. 'user_id'=>$v['user_id']
  646. ];
  647. }
  648. }
  649. if(!empty($a2)){
  650. Db::name('task')
  651. ->insertAll($a2);
  652. }
  653. }
  654. }
  655. }