package com.cloud.attendance.service.impl; import com.alibaba.fastjson.JSONObject; import com.cloud.attendance.dao.AttAbnormalRecordDao; import com.cloud.attendance.dao.AttDayDao; import com.cloud.attendance.dao.AttRuleDao; import com.cloud.attendance.model.AttDay; import com.cloud.attendance.model.AttMouthData; import com.cloud.attendance.model.AttRule; import com.cloud.attendance.model.DayStatus; import com.cloud.attendance.service.AttDayService; import com.cloud.attendance.service.TokenService; import com.cloud.attendance.service.UserService; import com.cloud.attendance.utils.EnumStr; import com.cloud.model.sys.LoginAppUser; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.*; @Slf4j @Service public class AttDayServiceImpl implements AttDayService { @Autowired private AttDayDao attDayDao; @Autowired private AttAbnormalRecordDao attAbnormalRecordDao; @Autowired private AttRuleDao attRuleDao; @Autowired private UserService userService; private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); @Override public int updateDayRecord(Long recordId, Date mouthDay, String leaveOrRepair){ AttDay attDay = new AttDay(); return attDayDao.updateByIdSelective(attDay); } @Override public Map findPerDayRecord(String mouthDate, String pid){ AttRule attRule = attRuleDao.selectAttRule(); // 获取考勤规则 String[] mouthDay = mouthDate.split("-"); int year = Integer.valueOf(mouthDay[0]); int month = Integer.valueOf(mouthDay[1]); if (attRule == null){ log.info("考勤规则暂无,考勤暂时做不了"); throw new RuntimeException("请先调整考勤规则,然后才会 依据规则 调整考勤数据。"); } int dayOfMonth = 0; Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); if ((new Date().getMonth()+1)==month){ String[] days = sdf.format(new Date()).split("-"); dayOfMonth = Integer.valueOf(days[2]); }else{ c.set(year, month, 0); //输入类型为int类型 // 2 判断 2月 dayOfMonth = c.get(Calendar.DAY_OF_MONTH); } log.info(year + "年" + month + "月有" + dayOfMonth + "天"); HashMap map = new HashMap<>(); List attDays = attDayDao.selectAttDayByUserAndMouth(mouthDate,pid); if (attDays == null){ // 新人员 刚加入 会查不到数据 或 未在考勤日期内 的数据 则会出现 null 数据 attDays = new ArrayList<>(); } Integer attendValue=0,isLeave=0,isRepair=0; if (attDays.size() person = attDayDao.getPersonInfoById( pid); // 调用人员接口 Map person = userService.getPersonInfoById(pid); if (person != null){ String name = (String) person.get("name"); String delFlag = (String) person.get("delFlag"); if ("1".equals(delFlag)){ name = name != null?(name+"(已删除)"):"(已删除)"; } map.put("employeeName",name != null?name:""); if (EnumStr.TEACHER_SQL.equals(person.get("type"))){ map.put("identity","教师"); }else if (EnumStr.STUDENT_SQL.equals(person.get("type"))){ map.put("identity","学生"); }else { Map personType = userService.queryPersonType(); String type = personType.get(person.get("type")); if (type != null){ map.put("identity",type); }else { map.put("identity","其他"); } } } return map; } // 补全当月数据 private List completionDate(List list , String begin , int daySub){ ArrayList dateResult = new ArrayList<>(); //时间增加一天 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); Calendar yesterday = Calendar.getInstance(); yesterday.add(Calendar.DAY_OF_MONTH, 1); String dateStr = sdf.format(yesterday.getTime()); //日期比较(求差多少天),时间也可以比较 Calendar now = Calendar.getInstance(); //字符串转化为时间 Calendar calendar10 = Calendar.getInstance(); Calendar calendar5 = Calendar.getInstance(); //循环处理日期数据,把缺失的日期补全。10是时间段内的天数,5是要处理的日期集合的天数 try { Date date = sdf.parse(begin); calendar10.setTime(date); } catch (ParseException e) { e.printStackTrace(); } for(int curr = 0 ; curr < daySub ; curr++){ boolean dbDataExist = false; int index = 0; for(int i = 0 ; i < list.size() ; i++){ try { // 操作 获取数据 AttDay attDay1 = list.get(i); Date date2 = attDay1.getSignDate(); calendar5.setTime(date2); } catch (Exception e) { e.printStackTrace(); } if(calendar10.compareTo(calendar5) == 0){ dbDataExist = true; index = i; break; } } if(dbDataExist){ // 操作 获取数据 AttDay attDay1 = list.get(index); Date date2 = attDay1.getSignDate(); dateResult.add(attDay1); }else{ Date time = calendar10.getTime(); AttDay attDay = new AttDay(); attDay.setSignDate(time); dateResult.add(attDay); } //还原calendar10 calendar10.add(Calendar.DAY_OF_MONTH, 1 ); } return dateResult; } @Autowired private TokenService tokenService; // 查询多人某月记录 @Override public List findAllDayRecord(String mouthDate, String gradeValue, String classValue, String identity, Integer days, Boolean delFlag, String orgIds){ AttRule attRule = attRuleDao.selectAttRule(); // 获取考勤规则 if (attRule == null || StringUtils.isBlank(attRule.getDeviceIds())){ // 新加 判断 return new ArrayList<>(); } /* if (EnumStr.STUDENT.equalsIgnoreCase(identity)){ identity = EnumStr.STUDENT_SQL; }else if (EnumStr.TEACHER.equalsIgnoreCase(identity)){ identity = EnumStr.TEACHER_SQL; }*/ if (StringUtils.isBlank(identity)){ identity = null; } String[] mouthDay = mouthDate.split("-"); int year = Integer.valueOf(mouthDay[0]); int month = Integer.valueOf(mouthDay[1]); Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00")); c.set(year, month, 0); //输入类型为int类型 int dayOfMonth = c.get(Calendar.DAY_OF_MONTH); if ((new Date().getMonth()+1)==month){ String[] dayDate = sdf.format(new Date()).split("-"); dayOfMonth = Integer.valueOf(dayDate[2]); } List orgList = null; if (!StringUtils.isBlank(orgIds)){ String[] orgIdStr = orgIds.split(","); orgList = Arrays.asList(orgIdStr); } // 添加权限 LoginAppUser appUser = tokenService.currentUser(); Long orgId = appUser.getOrgId(); if (StringUtils.isBlank(gradeValue)){ Integer officeId = appUser.getOfficeId(); gradeValue = officeId.toString(); } // 调用 人员管理 查询人员数据 List strings = userService.queryPersonByContent(null, gradeValue, classValue, identity, null,delFlag,orgList); log.info("查询人员列表:"+strings); // 查无人员 则 直接返回 if (strings == null || strings.size() < 1){ return new ArrayList(); } List list = attDayDao.selectAttDayByMouth(mouthDate,strings); // 查询 所有人员 的 姓名 Map persons = userService.queryAllPersonId(); log.info("查询人员列表:list"+list.size()); for(int i=0;i list1 = attMouthData.getTotalList(); if(list1.size() < dayOfMonth){ // list1 排好序 dayOfMonth days int d = 1; if (list1.size()>0){ Date startDate = list1.get(0).getDayDate();// 第一天记录是否从头开始 c.setTime(startDate); d=c.get(Calendar.DATE); if (d != 1){ for (int j=1;j queryPersonById(String id, String beginTime, String endTime) { AttRule attRule = attRuleDao.selectAttRule(); // 获取考勤规则 if (attRule == null){ // log.info("考勤规则暂无,考勤暂时做不了"); // throw new RuntimeException("请先调整考勤规则,然后才会 依据规则 调整考勤数据。"); } List list = attDayDao.queryPersonById(id,beginTime,endTime); return list; } }