| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 | <?phpnamespace app\common\model;use app\hander\HelpHander;use think\Db;use think\Exception;class AttendanceGroupUser extends Base{    public function userGroup($userId,$orgId,$day){        $reissue_count = config('reissue_count');        $reissue_day = config('reissue_day');        $rule = '每月最多补卡'.$reissue_count.'次,可补过去'.$reissue_day.'天内的缺卡';        if($day == date('Y-m-d')){ // 今日            $data = $this->userCurGroup($userId,$orgId,$rule);        }else{ // 历史            $device_sn = Db::name('user')->where('id',$userId)->value('device_sn');            $device_sn = $device_sn?$device_sn:"";            $info = Db::name('attendance_user_class')                ->where('day',$day)                ->where('user_id',$userId)                ->find();            if($info){                $record = Db::name('attendance_record')                    ->where('user_class_id',$info['id'])                    ->where('user_id',$userId)                    ->where('effective',1)                    ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')                    ->order('create_time asc')                    ->select();                $group = Db::name('attendance_group')->where('id',$info['group_id'])->find();                $addr = [];                if($group['addr']){                    $addr = Db::name('attendance_addr')                        ->where('id','in',$group['addr'])                        ->where('enable',1)                        ->where('del',0)                        ->field('id,title,distance,lat,lng')                        ->select();                }                $dates = json_decode($info['content'],true);                $newdate = [];                foreach ($dates as $k=>$v){                    $newdate[] = [                        'stime' => date('H:i',strtotime($v['stime'])),                        'etime' => date('H:i',strtotime($v['etime']))                    ];                }                $data = [                    'group' => [                        'name' => $group['name'],                        'dates' => $newdate?$newdate:null,                        'rule' => $rule,                        'day' => $day,                        'cate' => $group['cate'],                        'addr' => $addr?$addr:[]                    ],                    'record' => $record?$record:[],                    'device_sn' => $device_sn                ];            }else{ // 无考勤                $record = Db::name('attendance_record')                    ->where('create_time','>=',date('Y-m-d').' 00:00:00')                    ->where('create_time','<=',date('Y-m-d').' 00:00:00')                    ->where('user_id',$userId)                    ->where('effective',1)                    ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')                    ->order('create_time asc')                    ->select();                $data = [                    'group' => [                        'name' => '未设置考勤组',                        'dates' => null,                        'rule' => $rule,                        'day' => $day,                        'cate' => '',                        'addr' => []                    ],                    'record' => $record?$record:[],                    'device_sn' => $device_sn                ];            }        }        return $data;    }    private function userCurGroup($userId,$orgId,$rule){        $device_sn = Db::name('user')->where('id',$userId)->value('device_sn');        $curTime = time();        $day = date('Y-m-d');        $yday = date('Y-m-d',time() - 86400);        $nday = date('Y-m-d',time() + 86400);        // 获取今天的考勤组        $info = Db::name('attendance_user_class')            ->where('day',$day)            ->where('user_id',$userId)            ->where('status',0)            ->find();        if($info){ // 今日班次            $record = Db::name('attendance_record')                ->where('user_class_id',$info['id'])                ->where('user_id',$userId)                ->where('effective',1)                ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')                ->order('create_time asc')                ->select();            $group = Db::name('attendance_group')->where('id',$info['group_id'])->find();            $addr = [];            if($group['addr']){                $addr = Db::name('attendance_addr')                    ->where('id','in',$group['addr'])                    ->where('enable',1)                    ->where('del',0)                    ->field('id,title,distance,lat,lng')                    ->select();            }            $dates = json_decode($info['content'],true);            $newdate = [];            foreach ($dates as $k=>$v){                $newdate[] = [                    'stime' => date('H:i',strtotime($v['stime'])),                    'etime' => date('H:i',strtotime($v['etime']))                ];            }            $data = [                'group' => [                    'name' => $group['name'],                    'dates' => $newdate,                    'rule' => $rule,                    'day' => $day,                    'cate' => $group['cate'],                    'addr' => $addr?$addr:[]                ],                'record' => $record?$record:[],                'device_sn' => $device_sn            ];        }else{ // 今日班次不存在            // 昨日班次            $yinfo = Db::name('attendance_user_class')                ->where('day',$yday)                ->where('user_id',$userId)                ->where('next',1)                ->where('status',0)                ->find();            // 今日班次            $class = model('AttendanceGroupClass')->getClassByDay($userId);            if(!$class && !$yinfo){                $record = Db::name('attendance_record')                    ->where('create_time','>=',date('Y-m-d').' 00:00:00')                    ->where('user_id',$userId)                    ->where('effective',1)                    ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')                    ->order('create_time asc')                    ->select();                $data = [                    'group' => [                        'name' => '未设置考勤组',                        'dates' => [],                        'rule' => $rule,                        'day' => $day,                        'cate' => '',                        'addr' => []                    ],                    'record' => $record?$record:[],                    'device_sn' => $device_sn                ];            }else if($yinfo && !$class){ // 昨天班次                $group = Db::name('attendance_group')->where('id',$yinfo['group_id'])->find();                $record = Db::name('attendance_record')                    ->where('user_class_id',$yinfo['id'])                    ->where('user_id',$userId)                    ->where('effective',1)                    ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')                    ->order('create_time asc')                    ->select();                $addr = [];                if($group['addr']){                    $addr = Db::name('attendance_addr')                        ->where('id','in',$group['addr'])                        ->where('enable',1)                        ->where('del',0)                        ->field('id,title,distance,lat,lng')                        ->select();                }                $dates = json_decode($yinfo['content'],true);                $newdate = [];                foreach ($dates as $k=>$v){                    $newdate[] = [                        'stime' => date('H:i',strtotime($v['stime'])),                        'etime' => date('H:i',strtotime($v['etime']))                    ];                }                $data = [                    'group' => [                        'name' => $group['name'],                        'dates' => $newdate,                        'rule' => $rule,                        'day' => $day,                        'cate' => $group['cate'],                        'addr' => $addr?$addr:[]                    ],                    'record' => $record?$record:[],                    'device_sn' => $device_sn                ];            }else if(!$info && $class){ // 今日班次不为空                $nday = date('Y-m-d',time() + 86400);                $content = json_decode($class['content'],true);                $dates = $content['dates'];                $newdates = [];                $newdates2 = [];                foreach ($dates as $k=>$v) {                    $newdates[] = [                        "stime" => $v['snext'] > 0 ? $nday . ' ' . $v['stime'] . ':00' : $day . ' ' . $v['stime'] . ':00',                        "etime" => $v['enext'] > 0 ? $nday . ' ' . $v['etime'] . ':00' : $day . ' ' . $v['etime'] . ':00',                        "sstatus" => 0,                        "estatus" => 0,                    ];                    $newdates2[] = [                        "stime" => $v['stime'],                        "etime" => $v['etime'],                    ];                }                $addr = [];                if($class['addr']){                    $addr = Db::name('attendance_addr')                        ->where('id','in',$class['addr'])                        ->where('enable',1)                        ->where('del',0)                        ->field('id,title,distance,lat,lng')                        ->select();                }                $group = Db::name('attendance_group')                    ->where('id',$yinfo['group_id'])->find();                $data = [                    'group' => [                        'name' => $class['group_name'],                        'dates' => $newdates2,                        'rule' => $rule,                        'day' => $day,                        'cate' => $group['cate'],                        'addr' => $addr?$addr:[]                    ],                    'record' => [],                    'device_sn' => $device_sn                ];            }else{                $nday = date('Y-m-d',time() + 86400);                $content = json_decode($class['content'],true);                $dates = $content['dates'];                $newdates = [];                $newdates2 = [];                foreach ($dates as $k=>$v) {                    $newdates[] = [                        "stime" => $v['snext'] > 0 ? $nday . ' ' . $v['stime'] . ':00' : $day . ' ' . $v['stime'] . ':00',                        "etime" => $v['enext'] > 0 ? $nday . ' ' . $v['etime'] . ':00' : $day . ' ' . $v['etime'] . ':00',                        "sstatus" => 0,                        "estatus" => 0,                    ];                    $newdates2[] = [                        "stime" => $v['stime'],                        "etime" => $v['etime'],                    ];                }                if($curTime >= strtotime($newdates[0]['stime']) - 2*60){ // 显示今日                    $nday = date('Y-m-d',time() + 86400);                    $content = json_decode($class['content'],true);                    $dates = $content['dates'];                    $newdates = [];                    $newdates2 = [];                    foreach ($dates as $k=>$v) {                        $newdates[] = [                            "stime" => $v['snext'] > 0 ? $nday . ' ' . $v['stime'] . ':00' : $day . ' ' . $v['stime'] . ':00',                            "etime" => $v['enext'] > 0 ? $nday . ' ' . $v['etime'] . ':00' : $day . ' ' . $v['etime'] . ':00',                            "sstatus" => 0,                            "estatus" => 0,                        ];                        $newdates2[] = [                            "stime" => $v['stime'],                            "etime" => $v['etime'],                        ];                    }                    $addr = [];                    if($class['addr']){                        $addr = Db::name('attendance_addr')                            ->where('id','in',$class['addr'])                            ->where('enable',1)                            ->where('del',0)                            ->field('id,title,distance,lat,lng')                            ->select();                    }                    $data = [                        'group' => [                            'name' => $class['group_name'],                            'dates' => $newdates2,                            'rule' => $rule,                            'day' => $day,                            'cate' => $yinfo['cate'],                            'addr' => $addr?$addr:[]                        ],                        'record' => [],                        'device_sn' => $device_sn                    ];                }else{ // 显示昨日                    $group_name = Db::name('attendance_group')->where('id',$yinfo['group_id'])->value('name');                    $record = Db::name('attendance_record')                        ->where('user_class_id',$yinfo['id'])                        ->where('user_id',$userId)                        ->where('effective',1)                        ->field('id,lat,lng,address,type,from,create_time,kq_time,result,status,cate,remark')                        ->order('create_time asc')                        ->select();                    $dates = json_decode($yinfo['content'],true);                    $newdate = [];                    foreach ($dates as $k=>$v){                        $newdate[] = [                            'stime' => date('H:i',strtotime($v['stime'])),                            'etime' => date('H:i',strtotime($v['etime']))                        ];                    }                    $addr = [];                    if($yinfo['addr']){                        $addr = Db::name('attendance_addr')                            ->where('id','in',$yinfo['addr'])                            ->where('enable',1)                            ->where('del',0)                            ->field('id,title,distance,lat,lng')                            ->select();                    }                    $data = [                        'group' => [                            'name' => $group_name,                            'dates' => $newdate,                            'rule' => $rule,                            'day' => $day,                            'cate' => $yinfo['cate'],                            'addr' => $addr?$addr:[]                        ],                        'record' => $record?$record:[],                        'device_sn' => $device_sn                    ];                }            }        }        return $data;    }}
 |