huaz
2023-01-09 5fd2ac11a530b5b7ddfcb2a40cf5d68fdde193be
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java
@@ -20,14 +20,10 @@
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.security.service.OnlineUserService;
import me.zhengjie.modules.security.service.UserCacheManager;
import me.zhengjie.modules.system.domain.SysLevel;
import me.zhengjie.modules.system.domain.SysUserLevelWage;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.exception.EntityNotFoundException;
import me.zhengjie.modules.system.repository.SysLevelRepository;
import me.zhengjie.modules.system.repository.SysUserLevelWageRepository;
import me.zhengjie.modules.system.repository.UserRepository;
import me.zhengjie.modules.system.repository.*;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.*;
import me.zhengjie.modules.system.service.mapstruct.SysUserLevelWageMapper;
@@ -67,6 +63,8 @@
    private final UserLoginMapper userLoginMapper;
    private final SysLevelRepository sysLevelRepository;
    private final SysUserLevelWageRepository sysUserLevelWageRepository;
    private final SysUserLevelInsuranceRepository sysUserLevelInsuranceRepository;
    private final SysUserLevelOtherRepository sysUserLevelOtherRepository;
    @Override
    public Object queryAll(UserQueryCriteria criteria, Pageable pageable) {
@@ -250,15 +248,6 @@
        flushCache(username);
    }
    /**
     * 查询在职员工
     * @return
     */
    @Override
    public List<User> getUsers() {
        List<User> List = userRepository.findAllByEnabled(true);
        return List;
    }
    @Override
    public void download(List<UserDto> queryAll, HttpServletResponse response) throws IOException {
@@ -314,6 +303,10 @@
        if (oldLevelId == null) {
            // 创建用户级别工资
            createSysUserLevelWage(userId, newLevelId);
            // 创建用户级别保险
            createSysUserLevelInsurance(userId, newLevelId);
            // 创建用户级别其他
            createSysUserLevelOther(userId, newLevelId);
        } else if (oldLevelId != newLevelId) {
            // 旧用户级别工资
            SysUserLevelWage oldSysUserLevelWage = sysUserLevelWageRepository.findLastByUserId(userId);
@@ -342,6 +335,60 @@
            }
            // 创建用户级别工资
            createSysUserLevelWage(userId, newLevelId);
            // 旧用户级别保险
            SysUserLevelInsurance oldSysUserLevelInsurance = sysUserLevelInsuranceRepository.findLastByUserId(userId);
            // 判断旧用户级别保险是否为空
            if (oldSysUserLevelInsurance != null && oldSysUserLevelInsurance.getId() != null) {
                // 旧日期字符串
                String oldDateString = oldSysUserLevelInsurance.getStartDate().toString().substring(0,10);
                // 获取当前日期
                Date nowDate = new Date();
                // 创建Calendar实例
                Calendar nowCalendar = Calendar.getInstance();
                // 设置当前时间
                nowCalendar.setTime(nowDate);
                // 新日期字符串
                String nowDateString = new Timestamp(nowCalendar.getTime().getTime()).toString().substring(0,10);
                // 判断新、旧日期字符串
                if (oldDateString.equals(nowDateString)) {
                    throw new BadRequestException("每天只能调整一次级别,请明日再试!");
                }
                // 设置前一天时间
                nowCalendar.add(Calendar.DATE, -1);
                // 旧用户级别保险
                oldSysUserLevelInsurance.setEndDate(new Timestamp(nowCalendar.getTime().getTime()));
                // 保存
                sysUserLevelInsuranceRepository.save(oldSysUserLevelInsurance);
            }
            // 创建用户级别保险
            createSysUserLevelInsurance(userId, newLevelId);
            // 旧用户级别其他
            SysUserLevelOther oldSysUserLevelOther = sysUserLevelOtherRepository.findLastByUserId(userId);
            // 判断旧用户级别其他是否为空
            if (oldSysUserLevelOther != null && oldSysUserLevelOther.getId() != null) {
                // 旧日期字符串
                String oldDateString = oldSysUserLevelOther.getStartDate().toString().substring(0,10);
                // 获取当前日期
                Date nowDate = new Date();
                // 创建Calendar实例
                Calendar nowCalendar = Calendar.getInstance();
                // 设置当前时间
                nowCalendar.setTime(nowDate);
                // 新日期字符串
                String nowDateString = new Timestamp(nowCalendar.getTime().getTime()).toString().substring(0,10);
                // 判断新、旧日期字符串
                if (oldDateString.equals(nowDateString)) {
                    throw new BadRequestException("每天只能调整一次级别,请明日再试!");
                }
                // 设置前一天时间
                nowCalendar.add(Calendar.DATE, -1);
                // 旧用户级别保险
                oldSysUserLevelOther.setEndDate(new Timestamp(nowCalendar.getTime().getTime()));
                // 保存
                sysUserLevelOtherRepository.save(oldSysUserLevelOther);
            }
            // 创建用户级别其他
            createSysUserLevelOther(userId, newLevelId);
        } else {
        }
