DeviceTask.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Exception;
  5. use think\Db;
  6. class DeviceTask extends Base {
  7. public function updates(){
  8. $post = request()->post();
  9. $result = validate('DeviceTask')->check($post,[]);
  10. if(true !== $result){
  11. $this->error = validate('DeviceTask')->getError();
  12. return false;
  13. }
  14. if(!$post['addrs']){
  15. $this->error='任务地点不能为空';
  16. return false;
  17. }
  18. if(!$post['user_ids']){
  19. $this->error='检查人员不能为空';
  20. return false;
  21. }
  22. // if(strtotime($post['start_time']) < time()){
  23. // $this->error = '开始时间不能小于当前时间';
  24. // return false;
  25. // }
  26. if($post['start_time'] >= $post['end_time']){
  27. $this->error = '结束时间请大于开始时间';
  28. return false;
  29. }
  30. //添加操作
  31. if($post['id'] == 0){
  32. $post['addrs']=explode(',',$post['addrs']);
  33. $post['user_ids']=explode(',',$post['user_ids']);
  34. $curtime = date('Y-m-d H:i:s');
  35. $data = array(
  36. 'org_id' => cur_org_id(),
  37. 'title' => $post['title'],
  38. 'status' => 0,
  39. 'create_time' => $curtime,
  40. 'cate_id' => $post['cate_id'],
  41. 'uuid' => new_guid(), //TODO::是否检测已重复
  42. );
  43. if(date('Y-m-d',strtotime($post['start_time'])) != date('Y-m-d',strtotime($post['end_time']))){ //跨天,执行连续天数为0
  44. $post['days'] = 0;
  45. }
  46. $timearr = $this->get_time_arr($post['start_time'],$post['end_time'],$post['hours'],$post['days']);
  47. Db::startTrans();
  48. try{
  49. foreach ($timearr as $k=>$v){
  50. $data['start_time'] = $v['start_time'];
  51. $data['end_time'] = $v['end_time'];
  52. $data['create_yyyymm'] = date('Ym',strtotime($data['start_time']));
  53. $data['create_yyyy'] = date('Y',strtotime($data['start_time']));
  54. $data['create_yyyymmdd'] = date('Ymd',strtotime($data['start_time']));
  55. //添加任务
  56. $taskid = Db::name('device_task')->insertGetId($data);
  57. if(!$taskid){
  58. exception('创建任务失败');
  59. }
  60. //添加检查地点
  61. $addr = array();
  62. $post['addrs'] = array_unique($post['addrs']);
  63. foreach ($post['addrs'] as $k=>$v){
  64. $addr[] = array(
  65. 'task_id' => $taskid,
  66. 'device_id' => $v
  67. );
  68. }
  69. $taskAddr=Db::name('device_task_addr')->insertAll($addr);
  70. if(!$taskAddr){
  71. exception('任务地点保存失败');
  72. }
  73. //添加检查人员
  74. $user = array();
  75. $post['user_ids'] = array_unique($post['user_ids']);
  76. foreach ($post['user_ids'] as $k=>$v){
  77. $user[] = array(
  78. 'task_id' => $taskid,
  79. 'user_id' => $v
  80. );
  81. }
  82. $taskUser=Db::name('device_task_user')->insertAll($user);
  83. if(!$taskUser){
  84. exception('任务地点保存失败');
  85. }
  86. }
  87. Db::commit();
  88. return true;
  89. }catch (\Exception $e){
  90. // 回滚事务
  91. $this->error = $e->getMessage();
  92. Db::rollback();
  93. return false;
  94. }
  95. }
  96. //编辑操作
  97. if($post['id'] > 0){
  98. $curtime = date('Y-m-d H:i:s');
  99. $data = array(
  100. 'title' => $post['title'],
  101. 'cate_id' => $post['cate_id'],
  102. 'update_time' => $curtime,
  103. 'start_time' => $post['start_time'],
  104. 'end_time' => $post['end_time'],
  105. 'create_yyyymm' => date('Ym',strtotime($post['start_time'])),
  106. 'create_yyyy' => date('Y',strtotime($post['end_time'])),
  107. 'create_yyyymmdd' => date('Ymd',strtotime($post['start_time'])),
  108. );
  109. $taskInfo=$this->getTaskOne($post['id']);
  110. $addrIds=explode(',',$post['addrs']);
  111. $userIds=explode(',',$post['user_ids']);
  112. $addrnew = array_diff($addrIds,$taskInfo['old_addrs']);
  113. $addrdef = array_diff($taskInfo['old_addrs'],$addrIds);
  114. $usernew = array_diff($userIds,$taskInfo['old_user_ids']);
  115. $userdef = array_diff($taskInfo['old_user_ids'],$userIds);
  116. Db::startTrans();
  117. try {
  118. //编辑任务
  119. $res = Db::name('device_task')->where('id',$post['id'])->update($data);
  120. if(!$res){
  121. exception('编辑任务失败');
  122. }
  123. //编辑检查地点
  124. if($addrdef){
  125. $delAddr=Db::name('device_task_addr')->where('task_id',$post['id'])->whereIn('device_id',$addrdef)->delete();
  126. if(!$delAddr){
  127. exception('地点编辑失败');
  128. }
  129. }
  130. if($addrnew){
  131. $baddr = array();
  132. foreach ($addrnew as $k=>$v){
  133. $baddr[] = array(
  134. 'task_id' => $post['id'],
  135. 'device_id' => $v
  136. );
  137. }
  138. $addTaskAddr=Db::name('device_task_addr')->insertAll($baddr);
  139. if(!$addTaskAddr){
  140. exception('地点编辑失败');
  141. }
  142. }
  143. //添加检查人员
  144. if($userdef){
  145. $delTaskUser=Db::name('device_task_user')->where('task_id',$post['id'])->whereIn('user_id',$userdef)->delete();
  146. if(!$delTaskUser){
  147. \exception('检查人员编辑失败');
  148. }
  149. }
  150. if($usernew){
  151. $buser = array();
  152. foreach ($usernew as $k=>$v){
  153. $buser[] = array(
  154. 'task_id' => $post['id'],
  155. 'user_id' => $v
  156. );
  157. }
  158. $addTaskUser=Db::name('device_task_user')->insertAll($buser);
  159. if(!$addTaskUser){
  160. \exception('检查人员编辑失败');
  161. }
  162. }
  163. Db::commit();
  164. return true;
  165. }catch (\Exception $e){
  166. $this->error = $e->getMessage();
  167. Db::rollback();
  168. return false;
  169. }
  170. }
  171. }
  172. /**
  173. * 格式化时间,获取时间数组
  174. * @param $starttime 开始时间
  175. * @param $endtime 结束时间
  176. * @param $hours 重复小时
  177. * @param $days 执行天数
  178. * @return array
  179. */
  180. private function get_time_arr($starttime,$endtime,$hours,$days){
  181. $onehour = 60*60;
  182. $oneday = 24*60*60;
  183. $start = strtotime($starttime);
  184. $end = strtotime($endtime);
  185. $ctime = $end - $start;
  186. $timearr = array();
  187. $timearr[] = array(
  188. 'start_time' => $starttime,
  189. 'end_time' => $endtime,
  190. );
  191. if($hours > 0){
  192. if($ctime > $hours*$onehour){
  193. $timearr = array(); //置空
  194. $i = $hours;
  195. while(true){
  196. $nend = $start + $i*$onehour;
  197. $nstart = $start + ($i-$hours)*$onehour;
  198. $timearr[] = array(
  199. 'start_time' => date('Y-m-d H:i',$nstart),
  200. 'end_time' => date('Y-m-d H:i',$nend),
  201. );
  202. if($nend >= $end){
  203. break;
  204. }
  205. $i += $hours;
  206. }
  207. }
  208. }
  209. if($days > 0){
  210. $timearray = $timearr;
  211. for ($i=1; $i<=$days;$i++){
  212. foreach ($timearray as $k=>$v){
  213. $timearr[] = array(
  214. 'start_time' => date('Y-m-d H:i',strtotime($v['start_time']) + $i*$oneday),
  215. 'end_time' => date('Y-m-d H:i',strtotime($v['end_time']) + $i*$oneday),
  216. );
  217. }
  218. }
  219. }
  220. return $timearr;
  221. }
  222. //获取巡查计划任务
  223. public function getTaskOne($taskId){
  224. $ret=Db::name('device_task')->where('id',$taskId)->find();
  225. //获取计划任务检查组id
  226. $old_addrs=Db::name('device_task_addr')
  227. ->where('task_id',$ret['id'])->select();
  228. foreach ($old_addrs as $k=>$v){
  229. $ids[$k]=$v['device_id'];
  230. }
  231. $ret['old_addrs']=$ids;
  232. //获取计划任务检查人员
  233. $old_user_ids = Db::name('device_task_user')->where('task_id',$ret['id'])->column('user_id');
  234. // foreach ($old_user_ids as $k=>$v){
  235. // $ids[$k]=$v['user_id'];
  236. // }
  237. $ret['old_user_ids']=$old_user_ids;
  238. return $ret;
  239. }
  240. //分组获取任务计划最近12个月份
  241. public function get_task_month($org_id){
  242. $list = $this
  243. ->field('create_yyyymm')
  244. ->group('create_yyyymm')
  245. ->where('org_id',$org_id)
  246. ->where('del',0)
  247. ->order('create_yyyymm','desc')
  248. ->limit(12)
  249. ->select();
  250. $list = $list?$list->toArray():array();
  251. foreach ($list as $k=>$v){
  252. $list[$k]['create_yyyymm'] = date('Y-m',strtotime($v['create_yyyymm'].'01'));
  253. }
  254. return $list;
  255. }
  256. //定时处理超时任务
  257. public function timer_action(){
  258. $curTime = date('Y-m-d H:i:s');
  259. $map[] = ['del','=',0];
  260. $map[] = ['status','in',[0,1]];
  261. $map[] = ['end_time','<',$curTime];
  262. $this->where($map)
  263. ->update(['status'=>3]);
  264. $ids = Db::name('task')
  265. ->alias('a')
  266. ->join('device_task b','a.bus_id=b.id')
  267. ->where('b.end_time','<',$curTime)
  268. ->where('a.type',(new Task())::TASK_TYPE_DEVICE)
  269. ->column('a.id');
  270. if(!empty($ids)){
  271. Db::name('task')
  272. ->where('id','in',$ids)
  273. ->delete();
  274. }
  275. }
  276. public function addDeviceTask(){
  277. $curTime = date('Y-m-d H:i:s',time()+12*60*60);
  278. $map[] = ['a.del','=',0];
  279. $map[] = ['a.status','=',0];
  280. $map[] = ['a.start_time','<',$curTime];
  281. $ids = $this
  282. ->alias('a')
  283. ->join('device_task_user b','b.task_id=a.id')
  284. ->where($map)
  285. ->field('a.*,b.user_id')
  286. ->select();
  287. if(!empty($ids)){
  288. $ids = $ids->toArray();
  289. $a = [];
  290. foreach ($ids as $k=>$v){
  291. $check = Db::name('task')
  292. ->where('type',(new Task())::TASK_TYPE_DEVICE)
  293. ->where('org_id',$v['org_id'])
  294. ->where('bus_id',$v['id'])
  295. ->where('user_id',$v['user_id'])
  296. ->find();
  297. if(empty($check)){
  298. $a[]= [
  299. 'org_id'=>$v['org_id'],
  300. 'type'=>(new Task())::TASK_TYPE_DEVICE,
  301. 'start_time'=>$v['start_time'],
  302. 'create_time'=>getTime(),
  303. 'bus_id'=>$v['id'],
  304. 'user_id'=>$v['user_id']
  305. ];
  306. }
  307. }
  308. if(!empty($a)){
  309. Db::name('task')
  310. ->insertAll($a);
  311. }
  312. }
  313. }
  314. public function get_list_by_time($orgId,$start,$end){
  315. $map[] = ['org_id','=',$orgId];
  316. $map[] = ['del','=',0];
  317. $map[] = ['start_time','<',$end];
  318. $map[] = ['end_time','>=',$start];
  319. $list = $this->field('id,title,start_time,end_time,status')
  320. ->where($map)
  321. ->select();
  322. $list = $list?$list->toArray():[];
  323. foreach ($list as $k=>$v){
  324. $userList = Db::name('device_task_user')
  325. ->alias('a')
  326. ->field('u.id,u.real_name')
  327. ->join('user u','u.id = a.user_id')
  328. ->where('a.task_id',$v['id'])
  329. ->column('real_name');
  330. $list[$k]['users'] = $userList?implode(',',$userList):'';
  331. }
  332. return $list?$list:array();
  333. }
  334. //复制任务
  335. public function plan_data($post,$org){ //TODO::已复制过的月份是否可以继续复制
  336. $from = strtotime($post['from'].'-01');
  337. $to = strtotime($post['to'].'-01');
  338. $max = strtotime(date('Y-m-d', strtotime($post['to'].'-01 +1 month')));
  339. $cha = $to - $from;
  340. $map[] = ['del','=',0];
  341. $map[] = ['org_id','=',$org];
  342. $map[] = ['create_yyyymm','=',date('Ym',strtotime($post['from'].'-01'))];
  343. $list = $this->where($map)->select();
  344. if(!$list){
  345. $this->error = '要复制的月份下没有任务';
  346. return false;
  347. }
  348. $list = $list->toArray();
  349. $this->startTrans();
  350. try{
  351. $uuid = '';
  352. while (true) {
  353. $uuid = new_guid();
  354. $ret = $this
  355. ->where('uuid',$uuid)
  356. ->find();
  357. if(!$ret){
  358. break;
  359. }
  360. }
  361. foreach ($list as $k=>$v){
  362. $start_time = strtotime($v['start_time'])+$cha;
  363. if($start_time >= $max){
  364. continue;
  365. }
  366. $v['start_time'] = date('Y-m-d H:i:s',$start_time);
  367. $v['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time'])+$cha);
  368. $v['uuid'] = $uuid;
  369. $ret = $this->copy_one($v);
  370. if(!$ret){
  371. \exception('复制失败');
  372. }
  373. }
  374. Db::commit();
  375. return true;
  376. }catch (Exception $e){
  377. $this->error = $e->getMessage();
  378. Db::rollback();
  379. return false;
  380. }
  381. }
  382. //复制单个任务
  383. private function copy_one($data){
  384. $id = $data['id'];
  385. unset($data['id']);
  386. $data['status'] = 0;
  387. $data['create_time'] = date('Y-m-d H:i:s');
  388. $data['create_yyyymm'] = date('Ym',strtotime($data['start_time']));
  389. $data['create_yyyy'] = date('Y',strtotime($data['start_time']));
  390. $data['create_yyyymmdd'] = date('Ymd',strtotime($data['start_time']));
  391. $addrs = $this->get_task_addr($id);
  392. $userids = $this->get_task_user($id);
  393. //添加任务
  394. $taskid = $this->insertGetId($data);
  395. if(!$taskid){
  396. return false;
  397. }
  398. //添加检查地点
  399. $addr = array();
  400. foreach ($addrs as $k=>$v){
  401. $addr[] = array(
  402. 'task_id' => $taskid,
  403. 'device_id' => $v
  404. );
  405. }
  406. $res = Db::name('device_task_addr')->insertAll($addr);
  407. if(!$res){
  408. return false;
  409. }
  410. //添加检查人员
  411. $user = array();
  412. foreach ($userids as $k=>$v){
  413. $user[] = array(
  414. 'task_id' => $taskid,
  415. 'user_id' => $v
  416. );
  417. }
  418. $res = Db::name('device_task_user')->insertAll($user);
  419. if(!$res){
  420. return false;
  421. }
  422. return true;
  423. }
  424. //获取任务检查地点
  425. public function get_task_addr($taskid){
  426. $addrlist = Db::name('device_task_addr')
  427. ->where('task_id',$taskid)
  428. ->column('device_id');
  429. return $addrlist;
  430. }
  431. //获取任务检查人员
  432. public function get_task_user($taskid){
  433. $userlist = Db::name('device_task_user')
  434. ->where('task_id',$taskid)
  435. ->column('user_id');
  436. return $userlist;
  437. }
  438. public function del($id,$userId){
  439. try{
  440. $ret = Db::name('device_task')->where('id',$id)->update([
  441. 'del' => 1,
  442. 'del_user_id' => $userId,
  443. 'del_time' => getTime()
  444. ]);
  445. if(!$ret){
  446. \exception('删除失败');
  447. }
  448. Db::name('task')
  449. ->where('bus_id', $id)
  450. ->where('type', (new Task())::TASK_TYPE_DEVICE)
  451. ->delete();
  452. }catch (\Exception $e){
  453. trace($e->getMessage());
  454. return false;
  455. }
  456. return true;
  457. }
  458. public function byIdSDel($ids,$userId){
  459. $list = Db::name('device_task')->where('id','in',$ids)
  460. ->where('status',1)
  461. ->select();
  462. if(!empty($list)){
  463. $this->error ='正在进行中的计划无法删除';
  464. return false;
  465. }
  466. // $ids = array_column($list,'id');
  467. try{
  468. $ret = Db::name('device_task')->where('id','in',$ids)->update([
  469. 'del' => 1,
  470. 'del_user_id' => $userId,
  471. 'del_time' => getTime()
  472. ]);
  473. if(!$ret){
  474. \exception('删除失败');
  475. }
  476. Db::name('device_record')->whereIn('task_id',$ids)->delete();
  477. Db::name('task')
  478. ->where('bus_id','in', $ids)
  479. ->where('type', (new Task())::TASK_TYPE_DEVICE)
  480. ->delete();
  481. return true;
  482. }catch (\Exception $e){
  483. trace($e->getMessage());
  484. $this->error = $e->getMessage();
  485. return false;
  486. }
  487. return true;
  488. }
  489. public function batchDel($orgId,$userId){
  490. $data = request()->post();
  491. $minmonth = date('Y-m');
  492. if($minmonth >= $data['from']){
  493. $this->error = '当月任务不能删除';
  494. return false;
  495. }
  496. $where = array(
  497. 'org_id' => $orgId,
  498. 'del' => 0,
  499. 'create_yyyymm' => date('Ym',strtotime($data['from'].'-01'))
  500. );
  501. $type = (new Task())::TASK_TYPE_DEVICE;
  502. // 检查是否存在任务栏数据中
  503. $dailyIds = Db::name('device_task')
  504. ->alias('dt')
  505. ->join('task t','t.bus_id = dt.id')
  506. ->where('t.type',$type)
  507. ->where('dt.org_id',$orgId)
  508. ->where('dt.del',0)
  509. ->where('dt.create_yyyymm',date('Ym',strtotime($data['from'].'-01')))
  510. ->column('dt.id');
  511. Db::startTrans();
  512. try{
  513. $default['del'] = 1;
  514. $default['del_user_id'] = $userId;
  515. $default['del_time'] = date('Y-m-d H:i:s');
  516. $result = Db::name('device_task')
  517. ->where($where)
  518. ->update($default);
  519. if(!$result){
  520. \exception('操作失败');
  521. }
  522. if($dailyIds){
  523. $ret = Db::name('task')
  524. ->where('bus_id', 'in',implode(',',$dailyIds))
  525. ->where('type', (new Task())::TASK_TYPE_DEVICE)
  526. ->delete();
  527. if(!$ret){
  528. \exception('操作失败');
  529. }
  530. }
  531. Db::commit();
  532. return true;
  533. }catch (\Exception $e){
  534. Db::rollback();
  535. return false;
  536. }
  537. }
  538. public function delayTask($taskId,$endTime,$reason,$userId,$orgId){
  539. if($taskId < 0){
  540. HelpHander::error('参数错误');
  541. }
  542. if(!$endTime || date('Y-m-d H:i',strtotime($endTime)) != $endTime){
  543. HelpHander::error('未选择延期时间');
  544. }
  545. if(strtotime($endTime) <= time()){
  546. HelpHander::error('延期时间必须大于当前时间');
  547. }
  548. if(!$reason){
  549. HelpHander::error('未填写延期原因');
  550. }
  551. $task = Db::name('device_task')->where('org_id',$orgId)->where('id',$taskId)->where('del',0)->find();
  552. if(!$task){
  553. HelpHander::error('参数错误');
  554. }
  555. if($task['status'] != 3){
  556. HelpHander::error('只有未完成的任务可申请延期');
  557. }
  558. $ret = Db::name('device_task_user')->where('task_id',$taskId)->where('user_id',$userId)->find();
  559. if(!$ret){
  560. HelpHander::error('无权限操作');
  561. }
  562. // 检测是否有正在审核中的延期申请
  563. $ret = Db::name('device_task_delay')->where('task_id',$taskId)->where('status',0)->find();
  564. if($ret){
  565. HelpHander::error('该任务已存在未处理的延期申请');
  566. }
  567. $ret = Db::name('device_task_delay')->insert([
  568. 'org_id' => $orgId,
  569. 'user_id' => $userId,
  570. 'task_id' => $taskId,
  571. 'end_time' => $endTime,
  572. 'reason' => $reason,
  573. 'status' => 0,
  574. 'create_time' => date('Y-m-d H:i:s')
  575. ]);
  576. if(!$ret){
  577. HelpHander::error('操作失败');
  578. }
  579. return true;
  580. }
  581. public function cancelDelay($id,$userId){
  582. if($id < 0){
  583. HelpHander::error('参数错误');
  584. }
  585. $ret = Db::name('device_task_delay')->where('id',$id)->where('user_id',$userId)->find();
  586. if(!$ret){
  587. HelpHander::error('记录不存在');
  588. }
  589. if($ret['status'] != 0){
  590. HelpHander::error('申请已处理,无法取消');
  591. }
  592. $res = Db::name('device_task_delay')->where('id',$id)->update(['status'=>3,'update_time'=>date('Y-m-d H:i:s')]);
  593. if(!$res){
  594. HelpHander::error('操作失败');
  595. }
  596. return true;
  597. }
  598. public function delayLists($page,$size,$userId,$orgId){
  599. $record = Db::name('device_task_delay')
  600. ->where('user_id',$userId)
  601. ->where('org_id',$orgId)
  602. ->order('id desc')
  603. ->page($page,$size)
  604. ->select();
  605. $record = $record?$record:[];
  606. foreach ($record as $k=>$v){
  607. $taskTitle = Db::name('device_task')->where('id',$v['task_id'])->value('title');
  608. $record[$k]['task_title'] = $taskTitle?$taskTitle:'';
  609. $uname = Db::name('user')->where('id',$v['user_id'])->value('real_name');
  610. $record[$k]['uname'] = $uname?$uname:'';
  611. $dealUser = '';
  612. if($v['deal_user_id'] > 0){
  613. $dealUser = Db::name('user')->where('id',$v['deal_user_id'])->value('real_name');
  614. }
  615. $record[$k]['deal_user'] = $dealUser?$dealUser:'';
  616. $record[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
  617. $record[$k]['update_time'] = $v['update_time']?$v['update_time']:'';
  618. }
  619. return $record;
  620. }
  621. public function dealDelay($userId,$orgId){
  622. $id = input('id/d',0);
  623. $status = input('status/d',0);
  624. $endTime = input('end_time','');
  625. $remark = input('remark','','trim');
  626. if(!in_array($status,[1,2]) || $id <= 0){
  627. $this->error = "参数错误";
  628. return false;
  629. }
  630. if($status == 1 && !$endTime){
  631. $this->error = "延期时间不能为空";
  632. return false;
  633. }
  634. $info = Db::name('device_task_delay')->where('id',$id)->where('org_id',$orgId)->find();
  635. if(!$info){
  636. $this->error = "记录不存在";
  637. return false;
  638. }
  639. if($info['status'] != 0){
  640. $this->error = "申请已处理";
  641. return false;
  642. }
  643. if($status == 1 && strtotime($endTime) <= time()){
  644. $this->error = "延期时间不能小于当前时间";
  645. // Db::name('device_task_delay')->where('id',$id)->update(['status'=>3,'update_time'=>date('Y-m-d H:i:s')]);
  646. return false;
  647. }
  648. Db::startTrans();
  649. try{
  650. $ret = Db::name('device_task_delay')->where('id',$id)->update([
  651. 'deal_user_id'=>$userId,
  652. 'status'=>$status,
  653. 'remark'=>$remark,
  654. 'end_time'=>$endTime,
  655. 'update_time'=>date('Y-m-d H:i:s')
  656. ]);
  657. if(!$ret){
  658. \exception('操作失败');
  659. }
  660. if($status == 1){
  661. $task = Db::name('device_task')->where('id',$info['task_id'])->find();
  662. // 恢复计划任务
  663. $res = Db::name('device_task')->where('id',$info['task_id'])->update([
  664. 'status' => 1,
  665. 'del' => 0,
  666. 'end_time' => $info['end_time']
  667. ]);
  668. if(!$res){
  669. \exception('操作失败');
  670. }
  671. $userIds = Db::name('device_task_user')->where('task_id',$info['task_id'])->column('user_id');
  672. $arr = [];
  673. foreach ($userIds as $v){
  674. $arr[] = [
  675. 'org_id'=>$orgId,
  676. 'type'=>(new Task())::TASK_TYPE_DEVICE,
  677. 'start_time' => $task['start_time'],
  678. 'create_time'=>getTime(),
  679. 'bus_id'=>$task['id'],
  680. 'user_id'=>$v
  681. ];
  682. }
  683. $res = Db::name('task')->insertAll($arr);
  684. if($res != count($userIds)){
  685. \exception('操作失败');
  686. }
  687. }
  688. Db::commit();
  689. }catch (Exception $e){
  690. Db::rollback();
  691. $this->error = $e->getMessage();
  692. return false;
  693. }
  694. return true;
  695. }
  696. public function copyTask($data){
  697. $ret = $this->copy_one($data);
  698. return $ret;
  699. }
  700. }