/* * 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.SneakyThrows; import me.zhengjie.exception.BadRequestException; import me.zhengjie.modules.attendance.domain.AttendanceMonth; import me.zhengjie.modules.attendance.domain.AttendanceMonthLeave; import me.zhengjie.modules.attendance.repository.AttendanceMonthLeaveRepository; import me.zhengjie.modules.constant.Constant; import me.zhengjie.modules.salary.domain.Salary; import me.zhengjie.modules.salary.service.dto.SalaryDto; import me.zhengjie.modules.system.domain.Dept; import me.zhengjie.modules.system.domain.Job; import me.zhengjie.modules.system.domain.SysCompany; import me.zhengjie.modules.system.domain.User; import me.zhengjie.modules.system.repository.*; import me.zhengjie.modules.system.service.RoleService; import me.zhengjie.modules.system.service.dto.RoleSmallDto; import me.zhengjie.utils.*; import lombok.RequiredArgsConstructor; import me.zhengjie.modules.attendance.repository.AttendanceMonthRepository; import me.zhengjie.modules.attendance.service.AttendanceMonthService; import me.zhengjie.modules.attendance.service.dto.AttendanceMonthDto; import me.zhengjie.modules.attendance.service.dto.AttendanceMonthQueryCriteria; import me.zhengjie.modules.attendance.service.mapstruct.AttendanceMonthMapper; 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.util.*; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import java.util.function.Function; import java.util.stream.Collectors; /** * @website https://eladmin.vip * @description 服务实现 * @author wcd * @date 2023-02-10 **/ @Service @RequiredArgsConstructor public class AttendanceMonthServiceImpl implements AttendanceMonthService { private final AttendanceMonthRepository attendanceMonthRepository; private final AttendanceMonthMapper attendanceMonthMapper; private final UserRepository userRepository; private final SysCompanyRepository sysCompanyRepository; private final DeptRepository deptRepository; private final JobRepository jobRepository; private final AttendanceMonthLeaveRepository attendanceMonthLeaveRepository; private final AttendanceLeaveServiceImpl attendanceLeaveService; private final RoleService roleService; private final DictDetailRepository dictDetailRepository; @Override public Map queryAll(AttendanceMonthQueryCriteria criteria, Pageable pageable){ // 判断工资查询类年月是否为空 if (criteria.getAttendanceDate() != null && !"".equals(criteria.getAttendanceDate())) { // 年月:国际时间转中国时间 criteria.setAttendanceDate(DateUtil.internationalTimeToChinaTime(criteria.getAttendanceDate())); } // 获取登录用户角色等级 List levels = roleService.findByUsersId(SecurityUtils.getCurrentUserId()).stream().map(RoleSmallDto::getLevel).collect(Collectors.toList()); // 登录用户最大角色等级 int min = Collections.min(levels); // 判断角色等级 if (min == Constant.ROLE_LEVEL_MANAGER || min == Constant.ROLE_LEVEL_LEADER || min == Constant.ROLE_LEVEL_ORDINARY) { criteria.setUserId(SecurityUtils.getCurrentUserId()); criteria.setIsPublicity(Constant.PUBLICITY_STATUS_YES); } Page page = attendanceMonthRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); // 将实体类转换为DTO(重写) Function func = new Function(){ @SneakyThrows @Override public AttendanceMonthDto apply(AttendanceMonth t) { // 将实体类转换成DTO AttendanceMonthDto attendanceMonthDto = attendanceMonthMapper.toDto(t); // 根据用户ID查询用户信息 User user = userRepository.findById(attendanceMonthDto.getUserId()).orElseGet(User::new); // 用户昵称 attendanceMonthDto.setNickName(user.getNickName()); // 查询公司 SysCompany sysCompany = sysCompanyRepository.findById(attendanceMonthDto.getCompanyId()).orElseGet(SysCompany::new); attendanceMonthDto.setCompanyName(sysCompany.getLabel()); // 查询部门 Dept dept = deptRepository.findById(attendanceMonthDto.getDeptId()).orElseGet(Dept::new); attendanceMonthDto.setDeptName(dept.getName()); // 查询岗位 if(ObjectUtils.isNotEmpty(attendanceMonthDto.getJobId())){ Job job = jobRepository.findById(attendanceMonthDto.getJobId()).orElseGet(Job::new); attendanceMonthDto.setJobName(job.getName()); } List leaveTypeList = new ArrayList<>(); // 查询请假类型表 List attendanceMonthLeaves = attendanceMonthLeaveRepository.findByAttendanceMonthId(attendanceMonthDto.getId()); if(attendanceMonthLeaves.size() != 0){ for(int i = 0; i < attendanceMonthLeaves.size(); i++){ Map attTypeInfo = new HashMap(); attTypeInfo.put("leaveType", attendanceMonthLeaves.get(i).getLeaveType()); attTypeInfo.put("leaveCount", attendanceMonthLeaves.get(i).getLeaveCount()); attTypeInfo.put("leaveTime", attendanceMonthLeaves.get(i).getLeaveTime()); if(attendanceMonthLeaves.get(i).getLeaveType().equals(Constant.LEAVE_TYPE_ANNUAL_LEAVE) || attendanceMonthLeaves.get(i).getLeaveType().equals(Constant.LEAVE_TYPE_REPLACE)){ attTypeInfo.put("leaveYearTime", attendanceMonthLeaves.get(i).getSurplusDuration() == null ? 0 : attendanceMonthLeaves.get(i).getSurplusDuration()); } leaveTypeList.add(attTypeInfo); } }else{ Double yearTime = attendanceLeaveService.getYearTimeLength(attendanceMonthDto.getUserId()); attendanceMonthDto.setYearTime(yearTime); } attendanceMonthDto.setLeaveTypeList(leaveTypeList); // 返回处理好的DTO return attendanceMonthDto; } }; return PageUtil.toPage(page.map(func)); } @Override @SneakyThrows public List queryAll(AttendanceMonthQueryCriteria criteria) { // 判断工资查询类年月是否为空 if (criteria.getAttendanceDate() != null && !"".equals(criteria.getAttendanceDate())) { // 年月:国际时间转中国时间 criteria.setAttendanceDate(DateUtil.internationalTimeToChinaTime(criteria.getAttendanceDate())); } List attendanceMonthDtoList = attendanceMonthMapper.toDto(attendanceMonthRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder))); for (AttendanceMonthDto attendanceMonthDto : attendanceMonthDtoList) { // 根据用户ID查询用户信息 User user = userRepository.findById(attendanceMonthDto.getUserId()).orElseGet(User::new); // 用户昵称 attendanceMonthDto.setNickName(user.getNickName()); // 查询公司 SysCompany sysCompany = sysCompanyRepository.findById(attendanceMonthDto.getCompanyId()).orElseGet(SysCompany::new); attendanceMonthDto.setCompanyName(sysCompany.getLabel()); // 查询部门 Dept dept = deptRepository.findById(attendanceMonthDto.getDeptId()).orElseGet(Dept::new); attendanceMonthDto.setDeptName(dept.getName()); // 查询岗位 if(ObjectUtils.isNotEmpty(attendanceMonthDto.getJobId())){ Job job = jobRepository.findById(attendanceMonthDto.getJobId()).orElseGet(Job::new); attendanceMonthDto.setJobName(job.getName()); } // List leaveTypeList = new ArrayList<>(); // // 查询请假类型表 // List attendanceMonthLeaves = attendanceMonthLeaveRepository.findByAttendanceMonthId(attendanceMonthDto.getId()); // if(attendanceMonthLeaves.size() != 0){ // for(int i = 0; i < attendanceMonthLeaves.size(); i++){ // Map attTypeInfo = new HashMap(); // attTypeInfo.put("leaveType", attendanceMonthLeaves.get(i).getLeaveType()); // attTypeInfo.put("leaveCount", attendanceMonthLeaves.get(i).getLeaveCount()); // attTypeInfo.put("leaveTime", attendanceMonthLeaves.get(i).getLeaveTime()); // if(attendanceMonthLeaves.get(i).getLeaveType().equals(Constant.LEAVE_TYPE_ANNUAL_LEAVE) || attendanceMonthLeaves.get(i).getLeaveType().equals(Constant.LEAVE_TYPE_REPLACE)){ // attTypeInfo.put("leaveYearTime", attendanceMonthLeaves.get(i).getSurplusDuration() == null ? 0 : attendanceMonthLeaves.get(i).getSurplusDuration()); // } // leaveTypeList.add(attTypeInfo); // } // }else{ // Double yearTime = attendanceLeaveService.getYearTimeLength(attendanceMonthDto.getUserId()); // attendanceMonthDto.setYearTime(yearTime); // } // attendanceMonthDto.setLeaveTypeList(leaveTypeList); } return attendanceMonthDtoList; } @Override @Transactional public AttendanceMonthDto findById(Long id) { AttendanceMonth attendanceMonth = attendanceMonthRepository.findById(id).orElseGet(AttendanceMonth::new); ValidationUtil.isNull(attendanceMonth.getId(),"AttendanceMonth","id",id); return attendanceMonthMapper.toDto(attendanceMonth); } @Override @Transactional(rollbackFor = Exception.class) public AttendanceMonthDto create(AttendanceMonth resources) { // 根据用户ID和年月查询 AttendanceMonth attendanceMonth = attendanceMonthRepository.findByAttendanceDateAndUserId(resources.getAttendanceDate().toString().substring(0,19),resources.getUserId()); // 判断列表长度 if (ObjectUtils.isNotEmpty(attendanceMonth)) { throw new BadRequestException("找到相同用户相同年月的月报记录!"); } return attendanceMonthMapper.toDto(attendanceMonthRepository.save(resources)); } @Override @Transactional(rollbackFor = Exception.class) public void update(AttendanceMonth resources) { // 根据用户ID和年月查询 AttendanceMonth attendanceMonth = attendanceMonthRepository.findByAttendanceDateAndUserId(resources.getAttendanceDate().toString().substring(0,19),resources.getUserId()); // 判断列表长度 if (ObjectUtils.isNotEmpty(attendanceMonth) && attendanceMonth.getId() != resources.getId()) { throw new BadRequestException("找到相同用户相同年月的月报记录!"); } // 判断承认状态 if (ObjectUtils.isNotEmpty(attendanceMonth) && Constant.APPROVE_STATUS_ACKNOWLEDGED.equals(attendanceMonth.getIsAdmit())) { throw new BadRequestException("已承认的月报记录无法进行修改!"); } ValidationUtil.isNull( attendanceMonth.getId(),"AttendanceMonth","id",resources.getId()); attendanceMonth.copy(resources); attendanceMonthRepository.save(attendanceMonth); } @Override public void deleteAll(Long[] ids) { for (Long id : ids) { attendanceMonthRepository.deleteById(id); } } /** * 月报个人确认 * @param resources */ @Override public void updateConfirm(AttendanceMonth resources) { AttendanceMonth attendanceMonth = attendanceMonthRepository.findById(resources.getId()).orElseGet(AttendanceMonth::new); attendanceMonth.setIsAdmit(Constant.APPROVE_STATUS_ACKNOWLEDGED); attendanceMonthRepository.save(attendanceMonth); } /** * 公示月报信息 * @param ids / */ @Override public void publicity(Long[] ids) { // 循环ID列表 for (Long id : ids) { // 查询月报 AttendanceMonth attendanceMonth = attendanceMonthRepository.findById(id).orElseGet(AttendanceMonth::new); ValidationUtil.isNull( attendanceMonth.getId(),"AttendanceMonth","id",id); // 公示状态 attendanceMonth.setIsPublicity(Constant.PUBLICITY_STATUS_YES); // 保存 attendanceMonthRepository.save(attendanceMonth); } } @Override public void download(List all, HttpServletResponse response) throws IOException { List> list = new ArrayList<>(); for (AttendanceMonthDto attendanceMonth : all) { // // 根据用户ID查询用户信息 // User user = userRepository.findById(attendanceMonth.getUserId()).orElseGet(User::new); // // 查询公司 // SysCompany sysCompany = sysCompanyRepository.findById(attendanceMonth.getCompanyId()).orElseGet(SysCompany::new); // // 查询部门 // Dept dept = deptRepository.findById(attendanceMonth.getDeptId()).orElseGet(Dept::new); // Job job = new Job(); // // 查询岗位 // if(attendanceMonth.getJobId() != null){ // job = jobRepository.findById(attendanceMonth.getJobId()).orElseGet(Job::new); // } String isAdmit = attendanceMonth.getIsAdmit() != null ? dictDetailRepository.findByDictNameAndValue(Constant.APPROVE_STATUS_ACKNOWLEDGED, attendanceMonth.getIsAdmit()).getLabel() : ""; String isP = attendanceMonth.getIsPublicity() != null ? dictDetailRepository.findByDictNameAndValue(Constant.PUBLICITY_STATUS_YES, attendanceMonth.getIsPublicity()).getLabel() : ""; Double annualLeave = 0.0D; Double locationLeave = 0.0D; Double compassionateLeave = 0.0D; List attendanceMonthLeaves = attendanceMonthLeaveRepository.findByAttendanceMonthId(attendanceMonth.getId()); if(attendanceMonthLeaves.size() != 0){ for(AttendanceMonthLeave attendanceMonthLeave : attendanceMonthLeaves){ if(attendanceMonthLeave.getLeaveType().equals(Constant.LEAVE_TYPE_COMPASSIONATE_LEAVE) || attendanceMonthLeave.getLeaveType().equals(Constant.LEAVE_TYPE_SICK_LEAVE)){ compassionateLeave += attendanceMonthLeave.getLeaveTime(); } else if (attendanceMonthLeave.getLeaveType().equals(Constant.LEAVE_TYPE_ANNUAL_LEAVE)) { annualLeave += attendanceMonthLeave.getLeaveTime(); } else if (attendanceMonthLeave.getLeaveType().equals(Constant.LEAVE_TYPE_REPLACE)) { locationLeave += attendanceMonthLeave.getLeaveTime(); } } } Map map = new LinkedHashMap<>(); map.put("统计年月", attendanceMonth.getAttendanceDate()); map.put("用户", attendanceMonth.getNickName()); map.put("公司", attendanceMonth.getCompanyName()); map.put("部门", attendanceMonth.getDeptName()); map.put("岗位", attendanceMonth.getJobName()); map.put("应出勤天数", attendanceMonth.getRequiredAttendanceDays()); map.put("实际出勤天数", attendanceMonth.getActualAttendanceDays()); map.put("应出勤时长", attendanceMonth.getRequiredAttendanceTime()); // map.put("外出时长", attendanceMonth.getGoOutTime()); // map.put("出差时长", attendanceMonth.getBussTime()); map.put("实际出勤时长", attendanceMonth.getActualAttendanceTime()); map.put("迟到次数", attendanceMonth.getCountOfBeingLate()); map.put("迟到时长", attendanceMonth.getTimeOfBeingLate()); map.put("早退次数", attendanceMonth.getCountOfBeingEarly()); map.put("早退时长", attendanceMonth.getTimeOfBeingEarly()); // map.put("外出次数", attendanceMonth.getGoOutNumber()); // map.put("出差次数", attendanceMonth.getBussNumber()); // map.put("出差时长", attendanceMonth.getBussTime()); map.put("当月使用年假", annualLeave); map.put("当月使用调休假", locationLeave); map.put("当月使用事假", compassionateLeave); Double yearTime = attendanceLeaveService.getYearTimeLength(attendanceMonth.getUserId()); map.put("剩余年假", yearTime); Double overTime = attendanceLeaveService.getOverTimeLength(attendanceMonth.getUserId()); map.put("剩余调休假", overTime); map.put("上班缺卡次数", attendanceMonth.getOnCardNumber()); map.put("下班缺卡次数", attendanceMonth.getOffCardNumber()); map.put("公示状态", attendanceMonth.getIsPublicity()); map.put("承认状态", isAdmit); // map.put("备注", attendanceMonth.getRemarks()); // map.put("创建者", attendanceMonth.getCreateBy()); // map.put("创建时间", attendanceMonth.getCreateTime()); // map.put("更新者", attendanceMonth.getUpdateBy()); // map.put("更新时间", attendanceMonth.getUpdateTime()); list.add(map); } // list = list.subList(0, 1); // list.forEach(System.out::println); FileUtil.downloadExcel(list, response); } }