@@ -368,4 +415,54 @@
        // 保存
        sysUserLevelWageRepository.save(sysUserLevelWage);
    }
    // 创建用户级别保险
    private void createSysUserLevelInsurance(Long userId, Long levelId) {
        // 级别
        SysLevel sysLevel = sysLevelRepository.findById(levelId).orElseGet(SysLevel::new);
        // 用户级别工资
        SysUserLevelInsurance sysUserLevelInsurance = new SysUserLevelInsurance();
        sysUserLevelInsurance.setUserId(userId);
        sysUserLevelInsurance.setLevelId(levelId);
        sysUserLevelInsurance.setStartDate(new Timestamp(new Date().getTime()));
        sysUserLevelInsurance.setAccumulationRadix(sysLevel.getAccumulationRadix());
        sysUserLevelInsurance.setAccumulationPersonalProportion(sysLevel.getAccumulationPersonalProportion());
        sysUserLevelInsurance.setAccumulationCompanyProportion(sysLevel.getAccumulationCompanyProportion());
        sysUserLevelInsurance.setPensionRadix(sysLevel.getPensionRadix());
        sysUserLevelInsurance.setPensionPersonalProportion(sysLevel.getPensionPersonalProportion());
        sysUserLevelInsurance.setPensionCompanyProportion(sysLevel.getPensionCompanyProportion());
        sysUserLevelInsurance.setMedicalRadix(sysLevel.getMedicalRadix());
        sysUserLevelInsurance.setMedicalPersonalProportion(sysLevel.getMedicalPersonalProportion());
        sysUserLevelInsurance.setMedicalCompanyProportion(sysLevel.getMedicalCompanyProportion());
        sysUserLevelInsurance.setUnemploymentRadix(sysLevel.getUnemploymentRadix());
        sysUserLevelInsurance.setUnemploymentPersonalProportion(sysLevel.getUnemploymentPersonalProportion());
        sysUserLevelInsurance.setUnemploymentCompanyProportion(sysLevel.getUnemploymentCompanyProportion());
        sysUserLevelInsurance.setMaternityRadix(sysLevel.getMaternityRadix());
        sysUserLevelInsurance.setMaternityPersonalProportion(sysLevel.getMaternityPersonalProportion());
        sysUserLevelInsurance.setMaternityCompanyProportion(sysLevel.getMaternityCompanyProportion());
        sysUserLevelInsurance.setIndustrialInjuryRadix(sysLevel.getIndustrialInjuryRadix());
        sysUserLevelInsurance.setIndustrialInjuryPersonalProportion(sysLevel.getIndustrialInjuryPersonalProportion());
        sysUserLevelInsurance.setIndustrialInjuryCompanyProportion(sysLevel.getIndustrialInjuryCompanyProportion());
        // 保存
        sysUserLevelInsuranceRepository.save(sysUserLevelInsurance);
    }
    /**
     * 创建用户级别其他
     * @param userId
     * @param levelId
     */
    private void createSysUserLevelOther(Long userId, Long levelId) {
        // 级别
        SysLevel sysLevel = sysLevelRepository.findById(levelId).orElseGet(SysLevel::new);
        // 用户级别其他
        SysUserLevelOther sysUserLevelOther = new SysUserLevelOther();
        sysUserLevelOther.setUserId(userId);
        sysUserLevelOther.setLevelId(levelId);
        sysUserLevelOther.setStartDate(new Timestamp(new Date().getTime()));
        sysUserLevelOther.setPersonalPerformanceProportion(sysLevel.getPersonalPerformanceProportion());
        sysUserLevelOther.setAnnualLeaveDays(sysLevel.getAnnualLeaveDays());
        // 保存
        sysUserLevelOtherRepository.save(sysUserLevelOther);
    }
}