| | |
| | | |
| | | import me.zhengjie.exception.BadRequestException; |
| | | import me.zhengjie.modules.attendance.domain.Attendance; |
| | | import me.zhengjie.modules.attendance.domain.AttendanceCardreplacement; |
| | | import me.zhengjie.modules.attendance.domain.AttendanceLeave; |
| | | import me.zhengjie.modules.attendance.domain.Calculate; |
| | | import me.zhengjie.modules.attendance.service.AttendanceService; |
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceDto; |
| | | import me.zhengjie.modules.constant.Constant; |
| | | import me.zhengjie.modules.quartz.service.OpaiDingTalkService; |
| | | import me.zhengjie.modules.system.domain.DictDetail; |
| | | import me.zhengjie.modules.system.domain.SysUserLevelOther; |
| | | import me.zhengjie.modules.system.domain.User; |
| | | import me.zhengjie.modules.system.domain.*; |
| | | import me.zhengjie.modules.system.repository.DictDetailRepository; |
| | | import me.zhengjie.modules.system.repository.SysConfigHolidayRepository; |
| | | import me.zhengjie.modules.system.repository.SysUserLevelOtherRepository; |
| | | import me.zhengjie.modules.system.repository.UserRepository; |
| | | import me.zhengjie.utils.*; |
| | |
| | | import org.springframework.data.domain.Pageable; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.sql.Timestamp; |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.Instant; |
| | | import java.time.LocalDateTime; |
| | | import java.time.ZoneId; |
| | | import java.time.ZonedDateTime; |
| | | import java.time.*; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.io.IOException; |
| | | import java.util.function.Function; |
| | |
| | | private final SysUserLevelOtherRepository sysUserLevelOtherRepository; |
| | | private final UserRepository userRepository; |
| | | private final OpaiDingTalkService opaiDingTalkService; |
| | | private final SysConfigHolidayRepository sysConfigHolidayRepository; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(AttendanceLeaveQueryCriteria criteria, Pageable pageable){ |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AttendanceLeaveDto create(AttendanceLeave resources) throws Exception { |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | String LeaStartTime = df.format(resources.getLeaveStartTime()); |
| | | String LeaEndTime = df.format(resources.getLeaveEndTime()); |
| | | // 如果按天为单位请假拼接请假时间的时分秒,取上下班基准时间 |
| | |
| | | }else { |
| | | // 获取当前登陆人信息,设置为请假发起人 |
| | | User user = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | resources.setUserId(user.getId()); |
| | | resources.setApprovalStatus(Constant.APPROVAL_STATUS_UNDER_APPROVAL); |
| | | List<String> holiday = new ArrayList<>(), isWork = new ArrayList<>(); |
| | | // 查询节假日 |
| | | List<SysConfigHoliday> sysConfigHolidayList = sysConfigHolidayRepository.findAll(); |
| | | for(SysConfigHoliday sysConfigHoliday : sysConfigHolidayList){ |
| | | if(sysConfigHoliday.getDateType().equals(Constant.DATE_TYPE_WEEKDAY)){ |
| | | //工作日 |
| | | isWork.add(sysConfigHoliday.getHolidayDate().toString()); |
| | | }else{ |
| | | holiday.add(sysConfigHoliday.getHolidayDate().toString()); |
| | | } |
| | | } |
| | | // 循环添加开始时间到结束时间的每一天 |
| | | List<String> dateList = getBetweenDate(resources.getLeaveStartTime().toLocalDateTime().toString(),resources.getLeaveEndTime().toLocalDateTime().toString()); |
| | | int i = 0; |
| | | for(String date : dateList){ |
| | | LocalDate localDate = LocalDate.parse(date); |
| | | Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY; |
| | | // 不是周末并且节假日表中不存在在创建 |
| | | if((!isWeekend && !holiday.contains(date)) || (isWeekend && isWork.contains(date))){ |
| | | AttendanceLeave attendanceLeave = getAttendanceLeave(resources,date,user,i,LeaStartTime,LeaEndTime,null); |
| | | i++; |
| | | attendanceLeaveRepository.save(attendanceLeave); |
| | | } |
| | | } |
| | | //发送钉钉给审批人 |
| | | List<String> sendList = new ArrayList<>(); |
| | | //查询审批者信息 |
| | | User appUser = userRepository.getOne(resources.getApprovalUserId()); |
| | | sendList.add(appUser.getDingding()); |
| | | opaiDingTalkService.sendNotice(sendList, "审批提醒", "### 审批提示 \n >" + user.getNickName() + " 提交了请假审批,请登录PMS系统处理", "card"); |
| | | return attendanceLeaveMapper.toDto(attendanceLeaveRepository.save(resources)); |
| | | return attendanceLeaveMapper.toDto(resources); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 添加多条记录 |
| | | * @param resources |
| | | * @param date |
| | | * @param user |
| | | * @param i |
| | | * @param LeaStartTime |
| | | * @param LeaEndTime |
| | | * @return |
| | | */ |
| | | public AttendanceLeave getAttendanceLeave(AttendanceLeave resources,String date,User user,int i,String LeaStartTime,String LeaEndTime,Long resUserId){ |
| | | DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | AttendanceLeave attendanceLeave = new AttendanceLeave(); |
| | | //resUserId为null说明是添加,反之 |
| | | if(resUserId == null){ |
| | | resources.setUserId(user.getId()); |
| | | }else{ |
| | | resources.setUserId(resUserId); |
| | | } |
| | | //日期时间单位是小时 |
| | | if(resources.getLeaveDurationUnit().equals(Constant.LEAVE_TIME_TYPE_HOUR)){ |
| | | Calculate calculate = new Calculate(); |
| | | String startTime = date + " " + LeaStartTime.substring(11,19); |
| | | String endTime = date + " " + LeaEndTime.substring(11,19); |
| | | //请假开始时间结束时间不相等的情况 |
| | | if(!LeaStartTime.substring(0,10).equals(LeaEndTime.substring(0,10))){ |
| | | //第一次循环用下班基准时间和请假开始时间算时长 |
| | | if(i == 0){ |
| | | calculate.setStart(LocalDateTime.parse(startTime,sdf)); |
| | | calculate.setEnd(LocalDateTime.parse(date + " " + user.getSysRostering().getWorkEndTime() + ":00",sdf)); |
| | | resources.setLeaveStartTime(Timestamp.valueOf(startTime)); |
| | | resources.setLeaveEndTime(Timestamp.valueOf(date + " " + user.getSysRostering().getWorkEndTime() + ":00")); |
| | | }else if(date.equals(LeaEndTime.substring(0,10))){ |
| | | //最后一次循环 |
| | | calculate.setStart(LocalDateTime.parse(date + " " + user.getSysRostering().getWorkStartTime() + ":00",sdf)); |
| | | calculate.setEnd(LocalDateTime.parse(endTime,sdf)); |
| | | resources.setLeaveStartTime(Timestamp.valueOf(date + " " + user.getSysRostering().getWorkStartTime() + ":00")); |
| | | resources.setLeaveEndTime(Timestamp.valueOf(endTime)); |
| | | }else{ |
| | | calculate.setStart(LocalDateTime.parse(date + " " + user.getSysRostering().getWorkStartTime() + ":00",sdf)); |
| | | calculate.setEnd(LocalDateTime.parse(date + " " + user.getSysRostering().getWorkEndTime() + ":00",sdf)); |
| | | resources.setLeaveStartTime(Timestamp.valueOf(date + " " + user.getSysRostering().getWorkStartTime() + ":00")); |
| | | resources.setLeaveEndTime(Timestamp.valueOf(date + " " + user.getSysRostering().getWorkEndTime() + ":00")); |
| | | } |
| | | Integer timeSeconds = attendanceService.calculateTimes(calculate, SecurityUtils.getCurrentUserId()); |
| | | Double result = Double.valueOf(new BigDecimal((float) timeSeconds / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | resources.setLeaveDuration(result); |
| | | } |
| | | }else{ |
| | | String startTime = date + " " + user.getSysRostering().getWorkStartTime() + ":00"; |
| | | String endTime = date + " " + user.getSysRostering().getWorkEndTime() + ":00"; |
| | | resources.setLeaveStartTime(Timestamp.valueOf(startTime)); |
| | | resources.setLeaveEndTime(Timestamp.valueOf(endTime)); |
| | | resources.setLeaveDuration(Constant.NUMBER_ONE); |
| | | } |
| | | resources.setApprovalStatus(Constant.APPROVAL_STATUS_UNDER_APPROVAL); |
| | | attendanceLeave.copy(resources); |
| | | return attendanceLeave; |
| | | } |
| | | |
| | | @Override |
| | |
| | | AttendanceLeave attendanceLeave = attendanceLeaveRepository.findById(resources.getId()).orElseGet(AttendanceLeave::new); |
| | | ValidationUtil.isNull( attendanceLeave.getId(),"AttendanceLeave","id",resources.getId()); |
| | | attendanceLeave.copy(resources); |
| | | |
| | | //审核通过或者退回不能删除 |
| | | if(attendanceLeave.getApprovalStatus().equals(Constant.APPROVAL_STATUS_PASSED) || attendanceLeave.getApprovalStatus().equals(Constant.APPROVAL_STATUS_RETURNED)){ |
| | | throw new BadRequestException("审核通过或退回的不能进行修改"); |
| | | } |
| | | |
| | | //获取当前登入人信息 |
| | | User user1 = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | Job job = user1.getJobs().iterator().next(); |
| | | //除超管、总经理只能修改自己信息 |
| | | if(!job.getId().equals(Constant.ROLE_LEVEL_SUPER) || !job.getId().equals(Constant.ROLE_LEVEL_GENERAL)){ |
| | | if (!Long.valueOf(resources.getUserId()).equals(SecurityUtils.getCurrentUserId())){ |
| | | throw new BadRequestException("只能修改自己的记录"); |
| | | } |
| | | } |
| | | |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String LeaStartTime = df.format(resources.getLeaveStartTime()); |
| | | String LeaEndTime = df.format(resources.getLeaveEndTime()); |
| | | // 如果按天为单位请假拼接请假时间的时分秒,取上下班基准时间 |
| | | if(resources.getLeaveDurationUnit().equals(Constant.LEAVE_TIME_TYPE_DAY)){ |
| | | //查询用户的排班时间 |
| | | User user = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | LeaStartTime = LeaStartTime.substring(0,10) + " " + user.getSysRostering().getWorkStartTime().toString() + ":00"; |
| | | LeaEndTime = LeaEndTime.substring(0,10) + " " + user.getSysRostering().getWorkEndTime().toString() + ":00"; |
| | | } |
| | | // 验证是否存在时间冲突的请假记录 |
| | | Set<Long> ids = new HashSet<>(); |
| | | ids.add(attendanceLeave.getId()); |
| | |
| | | SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy年MM月dd日"); |
| | | throw new BadRequestException("请假时间冲突," + errorFormat.format(errorSysAskForLeave.getLeaveStartTime()) + "至" + errorFormat.format(errorSysAskForLeave.getLeaveEndTime()) |
| | | + "已经存在请假记录,请调整时间"); |
| | | }else{ |
| | | //暂存userId |
| | | Long resUserId = attendanceLeave.getUserId(); |
| | | // 删除以前的记录 |
| | | for (Long id : ids) { |
| | | attendanceLeaveRepository.deleteById(id); |
| | | } |
| | | // 获取当前登陆人信息,设置为请假发起人 |
| | | User user = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | List<String> holiday = new ArrayList<>(), isWork = new ArrayList<>(); |
| | | // 查询节假日 |
| | | List<SysConfigHoliday> sysConfigHolidayList = sysConfigHolidayRepository.findAll(); |
| | | for(SysConfigHoliday sysConfigHoliday : sysConfigHolidayList){ |
| | | if(sysConfigHoliday.getDateType().equals(Constant.DATE_TYPE_WEEKDAY)){ |
| | | //工作日 |
| | | isWork.add(sysConfigHoliday.getHolidayDate().toString()); |
| | | }else{ |
| | | holiday.add(sysConfigHoliday.getHolidayDate().toString()); |
| | | } |
| | | } |
| | | // 循环添加开始时间到结束时间的每一天 |
| | | List<String> dateList = getBetweenDate(resources.getLeaveStartTime().toLocalDateTime().toString(),resources.getLeaveEndTime().toLocalDateTime().toString()); |
| | | int i = 0; |
| | | for(String date : dateList){ |
| | | LocalDate localDate = LocalDate.parse(date); |
| | | Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY; |
| | | // 不是周末并且节假日表中不存在在创建 |
| | | if((!isWeekend && !holiday.contains(date)) || (isWeekend && isWork.contains(date))){ |
| | | AttendanceLeave attendanceLeave1 = getAttendanceLeave(resources,date,user,i,LeaStartTime,LeaEndTime,resUserId); |
| | | i++; |
| | | attendanceLeaveRepository.save(attendanceLeave1); |
| | | } |
| | | } |
| | | } |
| | | attendanceLeaveRepository.save(attendanceLeave); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 获取两个日期之间的所有日期 |
| | | * @param startTime |
| | | * @param endTime |
| | | * @return |
| | | */ |
| | | public List<String> getBetweenDate(String startTime, String endTime){ |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); |
| | | // 声明保存日期集合 |
| | | List<String> list = new ArrayList<String>(); |
| | | try { |
| | | // 转化成日期类型 |
| | | Date startDate = sdf.parse(startTime); |
| | | Date endDate = sdf.parse(endTime); |
| | | |
| | | //用Calendar 进行日期比较判断 |
| | | Calendar calendar = Calendar.getInstance(); |
| | | while (startDate.getTime()<=endDate.getTime()){ |
| | | // 把日期添加到集合 |
| | | list.add(sdf.format(startDate)); |
| | | // 设置日期 |
| | | calendar.setTime(startDate); |
| | | //把日期增加一天 |
| | | calendar.add(Calendar.DATE, 1); |
| | | // 获取增加后的日期 |
| | | startDate=calendar.getTime(); |
| | | } |
| | | } catch (ParseException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | return list; |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | public void deleteAll(Long[] ids) { |
| | | for (Long id : ids) { |
| | | //根据id查询记录 |
| | | Optional<AttendanceLeave> attendanceLeave = attendanceLeaveRepository.findById(id); |
| | | //审核通过或者退回不能删除 |
| | | if(attendanceLeave.get().getApprovalStatus().equals(Constant.APPROVAL_STATUS_PASSED) || attendanceLeave.get().getApprovalStatus().equals(Constant.APPROVAL_STATUS_RETURNED)){ |
| | | throw new BadRequestException("审核通过或退回的不能进行删除"); |
| | | } |
| | | |
| | | //获取当前登入人信息 |
| | | User user = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | Job job = user.getJobs().iterator().next(); |
| | | //根据id查询记录 |
| | | Optional<AttendanceLeave> attendanceLeave1 = attendanceLeaveRepository.findById(id); |
| | | //除超管、总经理只能删除自己信息 |
| | | if(!job.getId().equals(Constant.ROLE_LEVEL_SUPER) || !job.getId().equals(Constant.ROLE_LEVEL_GENERAL)){ |
| | | if (!Long.valueOf(attendanceLeave1.get().getUserId()).equals(SecurityUtils.getCurrentUserId())){ |
| | | throw new BadRequestException("只能删除自己的记录"); |
| | | } |
| | | } |
| | | attendanceLeaveRepository.deleteById(id); |
| | | } |
| | | } |