/** 
 | 
 * Copyright (c) 2018 人人开源 All rights reserved. 
 | 
 * <p> 
 | 
 * https://www.renren.io 
 | 
 * <p> 
 | 
 * 版权所有,侵权必究! 
 | 
 */ 
 | 
  
 | 
package com.zt.modules.sys.service; 
 | 
  
 | 
import cn.hutool.core.collection.CollectionUtil; 
 | 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; 
 | 
import com.fasterxml.jackson.core.PrettyPrinter; 
 | 
import com.zt.common.constant.CacheKey; 
 | 
import com.zt.common.constant.Constant; 
 | 
import com.zt.common.db.query.QueryFilter; 
 | 
import com.zt.common.exception.RenException; 
 | 
import com.zt.common.service.BaseService; 
 | 
import com.zt.common.servlet.Result; 
 | 
import com.zt.common.utils.CacheUtils; 
 | 
import com.zt.common.utils.TreeUtils; 
 | 
import com.zt.core.context.User; 
 | 
import com.zt.core.context.UserContext; 
 | 
import com.zt.core.security.BCryptPasswordEncoder; 
 | 
import com.zt.core.security.Md5Utils; 
 | 
import com.zt.core.sys.model.SysDept; 
 | 
import com.zt.core.sys.model.SysUser; 
 | 
import com.zt.core.sys.service.ISysUserService; 
 | 
import com.zt.modules.sys.dao.SysUserDao; 
 | 
import com.zt.modules.sys.dto.RoleDto; 
 | 
import com.zt.modules.sys.dto.UserTreeDto; 
 | 
import com.zt.modules.sys.enums.UserStatus; 
 | 
import org.springframework.beans.factory.annotation.Autowired; 
 | 
import org.springframework.beans.factory.annotation.Value; 
 | 
import org.springframework.cache.annotation.CacheEvict; 
 | 
import org.springframework.cache.annotation.Cacheable; 
 | 
import org.springframework.stereotype.Service; 
 | 
import org.springframework.transaction.annotation.Transactional; 
 | 
  
 | 
import java.util.*; 
 | 
import java.util.stream.Collectors; 
 | 
  
 | 
/** 
 | 
 * 系统用户 
 | 
 * 
 | 
 * @author hehz 
 | 
 */ 
 | 
@Service 
 | 
public class SysUserService extends BaseService<SysUserDao, SysUser> implements ISysUserService { 
 | 
  
 | 
    @Autowired 
 | 
    private SysRoleUserService sysRoleUserService; 
 | 
    @Autowired 
 | 
    private SysJobUserService sysJobUserService; 
 | 
    @Autowired 
 | 
    private SysPostUserService sysPostUserService; 
 | 
    @Autowired 
 | 
    private SysDeptService sysDeptService; 
 | 
    @Autowired 
 | 
    private SysParamsService paramsService; 
 | 
    @Autowired 
 | 
    SysMapService sysMapService; 
 | 
  
 | 
  
 | 
    public List<SysUser> page(QueryFilter queryFilter) { 
 | 
        // 普通管理员,只能查询所属部门及子部门的数据 
 | 
        User user = UserContext.getUser(); 
 | 
        if (!user.isSuperAdmin()) { 
 | 
            queryFilter.addParam("deptIds", sysDeptService.getWithDescendantIds(user.getDeptId())); 
 | 
        } 
 | 
        // 查询 
 | 
        return queryFilter.getPageList(baseDao.getList(queryFilter.getParams())); 
 | 
    } 
 | 
  
