123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AttendanceGroupUser extends Model
- {
- public function add(){
- $data = [
- 'user_id' => input('uId/d',0),
- 'group_id' => input('groupId/d',0),
- 'org_id' => input('orgId/d',0),
- ];
- if($data['group_id'] <= 0){
- HelpHander::error('未选择考勤组');
- }
- $info = Db::name('attendance_group_user')
- ->where('user_id',$data['user_id'])
- ->where('org_id',$data['org_id'])
- ->find();
- if($info){
- if($info['group_id'] != $data['group_id']){
- $ret = Db::name('attendance_group_user')->where('id',$info['id'])->setField('group_id',$data['group_id']);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- }
- }else{
- $ret = Db::name('attendance_group_user')->insertGetId($data);
- if(!$ret){
- HelpHander::error('操作失败');
- }
- }
- return true;
- }
- public function lists($page,$size,$name,$phone,$code,$jobId,$depId,$orgId){
- $map[] = ['u.del','=',0];
- $map[] = ['u.enable','=',1];
- $map[] = ['j.org_id','=',$orgId];
- if($name){
- $map[] = ['ui.name','like','%'.$name.'%'];
- }
- if($phone){
- $map[] = ['u.phone','like','%'.$phone.'%'];
- }
- if($code){
- $map[] = ['u.code','like','%'.$code.'%'];
- }
- if($jobId > 0){
- $map[] = ['uj.job_id','=',$jobId];
- }else{
- if($depId > 0){
- $jobs = Db::name('job')
- ->where('dep_id',$depId)
- ->where('enable',1)
- ->where('del',0)
- ->column('id');
- if($jobs){
- $map[] = ['uj.job_id','in',$jobs];
- }
- }
- }
- $lists = Db::name('user')
- ->alias('u')
- ->join('user_info ui','ui.user_id = u.id')
- ->join('user_job uj','uj.user_id = u.id')
- ->join('job j','j.id = uj.job_id')
- ->where($map)
- ->field('ui.user_id,ui.name as user_name,u.phone,u.code')
- ->page($page,$size)
- ->order('u.sorts asc,u.id desc')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $k=>$v){
- $group = Db::name('attendance_group_user')
- ->alias('agu')
- ->join('attendance_group ug','ug.id = agu.group_id')
- ->field('ug.id,ug.name')
- ->where('agu.user_id',$v['user_id'])
- ->where('agu.org_id',$orgId)
- ->find();
- $lists[$k]['group_id'] = null;
- $lists[$k]['group_name'] = '';
- if($group){
- $lists[$k]['group_id'] = $group['id'];
- $lists[$k]['group_name'] = $group['name'];
- }
- }
- $total = Db::name('user')
- ->alias('u')
- ->join('user_info ui','ui.user_id = u.id')
- ->join('user_job uj','uj.user_id = u.id')
- ->join('job j','j.id = uj.job_id')
- ->where($map)->count();
- $data = [
- 'total' => $total,
- 'list' => $lists?$lists:[]
- ];
- return $data;
- }
- 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_info')->where('user_id',$userId)->value('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_info')->where('user_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();
- }
- $data = [
- 'group' => [
- 'name' => $class['group_name'],
- 'dates' => $newdates2,
- 'rule' => $rule,
- 'day' => $day,
- 'cate' => $yinfo['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;
- }
- }
|