0
0

PatrolPlan.php 18 KB

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