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];
}
$is_exception = input('is_exception','','trim');
if($is_exception != ''){
$map[] = ['is_exception','=',$is_exception];
}
$plan_id = input('plan_id','','trim');
if($plan_id != ''){
$map[] = ['plan_id','=',$plan_id];
}
$map[] = ['org_id','=',$this->orgId];
$map[] =['del','=',0];
$map= empty($map) ? true: $map;
//数据查询
$lists = Db::name('green_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('green_task_user')
->alias('gtu')
->join('user u','u.id=gtu.user_id')
->where('gtu.green_task_id',$v['id'])
->column('u.real_name');
$lists[$k]['task_user']=$taskUser?implode(',',$taskUser):'';
}
//数据返回
$totalCount = Db::name('green_task')->where($map)->count();
$totalPage = ceil($totalCount/$length);
$result['page'] = $page;
$result['total'] = $totalPage;
$result['records'] = $totalCount;
$result['rows'] = $lists;
return json($result);
}else{
$plans = model('GreenPlan')->getList($this->orgId);
$this->assign('plans',$plans);
$this->assign('meta_title','绿化养护任务列表');
return $this->fetch();
}
}
/**
* 新增/编辑
*/
public function add($id=0){
$type = input('type');
$isCalendar = input('isCalendar');
if(request()->isPost()){
$model = new \app\common\model\GreenTask();
$res = $model->updates();
if($res){
if($type==1){
if($isCalendar==1){
$this->success('操作成功',url('calendar'));
}else{
$this->success('操作成功');
}
}else{
$this->success('操作成功',url('index'));
}
}else{
$this->error($model->getError());
}
}else{
$meta_title = '新增绿化养护计划';
if($id){
$info = Db::name('green_task')->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']));
$info['green_form'] = Db::name('green_task_addr')
->where('green_task_id',$id)
->column('green_form_id');
$info['task_user'] = Db::name('green_task_user')
->where('green_task_id',$id)
->column('user_id');
$addrForms = Db::name('green_task_addr')
->alias('a')
->join('green_addr_form b','a.green_form_id = b.id')
->join('green_addr c','c.id = a.green_addr_id')
->where('a.green_task_id',$info['id'])
->field('a.green_addr_id as addrId,a.green_form_id as formId,b.title as formTitle,c.title as addrTitle')
->select();
$info['addrForms'] = $addrForms?$addrForms:[];
$this->assign('info',$info);
$meta_title = '编辑绿化养护计划';;
}
//获取任务内容
$greenAddrForm = (new \app\common\model\GreenAddrForm())->getByList();
//获取用户
$user= (new User())->getGreenWorker();
$start = input('start','');
$end = input('end','');
$this->assign('greenForm',$greenAddrForm);
$this->assign('user',$user);
$this->assign('start',$start?date('Y-m-d H:i',strtotime($start)):'');
$this->assign('end',$end?date('Y-m-d H:i',strtotime($end)):'');
$this->assign('meta_title',$meta_title);
$this->assign('type',$type);
$this->assign('isCalendar',$isCalendar);
$this->assign('address',model('GreenAddr')->list());
return $this->fetch();
}
}
/**
* 删除记录
*/
public function del($id=0){
if(!$id){
$this->error('参数错误');
}
$info = Db::name('green_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
if(!$info){
$this->error('任务不存在');
}
if($info['status'] != 6){
$this->error('任务未关闭不可删除');
}
Db::startTrans();
try{
$res = Db::name('green_task')->where('id',$id)->update([
'del'=>1,
'del_user_id'=>is_login(),
'del_time'=>getTime(),
]);
if(!$res){
\exception('操作失败');
}
$ctype = (new Task())::TASK_TYPE_GREEN;
// 如果在任务栏删除任务
Db::name('task')->where('bus_id',$id)->where('type',$ctype)->delete();
Db::commit();
}catch (Exception $e){
Db::rollback();
$this->error('删除失败');
}
$this->success('删除成功');
}
/**
* 关闭记录
*/
public function close($id=0){
if(!$id){
$this->error('参数错误');
}
$info = Db::name('green_task')->where('org_id',$this->orgId)->where('id',$id)->where('del',0)->find();
if(!$info){
$this->error('任务不存在');
}
if($info['status'] == 6){
$this->error('任务已关闭');
}
Db::startTrans();
try{
$res = Db::name('green_task')->where('id',$id)->update([
'status'=>6,
'update_time'=>getTime(),
]);
if(!$res){
\exception('操作失败');
}
// 如果在任务栏删除任务
Db::name('task')->where('bus_id',$id)->where('type',5)->delete();
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();
if(!$info){
$this->error('记录不存在');
}
$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=gtu.user_id')
->where('gtu.green_task_id',$info['id'])
->column('u.real_name');
$info['task_user']=implode(',',$task_user);
$addrForms = Db::name('green_task_addr')
->alias('a')
->join('green_addr_form b','a.green_form_id = b.id')
->join('green_addr c','c.id = a.green_addr_id')
->where('a.green_task_id',$info['id'])
->field('a.*,b.title as formTitle,c.title as addrTitle')
->select();
$addrForms = $addrForms?$addrForms:[];
foreach ($addrForms as $k=>$v){
$record = Db::name('green_task_record')
->alias('a')
->join('user b','a.user_id = b.id')
->where('a.green_task_id',$v['green_task_id'])
->where('a.green_addr_id',$v['green_addr_id'])
->field('b.real_name as uname,a.*')
->find();
if($record){
$cj = $record['check_json']?json_decode($record['check_json'],true):[];
$record['check_json'] = $cj;
}
$addrForms[$k]['record'] = $record?$record:[];
}
$info['addrForms'] = $addrForms;
$this->assign('meta_title',$meta_title);
$this->assign('info',$info);
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 if($v['status'] == 3){
$arr['color'] = '#dd5a43';
}else if($v['status'] == 5){
$arr['color'] = '#53a2a7';
}else{
$arr['color'] = '#5b53a7';
}
$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);
$type = (new Task())::TASK_TYPE_GREEN;
// 检查是否存在任务栏数据中
$greenIds = Db::name('green_task')
->alias('gt')
->join('task t','t.bus_id = gt.id')
->where('t.type',$type)
->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();
}
}
}