huaz
2023-01-09 5fd2ac11a530b5b7ddfcb2a40cf5d68fdde193be
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java
@@ -20,12 +20,13 @@
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.User;
import me.zhengjie.modules.system.domain.*;
import me.zhengjie.exception.EntityExistException;
import me.zhengjie.exception.EntityNotFoundException;
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;
import me.zhengjie.modules.system.service.mapstruct.UserLoginMapper;
import me.zhengjie.modules.system.service.mapstruct.UserMapper;
import me.zhengjie.utils.*;
@@ -40,6 +41,7 @@
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;
@@ -59,6 +61,10 @@
    private final UserCacheManager userCacheManager;
    private final OnlineUserService onlineUserService;
    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) {
@@ -94,6 +100,8 @@
            throw new EntityExistException(User.class, "phone", resources.getPhone());
        }
        userRepository.save(resources);
        // 判断用户级别变更
        checkUserLevel(resources.getId(), null, resources.getLevelId());
    }
    @Override
@@ -127,6 +135,8 @@
        if(!resources.getEnabled()){
            onlineUserService.kickOutForUsername(resources.getUsername());
        }
        // 临时旧用户级别ID
        Long tempOldLevelId = user.getLevelId();;
        user.setUsername(resources.getUsername());
        user.setEmail(resources.getEmail());
        user.setEnabled(resources.getEnabled());
@@ -136,7 +146,16 @@
        user.setPhone(resources.getPhone());
        user.setNickName(resources.getNickName());
        user.setGender(resources.getGender());
        user.setBirthday(resources.getBirthday());
        user.setAddress(resources.getAddress());
        user.setDingding(resources.getDingding());
        user.setSysRostering(resources.getSysRostering());
        user.setLevelId(resources.getLevelId());
        // 临时新用户级别ID
        Long tempNewLevelId = user.getLevelId();
        userRepository.save(user);
        // 判断用户级别变更
        checkUserLevel(user.getId(), tempOldLevelId, tempNewLevelId);
        // 清除缓存
        delCaches(user.getId(), user.getUsername());
    }
@@ -229,29 +248,22 @@
        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 {
        List<Map<String, Object>> list = new ArrayList<>();
        for (UserDto userDTO : queryAll) {
            List<String> roles = userDTO.getRoles().stream().map(RoleSmallDto::getName).collect(Collectors.toList());
            String levelName = userDTO.getLevelId() != null ? sysLevelRepository.findById(userDTO.getLevelId()).orElseGet(SysLevel::new).getName() : "";
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("用户名", userDTO.getUsername());
            map.put("角色", roles);
            map.put("部门", userDTO.getDept().getName());
            map.put("岗位", userDTO.getJobs().stream().map(JobSmallDto::getName).collect(Collectors.toList()));
            map.put("邮箱", userDTO.getEmail());
            map.put("钉钉ID", userDTO.getDingding());
            map.put("排班ID", userDTO.getRosteringId());
            map.put("钉钉号", userDTO.getDingding());
            map.put("排班", userDTO.getSysRostering().getName());
            map.put("级别", levelName);
            map.put("状态", userDTO.getEnabled() ? "启用" : "禁用");
            map.put("手机号码", userDTO.getPhone());
            map.put("修改密码的时间", userDTO.getPwdResetTime());
@@ -279,4 +291,178 @@
    private void flushCache(String username) {
        userCacheManager.cleanUserCache(username);
    }
    /**
     * 判断用户级别变更
     * @param userId
     * @param oldLevelId
     * @param newLevelId
     */
    private void checkUserLevel(Long userId, Long oldLevelId, Long newLevelId) {
        // 判断新旧用户级别ID是否相等
        if (oldLevelId == null) {
            // 创建用户级别工资
            createSysUserLevelWage(userId, newLevelId);
            // 创建用户级别保险
            createSysUserLevelInsurance(userId, newLevelId);
            // 创建用户级别其他
            createSysUserLevelOther(userId, newLevelId);
        } else if (oldLevelId != newLevelId) {
            // 旧用户级别工资
            SysUserLevelWage oldSysUserLevelWage = sysUserLevelWageRepository.findLastByUserId(userId);
            // 判断旧用户级别工资是否为空
            if (oldSysUserLevelWage != null && oldSysUserLevelWage.getId() != null) {
                // 旧日期字符串
                String oldDateString = oldSysUserLevelWage.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);
                // 旧用户级别工资
                oldSysUserLevelWage.setEndDate(new Timestamp(nowCalendar.getTime().getTime()));
                // 保存
                sysUserLevelWageRepository.save(oldSysUserLevelWage);
            }
            // 创建用户级别工资
            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 {
        }
    }
    /**
     * 创建用户级别工资
     * @param userId
     * @param levelId
     */
    private void createSysUserLevelWage(Long userId, Long levelId) {
        // 级别
        SysLevel sysLevel = sysLevelRepository.findById(levelId).orElseGet(SysLevel::new);
        // 用户级别工资
        SysUserLevelWage sysUserLevelWage = new SysUserLevelWage();
        sysUserLevelWage.setUserId(userId);
        sysUserLevelWage.setLevelId(levelId);
        sysUserLevelWage.setStartDate(new Timestamp(new Date().getTime()));
        sysUserLevelWage.setLevelWage(sysLevel.getLevelWage());
        sysUserLevelWage.setBasicWageProportion(sysLevel.getBasicWageProportion());
        sysUserLevelWage.setAchievementsWageProportion(sysLevel.getAchievementsWageProportion());
        sysUserLevelWage.setPerformanceWageProportion(sysLevel.getPerformanceWageProportion());
        sysUserLevelWage.setTeamWageProportion(sysLevel.getTeamWageProportion());
        // 保存
        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);
    }
}