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];
}
$map[] = ['org_id','=',$this->orgId];
$map[] =['del','=',0];
$map= empty($map) ? true: $map;
//数据查询
$lists = Db::name('green_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('green_task')->where('plan_id',$v['id'])->where('del',0)->count();
}
//数据返回
$totalCount = Db::name('green_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('meta_title','计划列表');
return $this->fetch();
}
}
/**
* 新增/编辑
*/
public function add($id=0,$mode=0){
if(request()->isPost()){
$res = model('GreenPlan')->updates($this->userId);
if($res){
$this->success('操作成功',url('index'));
}else{
$this->error(model('GreenPlan')->getError());
}
}else{
if($id > 0){ // 复制计划
$info = Db::name('green_plan')->where('id',$id)->find();
if($info){
$info['content'] = json_decode($info['content'],true);
}
$this->assign('info',$info);
}
$meta_title = '新增'.'计划';
//获取任务内容
$greenForm=(new \app\common\model\GreenAddrForm())->getByList();
$this->assign('greenForm',$greenForm);
//获取用户
$user=(new User())->getGreenWorker();
$greenAddr = Db::name('green_addr')
->where('org_id',cur_org_id())
->where('enable',1)
->where('del',0)
->select();
$this->assign('address',$greenAddr);
$this->assign('user',$user);
$this->assign('meta_title',$meta_title);
return $this->fetch();
}
}
public function detail($id=0){
$info = Db::name('green_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);
$unames = Db::name('user')->where('id','in',$info['content']['userIds'])->column('real_name');
$info['unames'] = $unames?implode(',',$unames):'';
$this->assign('info',$info);
return $this->fetch();
}
/**
* 关闭计划
*/
public function close($id=0){
if(!$id){
$this->error('参数错误');
}
$info = Db::name('green_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('green_plan')->where('id',$id)->update([
'status' => 2,
'close' => 1,
'close_user_id' => $this->userId,
'close_time' => getTime(),
]);
if(!$res){
\exception('操作失败');
}
// 关闭任务
Db::name('green_task')
->where('del',0)
->where('plan_id',$id)
->where('status','in',[0,1])
->update([
'status' => 6,
'update_time' => getTime(),
]);
// 已经加入任务的,从任务中删除
$taskIds = Db::name('green_task')->where('del',0)->where('plan_id',$id)->column('id');
if($taskIds){
$ctype = (new Task())::TASK_TYPE_GREEN;
Db::name('task')
->where('org_id',$this->orgId)
->where('type',$ctype)
->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('green_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('green_plan')->where('id',$id)->update([
'del' => 1,
'del_user_id' => $this->userId,
'del_time' => getTime(),
]);
if(!$res){
\exception('操作失败');
}
// 已经加入任务的,从任务中删除
$taskIds = Db::name('green_task')->where('del',0)->where('plan_id',$id)->column('id');
if($taskIds){
$ctype = (new Task())::TASK_TYPE_GREEN;
Db::name('task')
->where('org_id',$this->orgId)
->where('type',$ctype)
->where('bus_id','in',$taskIds)
->delete();
}
// 删除任务
Db::name('green_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){
$meta_title = '绿化养护轨迹';
if(!$id){
$this->error('参数错误');
}
$info=Db::name('green_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('green_task_user')
->alias('gtu')
->join('user u','u.id=ptu.user_id')
->where('gtu.green_task_id',$info['id'])
->column('u.real_name');
$info['task_user']=implode(',',$task_user);
$map[] = ['gtr.green_task_id', '=', $info['id']];
$map[] = ['gtr.org_id', '=', $this->orgId];
//数据查询
$lists = Db::name('green_task_record')
->alias('gtr')
->field('gtr.*,ga.title')
->Leftjoin('green_addr ga', 'ga.id=gtr.green_addr_id')
->where($map)
->order('id','desc')
->select();
foreach ($lists as $k => $v) {
$lists[$k]['task_title'] = Db::name('green_task')
->where('id', $v['green_task_id'])->value('title');
$lists[$k]['task_addr_form'] = Db::name('green_addr_form')
->alias('a')
->join('green_task_addr b','a.id = b.green_form_id')
->where('b.id', $v['green_task_addr_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);
return $this->fetch();
}
public function calendar(){
return $this->fetch();
}
//获取正月数据
public function taskjson(){
$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\GreenTask())->get_list_by_time($this->orgId,$start,$end);
foreach ($list as $k=>$v){
$arr = array(
'taskid' => $v['id'],
'title' => "{$v['title']}
执行人:{$v['users']}
开始时间:{$v['start_time']}
结束时间:{$v['end_time']}
",
'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(){
$type = input('type');
if(request()->isPost()){
$data = request()->post();
if($data['from'] == $data['to']){
$this->error('不能复制同月的任务');
}
$ret = (new \app\common\model\GreenTask())->plan_data($data,$this->orgId);
if($ret){
if($type==1){
$this->success('操作成功',url('index'));
}else{
$this->success('操作成功',url('calendar'));
}
}else{
$this->error((new \app\common\model\GreenTask())->getError());
}
}else{
$from = (new \app\common\model\GreenTask())->get_task_month($this->orgId);
//间隔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('type',$type);
return $this->fetch();
}
}
public function batch(){
$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,
'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('green_task')
->where($where)
->update($default);
$ctype = (new Task())::TASK_TYPE_GREEN;
// 检查是否存在任务栏数据中
$greenIds = Db::name('green_task')
->alias('gt')
->join('task t','t.bus_id = dt.id')
->where('t.type',$ctype)
->where('gt.org_id',$this->orgId)
->where('gt.del',0)
->where('gt.create_yyyymm',date('Ym',strtotime($data['from'].'-01')))
->column('gt.id');
if($result){
if($greenIds){
Db::name('task')
->where('bus_id', 'in',implode(',',$greenIds))
->where('type', $type)
->delete();
}
if($type==1){
$this->success('操作成功',url('index'));
}else{
$this->success('操作成功',url('calendar'));
}
}else{
$this->error('删除失败');
}
}else{
$from = (new \app\common\model\GreenTask())->get_task_month($this->orgId);
$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('type',$type);
return $this->fetch();
}
}
}