| | |
| | | /* |
| | | * Copyright 2019-2020 Zheng Jie |
| | | * |
| | | * Licensed under the Apache License, Version 2.0 (the "License"); |
| | | * you may not use this file except in compliance with the License. |
| | | * You may obtain a copy of the License at |
| | | * |
| | | * http://www.apache.org/licenses/LICENSE-2.0 |
| | | * |
| | | * Unless required by applicable law or agreed to in writing, software |
| | | * distributed under the License is distributed on an "AS IS" BASIS, |
| | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| | | * See the License for the specific language governing permissions and |
| | | * limitations under the License. |
| | | */ |
| | | package me.zhengjie.modules.attendance.service.impl; |
| | | |
| | | import me.zhengjie.exception.BadRequestException; |
| | | import me.zhengjie.modules.attendance.domain.*; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceBusinessTravelRepository; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceLeaveRepository; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceRepository; |
| | | import me.zhengjie.modules.attendance.service.AttendanceService; |
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceLeaveDto; |
| | | import me.zhengjie.modules.constant.Constant; |
| | | import me.zhengjie.modules.quartz.service.OpaiDingTalkService; |
| | | import me.zhengjie.modules.quartz.service.impl.DingTalkServiceImpl; |
| | | import me.zhengjie.modules.system.domain.Job; |
| | | import me.zhengjie.modules.system.domain.Role; |
| | | import me.zhengjie.modules.system.domain.SysConfigHoliday; |
| | | import me.zhengjie.modules.system.domain.User; |
| | | import me.zhengjie.modules.system.repository.DictDetailRepository; |
| | | import me.zhengjie.modules.system.repository.SysConfigHolidayRepository; |
| | | import me.zhengjie.modules.system.repository.UserRepository; |
| | | import me.zhengjie.utils.*; |
| | | import lombok.RequiredArgsConstructor; |
| | | import me.zhengjie.modules.attendance.repository.AttendanceGoOutRepository; |
| | | import me.zhengjie.modules.attendance.service.AttendanceGoOutService; |
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceGoOutDto; |
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceGoOutQueryCriteria; |
| | | import me.zhengjie.modules.attendance.service.mapstruct.AttendanceGoOutMapper; |
| | | import org.apache.commons.lang3.ObjectUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | |
| | | import java.math.BigDecimal; |
| | | import java.sql.Timestamp; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.*; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.*; |
| | | import java.io.IOException; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.function.Function; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | | * @description 服务实现 |
| | | * @author wcd |
| | | * @date 2023-01-04 |
| | | **/ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class AttendanceGoOutServiceImpl implements AttendanceGoOutService { |
| | | |
| | | private final AttendanceGoOutRepository attendanceGoOutRepository; |
| | | private final AttendanceGoOutMapper attendanceGoOutMapper; |
| | | private final UserRepository userRepository; |
| | | private final AttendanceService attendanceService; |
| | | private final SysConfigHolidayRepository sysConfigHolidayRepository; |
| | | private final AttendanceLeaveServiceImpl attendanceLeaveService; |
| | | private final OpaiDingTalkService opaiDingTalkService; |
| | | private final AttendanceRepository attendanceRepository; |
| | | private final AttendanceLeaveRepository attendanceLeaveRepository; |
| | | private final AttendanceBusinessTravelRepository attendanceBusinessTravelRepository; |
| | | private final DictDetailRepository dictDetailRepository; |
| | | private final DingTalkServiceImpl dingTalkService; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(AttendanceGoOutQueryCriteria criteria, Pageable pageable){ |
| | | Page<AttendanceGoOut> page = attendanceGoOutRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); |
| | | |
| | | //将实体转换为Dto(重写) |
| | | Function<AttendanceGoOut, AttendanceGoOutDto> func = new Function<AttendanceGoOut, AttendanceGoOutDto>(){ |
| | | |
| | | @Override |
| | | public AttendanceGoOutDto apply(AttendanceGoOut attendanceGoOut) { |
| | | // 将实体类转换成DTO |
| | | AttendanceGoOutDto attendanceGoOutDto = attendanceGoOutMapper.toDto(attendanceGoOut); |
| | | //查询用户信息 |
| | | User user = userRepository.findById(attendanceGoOutDto.getUserId()).orElseGet(User::new); |
| | | attendanceGoOutDto.setUserName(user.getNickName()); |
| | | //查询审批用户名 |
| | | User appUser = userRepository.findById(attendanceGoOutDto.getApprovalUserId()).orElseGet(User::new); |
| | | attendanceGoOutDto.setAppUserName(appUser.getNickName()); |
| | | return attendanceGoOutDto; |
| | | } |
| | | }; |
| | | return PageUtil.toPage(page.map(func)); |
| | | } |
| | | |
| | | @Override |
| | | public List<AttendanceGoOutDto> queryAll(AttendanceGoOutQueryCriteria criteria){ |
| | | return attendanceGoOutMapper.toDto(attendanceGoOutRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder))); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public AttendanceGoOutDto findById(Long id) { |
| | | AttendanceGoOut attendanceGoOut = attendanceGoOutRepository.findById(id).orElseGet(AttendanceGoOut::new); |
| | | ValidationUtil.isNull(attendanceGoOut.getId(),"AttendanceGoOut","id",id); |
| | | return attendanceGoOutMapper.toDto(attendanceGoOut); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AttendanceGoOutDto create(AttendanceGoOut resources) throws Exception { |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String goStartTime = df.format(resources.getOutStartTime()); |
| | | String goEndTime = df.format(resources.getOutEndTime()); |
| | | // 查询其他状态的表是否有重复 |
| | | repeatValidation(goStartTime, goEndTime, resources.getUserId() == null ? SecurityUtils.getCurrentUserId() : resources.getUserId()); |
| | | // 验证请假时间是否冲突 |
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.checkCreateTime(goStartTime, goEndTime, resources.getUserId()); |
| | | if(attendanceGoOutList.size() > 0){ |
| | | AttendanceGoOut errTime = attendanceGoOutList.get(0); |
| | | //LocalDateTime转换为Date |
| | | LocalDateTime strartTime = errTime.getOutStartTime().toLocalDateTime(); |
| | | ZonedDateTime zonedDateTime = strartTime.atZone(ZoneId.systemDefault()); |
| | | Instant instant = zonedDateTime.toInstant(); |
| | | Date strTime = Date.from(instant); |
| | | LocalDateTime endTime = errTime.getOutEndTime().toLocalDateTime(); |
| | | ZonedDateTime zonedDateTime1 = endTime.atZone(ZoneId.systemDefault()); |
| | | Instant instant1 = zonedDateTime1.toInstant(); |
| | | Date endTim = Date.from(instant1); |
| | | |
| | | SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | throw new BadRequestException("外出时间冲突," + errorFormat.format(strTime) + "至" + errorFormat.format(endTim) |
| | | + "已经存在外出记录,请调整时间"); |
| | | }else { |
| | | // 获取当前登陆人信息,设置为请假发起人 |
| | | User user = userRepository.getOne(resources.getUserId() == null ? SecurityUtils.getCurrentUserId() : resources.getUserId()); |
| | | 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().substring(0,10)); |
| | | }else{ |
| | | holiday.add(sysConfigHoliday.getHolidayDate().toString().substring(0,10)); |
| | | } |
| | | } |
| | | // 循环添加开始时间到结束时间的每一天 |
| | | List<String> dateList = attendanceLeaveService.getBetweenDate(resources.getOutStartTime().toLocalDateTime().toString(),resources.getOutEndTime().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))){ |
| | | AttendanceGoOut attendanceGoOut = getAttendanceOut(resources,date,user,i,goStartTime,goEndTime,null); |
| | | i++; |
| | | attendanceGoOutRepository.save(attendanceGoOut); |
| | | } |
| | | } |
| | | //发送钉钉给审批人 |
| | | List<String> sendList = new ArrayList<>(); |
| | | //查询审批者信息 |
| | | User appUser = userRepository.getOne(resources.getApprovalUserId()); |
| | | sendList.add(appUser.getDingding()); |
| | | opaiDingTalkService.sendNotice(sendList, "审批提醒", "### 审批提示 \n >" + user.getNickName() + " 提交了外出审批,请登录PMS系统处理", "card"); |
| | | } |
| | | return attendanceGoOutMapper.toDto(resources); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void update(AttendanceGoOut resources) { |
| | | AttendanceGoOut attendanceGoOut = attendanceGoOutRepository.findById(resources.getId()).orElseGet(AttendanceGoOut::new); |
| | | ValidationUtil.isNull( attendanceGoOut.getId(),"AttendanceGoOut","id",resources.getId()); |
| | | attendanceGoOut.copy(resources); |
| | | |
| | | //审核通过或者退回不能修改 |
| | | if(attendanceGoOut.getApprovalStatus().equals(Constant.GOOUT_APPROVE_STATUS_AGREE) || attendanceGoOut.getApprovalStatus().equals(Constant.GOOUT_APPROVE_STATUS_REFUSE)){ |
| | | throw new BadRequestException("审核通过或退回的不能进行修改"); |
| | | } |
| | | |
| | | //获取当前登入人信息 |
| | | User user1 = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | Role role = user1.getRoles().iterator().next(); |
| | | //除超管、总经理只能修改自己信息 |
| | | if(role.getId() != Constant.ROLE_LEVEL_SUPER && role.getId() != 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 goStartTime = df.format(resources.getOutStartTime()); |
| | | String goEndTime = df.format(resources.getOutEndTime()); |
| | | // 查询其他状态的表是否有重复 |
| | | repeatValidation(goStartTime, goEndTime, resources.getUserId()); |
| | | // 验证是否存在时间冲突的请假记录 |
| | | Set<Long> ids = new HashSet<>(); |
| | | ids.add(attendanceGoOut.getId()); |
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.checkCreateUpdateTime(goStartTime, goEndTime, resources.getUserId(), ids); |
| | | if(attendanceGoOutList.size() > 0){ |
| | | AttendanceGoOut errorSysAskForLeave = attendanceGoOutList.get(0); |
| | | SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy年MM月dd日"); |
| | | throw new BadRequestException("外出时间冲突," + errorFormat.format(errorSysAskForLeave.getOutStartTime()) + "至" + errorFormat.format(errorSysAskForLeave.getOutEndTime()) |
| | | + "已经存在外出记录,请调整时间"); |
| | | }else { |
| | | //暂存userId |
| | | Long resUserId = attendanceGoOut.getUserId(); |
| | | // 删除以前的记录 |
| | | for (Long id : ids) { |
| | | attendanceGoOutRepository.deleteById(id); |
| | | } |
| | | // 获取当前登陆人信息,设置为请假发起人 |
| | | User user = userRepository.getOne(resources.getUserId()); |
| | | 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().substring(0,10)); |
| | | }else{ |
| | | holiday.add(sysConfigHoliday.getHolidayDate().toString().substring(0,10)); |
| | | } |
| | | } |
| | | // 循环添加开始时间到结束时间的每一天 |
| | | List<String> dateList = attendanceLeaveService.getBetweenDate(resources.getOutStartTime().toLocalDateTime().toString(),resources.getOutEndTime().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))){ |
| | | AttendanceGoOut attendanceGoOut1 = getAttendanceOut(resources,date,user,i,goStartTime,goEndTime,resUserId); |
| | | i++; |
| | | attendanceGoOutRepository.save(attendanceGoOut1); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 添加多条记录 |
| | | * @param resources |
| | | * @param date |
| | | * @param user |
| | | * @param i |
| | | * @param LeaStartTime |
| | | * @param LeaEndTime |
| | | * @param resUserId |
| | | * @return |
| | | */ |
| | | public AttendanceGoOut getAttendanceOut(AttendanceGoOut resources,String date,User user,int i,String LeaStartTime,String LeaEndTime,Long resUserId){ |
| | | DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| | | AttendanceGoOut attendanceGoOut = new AttendanceGoOut(); |
| | | //resUserId为null说明是添加,反之 |
| | | if(resUserId == null){ |
| | | resources.setUserId(user.getId()); |
| | | }else{ |
| | | resources.setUserId(resUserId); |
| | | } |
| | | 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.setOutStartTime(Timestamp.valueOf(startTime)); |
| | | resources.setOutEndTime(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.setOutStartTime(Timestamp.valueOf(date + " " + user.getSysRostering().getWorkStartTime() + ":00")); |
| | | resources.setOutEndTime(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.setOutStartTime(Timestamp.valueOf(date + " " + user.getSysRostering().getWorkStartTime() + ":00")); |
| | | resources.setOutEndTime(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.setOutDuration(result); |
| | | } |
| | | resources.setApprovalStatus(Constant.GOOUT_APPROVE_STATUS_REVIEWED); |
| | | attendanceGoOut.copy(resources); |
| | | return attendanceGoOut; |
| | | } |
| | | |
| | | /** |
| | | * 验证其他状态的表是否有重复 |
| | | * @param goStartTime |
| | | * @param goEndTime |
| | | */ |
| | | public void repeatValidation(String goStartTime, String goEndTime, Long userId){ |
| | | // 查询请假表(时单位) |
| | | List<AttendanceLeave> attendanceLeaveList = attendanceLeaveRepository.checkCreateLeaveTime(goStartTime,goEndTime, userId); |
| | | if(attendanceLeaveList.size() > 0){ |
| | | AttendanceLeave errorSysAskForLeave = attendanceLeaveList.get(0); |
| | | SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy年MM月dd日"); |
| | | throw new BadRequestException("请假时间冲突," + errorFormat.format(errorSysAskForLeave.getLeaveStartTime()) + "至" + errorFormat.format(errorSysAskForLeave.getLeaveEndTime()) |
| | | + "已经存在请假记录,请调整时间"); |
| | | } |
| | | // 查询出差表 |
| | | List<AttendanceBusinessTravel> attendanceBusinessTravelList = attendanceBusinessTravelRepository.checkCreateTime(goStartTime.substring(0,10),goEndTime.substring(0,10), userId); |
| | | if(attendanceBusinessTravelList.size() > 0){ |
| | | AttendanceBusinessTravel errorSysAskForLeave = attendanceBusinessTravelList.get(0); |
| | | SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy年MM月dd日"); |
| | | throw new BadRequestException("出差时间冲突," + errorFormat.format(errorSysAskForLeave.getTravelStartTime()) + "至" + errorFormat.format(errorSysAskForLeave.getTravelEndTime()) |
| | | + "已经存在出差记录,请调整时间"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void deleteAll(Long[] ids) { |
| | | for (Long id : ids) { |
| | | //根据id查询记录 |
| | | Optional<AttendanceGoOut> attendanceGoOut = attendanceGoOutRepository.findById(id); |
| | | //审核通过或者退回不能删除 |
| | | if(attendanceGoOut.get().getApprovalStatus().equals(Constant.GOOUT_APPROVE_STATUS_AGREE) || attendanceGoOut.get().getApprovalStatus().equals(Constant.GOOUT_APPROVE_STATUS_REFUSE)){ |
| | | throw new BadRequestException("审核通过或退回的不能进行删除"); |
| | | } |
| | | |
| | | //获取当前登入人信息 |
| | | User user = userRepository.getOne(SecurityUtils.getCurrentUserId()); |
| | | Role role = user.getRoles().iterator().next(); |
| | | //除超管、总经理只能删除自己信息 |
| | | if(role.getId() != Constant.ROLE_LEVEL_SUPER && role.getId() != Constant.ROLE_LEVEL_GENERAL){ |
| | | if (!Long.valueOf(attendanceGoOut.get().getUserId()).equals(SecurityUtils.getCurrentUserId())){ |
| | | throw new BadRequestException("只能删除自己的记录"); |
| | | } |
| | | } |
| | | |
| | | attendanceGoOutRepository.deleteById(id); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void download(List<AttendanceGoOutDto> all, HttpServletResponse response) throws IOException { |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (AttendanceGoOutDto attendanceGoOut : all) { |
| | | String outOfOfficeType = attendanceGoOut.getOutOfOfficeType() != null ? dictDetailRepository.findByDictNameAndValue(Constant.GOOUT_APPROVE_STATUS, attendanceGoOut.getOutOfOfficeType()).getLabel() : ""; |
| | | String outDurationUnit = attendanceGoOut.getDurationUnit() != null ? dictDetailRepository.findByDictNameAndValue(Constant.LEAVE_TIME_TYPE, attendanceGoOut.getDurationUnit()).getLabel() : ""; |
| | | Map<String,Object> map = new LinkedHashMap<>(); |
| | | map.put("用户id", attendanceGoOut.getUserId()); |
| | | map.put("审批用户id", attendanceGoOut.getApprovalUserId()); |
| | | map.put("外出开始时间", attendanceGoOut.getOutStartTime()); |
| | | map.put("外出结束时间", attendanceGoOut.getOutEndTime()); |
| | | map.put("外出时长", attendanceGoOut.getOutDuration()); |
| | | map.put("时长单位", outDurationUnit); |
| | | map.put("外出类型", outOfOfficeType); |
| | | map.put("外出原因", attendanceGoOut.getOutReason()); |
| | | map.put("审批状态", attendanceGoOut.getApprovalStatus()); |
| | | map.put("审批说明", attendanceGoOut.getApprovalDescription()); |
| | | map.put("备注", attendanceGoOut.getRemarks()); |
| | | map.put("创建者", attendanceGoOut.getCreateBy()); |
| | | map.put("创建时间", attendanceGoOut.getCreateTime()); |
| | | map.put("更新者", attendanceGoOut.getUpdateBy()); |
| | | map.put("更新时间", attendanceGoOut.getUpdateTime()); |
| | | list.add(map); |
| | | } |
| | | FileUtil.downloadExcel(list, response); |
| | | } |
| | | |
| | | /** |
| | | * 计算外出时长 |
| | | * @param attendanceGoOut |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Double getRemainingTime(AttendanceGoOut attendanceGoOut) { |
| | | Calculate calculate = new Calculate(); |
| | | calculate.setStart(attendanceGoOut.getOutStartTime().toLocalDateTime()); |
| | | calculate.setEnd(attendanceGoOut.getOutEndTime().toLocalDateTime()); |
| | | Double result = new Double(0.00); |
| | | Integer timeSeconds = attendanceService.calculateTimes(calculate, SecurityUtils.getCurrentUserId()); |
| | | result = Double.valueOf(new BigDecimal((float) timeSeconds / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString()); |
| | | return result; |
| | | } |
| | | |
| | | /** |
| | | * 审核/撤销功能 |
| | | * @param resources / |
| | | */ |
| | | @Override |
| | | public void updateApprove(AttendanceGoOut resources) throws Exception { |
| | | AttendanceGoOut attendanceGoOut = attendanceGoOutRepository.findById(resources.getId()).orElseGet(AttendanceGoOut::new); |
| | | ValidationUtil.isNull( attendanceGoOut.getId(),"AttendanceGoOut","id",resources.getId()); |
| | | attendanceGoOut.copy(resources); |
| | | attendanceGoOutRepository.save(attendanceGoOut); |
| | | // 调用定时任务修改考勤记录 |
| | | List<User> userList = new ArrayList<>(); |
| | | if(ObjectUtils.isNotEmpty(attendanceGoOut.getUserId())){ |
| | | User user = userRepository.findById(attendanceGoOut.getUserId()).orElseGet(User::new); |
| | | userList.add(user); |
| | | } |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String attendanceDate = df.format(attendanceGoOut.getOutStartTime()); |
| | | dingTalkService.getDingTalkAttendanceListOfNoonOrNight(attendanceDate,userList); |
| | | } |
| | | |
| | | /** |
| | | * 批量审核(外出) |
| | | * @param request |
| | | * @throws Exception |
| | | */ |
| | | @Override |
| | | public void updateAgree(AgreeUpdateRequest request) throws Exception { |
| | | for (Long id : request.getIds()) { |
| | | AttendanceGoOut attendanceGoOut = attendanceGoOutRepository.findById(id).orElseGet(AttendanceGoOut::new); |
| | | ValidationUtil.isNull( attendanceGoOut.getId(),"AttendanceGoOut","id",id); |
| | | if (request.getNum().equals(Long.valueOf(1))){ |
| | | attendanceGoOut.setApprovalStatus(Constant.GOOUT_APPROVE_STATUS_AGREE); |
| | | } else { |
| | | attendanceGoOut.setApprovalStatus(Constant.GOOUT_APPROVE_STATUS_REFUSE); |
| | | } |
| | | attendanceGoOutRepository.save(attendanceGoOut); |
| | | // 调用定时任务修改考勤记录 |
| | | List<User> userList = new ArrayList<>(); |
| | | if(ObjectUtils.isNotEmpty(attendanceGoOut.getUserId())){ |
| | | User user = userRepository.findById(attendanceGoOut.getUserId()).orElseGet(User::new); |
| | | userList.add(user); |
| | | } |
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd"); |
| | | String attendanceDate = df.format(attendanceGoOut.getOutStartTime()); |
| | | dingTalkService.getDingTalkAttendanceListOfNoonOrNight(attendanceDate,userList); |
| | | } |
| | | } |
| | | /*
|
| | | * Copyright 2019-2020 Zheng Jie
|
| | | *
|
| | | * Licensed under the Apache License, Version 2.0 (the "License");
|
| | | * you may not use this file except in compliance with the License.
|
| | | * You may obtain a copy of the License at
|
| | | *
|
| | | * http://www.apache.org/licenses/LICENSE-2.0
|
| | | *
|
| | | * Unless required by applicable law or agreed to in writing, software
|
| | | * distributed under the License is distributed on an "AS IS" BASIS,
|
| | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| | | * See the License for the specific language governing permissions and
|
| | | * limitations under the License.
|
| | | */
|
| | | package me.zhengjie.modules.attendance.service.impl;
|
| | |
|
| | | import lombok.RequiredArgsConstructor;
|
| | | import me.zhengjie.exception.BadRequestException;
|
| | | import me.zhengjie.modules.approval.domain.ApproveEvent;
|
| | | import me.zhengjie.modules.approval.repository.ApproveEventRepository;
|
| | | import me.zhengjie.modules.approval.service.TemplateControlFieldService;
|
| | | import me.zhengjie.modules.approval.service.mapstruct.ApproveEventMapper;
|
| | | import me.zhengjie.modules.attendance.domain.*;
|
| | | import me.zhengjie.modules.attendance.repository.AttendanceBusinessTravelRepository;
|
| | | import me.zhengjie.modules.attendance.repository.AttendanceGoOutRepository;
|
| | | import me.zhengjie.modules.attendance.repository.AttendanceLeaveRepository;
|
| | | import me.zhengjie.modules.attendance.repository.AttendanceRepository;
|
| | | import me.zhengjie.modules.attendance.service.AttendanceGoOutService;
|
| | | import me.zhengjie.modules.attendance.service.AttendanceOvertimeService;
|
| | | import me.zhengjie.modules.attendance.service.AttendanceService;
|
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceGoOutDto;
|
| | | import me.zhengjie.modules.attendance.service.dto.AttendanceGoOutQueryCriteria;
|
| | | import me.zhengjie.modules.attendance.service.mapstruct.AttendanceGoOutMapper;
|
| | | import me.zhengjie.modules.constant.Constant;
|
| | | import me.zhengjie.modules.quartz.service.impl.WeComServiceImpl;
|
| | | import me.zhengjie.modules.system.domain.DictDetail;
|
| | | import me.zhengjie.modules.system.domain.Role;
|
| | | import me.zhengjie.modules.system.domain.SysRostering;
|
| | | import me.zhengjie.modules.system.domain.User;
|
| | | import me.zhengjie.modules.system.repository.DictDetailRepository;
|
| | | import me.zhengjie.modules.system.repository.SysConfigHolidayRepository;
|
| | | import me.zhengjie.modules.system.repository.UserRepository;
|
| | | import me.zhengjie.modules.system.service.SysConfigHolidayService;
|
| | | import me.zhengjie.modules.system.service.dto.SysConfigHolidayDto;
|
| | | import me.zhengjie.utils.*;
|
| | | import org.apache.commons.lang3.ObjectUtils;
|
| | | import org.springframework.data.domain.Page;
|
| | | import org.springframework.data.domain.Pageable;
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.transaction.annotation.Transactional;
|
| | |
|
| | | import javax.servlet.http.HttpServletResponse;
|
| | | import java.io.IOException;
|
| | | import java.math.BigDecimal;
|
| | | import java.sql.Timestamp;
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.time.*;
|
| | | import java.time.format.DateTimeFormatter;
|
| | | import java.time.temporal.ChronoUnit;
|
| | | import java.util.*;
|
| | | import java.util.function.Function;
|
| | | import java.util.stream.Collectors;
|
| | | import java.util.stream.Stream;
|
| | |
|
| | | /**
|
| | | * @author wcd
|
| | | * @website https://eladmin.vip
|
| | | * @description 服务实现
|
| | | * @date 2023-01-04
|
| | | **/
|
| | | @Service
|
| | | @RequiredArgsConstructor
|
| | | public class AttendanceGoOutServiceImpl implements AttendanceGoOutService {
|
| | |
|
| | | private final AttendanceGoOutRepository attendanceGoOutRepository;
|
| | | private final AttendanceGoOutMapper attendanceGoOutMapper;
|
| | | private final UserRepository userRepository;
|
| | | private final SysConfigHolidayRepository sysConfigHolidayRepository;
|
| | | private final AttendanceLeaveRepository attendanceLeaveRepository;
|
| | | private final AttendanceBusinessTravelRepository attendanceBusinessTravelRepository;
|
| | | private final ApproveEventRepository approveEventRepository;
|
| | | private final ApproveEventMapper approveEventMapper;
|
| | | private final DictDetailRepository dictDetailRepository;
|
| | | private final WeComServiceImpl weComService;
|
| | | private final TemplateControlFieldService templateControlFieldService;
|
| | |
|
| | | private final SysConfigHolidayService sysConfigHolidayService;
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> queryAll(AttendanceGoOutQueryCriteria criteria, Pageable pageable) {
|
| | | Page<AttendanceGoOut> page = attendanceGoOutRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
|
| | |
|
| | | //将实体转换为Dto(重写)
|
| | | Function<AttendanceGoOut, AttendanceGoOutDto> func = new Function<AttendanceGoOut, AttendanceGoOutDto>() {
|
| | |
|
| | | @Override
|
| | | public AttendanceGoOutDto apply(AttendanceGoOut attendanceGoOut) {
|
| | | // 将实体类转换成DTO
|
| | | AttendanceGoOutDto attendanceGoOutDto = attendanceGoOutMapper.toDto(attendanceGoOut);
|
| | | //查询用户信息
|
| | | User user = userRepository.findById(attendanceGoOutDto.getUserId()).orElseGet(User::new);
|
| | | attendanceGoOutDto.setUserName(user.getNickName());
|
| | | //查询审批用户名
|
| | | User appUser = userRepository.findById(attendanceGoOutDto.getApprovalUserId()).orElseGet(User::new);
|
| | | attendanceGoOutDto.setAppUserName(appUser.getNickName());
|
| | |
|
| | | // 查询企微最新审批变化记录
|
| | | if (ObjectUtils.isNotEmpty(attendanceGoOut.getWecomEventId())) {
|
| | | ApproveEvent event = approveEventRepository.findById(attendanceGoOut.getWecomEventId()).orElseGet(ApproveEvent::new);
|
| | | attendanceGoOutDto.setEvent(approveEventMapper.toDto(event));
|
| | | }
|
| | |
|
| | | return attendanceGoOutDto;
|
| | | }
|
| | | };
|
| | | return PageUtil.toPage(page.map(func));
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<AttendanceGoOutDto> queryAll(AttendanceGoOutQueryCriteria criteria) {
|
| | | List<AttendanceGoOutDto> attendanceGoOutDtoList = attendanceGoOutMapper.toDto(attendanceGoOutRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
|
| | | attendanceGoOutDtoList.forEach(attendanceGoOutDto -> {
|
| | | //查询用户信息
|
| | | User user = userRepository.findById(attendanceGoOutDto.getUserId()).orElseGet(User::new);
|
| | | attendanceGoOutDto.setUserName(user.getNickName());
|
| | | //查询审批用户名
|
| | | User appUser = userRepository.findById(attendanceGoOutDto.getApprovalUserId()).orElseGet(User::new);
|
| | | attendanceGoOutDto.setAppUserName(appUser.getNickName());
|
| | | });
|
| | | return attendanceGoOutDtoList;
|
| | | }
|
| | |
|
| | | @Override
|
| | | @Transactional
|
| | | public AttendanceGoOutDto findById(Long id) {
|
| | | AttendanceGoOut attendanceGoOut = attendanceGoOutRepository.findById(id).orElseGet(AttendanceGoOut::new);
|
| | | ValidationUtil.isNull(attendanceGoOut.getId(), "AttendanceGoOut", "id", id);
|
| | | return attendanceGoOutMapper.toDto(attendanceGoOut);
|
| | | }
|
| | |
|
| | | @Override
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | public AttendanceGoOutDto create(AttendanceGoOut resources) throws Exception {
|
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | String goStartTime = df.format(resources.getOutStartTime());
|
| | | String goEndTime = df.format(resources.getOutEndTime());
|
| | | // // 查询其他状态的表是否有重复
|
| | | // repeatValidation(goStartTime, goEndTime, resources.getUserId() == null ? SecurityUtils.getCurrentUserId() : resources.getUserId());
|
| | | // 验证请假时间是否冲突
|
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.checkCreateTime(goStartTime, goEndTime, resources.getUserId());
|
| | | if (attendanceGoOutList.size() > 0) {
|
| | | AttendanceGoOut errTime = attendanceGoOutList.get(0);
|
| | | //LocalDateTime转换为Date
|
| | | LocalDateTime strartTime = errTime.getOutStartTime().toLocalDateTime();
|
| | | ZonedDateTime zonedDateTime = strartTime.atZone(ZoneId.systemDefault());
|
| | | Instant instant = zonedDateTime.toInstant();
|
| | | Date strTime = Date.from(instant);
|
| | | LocalDateTime endTime = errTime.getOutEndTime().toLocalDateTime();
|
| | | ZonedDateTime zonedDateTime1 = endTime.atZone(ZoneId.systemDefault());
|
| | | Instant instant1 = zonedDateTime1.toInstant();
|
| | | Date endTim = Date.from(instant1);
|
| | |
|
| | | SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | throw new BadRequestException("外出时间冲突," + errorFormat.format(strTime) + "至" + errorFormat.format(endTim)
|
| | | + "已经存在外出记录,请调整时间");
|
| | | } else {
|
| | | // 获取当前登陆人信息,设置为请假发起人
|
| | | User user = userRepository.getOne(resources.getUserId() == null ? SecurityUtils.getCurrentUserId() : resources.getUserId());
|
| | | User approvalUser = userRepository.getOne(resources.getApprovalUserId());
|
| | |
|
| | | LocalDate startDate = resources.getOutStartTime().toLocalDateTime().toLocalDate();
|
| | | LocalDate endDate = resources.getOutEndTime().toLocalDateTime().toLocalDate();
|
| | | LocalTime startTime = resources.getOutStartTime().toLocalDateTime().toLocalTime();
|
| | | LocalTime endTime = resources.getOutEndTime().toLocalDateTime().toLocalTime();
|
| | | long distance = ChronoUnit.DAYS.between(startDate, endDate);
|
| | | List<DictDetail> siteTypes = dictDetailRepository.findByDictName(Constant.APPROVAL_SITE_TYPE).stream().filter(siteType -> siteType.getValue().toLowerCase().equals("wecom")).collect(Collectors.toList());
|
| | | Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(localDate -> {
|
| | | // SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | // Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY;
|
| | | // if ((sysConfigHolidayDto == null && !isWeekend) ||
|
| | | // sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) {
|
| | | AttendanceGoOut attendanceGoOut = new AttendanceGoOut();
|
| | | // resUserId为null说明是添加,反之
|
| | | if (resources.getUserId() == null) {
|
| | | resources.setUserId(user.getId());
|
| | | }
|
| | |
|
| | | LocalDateTime goOutStartTime = localDate.atStartOfDay();
|
| | | LocalDateTime goOutEndTime = localDate.atTime(23, 59, 59);
|
| | | if (localDate.equals(endDate)) {
|
| | | goOutEndTime = localDate.atTime(endTime);
|
| | | }
|
| | | if (localDate.equals(startDate)) {
|
| | | goOutStartTime = localDate.atTime(startTime);
|
| | | }
|
| | | resources.setOutStartTime(Timestamp.valueOf(goOutStartTime));
|
| | | resources.setOutEndTime(Timestamp.valueOf(goOutEndTime));
|
| | |
|
| | | Double result = getRemainingTime(resources);
|
| | | resources.setOutDuration(result);
|
| | | resources.setApprovalStatus(Constant.GOOUT_APPROVE_STATUS_REVIEWED);
|
| | |
|
| | | // 判断是否提交企微审批
|
| | | if (siteTypes.size() > 0) {
|
| | | // 添加企微申请参数
|
| | | resources.setWecomType(dictDetailRepository.findByDictNameAndValue("go_out_type", resources.getOutOfOfficeType()).getLabel());
|
| | | resources.setWecomLocation("暂无");
|
| | | resources.setWecomDateType(ElAdminConstant.Constant.TEMPLATE_DATE_RANGE_TYPE_HOUR);
|
| | | resources.setWecomDuration(String.valueOf(new BigDecimal(resources.getOutDuration()).multiply(new BigDecimal(3600)).longValue()));
|
| | | resources.setWecomAttendanceType("4");
|
| | | resources.setWecomApproveWecomId(approvalUser.getWeComId());
|
| | | // 提交申请
|
| | | Map<String, String> approvalResultMap = templateControlFieldService.createWecomApproval(resources, user.getWeComId());
|
| | | if (approvalResultMap.containsKey("spNo")) {
|
| | | resources.setWecomSpNo(approvalResultMap.get("spNo"));
|
| | | } else {
|
| | | throw new BadRequestException(approvalResultMap.containsKey("error") ? approvalResultMap.get("error") : "企微申请提交失败");
|
| | | }
|
| | | }
|
| | |
|
| | | attendanceGoOut.copy(resources);
|
| | | attendanceGoOutRepository.save(attendanceGoOut);
|
| | | // }
|
| | | // SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | // Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY;
|
| | | // if ((sysConfigHolidayDto == null && isWeekend) ||
|
| | | // sysConfigHolidayDto != null && Constant.DATE_TYPE_HOLIDAYS.equals(sysConfigHolidayDto.getDateType())) {
|
| | | // AttendanceOvertime attendanceOvertime = new AttendanceOvertime();
|
| | | // attendanceOvertime.setUserId(resources.getUserId());
|
| | | // attendanceOvertime.setApprovalUserId(resources.getApprovalUserId());
|
| | | // attendanceOvertime.setOvertimeStartTime(resources.getOutStartTime());
|
| | | // attendanceOvertime.setOvertimeEndTime(resources.getOutEndTime());
|
| | | // attendanceOvertime.setOvertimeDuration(resources.getOutDuration());
|
| | | // attendanceOvertime.setApprovalStatus(resources.getApprovalStatus());
|
| | | // attendanceOvertime.setRemarks(resources.getRemarks());
|
| | | // attendanceOvertimeService.create(attendanceOvertime);
|
| | | // }
|
| | | });
|
| | | //
|
| | | // 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().substring(0,10));
|
| | | // }else{
|
| | | // holiday.add(sysConfigHoliday.getHolidayDate().toString().substring(0,10));
|
| | | // }
|
| | | // }
|
| | | // // 循环添加开始时间到结束时间的每一天
|
| | | // List<String> dateList = attendanceLeaveService.getBetweenDate(resources.getOutStartTime().toLocalDateTime().toString(),resources.getOutEndTime().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))){
|
| | | // AttendanceGoOut attendanceGoOut = getAttendanceOut(resources,date,user,i,goStartTime,goEndTime,null);
|
| | | // i++;
|
| | | // attendanceGoOutRepository.save(attendanceGoOut);
|
| | | // }
|
| | | // }
|
| | | }
|
| | | return attendanceGoOutMapper.toDto(resources);
|
| | | }
|
| | |
|
| | | @Override
|
| | | @Transactional(rollbackFor = Exception.class)
|
| | | public void update(AttendanceGoOut resources) {
|
| | | AttendanceGoOut attendanceGoOut = attendanceGoOutRepository.findById(resources.getId()).orElseGet(AttendanceGoOut::new);
|
| | | ValidationUtil.isNull(attendanceGoOut.getId(), "AttendanceGoOut", "id", resources.getId());
|
| | | attendanceGoOut.copy(resources);
|
| | |
|
| | | //审核通过或者退回不能修改
|
| | | if (attendanceGoOut.getApprovalStatus().equals(Constant.GOOUT_APPROVE_STATUS_AGREE) || attendanceGoOut.getApprovalStatus().equals(Constant.GOOUT_APPROVE_STATUS_REFUSE)) {
|
| | | throw new BadRequestException("审核通过或退回的不能进行修改");
|
| | | }
|
| | |
|
| | | //获取当前登入人信息
|
| | | User user1 = userRepository.getOne(SecurityUtils.getCurrentUserId());
|
| | | Role role = user1.getRoles().iterator().next();
|
| | | //除超管、总经理只能修改自己信息
|
| | | if (!Constant.SUPER_LEVEL_LIST.contains(Long.valueOf(role.getLevel()))) {
|
| | | if (!Long.valueOf(resources.getUserId()).equals(SecurityUtils.getCurrentUserId())) {
|
| | | throw new BadRequestException("只能修改自己的记录");
|
| | | }
|
| | | }
|
| | |
|
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | String goStartTime = df.format(resources.getOutStartTime());
|
| | | String goEndTime = df.format(resources.getOutEndTime());
|
| | | // // 查询其他状态的表是否有重复
|
| | | // repeatValidation(goStartTime, goEndTime, resources.getUserId());
|
| | | // 验证是否存在时间冲突的请假记录
|
| | | Set<Long> ids = new HashSet<>();
|
| | | ids.add(attendanceGoOut.getId());
|
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.checkCreateUpdateTime(goStartTime, goEndTime, resources.getUserId(), ids);
|
| | | if (attendanceGoOutList.size() > 0) {
|
| | | AttendanceGoOut errorSysAskForLeave = attendanceGoOutList.get(0);
|
| | | SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy年MM月dd日");
|
| | | throw new BadRequestException("外出时间冲突," + errorFormat.format(errorSysAskForLeave.getOutStartTime()) + "至" + errorFormat.format(errorSysAskForLeave.getOutEndTime())
|
| | | + "已经存在外出记录,请调整时间");
|
| | | } else {
|
| | |
|
| | | // 获取当前登陆人信息,设置为请假发起人
|
| | | User user = userRepository.getOne(resources.getUserId() == null ? SecurityUtils.getCurrentUserId() : resources.getUserId());
|
| | |
|
| | | LocalDate startDate = resources.getOutStartTime().toLocalDateTime().toLocalDate();
|
| | | LocalDate endDate = resources.getOutEndTime().toLocalDateTime().toLocalDate();
|
| | | LocalTime startTime = resources.getOutStartTime().toLocalDateTime().toLocalTime();
|
| | | LocalTime endTime = resources.getOutEndTime().toLocalDateTime().toLocalTime();
|
| | | long distance = ChronoUnit.DAYS.between(startDate, endDate);
|
| | | Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).forEach(localDate -> {
|
| | | SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY;
|
| | | if ((sysConfigHolidayDto == null && !isWeekend) ||
|
| | | sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) {
|
| | | AttendanceGoOut attendanceGoOut1 = new AttendanceGoOut();
|
| | | //resUserId为null说明是添加,反之
|
| | | if (resources.getUserId() == null) {
|
| | | resources.setUserId(user.getId());
|
| | | }
|
| | | resources.setOutStartTime(Timestamp.valueOf(localDate.atTime(startTime)));
|
| | | resources.setOutEndTime(Timestamp.valueOf(localDate.atTime(endTime)));
|
| | | long timeSeconds = ChronoUnit.SECONDS.between(startTime, endTime);
|
| | | Double result = Double.valueOf(new BigDecimal((float) timeSeconds / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
|
| | | resources.setOutDuration(result);
|
| | | resources.setApprovalStatus(Constant.GOOUT_APPROVE_STATUS_REVIEWED);
|
| | | attendanceGoOut1.copy(resources);
|
| | | attendanceGoOutRepository.save(attendanceGoOut1);
|
| | | }
|
| | | });
|
| | | // //暂存userId
|
| | | // Long resUserId = attendanceGoOut.getUserId();
|
| | | // // 删除以前的记录
|
| | | // for (Long id : ids) {
|
| | | // attendanceGoOutRepository.deleteById(id);
|
| | | // }
|
| | | // // 获取当前登陆人信息,设置为请假发起人
|
| | | // User user = userRepository.getOne(resources.getUserId());
|
| | | // 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().substring(0,10));
|
| | | // }else{
|
| | | // holiday.add(sysConfigHoliday.getHolidayDate().toString().substring(0,10));
|
| | | // }
|
| | | // }
|
| | | // // 循环添加开始时间到结束时间的每一天
|
| | | // List<String> dateList = attendanceLeaveService.getBetweenDate(resources.getOutStartTime().toLocalDateTime().toString(),resources.getOutEndTime().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))){
|
| | | // AttendanceGoOut attendanceGoOut1 = getAttendanceOut(resources,date,user,i,goStartTime,goEndTime,resUserId);
|
| | | // i++;
|
| | | // attendanceGoOutRepository.save(attendanceGoOut1);
|
| | | // }
|
| | | // }
|
| | | }
|
| | | }
|
| | |
|
| | | // /**
|
| | | // * 添加多条记录
|
| | | // * @param resources
|
| | | // * @param date
|
| | | // * @param user
|
| | | // * @param i
|
| | | // * @param LeaStartTime
|
| | | // * @param LeaEndTime
|
| | | // * @param resUserId
|
| | | // * @return
|
| | | // */
|
| | | // public AttendanceGoOut getAttendanceOut(AttendanceGoOut resources, User user, LocalDateTime LeaStartTime, LocalDateTime LeaEndTime){
|
| | | // DateTimeFormatter sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
|
| | | // AttendanceGoOut attendanceGoOut = new AttendanceGoOut();
|
| | | // //resUserId为null说明是添加,反之
|
| | | // if (resUserId == null) {
|
| | | // resources.setUserId(user.getId());
|
| | | // } else {
|
| | | // resources.setUserId(resUserId);
|
| | | // }
|
| | | // 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.setOutStartTime(Timestamp.valueOf(startTime));
|
| | | // resources.setOutEndTime(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.setOutStartTime(Timestamp.valueOf(date + " " + user.getSysRostering().getWorkStartTime() + ":00"));
|
| | | // resources.setOutEndTime(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.setOutStartTime(Timestamp.valueOf(date + " " + user.getSysRostering().getWorkStartTime() + ":00"));
|
| | | // resources.setOutEndTime(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.setOutDuration(result);
|
| | | // }
|
| | | // resources.setApprovalStatus(Constant.GOOUT_APPROVE_STATUS_REVIEWED);
|
| | | // attendanceGoOut.copy(resources);
|
| | | // return attendanceGoOut;
|
| | | // }
|
| | |
|
| | | /**
|
| | | * 验证其他状态的表是否有重复
|
| | | *
|
| | | * @param goStartTime
|
| | | * @param goEndTime
|
| | | */
|
| | | public void repeatValidation(String goStartTime, String goEndTime, Long userId) {
|
| | | // 查询请假表(时单位)
|
| | | List<AttendanceLeave> attendanceLeaveList = attendanceLeaveRepository.checkCreateLeaveTime(goStartTime, goEndTime, userId);
|
| | | if (attendanceLeaveList.size() > 0) {
|
| | | AttendanceLeave errorSysAskForLeave = attendanceLeaveList.get(0);
|
| | | SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy年MM月dd日");
|
| | | throw new BadRequestException("请假时间冲突," + errorFormat.format(errorSysAskForLeave.getLeaveStartTime()) + "至" + errorFormat.format(errorSysAskForLeave.getLeaveEndTime())
|
| | | + "已经存在请假记录,请调整时间");
|
| | | }
|
| | | // 查询出差表
|
| | | List<AttendanceBusinessTravel> attendanceBusinessTravelList = attendanceBusinessTravelRepository.checkCreateTime(goStartTime.substring(0, 10), goEndTime.substring(0, 10), userId);
|
| | | if (attendanceBusinessTravelList.size() > 0) {
|
| | | AttendanceBusinessTravel errorSysAskForLeave = attendanceBusinessTravelList.get(0);
|
| | | SimpleDateFormat errorFormat = new SimpleDateFormat("yyyy年MM月dd日");
|
| | | throw new BadRequestException("出差时间冲突," + errorFormat.format(errorSysAskForLeave.getTravelStartTime()) + "至" + errorFormat.format(errorSysAskForLeave.getTravelEndTime())
|
| | | + "已经存在出差记录,请调整时间");
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void deleteAll(Long[] ids) {
|
| | | for (Long id : ids) {
|
| | | //根据id查询记录
|
| | | Optional<AttendanceGoOut> attendanceGoOut = attendanceGoOutRepository.findById(id);
|
| | | //审核通过或者退回不能删除
|
| | | if (attendanceGoOut.get().getApprovalStatus().equals(Constant.GOOUT_APPROVE_STATUS_AGREE) || attendanceGoOut.get().getApprovalStatus().equals(Constant.GOOUT_APPROVE_STATUS_REFUSE)) {
|
| | | throw new BadRequestException("审核通过或退回的不能进行删除");
|
| | | }
|
| | |
|
| | | // 获取当前登入人信息
|
| | | User user = userRepository.getOne(SecurityUtils.getCurrentUserId());
|
| | | Role role = user.getRoles().iterator().next();
|
| | | // 除超管、总经理只能删除自己信息
|
| | | if (!Constant.SUPER_LEVEL_LIST.contains(Long.valueOf(role.getLevel()))) {
|
| | | if (!Long.valueOf(attendanceGoOut.get().getUserId()).equals(SecurityUtils.getCurrentUserId())) {
|
| | | throw new BadRequestException("只能删除自己的记录");
|
| | | }
|
| | | }
|
| | |
|
| | | attendanceGoOutRepository.deleteById(id);
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void download(List<AttendanceGoOutDto> all, HttpServletResponse response) throws IOException {
|
| | | List<Map<String, Object>> list = new ArrayList<>();
|
| | | for (AttendanceGoOutDto attendanceGoOut : all) {
|
| | | String outOfOfficeType = attendanceGoOut.getOutOfOfficeType() != null ? dictDetailRepository.findByDictNameAndValue("go_out_type", attendanceGoOut.getOutOfOfficeType()).getLabel() : "";
|
| | | String outDurationUnit = attendanceGoOut.getDurationUnit() != null ? dictDetailRepository.findByDictNameAndValue(Constant.LEAVE_TIME_TYPE, attendanceGoOut.getDurationUnit()).getLabel() : "";
|
| | | Map<String, Object> map = new LinkedHashMap<>();
|
| | | map.put("申请者", attendanceGoOut.getUserName());
|
| | | map.put("审批者", attendanceGoOut.getAppUserName());
|
| | | map.put("外出开始时间", attendanceGoOut.getOutStartTime());
|
| | | map.put("外出结束时间", attendanceGoOut.getOutEndTime());
|
| | | map.put("外出时长", attendanceGoOut.getOutDuration());
|
| | | map.put("时长单位", outDurationUnit);
|
| | | map.put("外出类型", outOfOfficeType);
|
| | | map.put("外出原因", attendanceGoOut.getOutReason());
|
| | | map.put("审批状态", attendanceGoOut.getApprovalStatus());
|
| | | map.put("审批说明", attendanceGoOut.getApprovalDescription());
|
| | | map.put("备注", attendanceGoOut.getRemarks());
|
| | | map.put("创建者", attendanceGoOut.getCreateBy());
|
| | | map.put("创建时间", attendanceGoOut.getCreateTime());
|
| | | map.put("更新者", attendanceGoOut.getUpdateBy());
|
| | | map.put("更新时间", attendanceGoOut.getUpdateTime());
|
| | | list.add(map);
|
| | | }
|
| | | FileUtil.downloadExcel(list, response);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 计算外出时长
|
| | | *
|
| | | * @param attendanceGoOut
|
| | | * @return
|
| | | */
|
| | | @Override
|
| | | public Double getRemainingTime(AttendanceGoOut attendanceGoOut) {
|
| | | Calculate calculate = new Calculate();
|
| | | calculate.setStart(attendanceGoOut.getOutStartTime().toLocalDateTime());
|
| | | calculate.setEnd(attendanceGoOut.getOutEndTime().toLocalDateTime());
|
| | |
|
| | | long timeSeconds = calculateTimes(calculate, SecurityUtils.getCurrentUserId());
|
| | | Double result = Double.valueOf(new BigDecimal((float) timeSeconds / 3600).setScale(2, BigDecimal.ROUND_HALF_UP).toString());
|
| | | return result;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void reflashTime(Long[] ids) {
|
| | | List<AttendanceGoOut> attendanceGoOutList = attendanceGoOutRepository.findAllById(Arrays.asList(ids));
|
| | | attendanceGoOutList.forEach(attendanceGoOut -> {
|
| | | attendanceGoOut.setOutDuration(getRemainingTime(attendanceGoOut));
|
| | | attendanceGoOutRepository.save(attendanceGoOut);
|
| | | });
|
| | | }
|
| | |
|
| | | private Long calculateTimes(Calculate calculate, Long userId) {
|
| | | User userInfo = userRepository.getOne(userId);
|
| | | SysRostering sysRostering = userInfo.getSysRostering();
|
| | |
|
| | | LocalDate startDate = calculate.getStart().toLocalDate();
|
| | | LocalDate endDate = calculate.getEnd().toLocalDate();
|
| | | LocalTime startTime = calculate.getStart().toLocalTime();
|
| | | LocalTime endTime = calculate.getEnd().toLocalTime();
|
| | | long distance = ChronoUnit.DAYS.between(startDate, endDate);
|
| | | return Stream.iterate(startDate, d -> d.plusDays(1)).limit(distance + 1).map(localDate -> {
|
| | | // SysConfigHolidayDto sysConfigHolidayDto = sysConfigHolidayService.queryByHolidayDate(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
|
| | | // Boolean isWeekend = localDate.getDayOfWeek() == DayOfWeek.SATURDAY || localDate.getDayOfWeek() == DayOfWeek.SUNDAY;
|
| | | // if ((sysConfigHolidayDto == null && !isWeekend) ||
|
| | | // sysConfigHolidayDto != null && Constant.DATE_TYPE_WEEKDAY.equals(sysConfigHolidayDto.getDateType())) {
|
| | | //
|
| | | // LocalDateTime goOutStartTime = localDate.atTime(startTime);
|
| | | // LocalDateTime goOutEndTime = localDate.atTime(endTime);
|
| | | // LocalDateTime lunchStartTime = localDate.atTime(sysRostering.getLunchStartTime());
|
| | | // LocalDateTime lunchEndTime = localDate.atTime(sysRostering.getLunchEndTime());
|
| | | //
|
| | | //
|
| | | // if (goOutEndTime.isBefore(lunchStartTime) || goOutStartTime.isAfter(lunchEndTime)) {
|
| | | // return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
|
| | | // } else if (!goOutEndTime.isBefore(lunchStartTime) && !goOutEndTime.isAfter(lunchEndTime)) {
|
| | | // return ChronoUnit.SECONDS.between(goOutStartTime, lunchStartTime);
|
| | | // } else if (!goOutStartTime.isBefore(lunchStartTime) && !goOutStartTime.isAfter(lunchEndTime)) {
|
| | | // return ChronoUnit.SECONDS.between(lunchEndTime, goOutEndTime);
|
| | | // } else {
|
| | | // return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime) - 3600;
|
| | | // }
|
| | | // } else {
|
| | | // LocalDateTime goOutStartTime = localDate.atTime(startTime);
|
| | | // LocalDateTime goOutEndTime = localDate.atTime(endTime);
|
| | | // return ChronoUnit.SECONDS.between(goOutStartTime, goOutEndTime);
|
| | | // }
|
| | |
|
| | | // TODO:20250806修改 减去下班后休息时段
|
| | | LocalDateTime goOutStartTime = localDate.atStartOfDay();
|
| | | LocalDateTime goOutEndTime = localDate.atTime(LocalTime.MAX);
|
| | | if (localDate.equals(endDate)) {
|
| | | goOutEndTime = localDate.atTime(endTime);
|
| | | }
|
| | | if (localDate.equals(startDate)) {
|
| | | goOutStartTime = localDate.atTime(startTime);
|
| | | }
|
| | | LocalDateTime lunchStartTime = localDate.atTime(sysRostering.getLunchStartTime());
|
| | | LocalDateTime lunchEndTime = localDate.atTime(sysRostering.getLunchEndTime());
|
| | | LocalDateTime offStartTime = localDate.atTime(sysRostering.getWorkEndTime());
|
| | | LocalDateTime offEndTime = offStartTime.plusMinutes(30L);
|
| | | Duration duration = Calculate.calculateRemainingDuration(new Calculate(goOutStartTime, goOutEndTime), new Calculate(lunchStartTime, lunchEndTime), new Calculate(offStartTime, offEndTime));
|
| | | return duration.get(ChronoUnit.SECONDS);
|
| | | }).reduce(0L, (result, current) -> result + current);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 审核/撤销功能
|
| | | *
|
| | | * @param resources /
|
| | | */
|
| | | @Override
|
| | | public void updateApprove(AttendanceGoOut resources) throws Exception {
|
| | | AttendanceGoOut attendanceGoOut = attendanceGoOutRepository.findById(resources.getId()).orElseGet(AttendanceGoOut::new);
|
| | | ValidationUtil.isNull(attendanceGoOut.getId(), "AttendanceGoOut", "id", resources.getId());
|
| | | attendanceGoOut.copy(resources);
|
| | | attendanceGoOutRepository.save(attendanceGoOut);
|
| | | // 调用定时任务修改考勤记录
|
| | | List<User> userList = new ArrayList<>();
|
| | | if (ObjectUtils.isNotEmpty(attendanceGoOut.getUserId())) {
|
| | | User user = userRepository.findById(attendanceGoOut.getUserId()).orElseGet(User::new);
|
| | | userList.add(user);
|
| | | }
|
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
| | | String attendanceDate = df.format(attendanceGoOut.getOutStartTime());
|
| | | weComService.getWeComAttendanceListOfNoonOrNight(attendanceDate, userList);
|
| | | }
|
| | |
|
| | | /**
|
| | | * 批量审核(外出)
|
| | | *
|
| | | * @param request
|
| | | * @throws Exception
|
| | | */
|
| | | @Override
|
| | | public void updateAgree(AgreeUpdateRequest request) throws Exception {
|
| | | for (Long id : request.getIds()) {
|
| | | AttendanceGoOut attendanceGoOut = attendanceGoOutRepository.findById(id).orElseGet(AttendanceGoOut::new);
|
| | | ValidationUtil.isNull(attendanceGoOut.getId(), "AttendanceGoOut", "id", id);
|
| | | if (request.getNum().equals(Long.valueOf(1))) {
|
| | | attendanceGoOut.setApprovalStatus(Constant.GOOUT_APPROVE_STATUS_AGREE);
|
| | | } else {
|
| | | attendanceGoOut.setApprovalStatus(Constant.GOOUT_APPROVE_STATUS_REFUSE);
|
| | | }
|
| | | attendanceGoOutRepository.save(attendanceGoOut);
|
| | | // 调用定时任务修改考勤记录
|
| | | List<User> userList = new ArrayList<>();
|
| | | if (ObjectUtils.isNotEmpty(attendanceGoOut.getUserId())) {
|
| | | User user = userRepository.findById(attendanceGoOut.getUserId()).orElseGet(User::new);
|
| | | userList.add(user);
|
| | | }
|
| | | SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
|
| | | String attendanceDate = df.format(attendanceGoOut.getOutStartTime());
|
| | | weComService.getWeComAttendanceListOfNoonOrNight(attendanceDate, userList);
|
| | | }
|
| | | }
|
| | | } |