isAjax()){
//分页参数
$length = input('rows',10,'intval'); //每页条数
$page = input('page',1,'intval'); //第几页
$start = ($page - 1) * $length; //分页开始位置
//排序
$sortRow = input('sidx','sort','trim'); //排序列
$sort = input('sord','asc','trim'); //排序方式
$order = $sortRow.' '.$sort.' ,id desc';
$month= input('month','','trim');
if($month != ''){
$map[] = ['create_yyyymm','=',$month];
}
$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= empty($map) ? true: $map;
//数据查询
$lists = Db::name('device_task')->where($map)
->limit($start,$length)->order($order)->select();
foreach ($lists as $k=>$v){
$lists[$k]['start_time'] = date('Y-m-d H:i',strtotime($v['start_time']));
$lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
$taskUser=Db::name('device_task_user')
->alias('dtu')
->field('u.real_name')
->join('user u','u.id=dtu.user_id')
->where('dtu.task_id',$v['id'])
->select();
$userName = [];
foreach ($taskUser as $kk=>$vv){
$userName[$kk]=$vv['real_name'];
}
$lists[$k]['task_user']=implode(',',$userName);
$info = Db::name('device_cate')
->where('id',$v['cate_id'])
->find();
$lists[$k]['cate_name'] = $info?$info['title']:'';
}
//数据返回
$totalCount = Db::name('device_task')->where($map)->count();
$totalPage = ceil($totalCount/$length);
$result['page'] = $page;
$result['total'] = $totalPage;
$result['records'] = $totalCount;
$result['rows'] = $lists;
return json($result);
}else{
$cate = model('DeviceCate')->list();
$this->assign('cate',$cate);
return $this->fetch();
}
}
/**
* 新增/编辑
*/
public function add($id=0){
if(request()->isPost()){
$res = model('DeviceTask')->updates();
if($res){
$this->success('操作成功',url('index'));
}else{
$this->error(model('DeviceTask')->getError());
}
}else{
$title = '新增';
if($id){
$title = '编辑';
$info = Db::name('DeviceTask')->where('id',$id)->find();
$info['device_addr'] = Db::name('device_task_addr')->where('task_id',$info['id'])->column('device_id');
foreach ($info['device_addr'] as $k=>&$v){
$v = (string)$v;
}
$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']));
$device_user = Db::name('device_task_user')->where('task_id',$info['id'])->column('user_id');
$info['device_user'] = isset($device_user)?implode(',',$device_user):'';
$this->assign('info',$info);
$deviceAddr= Db::name('Device')->where('org_id',cur_org_id())
->where('cate_id',$info['cate_id'])
->where('del',0)
->where('enable',1)->select();
$this->assign('deviceAddr',$deviceAddr);
}
$deviceUser = model('WorkTypeMode')->getRolesUser(10,cur_org_id());
$this->assign('deviceUser',$deviceUser);
$this->assign('send_user_num',0);
$this->assign('title',$title);
$cate = model('DeviceCate')->list();
$this->assign('cate',$cate);
return $this->fetch();
}
}
/**
* 删除记录
* @param int $id
*/
public function del($id=0){
if(!$id){
$this->error('参数错误');
}
$res = model('DeviceTask')->del($id,$this->userId);
if($res){
$this->success('删除成功');
}else{
$this->error('删除失败');
}
}
public function byIdSDel(){
$ids = input('ids',[]);
if(!$ids){
$this->error('参数错误');
}
$res = model('DeviceTask')->byIdSDel($ids,$this->userId);
if($res){
$this->success('删除成功',url('index'));
}else{
$this->error(model('DeviceTask')->getError());
}
}
public function info($id=0){
if(!$id){
$this->error('参数错误');
}
$info=Db::name('device_task')
->field('id,start_time,end_time,status,title')
->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('device_task_user')
->alias('dtu')
->join('user u','u.id=dtu.user_id')
->where('dtu.task_id',$info['id'])
->column('u.real_name');
$info['task_user']=implode(',',$task_user);
$task_addr = Db::name('device_task_addr')
->alias('dta')
->join('device d','d.id=dta.device_id')
->where('dta.task_id',$info['id'])
->column('d.title');
$info['task_addr']=implode(',',$task_addr);
$map[] = ['dr.task_id', '=', $info['id']];
$map[] = ['dr.org_id', '=', $this->orgId];
//数据查询
$lists = Db::name('device_record')
->alias('dr')
->field('dr.*,d.title')
->Leftjoin('device d', 'd.id=dr.device_id')
->where($map)
->order('dr.id','desc')
->select();
foreach ($lists as $k => $v) {
$lists[$k]['task_title'] = Db::name('device_task')
->where('id', $v['task_id'])->value('title');
$lists[$k]['task_user'] = Db::name('user')
->where('id', $v['user_id'])->value('real_name');
}
$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 = model('DeviceTask')->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 = model('DeviceTask')->plan_data($data,$this->orgId);
if($ret){
if($type==1){
$this->success('操作成功',url('index'));
}else{
$this->success('操作成功',url('calendar'));
}
}else{
$this->error(model('DeviceTask')->getError());
}
}else{
$from = model('DeviceTask')->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()){
$result = model('DeviceTask')->batchDel($this->orgId,$this->userId);
if($result){
if($type==1){
$this->success('操作成功',url('index'));
}else{
$this->success('操作成功',url('calendar'));
}
}else{
$this->error('删除失败');
}
}else{
$from = model('DeviceTask')->get_task_month($this->orgId);
$minmonth = date('Y-m');
$aa = [];
foreach ($from as $k=>$v){
// if($minmonth < $v['create_yyyymm']){
// $aa[] = $v['create_yyyymm'];
// }
$aa[] = $v['create_yyyymm'];
}
$this->assign('from',$aa);
$this->assign('type',$type);
return $this->fetch();
}
}
public function delay(){
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;
$status = input('status','','trim');
if($status != ''){
$map[] = ['status','=',$status];
}
$map[] = ['org_id','=',$this->orgId];
$map= empty($map) ? true: $map;
//数据查询
$lists = Db::name('device_task_delay')->where($map)
->limit($start,$length)->order($order)->select();
foreach ($lists as $k=>$v){
$taskTitle = Db::name('device_task')->where('id',$v['task_id'])->value('title');
$lists[$k]['task_title'] = $taskTitle?$taskTitle:'';
$lists[$k]['end_time'] = date('Y-m-d H:i',strtotime($v['end_time']));
$lists[$k]['uname'] = Db::name('user')->where('id',$v['user_id'])->value('real_name');
$lists[$k]['deal_user'] = '';
if($v['deal_user_id'] > 0){
$lists[$k]['deal_user'] = Db::name('user')->where('id',$v['deal_user_id'])->value('real_name');
}
}
//数据返回
$totalCount = Db::name('device_task_delay')->where($map)->count();
$totalPage = ceil($totalCount/$length);
$result['page'] = $page;
$result['total'] = $totalPage;
$result['records'] = $totalCount;
$result['rows'] = $lists;
return json($result);
}else{
return $this->fetch();
}
}
/**
* 处理
*/
public function deal($id = 0){
if(request()->isPost()){
$res = model('DeviceTask')->dealDelay($this->userId,$this->orgId);
if($res){
$this->success('操作成功',url('deal'));
}else{
$this->error(model('DeviceTask')->getError());
}
}else{
$info = Db::name('device_task_delay')->where('id',$id)->find();
$this->assign('info',$info);
return $this->fetch();
}
}
public function getByCate(){
$cate_id = input('id/d',0);
if($cate_id==0){
$this->success('操作成功','',[]);
}
$list = Db::name('device')
->where('org_id',$this->orgId)
->where('del',0)
->where('enable',1)
->where('cate_id',$cate_id)
->select();
$this->success('操作成功','',$list);
}
public function copy($id){
if(request()->isPost()){
$data = request()->post();
if(!$data['day']){
$this->error('日期不能为空');
}
if($data['day'] <= date('Y-m-d')){
$this->error('不能小于今天日期');
}
$id = $data['id'];
$day = $data['day'];
$info = Db::name('device_task')->where('id',$id)->find();
if(!$info){
$this->error('参数错误');
}
$cha = strtotime($info['end_time'])- strtotime($info['start_time']);
$shis = date('H:i:s',strtotime($info['start_time']));
$st = $day.' '.$shis;
$et = strtotime($st)+$cha;
$info['start_time'] = $st;
$info['end_time'] = date('Y-m-d H:i:s',$et);
$info['uuid'] = new_guid();
$ret = model('DeviceTask')->copyTask($info);
$ret ? $this->success('复制成功',url('index')):$this->error('操作失败');
}else{
$this->assign('id',$id);
return $this->fetch();
}
}
}