123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- namespace app\common\model;
- use app\hander\HelpHander;
- use think\Db;
- use think\Exception;
- use think\Model;
- class AttendanceUserClass extends Model
- {
- // 获取当前排班班次
- public function getCurGroup($userId)
- {
- $day = date('Y-m-d');
- $yday = date('Y-m-d', strtotime($day) - 1000);
- $info = Db::name('attendance_user_class')->where('user_id', $userId)->where('day', $day)->find();
- if ($info) { // 当日班次存在直接返回
- return $info;
- }
- // 查看当日是否排班
- $class = model('AttendanceGroupClass')->getClassByDay($userId);
- // 当日不存在,获取昨日未结束班次,看看昨日班次是否跨天
- $info = Db::name('attendance_user_class')->where('user_id', $userId)->where('status', 0)->where('next', 1)->where('day', $yday)->find();
- if ($info) {
- // 检查此跨天班次是否已结束
- if (!$class) { // 今天没有班次
- return $info; // 存在昨日跨天班次,直接返回
- }
- $content = json_decode($class['content'], true);
- $date = $content['dates'];
- $firstTime = strtotime(date('Y-m-d') . $date[0]['stime']);
- if ($firstTime - 2 * 60 <= time()) { // 打卡前2分钟,结束上个班次,开启本班次
- // 结束上个班次
- $ycontent = json_decode($info['content'], true);
- foreach ($ycontent as $k => $v) {
- $ycontent[$k]['sstatus'] = 1;
- $ycontent[$k]['estatus'] = 1;
- }
- $res = Db::name('attendance_user_class')->where('id', $info['id'])->update([
- 'content' => json_encode($ycontent),
- 'status' => 1,
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- if (!$res) { // 状态修改失败,直接抛出错误,终止程序
- \exception('状态修改失败');
- }
- // 添加今日班次
- $ret = $this->saveGroupClass($userId, $class);
- return $ret;
- } else { // 昨天班次可继续打卡
- return $info;
- }
- } else { // 添加今日班次
- if (!$class) { // 今天没有班次
- return false;
- }
- $ret = $this->saveGroupClass($userId, $class);
- return $ret;
- }
- }
- public function saveGroupClass($userId,$class){
- $day = date('Y-m-d');
- $nday = date('Y-m-d',strtotime($day) + 86400);
- // 只添加记录,不牵扯打卡
- $content = json_decode($class['content'],true);
- $dates = $content['dates'];
- $newdates = []; //[{"stime":"2021-03-22 08:00:00","etime":"2021-03-22 18:00:00","sstatus":0,"estatus":0,"ssign":"","esign":""}]
- $next = 0;
- 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,
- "ssign" => "",
- "esign" => ""
- ];
- if($v['snext'] == 1||$v['enext'] == 1){
- $next = 1;
- }
- }
- $userData = [
- 'user_id' => $userId,
- 'group_id' => $class['group_id'],
- 'class_id' => $class['class_id'],
- 'org_id' => $class['org_id'],
- 'content' => json_encode($newdates),
- 'create_time' => date('Y-m-d H:i:s'),
- 'day' => $day,
- 'next' => $next,
- 'status' => 0,
- 'duration' => 0
- ];
- $userClassId = Db::name('attendance_user_class')->insertGetId($userData);
- if(!$userClassId){ // 添加失败,直接抛出错误,终止程序
- \exception('添加今日班次失败');
- }
- $userData['id'] = $userClassId;
- return $userData;
- }
- // 生成某天未打卡人员固定班制的记录
- public function attendanceReissueFixed($day){
- $nday = date('Y-m-d',strtotime($day) + 24*60*60); // 下一天
- // 已参与考勤人员
- $userIds2 = Db::name('attendance_user_class')
- ->where('day',$day)
- ->column('user_id');
- // 查询固定班制考勤组
- $groups = Db::name('attendance_group')->where('type',1)->where('del',0)->select();
- $w = date('w',strtotime($day)); // 是周几
- foreach ($groups as $key=>$val){
- $content = json_decode($val['content'],true);
- $week = $content['week'];
- $classId = 0;
- foreach ($week as $k=>$v){
- if($w == $v['week']){
- $classId = empty($v['class_id'])?0:$v['class_id'];
- }
- }
- // 今日是否有必打卡特殊日
- $sign = $content['sign']?$content['sign']:[];
- foreach ($sign as $k=>$v){
- if($day == $v['day']){
- $classId = empty($v['class_id'])?0:$v['class_id'];
- }
- }
- // 今日是否有不必打卡特殊日
- $unsign = $content['unsign']?$content['unsign']:[];
- foreach ($unsign as $k=>$v){
- if($day == $v['day']){
- $classId = 0;
- }
- }
- if($classId <= 0){
- continue;
- }
- $map = [];
- $map[] = ['group_id','=',$val['id']];
- if($userIds2){
- $map[] = ['user_id','not in',$userIds2];
- }
- $uids = Db::name('attendance_group_user')->where($map)->column('user_id');
- $class = Db::name('attendance_class')->where('id',$classId)->find();
- if(!$uids || !$class){
- continue;
- }
- $content = json_decode($class['content'],true);
- $dates = $content['dates'];
- $newdates = []; //[{"stime":"2021-03-22 08:00:00","etime":"2021-03-22 18:00:00","sstatus":0,"estatus":0,"ssign":"","esign":""}]
- $next = 0;
- 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,
- "ssign" => '',
- "esign" => '',
- ];
- if($v['snext'] == 1||$v['enext'] == 1){
- $next = 1;
- }
- }
- if($next == 0){ // 不跨天
- foreach ($newdates as $k=>$v){
- $newdates[$k]['sstatus'] = 1;
- $newdates[$k]['estatus'] = 1;
- }
- $status = 1;
- }else{ // 跨天
- foreach ($newdates as $k=>$v){
- if($v['stime'] >= $nday.' 00:00:00'){
- $newdates[$k]['sstatus'] = 0;
- }else{
- $newdates[$k]['sstatus'] = 1;
- }
- if($v['etime'] >= $nday.' 00:00:00'){
- $newdates[$k]['estatus'] = 0;
- }else{
- $newdates[$k]['estatus'] = 1;
- }
- }
- $status = 0;
- }
- foreach ($uids as $k=>$v){
- Db::name('attendance_user_class')->insert([
- 'org_id' => $class['org_id'],
- 'user_id' => $v,
- 'class_id' => $class['id'],
- 'group_id' => $val['id'],
- 'content' => json_encode($newdates),
- 'day' => $day,
- 'next' => $next,
- 'status' => $status,
- 'create_time' => date('Y-m-d H:i:s'),
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- }
- }
- }
- // 生成某天未打卡人员排班制的记录
- public function attendanceReissueClass($day){
- $nday = date('Y-m-d',strtotime($day) + 24*60*60); // 下一天
- // 已参与考勤人员
- $userIds2 = Db::name('attendance_user_class')
- ->where('day',$day)
- ->column('user_id');
- $map = [];
- $map[] = ['u.del','=',0];
- $map[] = ['u.enable','=',1];
- $map[] = ['g.del','=',0];
- $map[] = ['agc.day','=',$day];
- if($userIds2){
- $map[] = ['u.id','not in',$userIds2];
- }
- $lists = Db::name('attendance_group_user')
- ->alias('agu')
- ->join('attendance_group_class agc','agu.group_id = agc.group_id')
- ->join('user u','u.id = agu.user_id')
- ->join('attendance_group g','g.id = agu.group_id')
- ->join('attendance_class c','c.id = agc.class_id')
- ->where($map)
- ->field('agu.group_id,agu.user_id,agu.org_id,agc.class_id,c.content,agc.day')
- ->select();
- $lists = $lists?$lists:[];
- foreach ($lists as $key=>$val){
- $content = json_decode($val['content'],true);
- $dates = $content['dates'];
- $newdates = [];
- $next = 0;
- 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,
- "ssign" => '',
- "esign" => '',
- ];
- if($v['snext'] == 1||$v['enext'] == 1){
- $next = 1;
- }
- }
- if($next == 0){ // 不跨天
- foreach ($newdates as $k=>$v){
- $newdates[$k]['sstatus'] = 1;
- $newdates[$k]['estatus'] = 1;
- }
- $status = 1;
- }else{ // 跨天
- foreach ($newdates as $k=>$v){
- if($v['stime'] >= $day.' 00:00:00'){
- $newdates[$k]['sstatus'] = 0;
- }else{
- $newdates[$k]['sstatus'] = 1;
- }
- if($v['etime'] >= $day.' 00:00:00'){
- $newdates[$k]['estatus'] = 0;
- }else{
- $newdates[$k]['estatus'] = 1;
- }
- }
- $status = 0;
- }
- Db::name('attendance_user_class')->insert([
- 'org_id' => $val['org_id'],
- 'user_id' => $val['user_id'],
- 'class_id' => $val['class_id'],
- 'group_id' => $val['group_id'],
- 'content' => json_encode($newdates),
- 'day' => $day,
- 'next' => $next,
- 'status' => $status,
- 'create_time' => date('Y-m-d H:i:s'),
- 'update_time' => date('Y-m-d H:i:s')
- ]);
- }
- }
- }
|