GreenTask.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <?php
  2. namespace app\common\model;
  3. use think\Exception;
  4. use think\Db;
  5. class GreenTask extends Base {
  6. public function updates(){
  7. $post = request()->post();
  8. $validate = new \app\common\validate\GreenTask();
  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 || $v['formId'] <= 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['end_time'])),
  50. 'create_yyyymmdd' => date('Ymd',strtotime($post['start_time'])),
  51. );
  52. Db::startTrans();
  53. try {
  54. //编辑任务
  55. $res = Db::name('green_task')->where('id',$post['id'])->update($data);
  56. if(!$res){
  57. exception('编辑任务失败');
  58. }
  59. Db::name('green_task_addr')->where('green_task_id',$post['id'])->delete();
  60. Db::name('green_task_user')->where('green_task_id',$post['id'])->delete();
  61. // 重新添加地点及表单
  62. $nforms = [];
  63. foreach ($addrs as $key=>$val){
  64. $nforms[] = [
  65. 'green_task_id' => $post['id'],
  66. 'green_addr_id' => $val['addrId'],
  67. 'green_form_id' => $val['formId']
  68. ];
  69. }
  70. $fromRet = Db::name('green_task_addr')->insertAll($nforms);
  71. if($fromRet != count($nforms)){
  72. \exception('操作失败');
  73. }
  74. // 重新添加用户
  75. $uds = [];
  76. $users = explode(',',$post['user_ids']);
  77. foreach ($users as $key=>$val){
  78. $uds[] = [
  79. 'green_task_id' => $post['id'],
  80. 'user_id' => $val
  81. ];
  82. }
  83. $userRet = Db::name('green_task_user')->insertAll($uds);
  84. if($userRet != count($uds)){
  85. \exception('操作失败');
  86. }
  87. Db::commit();
  88. return true;
  89. }catch (\Exception $e){
  90. $this->error = $e->getMessage();
  91. Db::rollback();
  92. return false;
  93. }
  94. }
  95. /**
  96. * 格式化时间,获取时间数组
  97. * @param $starttime 开始时间
  98. * @param $endtime 结束时间
  99. * @param $hours 重复小时
  100. * @param $days 执行天数
  101. * @return array
  102. */
  103. private function get_time_arr($starttime,$endtime,$hours,$days){
  104. $onehour = 60*60;
  105. $oneday = 24*60*60;
  106. $start = strtotime($starttime);
  107. $end = strtotime($endtime);
  108. $ctime = $end - $start;
  109. $timearr = array();
  110. $timearr[] = array(
  111. 'start_time' => $starttime,
  112. 'end_time' => $endtime,
  113. );
  114. if($hours > 0){
  115. if($ctime > $hours * $onehour){
  116. $timearr = array(); //置空
  117. $i = $hours;
  118. while(true){
  119. $nend = $start + $i*$onehour;
  120. $nstart = $start + ($i-$hours)*$onehour;
  121. $timearr[] = array(
  122. 'start_time' => date('Y-m-d H:i',$nstart),
  123. 'end_time' => date('Y-m-d H:i',$nend),
  124. );
  125. if($nend >= $end){
  126. break;
  127. }
  128. $i += $hours;
  129. }
  130. }
  131. }
  132. if($days > 0){
  133. $timearray = $timearr;
  134. for ($i=1; $i<=$days;$i++){
  135. foreach ($timearray as $k=>$v){
  136. $timearr[] = array(
  137. 'start_time' => date('Y-m-d H:i',strtotime($v['start_time']) + $i*$oneday),
  138. 'end_time' => date('Y-m-d H:i',strtotime($v['end_time']) + $i*$oneday),
  139. );
  140. }
  141. }
  142. }
  143. return $timearr;
  144. }
  145. //获取巡查计划任务
  146. public function getTaskOne($taskId){
  147. $ret = Db::name('green_task')->where('id',$taskId)->find();
  148. //获取计划任务检查组id
  149. $old_addrs = Db::name('green_task_addr')
  150. ->where('green_task_id',$ret['id'])
  151. ->column('green_form_id');
  152. $ret['old_addrs'] = $old_addrs;
  153. //获取计划任务检查人员
  154. $old_user_ids=Db::name('green_task_user')->where('green_task_id',$ret['id'])->column('user_id');
  155. $ret['old_user_ids'] = $old_user_ids;
  156. return $ret;
  157. }
  158. //api接口
  159. public function lists($page,$size,$type,$userId,$orgId){
  160. $ret['list']=[];
  161. if($type == 1){ // 待完成
  162. $map[] = ['t.user_id','=',$userId];
  163. $map[] = ['t.org_id','=',$orgId];
  164. $map[] = ['gt.del','=',0];
  165. $map[] = ['t.type','=',6];
  166. $ret['list'] = Db::name('task')
  167. ->alias('t')
  168. ->field('gt.id,gt.start_time,gt.end_time,gt.title,gt.status,gt.is_exception')
  169. ->join('green_task gt','t.bus_id = gt.id')
  170. ->where($map)
  171. ->whereIn('gt.status',[0,1])
  172. ->order('gt.start_time desc')
  173. ->page($page,$size)
  174. ->select();
  175. foreach ($ret['list'] as $k=>$v){
  176. $people_name = Db::name('green_task_user')
  177. ->alias('gtu')
  178. ->join('user u','u.id=gtu.user_id')
  179. ->where('gtu.green_task_id',$v['id'])
  180. ->column('u.real_name');
  181. $ret['list'][$k]['peoples'] = $people_name?implode(',',$people_name):'';
  182. }
  183. }else if($type == 2){ // 已完成
  184. $where[] = ['gt.org_id','=',$orgId];
  185. $where[] = ['gt.del','=',0];
  186. $where[] = ['ptu.user_id','=',$userId];
  187. $ret['list'] = Db::name('green_task_user')
  188. ->alias('gtu')
  189. ->field('gt.id,gt.start_time,gt.end_time,gt.title,gt.status,gtu.user_id,gt.is_exception')
  190. ->join('green_task gt','gtu.green_task_id=gt.id')
  191. ->where($where)
  192. ->whereIn('gt.status',[2,3,5])
  193. ->order('gt.start_time desc')
  194. ->page($page,$size)
  195. ->select();
  196. foreach ($ret['list'] as $k=>$v){
  197. $user = Db::name('user')
  198. ->alias('u')
  199. ->field('u.real_name,d.title')
  200. ->join('user_dep ud','ud.user_id=u.id')
  201. ->join('dep d','d.id=ud.dep_id')
  202. ->where('u.id',$v['user_id'])
  203. ->find();
  204. $ret['list'][$k]['dep_name'] = $user?$user['title']:"";
  205. $ret['list'][$k]['people_name'] = $user?$user['real_name']:"";
  206. }
  207. }
  208. return $ret;
  209. }
  210. public function addrList($taskId,$orgId,$userId){
  211. $ret['user_list'] = Db::name('green_task_user')
  212. ->alias('gtu')
  213. ->field('u.real_name')
  214. ->join('green_task gt','gtu.green_task_id=gt.id')
  215. ->join('user u','u.id=gtu.user_id')
  216. ->where('gt.id',$taskId)
  217. ->select();
  218. $task = Db::name('green_task')
  219. ->field('interrupt_reson,interrupt_img')
  220. ->where('id',$taskId)
  221. ->find();
  222. $ret['task']['interrupt_reson'] = isset($task['interrupt_reson'])?$task['interrupt_reson']:'';
  223. $ret['task']['interrupt_img'] = isset($task['interrupt_img'])?$task['interrupt_img']:'';
  224. $addrList = Db::name('green_task_addr')
  225. ->alias('gta')
  226. ->field('gt.id,gt.start_time,gt.end_time,gt.status,gta.id as green_task_addr_id,ga.title,gta.green_addr_id')
  227. ->join('green_task gt','gta.green_task_id = gt.id')
  228. ->join('green_addr ga','gta.green_addr_id = ga.id')
  229. ->where('gt.id',$taskId)
  230. ->select();
  231. $addrList = $addrList?$addrList:[];
  232. foreach ($addrList as $k=>$v){
  233. $record = Db::name('green_task_record')
  234. ->where('org_id',$orgId)
  235. ->where('green_task_id',$v['id'])
  236. ->where('green_addr_id',$v['green_addr_id'])
  237. ->where('user_id',$userId)
  238. ->where('green_task_addr_id',$v['green_task_addr_id'])
  239. ->find();
  240. $addrList[$k]['status'] = $record?1:0;
  241. }
  242. $ret['addr_list'] = $addrList;
  243. return $ret;
  244. }
  245. public function taskDetails($taskId,$greenTaskAddrId,$orgId){
  246. $ret['details'] = Db::name('green_task')
  247. ->field('id,title,create_time,end_time,green_form')
  248. ->where('id',$taskId)
  249. ->find();
  250. $greenRecord = Db::name('green_task_record')
  251. ->field('create_time,images,content,check_json,user_id')
  252. ->where('org_id',$orgId)
  253. ->where('green_task_id',$taskId)
  254. ->where('green_task_addr_id',$greenTaskAddrId)
  255. ->find();
  256. $jsonArray=json_decode($greenRecord['check_json'],true);
  257. $jsonArray = $jsonArray?$jsonArray:[];
  258. foreach ($jsonArray as $k=>$v){
  259. if(!empty($v['remark'])){
  260. $remark=$v['remark'];
  261. }else{
  262. $remark='';
  263. }
  264. $jsonArray[$k]['title'] = $v['title'];
  265. $jsonArray[$k]['remark'] = $remark;
  266. }
  267. $ret['forms']=$jsonArray;
  268. unset($greenRecord['check_json']);
  269. $ret['details']['images']=$greenRecord?$greenRecord['images']:'';
  270. $ret['details']['content']=$greenRecord?$greenRecord['content']:'';
  271. $ret['details']['user_id']=$greenRecord?$greenRecord['user_id']:'';
  272. $ret['details']['done_time']=$greenRecord?$greenRecord['create_time']:'';
  273. $user = Db::name('green_task_user')
  274. ->alias('gtu')
  275. ->field('u.real_name')
  276. ->join('green_task gt','gtu.green_task_id=gt.id')
  277. ->join('user u','u.id=gtu.user_id')
  278. ->where('gt.id',$taskId)
  279. ->select();
  280. $ret['users']=$user?$user:'';
  281. $ret['addr'] = Db::name('green_task_addr')
  282. ->alias('gta')
  283. ->field('ga.id,ga.title,ga.img,ga.content')
  284. ->join('green_addr ga','ga.id=gta.green_addr_id')
  285. ->where('gta.id',$greenTaskAddrId)
  286. ->find();
  287. return $ret;
  288. }
  289. public function forms($greenTaskAddrId){
  290. $greenForm = Db::name('green_addr_form')
  291. ->alias('a')
  292. ->join('green_task_addr b','b.green_form_id = a.id')
  293. ->where('b.id',$greenTaskAddrId)
  294. ->value('a.green_form');
  295. $greenForm = $greenForm?explode(',',$greenForm):[];
  296. $ret = Db::name('green_form')
  297. ->whereIn('id',$greenForm)
  298. ->select();
  299. return $ret;
  300. }
  301. public function updateStatus($greenTaskId,$interruptReson,$interruptImg){
  302. if(!$interruptReson) {
  303. $this->error = '中断原因不能为空';
  304. return false;
  305. }
  306. $data=[
  307. 'status'=>5,
  308. 'interrupt_reson'=>$interruptReson,
  309. 'interrupt_img'=>$interruptImg,
  310. ];
  311. $update = Db::name('green_task')
  312. ->where('id',$greenTaskId)
  313. ->update($data);
  314. return $update;
  315. }
  316. public function WorkerGreen($orgId,$search){
  317. $org_id = $orgId;
  318. $query = Db::name('green_task_record');
  319. if(!empty($search['start'])){
  320. $search['start'] = date('Ymd',strtotime($search['start']));
  321. $query->where('green_task_record.create_yyyymmdd','>=', $search['start']);
  322. }
  323. if(!empty($search['end'])){
  324. $search['end'] = date('Ymd',strtotime($search['end']));
  325. $query->where('green_task_record.create_yyyymmdd','<=', $search['end']);
  326. }
  327. $query->where('green_task_record.org_id',$org_id);
  328. $query->group('green_task_record.user_id');
  329. $query->join('user','user.id = green_record.user_id');
  330. $query->field('green_task_record.id,green_task_record.user_id,user.nickname');
  331. $list =$query->select();
  332. $list = $list?$list:array();
  333. foreach ($list as $k=>$v){
  334. $query1 = Db::name('green_task_record');
  335. $query1->where('org_id',$org_id);
  336. $query1->where('user_id',$v['user_id']);
  337. if(!empty($search['start'])){
  338. $search['start'] = date('Ymd',strtotime($search['start']));
  339. $query1->where('create_yyyymmdd','>=', $search['start']);
  340. }
  341. if(!empty($search['end'])){
  342. $search['end'] = date('Ymd',strtotime($search['end']));
  343. $query1->where('create_yyyymmdd','<=', $search['end']);
  344. }
  345. $count = $query1->count();
  346. $list[$k]['count'] = $count?$count:0;
  347. }
  348. return $list;
  349. }
  350. public function get_list_by_time($orgId,$start,$end){
  351. $map[] = ['org_id','=',$orgId];
  352. $map[] = ['del','=',0];
  353. $map[] = ['start_time','<',$end];
  354. $map[] = ['end_time','>=',$start];
  355. $list = $this->field('id,title,start_time,end_time,status')
  356. ->where($map)
  357. ->select();
  358. $list = $list?$list->toArray():[];
  359. foreach ($list as $k=>$v){
  360. $userList = Db::name('green_task_user')
  361. ->alias('a')
  362. ->field('u.id,u.real_name')
  363. ->join('user u','u.id = a.user_id')
  364. ->where('a.green_task_id',$v['id'])
  365. ->column('real_name');
  366. $list[$k]['users'] = $userList?implode(',',$userList):'';
  367. }
  368. return $list?$list:array();
  369. }
  370. //分组获取任务计划最近12个月份
  371. public function get_task_month($org_id){
  372. $list = $this
  373. ->field('create_yyyymm')
  374. ->group('create_yyyymm')
  375. ->where('org_id',$org_id)
  376. ->where('del',0)
  377. ->order('create_yyyymm','desc')
  378. ->limit(12)
  379. ->select();
  380. $list = $list?$list->toArray():array();
  381. foreach ($list as $k=>$v){
  382. $list[$k]['create_yyyymm'] = date('Y-m',strtotime($v['create_yyyymm'].'01'));
  383. }
  384. return $list;
  385. }
  386. //复制任务
  387. public function plan_data($post,$org){ //TODO::已复制过的月份是否可以继续复制
  388. $from = strtotime($post['from'].'-01');
  389. $to = strtotime($post['to'].'-01');
  390. $max = strtotime(date('Y-m-d', strtotime($post['to'].'-01 +1 month')));
  391. $cha = $to - $from;
  392. $map[] = ['del','=',0];
  393. $map[] = ['org_id','=',$org];
  394. $map[] = ['create_yyyymm','=',date('Ym',strtotime($post['from'].'-01'))];
  395. $list = $this->where($map)->select();
  396. if(!$list){
  397. $this->error = '要复制的月份下没有任务';
  398. return false;
  399. }
  400. $list = $list->toArray();
  401. $this->startTrans();
  402. try{
  403. $uuid = '';
  404. while (true) {
  405. $uuid = new_guid();
  406. $ret = $this
  407. ->where('uuid',$uuid)
  408. ->find();
  409. if(!$ret){
  410. break;
  411. }
  412. }
  413. foreach ($list as $k=>$v){
  414. $start_time = strtotime($v['start_time'])+$cha;
  415. if($start_time >= $max){
  416. continue;
  417. }
  418. $v['start_time'] = date('Y-m-d H:i:s',$start_time);
  419. $v['end_time'] = date('Y-m-d H:i:s',strtotime($v['end_time'])+$cha);
  420. $v['uuid'] = $uuid;
  421. $ret = $this->copy_one($v);
  422. if(!$ret){
  423. \exception('复制失败');
  424. }
  425. }
  426. $this->commit();
  427. return true;
  428. }catch (Exception $e){
  429. $this->db->trans_rollback();
  430. $this->error = $e->getMessage();
  431. return false;
  432. }
  433. }
  434. //复制单个任务
  435. private function copy_one($data){
  436. $id = $data['id'];
  437. unset($data['id']);
  438. $data['status'] = 0;
  439. $data['create_time'] = date('Y-m-d H:i:s');
  440. $data['create_yyyymm'] = date('Ym',strtotime($data['start_time']));
  441. $data['create_yyyy'] = date('Y',strtotime($data['start_time']));
  442. $data['create_yyyymmdd'] = date('Ymd',strtotime($data['start_time']));
  443. $addrs = $this->get_task_addr($id);
  444. $userids = $this->get_task_user($id);
  445. //添加任务
  446. $taskid = $this->insertGetId($data);
  447. if(!$taskid){
  448. return false;
  449. }
  450. //添加检查地点
  451. $addr = array();
  452. foreach ($addrs as $k=>$v){
  453. $addr[] = array(
  454. 'green_task_id' => $taskid,
  455. 'green_addr_form_id' => $v
  456. );
  457. }
  458. $res = Db::name('green_task_addr')->insertAll($addr);
  459. if(!$res){
  460. return false;
  461. }
  462. //添加检查人员
  463. $user = array();
  464. foreach ($userids as $k=>$v){
  465. $user[] = array(
  466. 'green_task_id' => $taskid,
  467. 'user_id' => $v
  468. );
  469. }
  470. $res = Db::name('green_task_user')->insertAll($user);
  471. if(!$res){
  472. return false;
  473. }
  474. return true;
  475. }
  476. //获取任务检查地点
  477. public function get_task_addr($taskid){
  478. $addrlist = Db::name('green_task_addr')
  479. ->where('green_task_id',$taskid)
  480. ->column('green_addr_id');
  481. return $addrlist;
  482. }
  483. //获取任务检查人员
  484. public function get_task_user($taskid){
  485. $userlist = Db::name('green_task_user')
  486. ->where('green_task_id',$taskid)
  487. ->column('user_id');
  488. return $userlist;
  489. }
  490. //定时处理超时任务
  491. public function timer_action(){
  492. $curTime = date('Y-m-d H:i:s');
  493. $map[] = ['del','=',0];
  494. $map[] = ['status','in',[0,1]];
  495. $map[] = ['end_time','<',$curTime];
  496. $this->where($map)
  497. ->update(['status'=>3]);
  498. $ids = Db::name('task')
  499. ->alias('a')
  500. ->join('green_task b','a.bus_id=b.id')
  501. ->where('b.end_time','<',$curTime)
  502. ->where('a.type',6)
  503. ->column('a.id');
  504. if(!empty($ids)){
  505. Db::name('task')
  506. ->where('id','in',$ids)
  507. ->delete();
  508. }
  509. }
  510. public function addGreenTask(){
  511. $curTime = date('Y-m-d H:i:s',time()+12*60*60);
  512. $map[] = ['a.del','=',0];
  513. $map[] = ['a.status','=',0];
  514. $map[] = ['a.start_time','<',$curTime];
  515. $ids = $this
  516. ->alias('a')
  517. ->join('green_task_user b','b.green_task_id=a.id')
  518. ->where($map)
  519. ->field('a.*,b.user_id')
  520. ->select();
  521. if(!empty($ids)){
  522. $ids = $ids->toArray();
  523. $a = [];
  524. foreach ($ids as $k=>$v){
  525. $check = Db::name('task')
  526. ->where('type',6)
  527. ->where('org_id',$v['org_id'])
  528. ->where('bus_id',$v['id'])
  529. ->where('user_id',$v['user_id'])
  530. ->find();
  531. if(empty($check)){
  532. $a[]= [
  533. 'org_id'=>$v['org_id'],
  534. 'type'=>6,
  535. 'start_time'=>$v['start_time'],
  536. 'create_time'=>getTime(),
  537. 'bus_id'=>$v['id'],
  538. 'user_id'=>$v['user_id']
  539. ];
  540. }
  541. }
  542. if(!empty($a)){
  543. Db::name('task')
  544. ->insertAll($a);
  545. }
  546. }
  547. }
  548. }