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[] =['patrol_mode','=',$mode];
$map= empty($map) ? true: $map;
//数据查询
$lists = Db::name('quality_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('quality_task_user')
->alias('ptu')
->field('u.real_name')
->join('user u','u.id=ptu.user_id')
->where('ptu.quality_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('quality_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('QualityPlan')->getListByMode($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\QualityTask();
$res = $model->updates();
if($res){
$this->success('操作成功',url('index'));
}else{
$this->error($model->getError());
}
}else{
$meta_title = '新增品控任务';
if($id){
$info = Db::name('quality_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['lines']=$info['lines']?json_decode($info['lines'],true):[];
$info['patrol_form'] =\db('quality_task_addr')->where('quality_task_id',$id)->column('quality_group_id');
$info['task_user']=\db('quality_task_user')->where('quality_task_id',$id)->column('user_id');
// $addrForms = Db::name('patrol_task_addr')
// ->alias('a')
// ->join('patrol_addr_form b','a.patrol_form_id = b.id')
// ->join('address c','c.id = a.address_id')
// ->where('a.patrol_task_id',$info['id'])
// ->field('a.address_id as addrId,a.patrol_form_id as formId,b.title as formTitle,c.title as addrTitle')
// ->select();
// $info['addrForms'] = $addrForms?$addrForms:[];
$addrForms = Db::name('quality_task_addr')
->alias('a')
->join('quality_group c','c.id = a.quality_group_id')
->where('a.quality_task_id',$info['id'])
->field('a.quality_group_id as addrId,c.title as title,a.sorts,c.type,c.busids,c.forms')
->order('a.id asc')
->select();
$addrForms = $addrForms?$addrForms:[];
foreach ($addrForms as $k=>$v){
$forms = [];
if($v['forms']){
$forms = Db::name('quality_cate')
->where('id','in',explode(',',$v['forms']))
->field('id,title')->select();
foreach ($forms as $k11=>$v11){
$forms[$k11]['type'] = 1;
}
}
$buss = [];
if($v['type'] >0){
if(in_array($v['type'],[1,2,3,4,5])){
$table = 'address';
}
// else if($v['type'] == 5){ // 日常
// $table = 'daily';
// }
else if($v['type'] == 6){ // 设备
$table = 'device';
}
$buss = Db::name($table)
->where('id','in',explode(',',$v['busids']))
->select();
}
$addrForms[$k]['buss'] = $buss?$buss:[];
$addrForms[$k]['forms'] = $forms?$forms:[];
$addrForms[$k]['busids'] = $v['busids']?explode(',', $v['busids']):[];
}
$info['addrForms'] = $addrForms?$addrForms:[];
$info['addrForms_json'] = json_encode($addrForms);
$this->assign('info',$info);
$meta_title = '编辑品控任务';
}
//获取任务内容
$patrolAddrForm=(new \app\common\model\QualityGroup())->getByModeList();
$user=(new User())->getPatrolWorker(18);
$start = input('start','');
$end = input('end','');
$this->assign('patrolForm',$patrolAddrForm);
$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('address',(new \app\common\model\Address())->getListByType($mode+2));
return $this->fetch();
}
}
/**
* 删除记录
*/
public function del($id=0){
if(!$id){
$this->error('参数错误');
}
$info = Db::name('quality_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('quality_task')->where('id',$id)->update([
'del'=>1,
'del_user_id'=>is_login(),
'del_time'=>getTime(),
]);
if(!$res){
\exception('操作失败');
}
// 如果在任务栏删除任务
model('Task')->del(5,$id);
Db::commit();
}catch (Exception $e){
Db::rollback();
$this->error('删除失败');
}
$this->success('删除成功');
}
/**
* 关闭记录
*/
public function close($id=0){
if(!$id){
$this->error('参数错误');
}
$info = Db::name('quality_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('quality_task')->where('id',$id)->update([
'status'=>6,
'update_time'=>getTime(),
]);
if(!$res){
\exception('操作失败');
}
// 如果在任务栏删除任务
model('Task')->del(5,$id);
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('quality_task')
->field('id,start_time,end_time,status,title')
->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('quality_task_user')
->alias('ptu')
->join('user u','u.id=ptu.user_id')
->where('ptu.quality_task_id',$info['id'])
->column('u.real_name');
$info['task_user']=implode(',',$task_user);
$addrForms = Db::name('quality_task_addr')
->alias('a')
->join('quality_group c','c.id = a.quality_group_id')
->where('a.quality_task_id',$info['id'])
->field('a.*,c.title as addrTitle')
->select();
$addrForms = $addrForms?$addrForms:[];
foreach ($addrForms as $k=>$v){
// $btitles = Db::name('patrol_addr_form')->where('id','in',$v['patrol_forms'])->column('title');
// $addrForms[$k]['formTitle'] = $btitles?implode(',',$btitles):'';
$record = Db::name('quality_record')
->alias('a')
->join('user b','a.user_id = b.id')
->where('a.quality_task_id',$v['quality_task_id'])
->where('a.quality_addr_id',$v['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;
}
$group = Db::name('quality_group')
->where('id',$record['quality_group_id'])
->find();
$t = '';
$ts = '';
if(!empty($group)){
switch ($group['type']){
case 1:
$t='巡更';
break;
case 2:
$t='巡视';
break;
case 3:
$t='巡查';
break;
case 4:
$t='巡检';
break;
case 5:
$t='日常工作';
break;
case 6:
$t='设备台账';
break;
}
if($group['type'] >0){
$busids = explode(',',$group['busids']);
$bus = (new \app\common\model\QualityGroup())->getListByType($group['type'],$record['org_id']);
$nbus = [];
foreach ($bus as $kk=>$vv){
if(in_array($vv['id'],$busids)){
$nbus[] = $vv['title'];
}
}
$ts = implode(',',$nbus);
}
}
$addrForms[$k]['ts'] = $ts;
if(!empty($t)){
$addrForms[$k]['addrTitle'] = $v['addrTitle'].'('.$t.')';
}
$addrForms[$k]['record'] = $record?$record:[];
}
$info['addrForms'] = $addrForms;
$this->assign('meta_title',$meta_title);
$this->assign('info',$info);
// $this->assign('mode',$mode);
// $this->assign('modeName',$modeName);
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\QualityTask())->get_list_by_time($this->orgId,$mode,$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($mode){
$type = input('type');
if(request()->isPost()){
$data = request()->post();
if($data['from'] == $data['to']){
$this->error('不能复制同月的任务');
}
$ret = (new \app\common\model\QualityTask())->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\QualityTask())->getError());
}
}else{
$from = (new \app\common\model\QualityTask())->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('quality_task')
->where($where)
->update($default);
$type = (new Task())::TASK_TYPE_QUALITY;
// 检查是否存在任务栏数据中
$dailyIds = Db::name('quality_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\QualityTask())->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();
}
}
}