PatrolTask.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\model\Task;
  4. use app\common\model\User;
  5. use think\App;
  6. use think\Db;
  7. use think\Exception;
  8. class PatrolTask extends Auth
  9. {
  10. public function index($mode){
  11. if(request()->isAjax()){
  12. //分页参数
  13. $length = input('rows',10,'intval'); //每页条数
  14. $page = input('page',1,'intval'); //第几页
  15. $start = ($page - 1) * $length; //分页开始位置
  16. //排序
  17. $sortRow = input('sidx','sort','trim'); //排序列
  18. $sort = input('sord','asc','trim'); //排序方式
  19. $order = $sortRow.' '.$sort.' ,id desc';
  20. $month= input('month','','trim');
  21. if($month != ''){
  22. $map[] = ['create_yyyymm','=',$month];
  23. }
  24. $title = input('title','','trim');
  25. if($title){
  26. $map[] = ['title','like','%'.$title.'%'];
  27. }
  28. $status = input('status','','trim');
  29. if($status != ''){
  30. $map[] = ['status','=',$status];
  31. }
  32. $is_exception = input('is_exception','','trim');
  33. if($is_exception != ''){
  34. $map[] = ['is_exception','=',$is_exception];
  35. }
  36. $plan_id = input('plan_id','','trim');
  37. if($plan_id != ''){
  38. $map[] = ['plan_id','=',$plan_id];
  39. }
  40. $cate_id = input('cate_id','','trim');
  41. if($cate_id != ''){
  42. $map[] = ['cate_id','=',$cate_id];
  43. }
  44. $map[] = ['org_id','=',$this->orgId];
  45. $map[] =['del','=',0];
  46. $map[] =['patrol_mode','=',$mode];
  47. $map= empty($map) ? true: $map;
  48. //数据查询
  49. $lists = Db::name('patrol_task')->where($map)
  50. ->limit($start,$length)->order($order)->select();
  51. foreach ($lists as $k=>$v){
  52. $lists[$k]['start_time'] = date('Y-m-d H:i',strtotime($v['start_time']));
  53. $lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
  54. $taskUser=Db::name('patrol_task_user')
  55. ->alias('ptu')
  56. ->field('u.real_name')
  57. ->join('user u','u.id=ptu.user_id')
  58. ->where('ptu.patrol_task_id',$v['id'])
  59. ->select();
  60. $userName = [];
  61. foreach ($taskUser as $kk=>$vv){
  62. $userName[$kk]=$vv['real_name'];
  63. }
  64. $lists[$k]['task_user']=implode(',',$userName);
  65. $lists[$k]['time'] = 0;
  66. $time = Db::name('patrol_record')->where('patrol_task_id',$v['id'])->where('time',1)->find();
  67. if($time){
  68. $lists[$k]['time'] = 1;
  69. }
  70. $lists[$k]['cateName'] = $v['cate_id']?implode(',',Db::name('patrol_cate')
  71. ->where('id','in',explode(',',$v['cate_id']))
  72. ->column('title')):'';
  73. }
  74. //数据返回
  75. $totalCount = Db::name('patrol_task')->where($map)->count();
  76. $totalPage = ceil($totalCount/$length);
  77. $result['page'] = $page;
  78. $result['total'] = $totalPage;
  79. $result['records'] = $totalCount;
  80. $result['rows'] = $lists;
  81. return json($result);
  82. }else{
  83. $plans = model('PatrolPlan')->getListByMode($this->orgId,$mode);
  84. $this->assign('plans',$plans);
  85. $this->assign('mode',$mode);
  86. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  87. $this->assign('meta_title',$modeName.'任务列表');
  88. $cate = model('PatrolCate')->getByModeList($mode);
  89. $this->assign('cate',$cate);
  90. return $this->fetch();
  91. }
  92. }
  93. /**
  94. * 新增/编辑
  95. */
  96. public function add($id=0,$mode){
  97. $type = input('type');
  98. $isCalendar = input('isCalendar');
  99. if(request()->isPost()){
  100. $model = new \app\common\model\PatrolTask();
  101. $res = $model->updates($mode);
  102. if($res){
  103. if($type==1){
  104. if($isCalendar==1){
  105. $this->success('操作成功',url('calendar',['mode'=>$mode]));
  106. }else{
  107. $this->success('操作成功');
  108. }
  109. }else{
  110. $this->success('操作成功',url('index',['mode'=>$mode]));
  111. }
  112. }else{
  113. $this->error($model->getError());
  114. }
  115. }else{
  116. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  117. $meta_title = '新增'.$modeName.'计划';
  118. if($id){
  119. $info = Db::name('patrol_task')->where('id',$id)->find();
  120. $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
  121. $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
  122. $info['lines']=$info['lines']?json_decode($info['lines'],true):[];
  123. $info['patrol_form'] =\db('patrol_task_addr')
  124. ->where('patrol_task_id',$id)
  125. ->column('patrol_form_id');
  126. $info['task_user']=\db('patrol_task_user')
  127. ->where('patrol_task_id',$id)
  128. ->column('user_id');
  129. // $addrForms = Db::name('patrol_task_addr')
  130. // ->alias('a')
  131. // ->join('patrol_addr_form b','a.patrol_form_id = b.id')
  132. // ->join('address c','c.id = a.address_id')
  133. // ->where('a.patrol_task_id',$info['id'])
  134. // ->field('a.address_id as addrId,a.patrol_form_id as formId,b.title as formTitle,c.title as addrTitle')
  135. // ->select();
  136. // $info['addrForms'] = $addrForms?$addrForms:[];
  137. $addrForms = Db::name('patrol_task_addr')
  138. ->alias('a')
  139. ->join('address c','c.id = a.address_id')
  140. ->where('a.patrol_task_id',$info['id'])
  141. ->field('a.address_id as addrId,a.sorts,c.title as addrTitle,a.patrol_forms')
  142. ->order(['a.sorts'=>'asc','a.id'=>'desc'])
  143. ->select();
  144. $addrForms = $addrForms?$addrForms:[];
  145. foreach ($addrForms as $k=>$v){
  146. $forms = [];
  147. if($v['patrol_forms']){
  148. $forms = Db::name('patrol_addr_form')->where('id','in',$v['patrol_forms'])->field('id,title')->select();
  149. }
  150. $addrForms[$k]['forms'] = $forms?$forms:[];
  151. }
  152. $info['addrForms'] = $addrForms?$addrForms:[];
  153. $info['cate_id'] = $info['cate_id']?explode(',',$info['cate_id']):[];
  154. $this->assign('info',$info);
  155. $meta_title = '编辑'.$modeName.'计划';
  156. }
  157. //获取任务内容
  158. $patrolAddrForm=(new \app\common\model\PatrolAddrForm())->getByModeList($mode);
  159. //获取用户
  160. switch ($mode){
  161. case 1:
  162. $pType=5;
  163. break;
  164. case 2:
  165. $pType=6;
  166. break;
  167. case 3:
  168. $pType=7;
  169. break;
  170. case 4:
  171. $pType=8;
  172. break;
  173. }
  174. $user=(new User())->getPatrolWorker($pType);
  175. $start = input('start','');
  176. $end = input('end','');
  177. $this->assign('patrolForm',$patrolAddrForm);
  178. $this->assign('user',$user);
  179. $this->assign('mode',$mode);
  180. $this->assign('start',$start?date('Y-m-d H:i',strtotime($start)):'');
  181. $this->assign('end',$end?date('Y-m-d H:i',strtotime($end)):'');
  182. $this->assign('meta_title',$meta_title);
  183. $this->assign('type',$type);
  184. $this->assign('isCalendar',$isCalendar);
  185. $this->assign('address',(new \app\common\model\Address())->getListByType($mode+2));
  186. $cate = model('PatrolCate')->getByModeList($mode);
  187. $this->assign('cateList',$cate);
  188. return $this->fetch();
  189. }
  190. }
  191. /**
  192. * 删除记录
  193. */
  194. public function del($id=0){
  195. if(!$id){
  196. $this->error('参数错误');
  197. }
  198. $info = Db::name('patrol_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
  199. if(!$info){
  200. $this->error('任务不存在');
  201. }
  202. if($info['status'] != 6){
  203. $this->error('任务未关闭不可删除');
  204. }
  205. Db::startTrans();
  206. try{
  207. $res = Db::name('patrol_task')->where('id',$id)->update([
  208. 'del'=>1,
  209. 'del_user_id'=>is_login(),
  210. 'del_time'=>getTime(),
  211. ]);
  212. if(!$res){
  213. \exception('操作失败');
  214. }
  215. // 如果在任务栏删除任务
  216. Db::name('task')->where('bus_id',$id)->where('type',2)->delete();
  217. Db::commit();
  218. }catch (Exception $e){
  219. Db::rollback();
  220. $this->error('删除失败');
  221. }
  222. $this->success('删除成功');
  223. }
  224. /**
  225. * 关闭记录
  226. */
  227. public function close($id=0){
  228. if(!$id){
  229. $this->error('参数错误');
  230. }
  231. $info = Db::name('patrol_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
  232. if(!$info){
  233. $this->error('任务不存在');
  234. }
  235. if($info['status'] == 6){
  236. $this->error('任务已关闭');
  237. }
  238. Db::startTrans();
  239. try{
  240. $res = Db::name('patrol_task')->where('id',$id)->update([
  241. 'status'=>6,
  242. 'update_time'=>getTime(),
  243. ]);
  244. if(!$res){
  245. \exception('操作失败');
  246. }
  247. // 如果在任务栏删除任务
  248. Db::name('task')->where('bus_id',$id)->where('type',2)->delete();
  249. Db::commit();
  250. }catch (Exception $e){
  251. Db::rollback();
  252. $this->error('操作失败');
  253. }
  254. $this->success('操作成功');
  255. }
  256. public function info($id=0,$mode){
  257. $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
  258. $meta_title=$modeName.'巡视轨迹';
  259. if(!$id){
  260. $this->error('参数错误');
  261. }
  262. $info=Db::name('patrol_task')
  263. ->field('id,start_time,end_time,status,title,interrupt_img,interrupt_reson,in_order,cate_id')
  264. ->where('id',$id)->find();
  265. if(!$info){
  266. $this->error('记录不存在');
  267. }
  268. $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
  269. $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
  270. //获取执行用户
  271. $task_user=Db::name('patrol_task_user')
  272. ->alias('ptu')
  273. ->join('user u','u.id=ptu.user_id')
  274. ->where('ptu.patrol_task_id',$info['id'])
  275. ->column('u.real_name');
  276. $info['task_user']=implode(',',$task_user);
  277. $addrForms = Db::name('patrol_task_addr')
  278. ->alias('a')
  279. // ->join('patrol_addr_form b','a.patrol_form_id = b.id')
  280. ->join('address c','c.id = a.address_id')
  281. ->where('a.patrol_task_id',$info['id'])
  282. ->field('a.*,c.title as addrTitle')
  283. ->select();
  284. $addrForms = $addrForms?$addrForms:[];
  285. foreach ($addrForms as $k=>$v){
  286. $btitles = Db::name('patrol_addr_form')->where('id','in',$v['patrol_forms'])->column('title');
  287. $addrForms[$k]['formTitle'] = $btitles?implode(',',$btitles):'';
  288. $record = Db::name('patrol_record')
  289. ->alias('a')
  290. ->join('user b','a.user_id = b.id')
  291. ->where('a.patrol_task_id',$v['patrol_task_id'])
  292. ->where('a.patrol_addr_id',$v['address_id'])
  293. ->field('b.real_name as uname,a.*')
  294. ->find();
  295. if($record){
  296. $cj = $record['check_json']?json_decode($record['check_json'],true):[];
  297. $record['check_json'] = $cj;
  298. }
  299. $addrForms[$k]['record'] = $record?$record:[];
  300. }
  301. $info['addrForms'] = $addrForms;
  302. $info['cateName'] = $info['cate_id']?implode(',',Db::name('patrol_cate')
  303. ->where('id','in',explode(',',$info['cate_id']))
  304. ->column('title')):'';
  305. $this->assign('meta_title',$meta_title);
  306. $this->assign('info',$info);
  307. $this->assign('mode',$mode);
  308. $this->assign('modeName',$modeName);
  309. return $this->fetch();
  310. }
  311. public function calendar($mode){
  312. $this->assign('mode',$mode);
  313. return $this->fetch();
  314. }
  315. //获取正月数据
  316. public function taskjson(){
  317. $mode = (int)input('mode');
  318. $start = input('start');
  319. $end = input('end');
  320. $data = array();
  321. if(!$start||!$end||$start>$end){
  322. header('Content-Type:application/json; charset=utf-8');
  323. exit(json_encode($data));
  324. }
  325. $start = date('Y-m-d H:i:s',strtotime($start));
  326. $end = date('Y-m-d H:i:s',strtotime($end));
  327. $map[] = [''];
  328. $list = (new \app\common\model\PatrolTask())->get_list_by_time($this->orgId,$mode,$start,$end);
  329. foreach ($list as $k=>$v){
  330. $arr = array(
  331. 'taskid' => $v['id'],
  332. 'title' => "{$v['title']}<br>执行人:{$v['users']}<br>开始时间:{$v['start_time']}<br>结束时间:{$v['end_time']}<br>",
  333. 'status' => $v['status'],
  334. 'start' => $v['start_time'],
  335. 'end' => $v['end_time']
  336. );
  337. if($v['status'] == 0){
  338. $arr['color'] = '#777777';
  339. }else if($v['status'] == 1){
  340. $arr['color'] = '#478fca';
  341. }else if($v['status'] == 2){
  342. $arr['color'] = '#69aa46';
  343. }else if($v['status'] == 3){
  344. $arr['color'] = '#dd5a43';
  345. }else if($v['status'] == 5){
  346. $arr['color'] = '#53a2a7';
  347. }else{
  348. $arr['color'] = '#5b53a7';
  349. }
  350. $data[] = $arr;
  351. }
  352. header('Content-Type:application/json; charset=utf-8');
  353. exit(json_encode($data));
  354. }
  355. //复制月计划
  356. public function plan($mode){
  357. $type = input('type');
  358. if(request()->isPost()){
  359. $data = request()->post();
  360. if($data['from'] == $data['to']){
  361. $this->error('不能复制同月的任务');
  362. }
  363. $ret = (new \app\common\model\PatrolTask())->plan_data($data,$this->orgId);
  364. if($ret){
  365. if($type==1){
  366. $this->success('操作成功',url('index',['mode'=>$mode]));
  367. }else{
  368. $this->success('操作成功',url('calendar',['mode'=>$mode]));
  369. }
  370. }else{
  371. $this->error((new \app\common\model\PatrolTask())->getError());
  372. }
  373. }else{
  374. $from = (new \app\common\model\PatrolTask())->get_task_month($this->orgId,$mode);
  375. //间隔1月往后数11次
  376. $timenow = new \Datetime();
  377. $datetin = \DateInterval::createFromDateString('1 month');
  378. $datePer = new \DatePeriod($timenow, $datetin, 11);
  379. $to = [];
  380. foreach ($datePer as $day) {
  381. $m = $day->format('Y-m');
  382. $to[]= $m;
  383. }
  384. $this->assign('from',$from);
  385. $this->assign('to',$to);
  386. $this->assign('mode',$mode);
  387. $this->assign('type',$type);
  388. return $this->fetch();
  389. }
  390. }
  391. public function batch($mode){
  392. $type = input('type');
  393. if(request()->isPost()){
  394. $data = request()->post();
  395. $minmonth = date('Y-m');
  396. if($minmonth >= $data['from']){
  397. $this->error('参数错误');
  398. }
  399. $where = array(
  400. 'org_id' => $this->orgId,
  401. 'patrol_mode' => $data['mode'],
  402. 'del' => 0,
  403. 'create_yyyymm' => date('Ym',strtotime($data['from'].'-01'))
  404. );
  405. $default['del'] = 1;
  406. $default['del_user_id'] = $this->userId;
  407. $default['del_time'] = date('Y-m-d H:i:s');
  408. $result = Db::name('patrol_task')
  409. ->where($where)
  410. ->update($default);
  411. $type = (new Task())::TASK_TYPE_PATROL;
  412. // 检查是否存在任务栏数据中
  413. $dailyIds = Db::name('patrol_task_task')
  414. ->alias('dt')
  415. ->join('task t','t.bus_id = dt.id')
  416. ->where('t.type',$type)
  417. ->where('dt.org_id',$this->orgId)
  418. ->where('dt.del',0)
  419. ->where('dt.create_yyyymm',date('Ym',strtotime($data['from'].'-01')))
  420. ->column('dt.id');
  421. if($result){
  422. if($dailyIds){
  423. Db::name('task')
  424. ->where('bus_id', 'in',implode(',',$dailyIds))
  425. ->where('type', $type)
  426. ->delete();
  427. }
  428. if($type==1){
  429. $this->success('操作成功',url('index',['mode'=>$mode]));
  430. }else{
  431. $this->success('操作成功',url('calendar',['mode'=>$mode]));
  432. }
  433. }else{
  434. $this->error('删除失败');
  435. }
  436. }else{
  437. $from = (new \app\common\model\PatrolTask())->get_task_month($this->orgId,$mode);
  438. $minmonth = date('Y-m');
  439. $aa = [];
  440. foreach ($from as $k=>$v){
  441. if($minmonth < $v['create_yyyymm']){
  442. $aa[] = $v['create_yyyymm'];
  443. }
  444. }
  445. $this->assign('from',$aa);
  446. $this->assign('mode',$mode);
  447. $this->assign('type',$type);
  448. return $this->fetch();
  449. }
  450. }
  451. }