123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- <?php
- namespace app\admin\controller;
- use app\common\model\Task;
- use app\common\model\User;
- use think\App;
- use think\Db;
- use think\Exception;
- class PatrolPlan extends Auth
- {
- public function index($mode=1){
- if(request()->isAjax()){
- //分页参数
- $length = input('rows',10,'intval'); //每页条数
- $page = input('page',1,'intval'); //第几页
- $start = ($page - 1) * $length; //分页开始位置
- //排序
- $sortRow = input('sidx','id','trim'); //排序列
- $sort = input('sord','desc','trim'); //排序方式
- $order = $sortRow.' '.$sort;
- $title = input('title','','trim');
- if($title){
- $map[] = ['title','like','%'.$title.'%'];
- }
- $status = input('status','','trim');
- if($status != ''){
- $map[] = ['status','=',$status];
- }
- $cate_id = input('cate_id','','trim');
- if($cate_id != ''){
- $map[] = ['cate_id','=',$cate_id];
- }
- $map[] = ['org_id','=',$this->orgId];
- $map[] =['del','=',0];
- $map[] =['patrol_mode','=',$mode];
- $map= empty($map) ? true: $map;
- //数据查询
- $lists = Db::name('patrol_plan')
- ->where($map)
- ->limit($start,$length)
- ->order($order)
- ->select();
- foreach ($lists as $k=>$v){
- $lists[$k]['uname'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
- $lists[$k]['count'] = Db::name('patrol_task')->where('plan_id',$v['id'])->where('del',0)->count();
- $lists[$k]['cateName'] = $v['cate_id']?implode(',',Db::name('patrol_cate')
- ->where('id','in',explode(',',$v['cate_id']))
- ->column('title')):'';
- }
- //数据返回
- $totalCount = Db::name('patrol_plan')->where($map)->count();
- $totalPage = ceil($totalCount/$length);
- $result['page'] = $page;
- $result['total'] = $totalPage;
- $result['records'] = $totalCount;
- $result['rows'] = $lists;
- return json($result);
- }else{
- $this->assign('mode',$mode);
- $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
- $this->assign('meta_title',$modeName.'计划列表');
- $cate = model('PatrolCate')->getByModeList($mode);
- $this->assign('cate',$cate);
- return $this->fetch();
- }
- }
- /**
- * 新增/编辑
- */
- public function add($id=0,$mode=0){
- if(request()->isPost()){
- $res = model('PatrolPlan')->updates($this->userId);
- if($res){
- $this->success('操作成功',url('index',['mode'=>$mode]));
- }else{
- $this->error(model('PatrolPlan')->getError());
- }
- }else{
- if($id > 0){ // 复制计划
- $info = Db::name('patrol_plan')->where('id',$id)->find();
- if($info){
- $info['content'] = json_decode($info['content'],true);
- $mode = $info['patrol_mode'];
- $info['lines'] = $info['lines']?json_decode($info['lines'],true):[];
- $info['cate_id'] = $info['cate_id']?explode(',',$info['cate_id']):[];
- }
- $this->assign('info',$info);
- }
- $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
- $meta_title = '新增'.$modeName.'计划';
- //获取任务内容
- $patrolForm=(new \app\common\model\PatrolAddrForm())->getByModeList($mode);
- $this->assign('patrolForm',$patrolForm);
- //获取用户
- switch ($mode){
- case 1:
- $pType=5;
- break;
- case 2:
- $pType=6;
- break;
- case 3:
- $pType=7;
- break;
- case 4:
- $pType=8;
- break;
- }
- $user=(new User())->getPatrolWorker($pType);
- $this->assign('address',(new \app\common\model\Address())->getListByType($mode+2));
- $this->assign('user',$user);
- $this->assign('mode',$mode);
- $this->assign('meta_title',$meta_title);
- $cate = model('PatrolCate')->getByModeList($mode);
- $this->assign('cateList',$cate);
- return $this->fetch();
- }
- }
- public function edit($id=0,$mode=0){
- if(request()->isPost()){
- $res = model('PatrolPlan')->updates($this->userId);
- if($res){
- $this->success('操作成功',url('index',['mode'=>$mode]));
- }else{
- $this->error(model('PatrolPlan')->getError());
- }
- }else{
- if($id > 0){ // 复制计划
- $info = Db::name('patrol_plan')->where('id',$id)->find();
- if($info){
- $info['cate_id'] = $info['cate_id']?explode(',',$info['cate_id']):[];
- $info['content'] = json_decode($info['content'],true);
- $mode = $info['patrol_mode'];
- $info['lines'] = $info['lines']?json_decode($info['lines'],true):[];
- }
- $this->assign('info',$info);
- }
- $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
- $meta_title = '新增'.$modeName.'计划';
- //获取任务内容
- $patrolForm=(new \app\common\model\PatrolAddrForm())->getByModeList($mode);
- $this->assign('patrolForm',$patrolForm);
- //获取用户
- switch ($mode){
- case 1:
- $pType=5;
- break;
- case 2:
- $pType=6;
- break;
- case 3:
- $pType=7;
- break;
- case 4:
- $pType=8;
- break;
- }
- $user=(new User())->getPatrolWorker($pType);
- $this->assign('address',(new \app\common\model\Address())->getListByType($mode+2));
- $this->assign('user',$user);
- $this->assign('mode',$mode);
- $this->assign('id',$id);
- $this->assign('meta_title',$meta_title);
- $cate = model('PatrolCate')->getByModeList($mode);
- $this->assign('cateList',$cate);
- return $this->fetch();
- }
- }
- public function detail($id=0){
- $info = Db::name('patrol_plan')->where('id',$id)->find();
- if(!$info){
- $this->error('计划不存在');
- }
- $info['uname'] = Db::name('user')->where('id',$info['user_id'])->value('real_name');
- $info['content'] = json_decode($info['content'],true);
- $info['lines'] = $info['lines']?json_decode($info['lines'],true):[];
- $unames = Db::name('user')->where('id','in',$info['content']['userIds'])->column('real_name');
- $info['unames'] = $unames?implode(',',$unames):'';
- $info['cateName'] = $info['cate_id']?implode(',',Db::name('patrol_cate')
- ->where('id','in',explode(',',$info['cate_id']))
- ->column('title')):'';
- $this->assign('info',$info);
- return $this->fetch();
- }
- /**
- * 关闭计划
- */
- public function close($id=0){
- if(!$id){
- $this->error('参数错误');
- }
- $info = Db::name('patrol_plan')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
- if(!$info){
- $this->error('计划不存在');
- }
- if($info['close'] == 1){
- $this->error('计划已关闭');
- }
- if($info['status'] == 2){
- $this->error('计划已结束');
- }
- Db::startTrans();
- try{
- $res = Db::name('patrol_plan')->where('id',$id)->update([
- 'status' => 2,
- 'close' => 1,
- 'close_user_id' => $this->userId,
- 'close_time' => getTime(),
- ]);
- if(!$res){
- \exception('操作失败');
- }
- // 关闭任务
- Db::name('patrol_task')
- ->where('del',0)
- ->where('plan_id',$id)
- ->where('status','in',[0,1])
- ->update([
- 'status' => 6,
- 'update_time' => getTime(),
- ]);
- // 已经加入任务的,从任务中删除
- $taskIds = Db::name('patrol_task')->where('del',0)->where('plan_id',$id)->column('id');
- if($taskIds){
- Db::name('task')
- ->where('org_id',$this->orgId)
- ->where('type',2)
- ->where('bus_id','in',$taskIds)
- ->delete();
- }
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error('操作失败');
- }
- $this->success('操作成功');
- }
- /**
- * 删除计划
- */
- public function del($id=0){
- if(!$id){
- $this->error('参数错误');
- }
- $info = Db::name('patrol_plan')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
- if(!$info){
- $this->error('计划不存在');
- }
- if($info['status'] != 2){
- $this->error('计划未结束');
- }
- Db::startTrans();
- try{
- $res = Db::name('patrol_plan')->where('id',$id)->update([
- 'del' => 1,
- 'del_user_id' => $this->userId,
- 'del_time' => getTime(),
- ]);
- if(!$res){
- \exception('操作失败');
- }
- // 已经加入任务的,从任务中删除
- $taskIds = Db::name('patrol_task')->where('del',0)->where('plan_id',$id)->column('id');
- if($taskIds){
- Db::name('task')
- ->where('org_id',$this->orgId)
- ->where('type',2)
- ->where('bus_id','in',$taskIds)
- ->delete();
- }
- // 删除任务
- Db::name('patrol_task')
- ->where('del',0)
- ->where('plan_id',$id)
- ->update([
- 'del' => 1,
- 'del_user_id' => $this->userId,
- 'del_time' => getTime(),
- ]);
- Db::commit();
- }catch (Exception $e){
- Db::rollback();
- $this->error('操作失败');
- }
- $this->success('操作成功');
- }
- public function info($id=0,$mode){
- $modeName = (new \app\common\model\PatrolAddrForm())->getModeTitle($mode);
- $meta_title=$modeName.'巡视轨迹';
- if(!$id){
- $this->error('参数错误');
- }
- $info=Db::name('patrol_task')
- ->field('id,start_time,end_time,status,title,interrupt_img,interrupt_reson')
- ->where('id',$id)->find();
- $info['start_time']=date('Y-m-d H:i',strtotime($info['start_time']));
- $info['end_time']=date('Y-m-d H:i',strtotime($info['end_time']));
- //获取执行用户
- $task_user=Db::name('patrol_task_user')
- ->alias('ptu')
- ->join('user u','u.id=ptu.user_id')
- ->where('ptu.patrol_task_id',$info['id'])
- ->column('u.real_name');
- $info['task_user']=implode(',',$task_user);
- $map[] = ['pr.patrol_task_id', '=', $info['id']];
- $map[] = ['pr.org_id', '=', $this->orgId];
- $map[] = ['pr.patrol_mode', '=', $mode];
- //数据查询
- $lists = Db::name('patrol_record')
- ->alias('pr')
- ->field('pr.*,pa.title')
- ->Leftjoin('address pa', 'pa.id=pr.patrol_addr_id')
- ->where($map)
- ->order('id','desc')
- ->select();
- foreach ($lists as $k => $v) {
- $lists[$k]['task_title'] = Db::name('patrol_task')
- ->where('id', $v['patrol_task_id'])->value('title');
- $lists[$k]['task_addr_form'] = Db::name('patrol_addr_form')
- ->alias('a')
- ->join('patrol_task_addr b','a.id = b.partol_form_id')
- ->where('b.id', $v['patrol_addr_form_id'])
- ->value('a.title');
- $lists[$k]['task_user'] = Db::name('user')
- ->where('id', $v['user_id'])->value('real_name');
- }
- $this->assign('meta_title',$meta_title);
- $this->assign('info',$info);
- $this->assign('recordList',$lists);
- $this->assign('mode',$mode);
- return $this->fetch();
- }
- public function calendar($mode){
- $this->assign('mode',$mode);
- return $this->fetch();
- }
- //获取正月数据
- public function taskjson(){
- $mode = (int)input('mode');
- $start = input('start');
- $end = input('end');
- $data = array();
- if(!$start||!$end||$start>$end){
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode($data));
- }
- $start = date('Y-m-d H:i:s',strtotime($start));
- $end = date('Y-m-d H:i:s',strtotime($end));
- $map[] = [''];
- $list = (new \app\common\model\PatrolTask())->get_list_by_time($this->orgId,$mode,$start,$end);
- foreach ($list as $k=>$v){
- $arr = array(
- 'taskid' => $v['id'],
- 'title' => "{$v['title']}<br>执行人:{$v['users']}<br>开始时间:{$v['start_time']}<br>结束时间:{$v['end_time']}<br>",
- 'status' => $v['status'],
- 'start' => $v['start_time'],
- 'end' => $v['end_time']
- );
- if($v['status'] == 0){
- $arr['color'] = '#777777';
- }else if($v['status'] == 1){
- $arr['color'] = '#478fca';
- }else if($v['status'] == 2){
- $arr['color'] = '#69aa46';
- }else{
- $arr['color'] = '#dd5a43';
- }
- $data[] = $arr;
- }
- header('Content-Type:application/json; charset=utf-8');
- exit(json_encode($data));
- }
- //复制月计划
- public function plan($mode){
- $type = input('type');
- if(request()->isPost()){
- $data = request()->post();
- if($data['from'] == $data['to']){
- $this->error('不能复制同月的任务');
- }
- $ret = (new \app\common\model\PatrolTask())->plan_data($data,$this->orgId);
- if($ret){
- if($type==1){
- $this->success('操作成功',url('index',['mode'=>$mode]));
- }else{
- $this->success('操作成功',url('calendar',['mode'=>$mode]));
- }
- }else{
- $this->error((new \app\common\model\PatrolTask())->getError());
- }
- }else{
- $from = (new \app\common\model\PatrolTask())->get_task_month($this->orgId,$mode);
- //间隔1月往后数11次
- $timenow = new \Datetime();
- $datetin = \DateInterval::createFromDateString('1 month');
- $datePer = new \DatePeriod($timenow, $datetin, 11);
- $to = [];
- foreach ($datePer as $day) {
- $m = $day->format('Y-m');
- $to[]= $m;
- }
- $this->assign('from',$from);
- $this->assign('to',$to);
- $this->assign('mode',$mode);
- $this->assign('type',$type);
- return $this->fetch();
- }
- }
- public function batch($mode){
- $type = input('type');
- if(request()->isPost()){
- $data = request()->post();
- $minmonth = date('Y-m');
- if($minmonth >= $data['from']){
- $this->error('参数错误');
- }
- $where = array(
- 'org_id' => $this->orgId,
- 'patrol_mode' => $data['mode'],
- 'del' => 0,
- 'create_yyyymm' => date('Ym',strtotime($data['from'].'-01'))
- );
- $default['del'] = 1;
- $default['del_user_id'] = $this->userId;
- $default['del_time'] = date('Y-m-d H:i:s');
- $result = Db::name('patrol_task')
- ->where($where)
- ->update($default);
- $type = (new Task())::TASK_TYPE_PATROL;
- // 检查是否存在任务栏数据中
- $dailyIds = Db::name('patrol_task_task')
- ->alias('dt')
- ->join('task t','t.bus_id = dt.id')
- ->where('t.type',$type)
- ->where('dt.org_id',$this->orgId)
- ->where('dt.del',0)
- ->where('dt.create_yyyymm',date('Ym',strtotime($data['from'].'-01')))
- ->column('dt.id');
- if($result){
- if($dailyIds){
- Db::name('task')
- ->where('bus_id', 'in',implode(',',$dailyIds))
- ->where('type', $type)
- ->delete();
- }
- if($type==1){
- $this->success('操作成功',url('index',['mode'=>$mode]));
- }else{
- $this->success('操作成功',url('calendar',['mode'=>$mode]));
- }
- }else{
- $this->error('删除失败');
- }
- }else{
- $from = (new \app\common\model\PatrolTask())->get_task_month($this->orgId,$mode);
- $minmonth = date('Y-m');
- $aa = [];
- foreach ($from as $k=>$v){
- if($minmonth < $v['create_yyyymm']){
- $aa[] = $v['create_yyyymm'];
- }
- }
- $this->assign('from',$aa);
- $this->assign('mode',$mode);
- $this->assign('type',$type);
- return $this->fetch();
- }
- }
- }
|