QualityTask.php 26 KB

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