赵士淞
2022-12-22 989b55ce011c7f296e9a5b9a048bf4c539083553
eladmin-master/eladmin-system/src/main/java/me/zhengjie/modules/system/service/impl/UserServiceImpl.java
@@ -20,12 +20,17 @@
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.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.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 +45,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 +65,8 @@
    private final UserCacheManager userCacheManager;
    private final OnlineUserService onlineUserService;
    private final UserLoginMapper userLoginMapper;
    private final SysLevelRepository sysLevelRepository;
    private final SysUserLevelWageRepository sysUserLevelWageRepository;
    @Override
    public Object queryAll(UserQueryCriteria criteria, Pageable pageable) {
@@ -94,6 +102,8 @@
            throw new EntityExistException(User.class, "phone", resources.getPhone());
        }
        userRepository.save(resources);
        // 判断用户级别变更
        checkUserLevel(resources.getId(), null, resources.getLevelId());
    }
    @Override
@@ -127,6 +137,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());
@@ -140,7 +152,12 @@
        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());
    }
@@ -248,14 +265,16 @@
        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.getSysRostering().getId());
            map.put("钉钉号", userDTO.getDingding());
            map.put("排班", userDTO.getSysRostering().getName());
            map.put("级别", levelName);
            map.put("状态", userDTO.getEnabled() ? "启用" : "禁用");
            map.put("手机号码", userDTO.getPhone());
            map.put("修改密码的时间", userDTO.getPwdResetTime());
@@ -283,4 +302,54 @@
    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);
        } else if (oldLevelId != newLevelId) {
            // 旧用户级别工资
            SysUserLevelWage oldSysUserLevelWage = sysUserLevelWageRepository.findLastByUserId(userId);
            // 判断旧用户级别工资是否为空
            if (oldSysUserLevelWage != null && oldSysUserLevelWage.getId() != null) {
                // 旧用户级别工资
                oldSysUserLevelWage.setEndDate(new Timestamp(new Date().getTime()));
                // 保存
                sysUserLevelWageRepository.save(oldSysUserLevelWage);
            }
            // 创建用户级别工资
            createSysUserLevelWage(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);
    }
}