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];
}
$map[] = ['org_id','=',$this->orgId];
$map[] =['del','=',0];
$map= empty($map) ? true: $map;
//数据查询
$lists = Db::name('daily_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('daily_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);
}
//数据返回
$totalCount = Db::name('daily_task')->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 add($id=0){
if(request()->isPost()){
$res = model('DailyTask')->updates();
if($res){
$this->success('操作成功',url('index'));
}else{
$this->error(model('DailyTask')->getError());
}
}else{
$title = '新增';
if($id){
$title = '编辑';
$info = Db::name('DailyTask')->where('id',$id)->find();
$info['daily_addr'] = Db::name('daily_task_addr')->where('task_id',$info['id'])->column('daily_id');
$daily_user = Db::name('daily_task_user')->where('task_id',$info['id'])->column('user_id');
$info['daily_user'] = isset($daily_user)?implode(',',$daily_user):'';
$this->assign('info',$info);
}
$dailyAddr= Db::name('Daily')->where('org_id',cur_org_id())->where('del',0)->where('enable',1)->select();
$dailyUser = model('WorkTypeMode')->getRolesUser(9,cur_org_id());
$this->assign('dailyAddr',$dailyAddr);
$this->assign('dailyUser',$dailyUser);
$this->assign('send_user_num',0);
$this->assign('title',$title);
return $this->fetch();
}
}
/**
* 删除记录
* @param int $id
*/
public function del($id=0){
if(!$id){
$this->error('参数错误');
}
$res = model('DailyTask')->del($id,$this->userId);
if($res){
$this->success('删除成功');
}else{
$this->error('删除失败');
}
}
public function info($id=0){
if(!$id){
$this->error('参数错误');
}
$info=Db::name('daily_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('daily_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('daily_task_addr')
->alias('dta')
->join('daily d','d.id=dta.daily_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('daily_record')
->alias('dr')
->field('dr.*,d.title')
->Leftjoin('daily d', 'd.id=dr.daily_id')
->where($map)
->order('dr.id','desc')
->select();
foreach ($lists as $k => $v) {
$lists[$k]['task_title'] = Db::name('daily_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('DailyTask')->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('DailyTask')->plan_data($data,$this->orgId);
if($ret){
if($type==1){
$this->success('操作成功',url('index'));
}else{
$this->success('操作成功',url('calendar'));
}
}else{
$this->error(model('DailyTask')->getError());
}
}else{
$from = model('DailyTask')->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('DailyTask')->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('DailyTask')->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();
}
}
}