| | |
| | | */ |
| | | package me.zhengjie.modules.salary.service.impl; |
| | | |
| | | import me.zhengjie.exception.BadRequestException; |
| | | import me.zhengjie.modules.constant.Constant; |
| | | import me.zhengjie.modules.salary.domain.Salary; |
| | | 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.modules.system.service.RoleService; |
| | | import me.zhengjie.modules.system.service.dto.RoleSmallDto; |
| | | import me.zhengjie.modules.system.service.dto.SysConfigHolidayQueryCriteria; |
| | | import me.zhengjie.utils.*; |
| | | import lombok.RequiredArgsConstructor; |
| | |
| | | import org.springframework.data.domain.Page; |
| | | import org.springframework.data.domain.Pageable; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.*; |
| | | import java.io.IOException; |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.util.ArrayList; |
| | | import java.util.LinkedHashMap; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * @website https://eladmin.vip |
| | |
| | | private final SalaryMapper salaryMapper; |
| | | private final UserRepository userRepository; |
| | | private final SysConfigHolidayRepository sysConfigHolidayRepository; |
| | | private final RoleService roleService; |
| | | private final DictDetailRepository dictDetailRepository; |
| | | |
| | | @Override |
| | | public Map<String,Object> queryAll(SalaryQueryCriteria criteria, Pageable pageable){ |
| | | // 判断工资查询类工资年月是否为空 |
| | | if (criteria.getSalaryYearMonth() != null && !"".equals(criteria.getSalaryYearMonth())) { |
| | | // 工资年月:国际时间转中国时间 |
| | | criteria.setSalaryYearMonth(DateUtil.internationalTimeToChinaTime(criteria.getSalaryYearMonth())); |
| | | } |
| | | // 获取登录用户角色等级 |
| | | List<Integer> 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.setPublicityStatus(Constant.PUBLICITY_STATUS_YES); |
| | | } |
| | | Page<Salary> page = salaryRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable); |
| | | // 将实体类转换为DTO(重写) |
| | | Function<Salary, SalaryDto> func = new Function<Salary, SalaryDto>(){ |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public SalaryDto create(Salary resources) { |
| | | // 根据用户ID和工资年月查询 |
| | | List<Salary> salaryList = salaryRepository.findByUserIdAndSalaryYearMonth(resources.getUserId(), resources.getSalaryYearMonth()); |
| | | // 判断列表长度 |
| | | if (salaryList.size() > 0) { |
| | | throw new BadRequestException("找到相同用户相同年月的工资记录!"); |
| | | } |
| | | return salaryMapper.toDto(salaryRepository.save(resources)); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void update(Salary resources) { |
| | | // 根据用户ID和工资年月查询 |
| | | List<Salary> salaryList = salaryRepository.findByUserIdAndSalaryYearMonth(resources.getUserId(), resources.getSalaryYearMonth()); |
| | | // 判断列表长度 |
| | | if (salaryList.size() > 1) { |
| | | throw new BadRequestException("找到相同用户相同年月的工资记录!"); |
| | | } else if (salaryList.size() == 1) { |
| | | // 判断是否是同一条记录 |
| | | if (salaryList.get(0).getId() != resources.getId()) { |
| | | throw new BadRequestException("找到相同用户相同年月的工资记录!"); |
| | | } |
| | | } |
| | | Salary salary = salaryRepository.findById(resources.getId()).orElseGet(Salary::new); |
| | | // 判断承认状态 |
| | | if (Constant.APPROVE_STATUS_ACKNOWLEDGED.equals(salary.getApproveStatus())) { |
| | | throw new BadRequestException("已承认的工资记录无法进行修改!"); |
| | | } |
| | | ValidationUtil.isNull( salary.getId(),"Salary","id",resources.getId()); |
| | | salary.copy(resources); |
| | | salaryRepository.save(salary); |
| | |
| | | List<Map<String, Object>> list = new ArrayList<>(); |
| | | for (SalaryDto salary : all) { |
| | | String userNickName = salary.getUserId() != null ? userRepository.findById(salary.getUserId()).orElseGet(User::new).getNickName() : ""; |
| | | String publicityStatus = salary.getPublicityStatus() != null ? dictDetailRepository.findByDictNameAndValue(Constant.PUBLICITY_STATUS, salary.getPublicityStatus()).getLabel() : ""; |
| | | String approveStatus = salary.getApproveStatus() != null ? dictDetailRepository.findByDictNameAndValue(Constant.APPROVE_STATUS, salary.getApproveStatus()).getLabel() : ""; |
| | | Map<String,Object> map = new LinkedHashMap<>(); |
| | | map.put("员工姓名", userNickName); |
| | | map.put("工资年月", salary.getSalaryYearMonth()); |
| | |
| | | map.put("个人所得税", salary.getIndividualIncomeTax()); |
| | | map.put("税后金额", salary.getAfterTaxAmount()); |
| | | map.put("备注", salary.getRemarks()); |
| | | map.put("公示状态", publicityStatus); |
| | | map.put("承认状态", approveStatus); |
| | | map.put("创建者", salary.getCreateBy()); |
| | | map.put("创建时间", salary.getCreateTime()); |
| | | map.put("更新者", salary.getUpdateBy()); |
| | |
| | | // 查询某月工作天数 |
| | | return DateUtil.getWorkDayOfMonth(sysConfigHolidayList, targetYearMonth); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void deleteByUserIdAndSalaryYearMonthAndCreateNew(Salary resources) { |
| | | // 根据用户ID和工资年月删除 |
| | | salaryRepository.deleteByUserIdAndSalaryYearMonth(resources.getUserId(), resources.getSalaryYearMonth().toString().substring(0,7)); |
| | | // 保存 |
| | | salaryRepository.save(resources); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void publicity(Long[] ids) { |
| | | // 循环ID列表 |
| | | for (Long id : ids) { |
| | | // 查询工资 |
| | | Salary salary = salaryRepository.findById(id).orElseGet(Salary::new); |
| | | ValidationUtil.isNull( salary.getId(),"Salary","id",id); |
| | | // 公示状态 |
| | | salary.setPublicityStatus(Constant.PUBLICITY_STATUS_YES); |
| | | // 保存 |
| | | salaryRepository.save(salary); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void approve(Long[] ids) { |
| | | // 循环ID列表 |
| | | for (Long id : ids) { |
| | | // 查询工资 |
| | | Salary salary = salaryRepository.findById(id).orElseGet(Salary::new); |
| | | ValidationUtil.isNull( salary.getId(),"Salary","id",id); |
| | | // 承认状态 |
| | | salary.setApproveStatus(Constant.APPROVE_STATUS_ACKNOWLEDGED); |
| | | // 保存 |
| | | salaryRepository.save(salary); |
| | | } |
| | | } |
| | | } |