DailyTask.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. namespace app\common\model;
  3. use think\Exception;
  4. use think\Db;
  5. class DailyTask extends Base {
  6. public function updates(){
  7. $post = request()->post();
  8. $result = validate('DailyTask')->check($post,[]);
  9. if(true !== $result){
  10. $this->error = validate('DailyTask')->getError();
  11. return false;
  12. }
  13. if(!$post['addrs']){
  14. $this->error='任务地点不能为空';
  15. return false;
  16. }
  17. if(!$post['user_ids']){
  18. $this->error='检查人员不能为空';
  19. return false;
  20. }
  21. // if(strtotime($post['start_time']) < time()){
  22. // $this->error = '开始时间不能小于当前时间';
  23. // return false;
  24. // }
  25. if($post['start_time'] >= $post['end_time']){
  26. $this->error = '结束时间请大于开始时间';
  27. return false;
  28. }
  29. //添加操作
  30. if($post['id'] == 0){
  31. $post['addrs']=explode(',',$post['addrs']);
  32. $post['user_ids']=explode(',',$post['user_ids']);
  33. $curtime = date('Y-m-d H:i:s');
  34. $data = array(
  35. 'org_id' => cur_org_id(),
  36. 'title' => $post['title'],
  37. 'status' => 0,
  38. 'create_time' => $curtime,
  39. 'uuid' => new_guid(), //TODO::是否检测已重复
  40. );
  41. if(date('Y-m-d',strtotime($post['start_time'])) != date('Y-m-d',strtotime($post['end_time']))){ //跨天,执行连续天数为0
  42. $post['days'] = 0;
  43. }
  44. $timearr = $this->get_time_arr($post['start_time'],$post['end_time'],$post['hours'],$post['days']);
  45. Db::startTrans();
  46. try{
  47. foreach ($timearr as $k=>$v){
  48. $data['start_time'] = $v['start_time'];
  49. $data['end_time'] = $v['end_time'];
  50. $data['create_yyyymm'] = date('Ym',strtotime($data['start_time']));
  51. $data['create_yyyy'] = date('Y',strtotime($data['start_time']));
  52. $data['create_yyyymmdd'] = date('Ymd',strtotime($data['start_time']));
  53. //添加任务
  54. $taskid = Db::name('daily_task')->insertGetId($data);
  55. if(!$taskid){
  56. exception('创建任务失败');
  57. }
  58. //添加检查地点
  59. $addr = array();
  60. $post['addrs'] = array_unique($post['addrs']);
  61. foreach ($post['addrs'] as $k=>$v){
  62. $addr[] = array(
  63. 'task_id' => $taskid,
  64. 'daily_id' => $v
  65. );
  66. }
  67. $taskAddr=Db::name('daily_task_addr')->insertAll($addr);
  68. if(!$taskAddr){
  69. exception('任务地点保存失败');
  70. }
  71. //添加检查人员
  72. $user = array();
  73. $post['user_ids'] = array_unique($post['user_ids']);
  74. foreach ($post['user_ids'] as $k=>$v){
  75. $user[] = array(
  76. 'task_id' => $taskid,
  77. 'user_id' => $v
  78. );
  79. }
  80. $taskUser=Db::name('daily_task_user')->insertAll($user);
  81. if(!$taskUser){
  82. exception('任务地点保存失败');
  83. }
  84. }
  85. Db::commit();
  86. return true;
  87. }catch (\Exception $e){
  88. // 回滚事务
  89. $this->error = $e->getMessage();
  90. Db::rollback();
  91. return false;
  92. }
  93. }
  94. //编辑操作
  95. if($post['id'] > 0){
  96. $curtime = date('Y-m-d H:i:s');
  97. $data = array(
  98. 'title' => $post['title'],
  99. 'update_time' => $curtime,
  100. 'start_time' => $post['start_time'],
  101. 'end_time' => $post['end_time'],
  102. 'create_yyyymm' => date('Ym',strtotime($post['start_time'])),
  103. 'create_yyyy' => date('Y',strtotime($post['end_time'])),
  104. 'create_yyyymmdd' => date('Ymd',strtotime($post['start_time'])),
  105. );
  106. $taskInfo=$this->getTaskOne($post['id']);
  107. $addrIds=explode(',',$post['addrs']);
  108. $userIds=explode(',',$post['user_ids']);
  109. $addrnew = array_diff($addrIds,$taskInfo['old_addrs']);
  110. $addrdef = array_diff($taskInfo['old_addrs'],$addrIds);
  111. $usernew = array_diff($userIds,$taskInfo['old_user_ids']);
  112. $userdef = array_diff($taskInfo['old_user_ids'],$userIds);
  113. Db::startTrans();
  114. try {
  115. //编辑任务
  116. $res = Db::name('daily_task')->where('id',$post['id'])->update($data);
  117. if(!$res){
  118. exception('编辑任务失败');
  119. }
  120. //编辑检查地点
  121. if($addrdef){
  122. $delAddr=Db::name('daily_task_addr')->where('task_id',$post['id'])->whereIn('daily_id',$addrdef)->delete();
  123. if(!$delAddr){
  124. exception('地点编辑失败');
  125. }
  126. }
  127. if($addrnew){
  128. $baddr = array();
  129. foreach ($addrnew as $k=>$v){
  130. $baddr[] = array(
  131. 'task_id' => $post['id'],
  132. 'daily_id' => $v
  133. );
  134. }
  135. $addTaskAddr=Db::name('daily_task_addr')->insertAll($baddr);
  136. if(!$addTaskAddr){
  137. exception('地点编辑失败');
  138. }
  139. }
  140. //添加检查人员
  141. if($userdef){
  142. $delTaskUser=Db::name('daily_task_user')->where('task_id',$post['id'])->whereIn('user_id',$userdef)->delete();
  143. if(!$delTaskUser){
  144. \exception('检查人员编辑失败');
  145. }
  146. }
  147. if($usernew){
  148. $buser = array();
  149. foreach ($usernew as $k=>$v){
  150. $buser[] = array(
  151. 'task_id' => $post['id'],
  152. 'user_id' => $v
  153. );
  154. }
  155. $addTaskUser=Db::name('daily_task_user')->insertAll($buser);
  156. if(!$addTaskUser){
  157. \exception('检查人员编辑失败');
  158. }
  159. }
  160. Db::commit();
  161. return true;
  162. }catch (\Exception $e){
  163. $this->error = $e->getMessage();
  164. Db::rollback();
  165. return false;
  166. }
  167. }
  168. }
  169. /**
  170. * 格式化时间,获取时间数组
  171. * @param $starttime 开始时间
  172. * @param $endtime 结束时间
  173. * @param $hours 重复小时
  174. * @param $days 执行天数
  175. * @return array
  176. */
  177. private function get_time_arr($starttime,$endtime,$hours,$days){
  178. $onehour = 60*60;
  179. $oneday = 24*60*60;
  180. $start = strtotime($starttime);
  181. $end = strtotime($endtime);
  182. $ctime = $end - $start;
  183. $timearr = array();
  184. $timearr[] = array(
  185. 'start_time' => $starttime,
  186. 'end_time' => $endtime,
  187. );
  188. if($hours > 0){
  189. if($ctime > $hours*$onehour){
  190. $timearr = array(); //置空
  191. $i = $hours;
  192. while(true){
  193. $nend = $start + $i*$onehour;
  194. $nstart = $start + ($i-$hours)*$onehour;
  195. $timearr[] = array(
  196. 'start_time' => date('Y-m-d H:i',$nstart),
  197. 'end_time' => date('Y-m-d H:i',$nend),
  198. );
  199. if($nend >= $end){
  200. break;
  201. }
  202. $i += $hours;
  203. }
  204. }
  205. }
  206. if($days > 0){
  207. $timearray = $timearr;
  208. for ($i=1; $i<=$days;$i++){
  209. foreach ($timearray as $k=>$v){
  210. $timearr[] = array(
  211. 'start_time' => date('Y-m-d H:i',strtotime($v['start_time']) + $i*$oneday),
  212. 'end_time' => date('Y-m-d H:i',strtotime($v['end_time']) + $i*$oneday),
  213. );
  214. }
  215. }
  216. }
  217. return $timearr;
  218. }
  219. //获取巡查计划任务
  220. public function getTaskOne($taskId){
  221. $ret=Db::name('daily_task')->where('id',$taskId)->find();
  222. //获取计划任务检查组id
  223. $old_addrs=Db::name('daily_task_addr')
  224. ->where('task_id',$ret['id'])->select();
  225. foreach ($old_addrs as $k=>$v){
  226. $ids[$k]=$v['daily_id'];
  227. }
  228. $ret['old_addrs']=$ids;
  229. //获取计划任务检查人员
  230. $old_user_ids=Db::name('daily_task_user')->where('task_id',$ret['id'])->select();
  231. foreach ($old_user_ids as $k=>$v){
  232. $ids[$k]=$v['user_id'];
  233. }
  234. $ret['old_user_ids']=$ids;
  235. return $ret;
  236. }
  237. public function get_list_by_time($orgId,$start,$end){
  238. $map[] = ['org_id','=',$orgId];
  239. $map[] = ['del','=',0];
  240. $map[] = ['start_time','<',$end];
  241. $map[] = ['end_time','>=',$start];
  242. $list = $this->field('id,title,start_time,end_time,status')
  243. ->where($map)
  244. ->select();
  245. $list = $list?$list->toArray():[];
  246. foreach ($list as $k=>$v){
  247. $userList = Db::name('daily_task_user')
  248. ->alias('a')
  249. ->field('u.id,u.real_name')
  250. ->join('user u','u.id = a.user_id')
  251. ->where('a.task_id',$v['id'])
  252. ->column('real_name');
  253. $list[$k]['users'] = $userList?implode(',',$userList):'';
  254. }
  255. return $list?$list:array();
  256. }
  257. //分组获取任务计划最近12个月份
  258. public function get_task_month($org_id){
  259. $list = $this
  260. ->field('create_yyyymm')
  261. ->group('create_yyyymm')
  262. ->where('org_id',$org_id)
  263. ->where('del',0)
  264. ->order('create_yyyymm','desc')
  265. ->limit(12)
  266. ->select();
  267. $list = $list?$list->toArray():array();
  268. foreach ($list as $k=>$v){
  269. $list[$k]['create_yyyymm'] = date('Y-m',strtotime($v['create_yyyymm'].'01'));
  270. }
  271. return $list;
  272. }
  273. //复制任务
  274. public function plan_data($post,$org){ //TODO::已复制过的月份是否可以继续复制
  275. $from = strtotime($post['from'].'-01');
  276. $to = strtotime($post['to'].'-01');
  277. $max = strtotime(date('Y-m-d', strtotime($post['to'].'-01 +1 month')));
  278. $cha = $to - $from;
  279. $map[] = ['del','=',0];
  280. $map[] = ['org_id','=',$org];
  281. $map[] = ['create_yyyymm','=',date('Ym',strtotime($post['from'].'-01'))];
  282. $list = $this->where($map)->select();
  283. if(!$list){
  284. $this->error = '要复制的月份下没有任务';
  285. return false;
  286. }
  287. $list = $list->toArray();
  288. $this->startTrans();
  289. try{
  290. $uuid = '';
  291. while (true) {
  292. $uuid = new_guid();
  293. $ret = $this
  294. ->where('uuid',$uuid)
  295. ->find();
  296. if(!$ret){
  297. break;
  298. }
  299. }
  300. foreach ($list as $k=>$v){
  301. $start_time = strtotime($v['start_time'])+$cha;
  302. if($start_time >= $max){
  303. continue;
  304. }
  305. $v['start_time'] = date('Y-m-d H:i:s',$start_time);
  306. $v['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time'])+$cha);
  307. $v['uuid'] = $uuid;
  308. $ret = $this->copy_one($v);
  309. if(!$ret){
  310. \exception('复制失败');
  311. }
  312. }
  313. Db::commit();
  314. return true;
  315. }catch (Exception $e){
  316. $this->error = $e->getMessage();
  317. Db::rollback();
  318. return false;
  319. }
  320. }
  321. //复制单个任务
  322. private function copy_one($data){
  323. $id = $data['id'];
  324. unset($data['id']);
  325. $data['status'] = 0;
  326. $data['create_time'] = date('Y-m-d H:i:s');
  327. $data['create_yyyymm'] = date('Ym',strtotime($data['start_time']));
  328. $data['create_yyyy'] = date('Y',strtotime($data['start_time']));
  329. $data['create_yyyymmdd'] = date('Ymd',strtotime($data['start_time']));
  330. $addrs = $this->get_task_addr($id);
  331. $userids = $this->get_task_user($id);
  332. //添加任务
  333. $taskid = $this->insertGetId($data);
  334. if(!$taskid){
  335. return false;
  336. }
  337. //添加检查地点
  338. $addr = array();
  339. foreach ($addrs as $k=>$v){
  340. $addr[] = array(
  341. 'task_id' => $taskid,
  342. 'daily_id' => $v
  343. );
  344. }
  345. $res = Db::name('daily_task_addr')->insertAll($addr);
  346. if(!$res){
  347. return false;
  348. }
  349. //添加检查人员
  350. $user = array();
  351. foreach ($userids as $k=>$v){
  352. $user[] = array(
  353. 'task_id' => $taskid,
  354. 'user_id' => $v
  355. );
  356. }
  357. $res = Db::name('daily_task_user')->insertAll($user);
  358. if(!$res){
  359. return false;
  360. }
  361. return true;
  362. }
  363. //获取任务检查地点
  364. public function get_task_addr($taskid){
  365. $addrlist = Db::name('daily_task_addr')
  366. ->where('task_id',$taskid)
  367. ->column('daily_id');
  368. return $addrlist;
  369. }
  370. //获取任务检查人员
  371. public function get_task_user($taskid){
  372. $userlist = Db::name('daily_task_user')
  373. ->where('task_id',$taskid)
  374. ->column('user_id');
  375. return $userlist;
  376. }
  377. //定时处理超时任务
  378. public function timer_action(){
  379. $curTime = date('Y-m-d');
  380. $map[] = ['del','=',0];
  381. $map[] = ['status','in',[0,1]];
  382. $map[] = ['end_time','<',$curTime];
  383. $this->where($map)
  384. ->update(['status'=>3]);
  385. if(!empty($ids)){
  386. Db::name('task')
  387. ->where('type',3)
  388. ->where('bus_id','in',$ids)
  389. ->delete();
  390. }
  391. }
  392. public function addDailyTask(){
  393. $curTime = date('Y-m-d H:i:s',time()+12*60*60);
  394. $map[] = ['a.del','=',0];
  395. $map[] = ['a.status','=',0];
  396. $map[] = ['a.start_time','<',$curTime];
  397. $ids = $this
  398. ->alias('a')
  399. ->join('daily_task_user b','b.task_id=a.id')
  400. ->where($map)
  401. ->field('a.*,b.user_id')
  402. ->select();
  403. if(!empty($ids)){
  404. $ids = $ids->toArray();
  405. $a = [];
  406. foreach ($ids as $k=>$v){
  407. $check = Db::name('task')
  408. ->where('type',3)
  409. ->where('org_id',$v['org_id'])
  410. ->where('bus_id',$v['id'])
  411. ->where('user_id',$v['user_id'])
  412. ->find();
  413. if(empty($check)){
  414. $a[]= [
  415. 'org_id'=>$v['org_id'],
  416. 'type'=>3,
  417. 'start_time'=>$v['start_time'],
  418. 'create_time'=>getTime(),
  419. 'bus_id'=>$v['id'],
  420. 'user_id'=>$v['user_id']
  421. ];
  422. }
  423. }
  424. if(!empty($a)){
  425. Db::name('task')
  426. ->insertAll($a);
  427. }
  428. }
  429. }
  430. public function del($id,$userId){
  431. try{
  432. $ret = Db::name('daily_task')->where('id',$id)->update([
  433. 'del' => 1,
  434. 'del_user_id' => $userId,
  435. 'del_time' => getTime()
  436. ]);
  437. if(!$ret){
  438. \exception('删除失败');
  439. }
  440. Db::name('task')
  441. ->where('bus_id', $id)
  442. ->where('type', (new Task())::TASK_TYPE_DAILY)
  443. ->delete();
  444. }catch (\Exception $e){
  445. trace($e->getMessage());
  446. return false;
  447. }
  448. return true;
  449. }
  450. public function batchDel($orgId,$userId){
  451. $data = request()->post();
  452. $minmonth = date('Y-m');
  453. if($minmonth >= $data['from']){
  454. $this->error = '当月任务不能删除';
  455. return false;
  456. }
  457. $where = array(
  458. 'org_id' => $orgId,
  459. 'del' => 0,
  460. 'create_yyyymm' => date('Ym',strtotime($data['from'].'-01'))
  461. );
  462. $type = (new Task())::TASK_TYPE_DAILY;
  463. // 检查是否存在任务栏数据中
  464. $dailyIds = Db::name('daily_task')
  465. ->alias('dt')
  466. ->join('task t','t.bus_id = dt.id')
  467. ->where('t.type',$type)
  468. ->where('dt.org_id',$orgId)
  469. ->where('dt.del',0)
  470. ->where('dt.create_yyyymm',date('Ym',strtotime($data['from'].'-01')))
  471. ->column('dt.id');
  472. Db::startTrans();
  473. try{
  474. $default['del'] = 1;
  475. $default['del_user_id'] = $userId;
  476. $default['del_time'] = date('Y-m-d H:i:s');
  477. $result = Db::name('daily_task')
  478. ->where($where)
  479. ->update($default);
  480. if(!$result){
  481. \exception('操作失败');
  482. }
  483. if($dailyIds){
  484. $ret = Db::name('task')
  485. ->where('bus_id', 'in',implode(',',$dailyIds))
  486. ->where('type', (new Task())::TASK_TYPE_DAILY)
  487. ->delete();
  488. if(!$ret){
  489. \exception('操作失败');
  490. }
  491. }
  492. Db::commit();
  493. return true;
  494. }catch (\Exception $e){
  495. Db::rollback();
  496. return false;
  497. }
  498. }
  499. }