weicd
2023-09-28 d0c51ac702c57f2cae84a8d879a5b605f2dab630
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/salary/service/impl/NewSalaryServiceImpl.java
@@ -15,6 +15,7 @@
 */
package me.zhengjie.modules.salary.service.impl;
import cn.hutool.core.io.resource.MultiFileResource;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import me.zhengjie.exception.BadRequestException;
@@ -55,6 +56,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletResponse;
@@ -151,6 +153,13 @@
        if (newSalaryList.size() > 0) {
            throw new BadRequestException("找到相同用户相同年月的工资记录!");
        }
//        NewSalary savedSalary = newSalaryRepository.save(resources);
//        if (savedSalary.getId() != null) {
//            // 保存成功
//        } else {
//            // 保存失败
//            Prin
//        }
        return newSalaryMapper.toDto(newSalaryRepository.save(resources));
    }
@@ -309,20 +318,36 @@
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
//    @Transactional(rollbackFor = Exception.class)
    public void generate(NewSalaryQueryCriteria criteria) {
        List<String> errors = new ArrayList<>();
        for (Long id : criteria.getSelectedValue1()) {
            try {
        // 用户工资结算
        userSalarySettlement(criteria.getUserId(), DateUtil.internationalTimeToChinaTime(criteria.getTargetYearMonth()));
                userSalarySettlement(id, DateUtil.internationalTimeToChinaTime(criteria.getTargetYearMonth()), errors);
            } catch (Exception e) {
                // 记录错误信息
                errors.add("ID " + id + ": " + e.getMessage());
            }
        }
        // 在循环结束后处理错误
        if (!errors.isEmpty()) {
            // 将所有的错误信息收集到一个字符串中
            String errorMessage = String.join("; ", errors);
            // 抛出一个新的异常
            throw new RuntimeException(errorMessage);
        }
    }
    // ==================== 以下为定时生成工资记录主要逻辑 ====================
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void userSalarySettlement(Long userId, String targetYearMonth) {
    @Transactional(rollbackFor = Exception.class) // propagation = Propagation.REQUIRES_NEW,
    public void userSalarySettlement(Long userId, String targetYearMonth, List<String> errors) {
        // 判断用户ID是否为空
        if (userId != null) {
            // 单个用户工资结算
            singleUserSalarySettlement(userId, targetYearMonth);
            singleUserSalarySettlement(userId, targetYearMonth, errors);
        } else {
            // 用户检索类
            UserQueryCriteria userQueryCriteria = new UserQueryCriteria();
@@ -333,7 +358,7 @@
            for (int i = 0; i < userDtoList.size(); i++) {
                try {
                    // 单个用户工资结算
                    singleUserSalarySettlement(userDtoList.get(i).getId(), targetYearMonth);
                    singleUserSalarySettlement(userDtoList.get(i).getId(), targetYearMonth, errors);
                } catch (Exception e) {
                    // 跳过本次循环
                    continue;
@@ -360,7 +385,7 @@
    // 单个用户工资结算
    private void singleUserSalarySettlement(Long userId, String targetYearMonth) {
    private void singleUserSalarySettlement(Long userId, String targetYearMonth, List<String> errors) {
        // 日历类
        Calendar cal = Calendar.getInstance();
        // 设置年份
@@ -383,8 +408,9 @@
        newSalary.setSalaryYearMonth(new Timestamp(cal.getTime().getTime()));
        newSalary.setPublicityStatus(Constant.PUBLICITY_STATUS_NOT);
        newSalary.setApproveStatus(Constant.APPROVE_STATUS_UNRECOGNIZED);
        try {
        // 获取用户级别工资
        newSalary = getUserLevelWage(userId, targetYearMonth, newSalary);
            newSalary = getUserLevelWage(userId, targetYearMonth, newSalary, errors);
        // 获取用户级别保险
        newSalary = getUserLevelInsurance(userId, targetYearMonth, newSalary);
        // 获取用户级别其他
@@ -397,6 +423,10 @@
        newSalary = getComplianceWork(userId, targetYearMonth, newSalary);
        // 获取已完成工时
        newSalary = getCompletedWork(userId, targetYearMonth, newSalary);
        } catch (BadRequestException e) {
            errors.add(e.getMessage());
            return;
        }
        try {
            // 保存
            create(newSalary);
@@ -407,11 +437,12 @@
    }
    // 获取用户级别工资
    private NewSalary getUserLevelWage(Long userId, String targetYearMonth, NewSalary newSalary) {
    private NewSalary getUserLevelWage(Long userId, String targetYearMonth, NewSalary newSalary, List<String> errors) {
        // 用户级别工资检索类
        UserLevelWageQueryCriteria userLevelWageQueryCriteria = new UserLevelWageQueryCriteria();
        userLevelWageQueryCriteria.setUserId(userId);
        userLevelWageQueryCriteria.setTargetYearMonth(targetYearMonth);
        try {
        // 查询指定年月用户级别工资
        UserLevelWageDto userLevelWageDto = userLevelWageService.queryTargetYearMonth(userLevelWageQueryCriteria);
        // 级别基本工资
@@ -428,6 +459,10 @@
        newSalary.setPerformanceWage(BigDecimal.ZERO);
        // 设置工资条的基本工资
        newSalary.setTeamWage(BigDecimal.ZERO);
        } catch (RuntimeException e) {
            errors.add(e.getMessage());
            return null;
        }
        // 返回
        return newSalary;
    }