 | 
    public List<SysUser> list(QueryFilter queryFilter) { 
 | 
        // 普通管理员,只能查询所属部门及子部门的数据 
 | 
        User user = UserContext.getUser(); 
 | 
        if (!user.isSuperAdmin()) { 
 | 
            queryFilter.addParam("deptIds", sysDeptService.getWithDescendantIds(user.getDeptId())); 
 | 
        } 
 | 
        return baseDao.getList(queryFilter.getParams()); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 用户树 
 | 
     * 
 | 
     * @return 
 | 
     */ 
 | 
    public List<UserTreeDto> getUserDeptTree() { 
 | 
        List<SysDept> deptList = TreeUtils.toList(sysDeptService.getDeptTree()); 
 | 
        List<UserTreeDto> list = deptList.stream().map(d -> { 
 | 
            UserTreeDto dto = new UserTreeDto(); 
 | 
            dto.setId(d.getId()); 
 | 
            dto.setPid(d.getPid()); 
 | 
            dto.setName(d.getName()); 
 | 
            dto.setType(d.isCompany() ? 1 : 2); 
 | 
            return dto; 
 | 
        }).collect(Collectors.toList()); 
 | 
  
 | 
        List<SysUser> users = this.getByDeptIds(deptList.stream().map(d -> d.getId()).collect(Collectors.toList())); 
 | 
        users.stream().forEach(u -> { 
 | 
            UserTreeDto dto = new UserTreeDto(); 
 | 
            dto.setId(u.getId()); 
 | 
            dto.setPid(u.getDeptId()); 
 | 
            dto.setName(u.getRealName()); 
 | 
            dto.setType(3); 
 | 
            list.add(dto); 
 | 
        }); 
 | 
        return TreeUtils.build(list); 
 | 
    } 
 | 
  
 | 
    public SysUser getByUsername(String username) { 
 | 
        return baseDao.getByUsername(username); 
 | 
    } 
 | 
  
 | 
    public List<SysUser> checkUserArea(Long userId,String localServer) { 
 | 
        return baseDao.checkUserArea(userId,localServer); 
 | 
    } 
 | 
  
 | 
    @Override 
 | 
    @Cacheable(value = Constant.Cache.USER, key = "'id:' + #id") 
 | 
    public SysUser get(Long id) { 
 | 
        SysUser entity = baseDao.getEntity(id); 
 | 
        return entity; 
 | 
        //return super.get(id); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据部门ID查询 
 | 
     */ 
 | 
    @Override 
 | 
    public List<SysUser> getByDeptId(Long deptId) { 
 | 
        return baseDao.selectList(new QueryWrapper<SysUser>().lambda().eq(SysUser::getDeptId, deptId)); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据部门ID查询 
 | 
     */ 
 | 
    @Override 
 | 
    public List<SysUser> getByDeptIds(List<Long> deptIds) { 
 | 
        if (CollectionUtil.isNotEmpty(deptIds)) { 
 | 
            return baseDao.getByDeptIds(deptIds); 
 | 
            //return baseDao.selectList(new QueryWrapper<SysUser>().lambda().in(SysUser::getDeptId, deptIds)); 
 | 
        } else { 
 | 
            return new ArrayList<>(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    public void insert(SysUser entity) { 
 | 
        if (this.getByUsername(entity.getUsername()) != null) { 
 | 
            throw new RenException("用户名已存在!"); 
 | 
        } 
 | 
        //entity.setStatus(UserStatus.ENABLE.getValue()); 
 | 
        // 密码加密 
 | 
        String password = Md5Utils.hash(paramsService.getValue(Constant.ParamKey.INIT_PASSWORD_KEY)); 
 | 
        entity.setPassword(password); 
 | 
        entity.setIsLocked(0); 
 | 
  
 | 
        entity.setCompanyId(sysDeptService.getCompanyIdByDeptId(entity.getDeptId())); 
 | 
        // 保存用户 
 | 
        super.insert(entity); 
 | 
  
 | 
        sysPostUserService.saveOrUpdate(entity.getId(), entity.getPostIdList()); 
 | 
        sysJobUserService.saveOrUpdate(entity.getId(), entity.getJobIdList()); 
 | 
        sysMapService.saveOrUpdate("userTeamggroup", entity.getId(), entity.getTeamgroupIds()); 
 | 
        sysMapService.saveOrUpdate("userShip", entity.getId(), entity.getShipIds()); 
 | 
    } 
 | 
  
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    public void update(SysUser entity) { 
 | 
        // 更新用户 
 | 
        entity.setCompanyId(sysDeptService.getCompanyIdByDeptId(entity.getDeptId())); 
 | 
        super.update(entity); 
 | 
  
 | 
        // 保存岗位用户关系 
 | 
        sysPostUserService.saveOrUpdate(entity.getId(), entity.getPostIdList()); 
 | 
  
 | 
        // 保存职位用户关系 
 | 
        sysJobUserService.saveOrUpdate(entity.getId(), entity.getJobIdList()); 
 | 
        sysMapService.saveOrUpdate("userTeamggroup", entity.getId(), entity.getTeamgroupIds()); 
 | 
        sysMapService.saveOrUpdate("userShip", entity.getId(), entity.getShipIds()); 
 | 
        // 处理缓存 
 | 
        CacheUtils.remove(Constant.Cache.USER, CacheKey.USER_ID.getKey() + entity.getId()); 
 | 
    } 
 | 
  
 | 
    @CacheEvict(value = Constant.Cache.USER, allEntries = true) 
 | 
    public void delete(Long[] ids) { 
 | 
        // 删除用户 
 | 
        super.deleteLogic(ids); 
 | 
  
 | 
        // 删除角色用户关系 
 | 
        sysRoleUserService.deleteByUserIds(ids); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 修改密码 
 | 
     * 
 | 
     * @param id          用户ID 
 | 
     * @param newPassword 新密码 
 | 
     */ 
 | 
    @Transactional(rollbackFor = Exception.class) 
 | 
    public void updatePassword(Long id, String newPassword) { 
 | 
//        newPassword = Md5Utils.hash(newPassword); 
 | 
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); 
 | 
        newPassword = encoder.encode(newPassword); 
 | 
  
 | 
        SysUser entity = get(id); 
 | 
        if (entity != null) { 
 | 
            entity.setPassword(newPassword); 
 | 
            baseDao.updatePassword(entity); 
 | 
  
 | 
            // 处理缓存 
 | 
            CacheUtils.remove(Constant.Cache.USER, CacheKey.USER_ID.getKey() + id); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 重置用户密码 
 | 
     * 
 | 
     * @param id 
 | 
     */ 
 | 
    public void resetPassword(Long id) { 
 | 
        updatePassword(id, paramsService.getValue(Constant.ParamKey.INIT_PASSWORD_KEY)); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 登录验证修改当前用户的状态 
 | 
     * 
 | 
     * @param id 
 | 
     * @param loginErrorCount 
 | 
     * @param isLocked 
 | 
     * @param lastLoginErrorTime 
 | 
     */ 
 | 
    public void updateLogin(Long id, Integer loginErrorCount, Integer isLocked, Date lastLoginErrorTime) { 
 | 
        return; 
 | 
        //baseDao.updateLogin(id, loginErrorCount, isLocked, lastLoginErrorTime); 
 | 
    } 
 | 
  
 | 
    public List<SysUser> isGetByUsernameList(String username, Long unitid, String code, String password) { 
 | 
        List<SysUser> getByUsernameList = baseDao.isGetByUsernameList(username, unitid, code, password); 
 | 
        return getByUsernameList; 
 | 
    } 
 | 
  
 | 
    public List<SysUser> isDomainName(String code) { 
 | 
        return baseDao.isDomainName(code); 
 | 
    } 
 | 
  
 | 
    public Map adminRole() { 
 | 
        //User sysUser = new User(); 
 | 
        Map<String, Object> sysUser = new Hashtable(); 
 | 
  
 | 
        sysUser.put("isAdmin", false); 
 | 
        sysUser.put("isTyRole", false); 
 | 
        sysUser.put("isYwzRole", false); 
 | 
        sysUser.put("isTzRole", false); 
 | 
        sysUser.put("isAssistant", false); 
 | 
        sysUser.put("isCzRole", false); 
 | 
        sysUser.put("isBzRole", false); 
 | 
        sysUser.put("isCjRole", false); 
 | 
        sysUser.put("isZcRole", false); 
 | 
        List<RoleDto> roles = sysRoleUserService.getUserRoles(UserContext.getUser().getId()); 
 | 
        if (roles != null && roles.size() > 0) { 
 | 
            if (roles.stream().filter(p -> p.getCode().equals("xtglybm") || p.getCode().equals("all")).count() > 0) { 
 | 
                sysUser.put("isAdmin", true); 
 | 
            } 
 | 
            if (roles.stream().filter(p -> p.getCode().equals("tybm")).count() > 0) { 
 | 
                sysUser.put("isTyRole", true); 
 | 
            } 
 | 
            if (roles.stream().filter(p -> p.getCode().equals("ywzbm")).count() > 0) { 
 | 
                sysUser.put("isYwzRole", true); 
 | 
            } 
 | 
            if (roles.stream().filter(p -> p.getCode().equals("tzbm")).count() > 0) { 
 | 
                sysUser.put("isTzRole", true); 
 | 
            } 
 | 
            if (roles.stream().filter(p -> p.getCode().equals("zlbm")).count() > 0) { 
 | 
                sysUser.put("isAssistant", true); 
 | 
            } 
 | 
            if (roles.stream().filter(p -> p.getCode().equals("czbm")).count() > 0) { 
 | 
                sysUser.put("isCzRole", true); 
 | 
            } 
 | 
            if (roles.stream().filter(p -> p.getCode().equals("bzbm")).count() > 0) { 
 | 
                sysUser.put("isBzRole", true); 
 | 
            } 
 | 
  
 | 
            if (roles.stream().filter(p -> p.getCode().equals("cjbm") || p.getCode().equals("zcbm")).count() > 0) { 
 | 
                sysUser.put("isCjRole", true); 
 | 
            } 
 | 
            if (roles.stream().filter(p -> p.getCode().equals("zcbm")).count() > 0) { 
 | 
                sysUser.put("isZcRole", true); 
 | 
            } 
 | 
        } 
 | 
        return sysUser; 
 | 
    } 
 | 
  
 | 
    public User userRoleInfo(String username, String systemId) { 
 | 
        Long userId = null; 
 | 
        if (username == null) 
 | 
            userId = UserContext.getUser().getId(); 
 | 
        User user = baseDao.userRoleInfo(userId, username, systemId); 
 | 
        return user; 
 | 
    } 
 | 
  
 | 
    public String getRoles() { 
 | 
        Long userId = UserContext.getUser().getId(); 
 | 
        return baseDao.getRoles(userId); 
 | 
    } 
 | 
    public String getRoleNames(Long userId) { 
 | 
        return baseDao.getRoleNames(userId); 
 | 
    } 
 | 
  
 | 
    public List<SysUser> getNewConnectUser(String newShipTeam) { 
 | 
        return baseDao.getNewConnectUser(newShipTeam); 
 | 
    } 
 | 
  
 | 
  
 | 
    public Integer checkTestHome(String systemId) { 
 | 
        Long userId = UserContext.getUser().getId(); 
 | 
        Integer num =  baseDao.checkTestHome(systemId,userId); 
 | 
        return num; 
 | 
    } 
 | 
  
 | 
    public List<SysUser> getUsersList() { 
 | 
        return baseDao.getUsersList(); 
 | 
    } 
 | 
} 
 |