package cn.common.service.impl;


import cn.common.common.constants.AuthConstants;
import cn.common.common.constants.BaseConstant;
import cn.common.common.constants.PlatformConstant;
import cn.common.common.entity.RouterMeta;
import cn.common.common.entity.VueRouter;
import cn.common.common.enums.ErrorCode;
import cn.common.common.enums.auth.AuthUserLockStatus;
import cn.common.common.exception.BusinessException;
import cn.common.common.redis.RedisRepository;
import cn.common.common.utils.BaseQueryRepository;
import cn.common.common.utils.alibaba.AliMessageSendComponent;
import cn.common.common.utils.auth.AuthEncrypt;
import cn.common.common.utils.auth.EncryptUtils;
import cn.common.common.utils.collection.CollectionUtils;
import cn.common.common.utils.common.BaseUtil;
import cn.common.common.utils.common.CheckParam;
import cn.common.common.utils.common.DateTimeUtil;
import cn.common.common.utils.common.TreeUtil;
import cn.common.common.utils.spring.SnowflakeIdWorker;
import cn.common.interaction.helper.PageBuilder;
import cn.common.interaction.internal.auth.ActiveUser;
import cn.common.interaction.internal.auth.AsyncAuthMeta;
import cn.common.interaction.internal.auth.AsyncAuthUserMeta;
import cn.common.interaction.internal.auth.AsyncJwtToken;
import cn.common.interaction.internal.auth.AuthUserMeta;
import cn.common.interaction.req.auth.AuthLoginReq;
import cn.common.interaction.req.auth.AuthUserAddReq;
import cn.common.interaction.req.auth.AuthUserPageReq;
import cn.common.interaction.req.auth.AuthUserUpdateReq;
import cn.common.interaction.req.auth.UserSketchReq;
import cn.common.interaction.req.biz.AuthUserConfigUpdateReq;
import cn.common.interaction.req.biz.UserProfileUpdateReq;
import cn.common.interaction.resp.auth.AuthUserConfigResp;
import cn.common.interaction.resp.auth.AuthorizedUserResp;
import cn.common.interaction.resp.auth.AuthorizedUserWrapResp;
import cn.common.interaction.resp.page.Pagination;
import cn.common.interaction.resp.role.AuthRoleResp;
import cn.common.interaction.resp.statistics.StatisticsVisitCountResp;
import cn.common.interaction.resp.statistics.VisitCountResp;
import cn.common.interaction.resp.biz.AuthUserPageQueryResp;
import cn.common.interaction.resp.biz.AuthUserSketchResp;
import cn.common.repository.entity.AuthDepartment;
import cn.common.repository.entity.AuthRole;
import cn.common.repository.entity.AuthUser;
import cn.common.repository.entity.PlatformUserConfig;
import cn.common.repository.entity.AuthUserRole;
import cn.common.repository.entity.AuthUserSketch;
import cn.common.repository.entity.PlatformLoginLog;
import cn.common.repository.repository.AuthDepartmentRepository;
import cn.common.repository.repository.AuthMenuRepository;
import cn.common.repository.repository.AuthPermissionRepository;
import cn.common.repository.repository.AuthRoleRepository;
import cn.common.repository.repository.PlatformUserConfigRepository;
import cn.common.repository.repository.AuthUserRepository;
import cn.common.repository.repository.AuthUserRoleRepository;
import cn.common.repository.repository.AuthUserSketchRepository;
import cn.common.repository.repository.PlatformLoginLogRepository;
import cn.common.repository.result.AuthMenuQueryResult;
import cn.common.repository.result.AuthPermissionQueryResult;
import cn.common.repository.result.AuthRoleResult;
import cn.common.repository.result.AuthUserResult;
import cn.common.service.AsyncHandlerService;
import cn.common.service.AuthCacheService;
import cn.common.service.AuthUserService;
import cn.common.service.config.shiro.JwtToken;
import cn.common.service.config.shiro.JwtUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Singer create by singer email:singer-coder@qq.com
 * @projectName health-platform-api
 * @Description:
 * @date 2019-08-13
 */
@Service(value = "authUserService")
@Slf4j
public class AuthUserServiceImpl implements AuthUserService {

    @Resource
    private AuthUserRepository authUserRepository;

    @Resource
    private AuthPermissionRepository authPermissionRepository;

    @Resource
    private AuthRoleRepository authRoleRepository;

    @Resource
    private AuthUserRoleRepository authUserRoleRepository;

    @Resource
    private BaseConstant baseConstant;

    @Resource
    private AuthCacheService authCacheService;

    @Resource
    private PlatformLoginLogRepository platformLoginLogRepository;

    @Resource
    private RedisRepository redisRepository;

    @Resource
    private PlatformUserConfigRepository platformUserConfigRepository;

    @Resource
    private AuthMenuRepository authMenuRepository;

    @Resource
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Resource
    private AliMessageSendComponent aliMessageSendComponent;

    @Resource
    private PlatformConstant platformConstant;

    @Resource
    private AuthDepartmentRepository authDepartmentRepository;

    @Resource
    private MapperFacade mapperFacade;

    @Resource
    private AuthUserSketchRepository authUserSketchRepository;

    @Resource
    private HttpServletRequest request;

    @Resource
    private AsyncHandlerService asyncHandlerService;


    /**
     * 返回当前用户的业务主键ID
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2021/5/22
     * @return java.lang.String
     */
    @Override
    public String currentAuthUserId(){
        AuthUserMeta authUserMeta = currentUserMeta(true);

        if(CheckParam.isNull(authUserMeta)){
            throw new BusinessException(ErrorCode.NO_AUTH_META_ERROR.getCode(),
                    ErrorCode.NO_AUTH_META_ERROR.getMessage());
        }

        return authUserMeta.getAuthUserId();
    }

    /**
      * 返回当前用户的角色代码
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/5/22
      * @return java.lang.String
      */
    @Override
    public String currentUserRoleCode(){
        AuthUserMeta authUserMeta = currentUserMeta(true);

        if(CheckParam.isNull(authUserMeta)){
            throw new BusinessException(ErrorCode.NO_AUTH_META_ERROR.getCode(),
                    ErrorCode.NO_AUTH_META_ERROR.getMessage());
        }

        return authUserMeta.getRoleCode();
    }


    /**
      * 当前用户的缓存信息
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-29
      * @param needThrow 为空是否需要抛出
      * @return AuthUserCache
      */
    @Override
    public AuthUserMeta currentUserMeta(Boolean needThrow){
        Object principal = SecurityUtils.getSubject().getPrincipal();
        String token;
        //此处拿不到认证信息的原因有可能是因为接口是匿名访问的
        if(!CheckParam.isNull(principal)){
            token = AuthEncrypt.encryptToken(String.valueOf(principal));
        }else{
            token = request.getHeader(AuthConstants.TOKEN);
        }
        String authUserMeta = redisRepository.get(AuthConstants.AUTHENTICATED_USER_META_TOKEN + token);
        if(CheckParam.isNull(authUserMeta)) {
            if(needThrow){
                throw new BusinessException(ErrorCode.NO_AUTH_META_ERROR.getCode(),
                        ErrorCode.NO_AUTH_META_ERROR.getMessage());
            }else{
                return null;
            }
        }
        //进行反解析
        return JSON.parseObject(authUserMeta, AuthUserMeta.class);
    }


    /**
      *  统计系统访问总量
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-22
      * @param  userName 用户名
      * @return StatisticsVisitCountResp
      */
    @Override
    public StatisticsVisitCountResp statisticsVisitCount(String userName){
        StatisticsVisitCountResp resp = new StatisticsVisitCountResp();
        resp.setTotalVisitCount(platformLoginLogRepository.queryTotalVisitCount(baseConstant.getUnDeleteStatus()));
        resp.setTodayVisitCount(platformLoginLogRepository.queryTodayVisitCount(baseConstant.getUnDeleteStatus()));
        resp.setTodayIp(platformLoginLogRepository.queryTodayIp(baseConstant.getUnDeleteStatus()));
        //计算出七天前的时间
        String sevenDaysAgo = DateTimeUtil
                .localDateTime(DateTimeUtil.DEFAULT_DATETIME_PATTERN, LocalDateTime.now().minusDays(7L));
        //查询出整个系统七天的访问量
        List<PlatformLoginLog> sevenDaysAgoLoginLogList = platformLoginLogRepository.selectList(new LambdaQueryWrapper<PlatformLoginLog>()
                .ge(PlatformLoginLog::getLoginTime, sevenDaysAgo));
        if(!sevenDaysAgoLoginLogList.isEmpty()){
            //整个系统七天的访问量
            List<VisitCountResp> allLastSevenVisitCount = new ArrayList<>();
            //此处需要按照时间进行分组求和
            Map<Date, Long> lastSevenVisitCountGroupedHashMap = sevenDaysAgoLoginLogList.stream()
                    .collect(Collectors.groupingBy(PlatformLoginLog::getLoginTime, Collectors.counting()));
            lastSevenVisitCountGroupedHashMap.forEach((k,v) -> {
                VisitCountResp visitCountResp = new VisitCountResp();
                //key的Date类型变成LocalDateTime
                LocalDateTime keyLocalDate = k.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                visitCountResp.setCount(v);
                visitCountResp.setDays(DateTimeUtil.localDateTime("MM-dd", keyLocalDate));
                allLastSevenVisitCount.add(visitCountResp);
            });
            resp.setLastSevenVisitCount(allLastSevenVisitCount);
            //需要统计出访问记录等于当前用户的用户登陆记录
            List<PlatformLoginLog> currentUserVisitLogList = sevenDaysAgoLoginLogList.stream().filter(n1 -> n1.getUserName().equalsIgnoreCase(userName)).collect(Collectors.toList());
            //当前用户最近七天的访问记录
            List<VisitCountResp> currentUserAllLastSevenVisitCount = new ArrayList<>();
            if(!CheckParam.isNull(currentUserVisitLogList) && !currentUserVisitLogList.isEmpty()){
                //此处需要按照时间进行分组求和
                Map<Date, Long> currentUserLastSevenVisitCountGroupedHashMap = currentUserVisitLogList.stream()
                        .collect(Collectors.groupingBy(PlatformLoginLog::getLoginTime, Collectors.counting()));
                currentUserLastSevenVisitCountGroupedHashMap.forEach((k,v) -> {
                    VisitCountResp currentUserVisitCountResp = new VisitCountResp();
                    //key的Date类型变成LocalDateTime
                    LocalDateTime keyLocalDate = k.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
                    currentUserVisitCountResp.setCount(v);
                    currentUserVisitCountResp.setDays(DateTimeUtil.localDateTime("MM-dd", keyLocalDate));
                    currentUserAllLastSevenVisitCount.add(currentUserVisitCountResp);
                });
                resp.setLastSevenUserVisitCount(currentUserAllLastSevenVisitCount);
            }
        }
        return resp;
    }

    /**
      * 根据用户名查询用户能访问的菜单
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-22
      * @param userName 用户名称
      * @return java.util.List
      */
    @Override
    public List<VueRouter<AuthMenuQueryResult>> queryVueRouterByUserName(String userName){
        List<AuthMenuQueryResult> authMenuQueryResultList = authMenuRepository.
                queryAuthMenuByUserName(baseConstant.getUnDeleteStatus(), userName);
        if(CheckParam.isNull(authMenuQueryResultList) || authMenuQueryResultList.isEmpty()) {
            return new ArrayList<>();
        }
        List<VueRouter<AuthMenuQueryResult>> routes = new ArrayList<>();
        authMenuQueryResultList.forEach(menu -> {
            VueRouter<AuthMenuQueryResult> route = new VueRouter<>();
            route.setId(menu.getAuthMenuId());
            route.setParentId(menu.getParentId());
            route.setIcon(menu.getMenuIcon());
            route.setPath(menu.getPath());
            route.setComponent(menu.getComponent());
            route.setName(menu.getMenuName());
            route.setMeta(new RouterMeta(true, null));
            routes.add(route);
        });
        return TreeUtil.buildVueRouter(routes);
    }


    /**
      *
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-21
      * @param authUserPageReq 用户信息分页查询Req
      * @return
      */
    @Override
    public Pagination<AuthUserPageQueryResp> queryUserByPage(AuthUserPageReq authUserPageReq){
        log.info(">>>>>>>>>>>>>分页查询用户信息<<<<<<<<<<<<<<<<<<");
        //构建查询条件
        LambdaQueryWrapper<AuthUser> queryWrapper = new LambdaQueryWrapper<>();
        if(!CheckParam.isNull(authUserPageReq.getDepartmentId())){
            queryWrapper.eq(AuthUser::getDepartmentId,authUserPageReq.getDepartmentId());
        }
        if(!CheckParam.isNull(authUserPageReq.getUserName())){
            queryWrapper.like(AuthUser::getUserName,authUserPageReq.getUserName());
        }
        if(!CheckParam.isNull((authUserPageReq.getBeginTime()))){
            queryWrapper.ge(AuthUser::getCreateTime,authUserPageReq.getBeginTime());
        }
        if(!CheckParam.isNull((authUserPageReq.getEndTime()))){
            queryWrapper.le(AuthUser::getCreateTime,authUserPageReq.getEndTime());
        }
        queryWrapper.orderByDesc(AuthUser::getCreateTime);
        //开始分页
        Page<Object> page = PageHelper.startPage(authUserPageReq.getCurrentPage(), authUserPageReq.getItemsPerPage());
        List<AuthUser> authUserList = authUserRepository.selectList(queryWrapper);
        if(CollectionUtils.isEmpty(authUserList)) {
            return PageBuilder.buildPageResult(page,new ArrayList<>());
        }
        List<String> authUserIdList = authUserList.stream().filter(u1 -> !CheckParam.isNull(u1.getAuthUserId())).map(u1 -> u1.getAuthUserId()).collect(Collectors.toList());
        List<String> departmentIdList = authUserList.stream().filter(u1 -> !CheckParam.isNull(u1.getDepartmentId())).map(u1 -> u1.getDepartmentId()).collect(Collectors.toList());
        List<AuthUserSketch> authUserSketchList = authUserSketchRepository.selectList(new LambdaQueryWrapper<AuthUserSketch>()
                .in(AuthUserSketch::getAuthUserId, authUserIdList));
        //形成对应简略信息map
        HashMap<String, AuthUserSketch> authUserSketchHashMap = authUserSketchList.stream().collect(Collectors.toMap(AuthUserSketch::getAuthUserId, a -> a, (k1, k2) -> k1, HashMap::new));
        //查询用户的角色信息
        List<AuthRoleResult> authRoleResultList = authRoleRepository.queryUserRoleByUserIdList(baseConstant.getUnDeleteStatus(), authUserIdList);
        //形成对应map
        HashMap<String, AuthRoleResult> authUserRoleHashMap = authRoleResultList.stream().collect(Collectors.toMap(AuthRoleResult::getAuthUserId, a -> a, (k1, k2) -> k1, HashMap::new));
        //查询部门信息
        List<AuthDepartment> departmentList = authDepartmentRepository.selectList(
                new LambdaQueryWrapper<AuthDepartment>().in(AuthDepartment::getDepartmentId, departmentIdList)
        );
        HashMap<String, AuthDepartment> authDepartmentHashMap = departmentList.stream().collect(Collectors.toMap(AuthDepartment::getDepartmentId, a -> a, (k1, k2) -> k1, HashMap::new));
        List<AuthUserPageQueryResp> authUserRespList = new ArrayList<>();
        authUserList.stream().forEach(item -> {
            AuthUserPageQueryResp resp = new AuthUserPageQueryResp();

            resp.setId(item.getId());
            resp.setAuthCacheKey(item.getAuthUserId());
            resp.setAuthUserId(item.getAuthUserId());
            resp.setDepartmentId(item.getDepartmentId());
            resp.setAvatar(item.getAvatar());
            resp.setCreateTime(item.getCreateTime());
            resp.setDescription(item.getDescription());
            resp.setLastLoginTime(item.getLastLoginTime());
            resp.setLockStatus(item.getLockStatus());
            resp.setPhoneNumber(item.getPhoneNumber());
            resp.setPassword(item.getAuthPassword());
            resp.setUpdateTime(item.getUpdateTime());
            resp.setUserName(item.getUserName());
            resp.setRealName(item.getRealName());
            resp.setPassword("it's a secret");
            AuthUserSketch authUserSketch = authUserSketchHashMap.get(item.getAuthUserId());
            if(!CheckParam.isNull(authUserSketch)){
                resp.setSketchOtherType(authUserSketch.getSketchOtherType());
                resp.setSketchOtherId(authUserSketch.getSketchOtherId());
            }
            AuthRoleResult authRoleResult = authUserRoleHashMap.get(item.getAuthUserId());
            if(!CheckParam.isNull(authRoleResult)){
                resp.setAuthRoleId(authRoleResult.getAuthRoleId());
                resp.setRoleName(authRoleResult.getRoleName());
            }
            AuthDepartment authDepartment = authDepartmentHashMap.get(item.getDepartmentId());
            if(!CheckParam.isNull(authDepartment)){
                resp.setDepartmentName(authDepartment.getDepartmentName());
                resp.setDepartmentId(authDepartment.getDepartmentId());
            }
            authUserRespList.add(resp);
        });
        return PageBuilder.buildPageResult(page,authUserRespList);
    }

    /**
      * 根据用户名称判断用户是否存在
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-21
      * @param userName 用户名称
      * @return Boolean
      */
    @Override
    public Boolean judgeUserExistByUserName(String userName){
        AuthUser authUser = authUserRepository.selectOne(new LambdaQueryWrapper<AuthUser>().eq(AuthUser::getUserName, userName));
        return CheckParam.isNull(authUser);
    }

    /**
      * 根据用户名称查询用户信息
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-21
      * @param userName 用户名称
      * @return AuthorizedUserResp
      */
    @Override
    public AuthorizedUserResp queryAuthorizedUserByUserName(String userName){
        AuthUserResult authUserResult = queryAuthUserByName(userName);
        if(CheckParam.isNull(authUserResult)) {
            return new AuthorizedUserResp();
        }
        AuthorizedUserResp resp = new AuthorizedUserResp();
        setAuthorizedUserRespData(resp,authUserResult);
        return resp;
    }



    /**
      * 用户登陆逻辑
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-21
      * @param loginReq 登录请求参数
      * @return cn.common.interaction.resp.auth.AuthorizedUserWrapResp
      */
    @Override
     public AuthorizedUserWrapResp authUserLogin(AuthLoginReq loginReq){
        Long beginTime = System.currentTimeMillis();

        log.info(">>>>>>>>>>>>>管理系统用户登陆<<<<<<<<<<<<<<<<<<");
        String password = loginReq.getPassword();
        String userName = loginReq.getUserName();
        //查询当前登陆用户
        AuthUserResult authUserResult = queryAuthUserByName(userName);
        if (CheckParam.isNull(authUserResult)) {
            throw new BusinessException(ErrorCode.AUTH__ERROR.getCode(),"用户不存在");
        }
        boolean passwordVerify = EncryptUtils.verifyPassword(authUserResult.getAuthPassword(), password, authUserResult.getAuthSalt());
        if (!passwordVerify) {
            throw new BusinessException(ErrorCode.AUTH__ERROR.getCode(),"密码错误");
        }
        //用户锁定状态 1:锁定,2:有效
        if (PlatformConstant.STATUS_LOCK.compareTo(authUserResult.getLockStatus()) == 0) {
            throw new BusinessException(ErrorCode.AUTH__ERROR.getCode(),"账号已被锁定,请联系管理员");
        }
        String authUserId = authUserResult.getAuthUserId();
        //控制单点登录
        String currentActiveUser = redisRepository.get(AuthConstants.AUTHENTICATED_ACTIVE_USER+authUserId);
        if(!CheckParam.isNull(currentActiveUser)){
            //throw new BusinessException(ErrorCode.BUSINESS_REFUSE.getCode(),"当前用户已经登录,请先退出登录");
            authUserLogOut(authUserId);
        }
        //登陆的token处理
        String token = AuthEncrypt.encryptToken(JwtUtil.sign(userName, password));
        LocalDateTime expireTime = LocalDateTime.now().plusMinutes(baseConstant.getAuthExpiredTime());
        String expireTimeStr = DateTimeUtil.convertTimeToString(DateTimeUtil.DEFAULT_DATETIME_PATTERN, expireTime);
        JwtToken jwtToken = new JwtToken(token, expireTimeStr);

        String ipAddress = BaseUtil.getIpAddress(request);
        AsyncAuthMeta authMeta = new AsyncAuthMeta();
        authMeta.setLoginReq(loginReq);
        authMeta.setAuthUserResult(mapperFacade.map(authUserResult, AsyncAuthUserMeta.class));
        authMeta.setJwtToken(mapperFacade.map(jwtToken, AsyncJwtToken.class));
        authMeta.setIpAddress(ipAddress);
        //异步处理
        asyncHandlerService.handleLoginAsync(authMeta);

        //生成返回数据
        AuthorizedUserWrapResp resp = generateUserMeta(jwtToken, authUserResult);
        Long endTime = System.currentTimeMillis();
        log.info(">>>>>>>>>>>>>>登录成功管理系统用户登录返回:{}<<<<<<<<<<<<<",JSON.toJSONString(resp));
        log.info(">>>>>>>>>>>>>>登录成功管理系统用户登录耗时:{}<<<<<<<<<<<<<",(endTime-beginTime));
        return resp;
     }


    /**
     * 生成前端需要的用户信息，包括：
     * 1. token
     * 2. Vue Router
     * 3. 用户角色
     * 4. 用户权限
     * 5. 前端系统个性化配置信息
     * @param token token
     * @param authUserResult  用户信息
     * @return AuthorizedUserWrapResp
     */
    private AuthorizedUserWrapResp generateUserMeta(JwtToken token, AuthUserResult authUserResult) {

            AuthorizedUserWrapResp resp = new AuthorizedUserWrapResp();

            String userName = authUserResult.getUserName();
            resp.setToken(token.getToken());
            resp.setExpireTime(DateTimeUtil.convertStringToLongTime(token.getExipreAt()));

            List<AuthRoleResult> authRoleResults = authRoleRepository.queryUserRoleDataByUserName(baseConstant.getUnDeleteStatus(), userName);
            //设置角色名称信息
            resp.setRoles(authRoleResults.stream().map(u1 -> u1.getRoleName()).collect(Collectors.toList()));

            List<AuthPermissionQueryResult> permissionList = BaseQueryRepository.queryByFunctional(
                    () -> authCacheService.getPermissions(userName),
                    () -> authUserRepository.queryAuthUserPermissionByUserName(baseConstant.getUnDeleteStatus(), userName));

            resp.setPermissions(permissionList.stream().filter(n1 -> !CheckParam.isNull(n1.getPerms())).map(AuthPermissionQueryResult::getPerms).collect(Collectors.toList()));
            //查询用户的配置并且设置到换回值里面
            PlatformUserConfig
                    platformUserConfig = platformUserConfigRepository.selectOne(new LambdaQueryWrapper<PlatformUserConfig>()
                    .eq(PlatformUserConfig::getAuthUserId, authUserResult.getAuthUserId()));
            if(!CheckParam.isNull(platformUserConfig)){
                AuthUserConfigResp config = new AuthUserConfigResp();

                config.setColor(platformUserConfig.getColor());
                config.setFixHeader(platformUserConfig.getFixHeader());
                config.setFixSiderBar(platformUserConfig.getFixSiderBar());
                config.setLayout(platformUserConfig.getLayout());
                config.setMultiPage(platformUserConfig.getMultiPage());
                config.setSystemUserConfigId(platformUserConfig.getPlatformUserConfigId());
                config.setTheme(platformUserConfig.getTheme());
                config.setAuthUserId(platformUserConfig.getAuthUserId());

                resp.setConfig(config);
            }
            AuthorizedUserResp user = new AuthorizedUserResp();
            setAuthorizedUserRespData(user,authUserResult);
            resp.setUser(user);
            return resp;
    }

     /**
      * 设置用户详细信息
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-21
      * @param resp 用户信息
      * @param authUserResult 用户详细信息
      */
     private void setAuthorizedUserRespData(AuthorizedUserResp resp,AuthUserResult authUserResult){
            resp.setAuthCacheKey(authUserResult.getAuthUserId());
            resp.setAuthUserId(authUserResult.getAuthUserId());
            resp.setAvatar(authUserResult.getAvatar());
            resp.setCreateTime(authUserResult.getCreateTime());
            resp.setDepartmentId(authUserResult.getDepartmentId());
            resp.setDepartmentName(authUserResult.getDepartmentName());
            resp.setDescription(authUserResult.getDescription());
            resp.setEmail(authUserResult.getEmail());
            resp.setId(authUserResult.getAuthUserId());
            resp.setLastLoginTime(authUserResult.getLastLoginTime());
            resp.setLockStatus(authUserResult.getLockStatus());
            resp.setPhoneNumber(authUserResult.getPhoneNumber());
            resp.setPassword(authUserResult.getAuthPassword());
            resp.setAuthRoleId(authUserResult.getAuthRoleId());
            resp.setRoleName(authUserResult.getRoleName());
            resp.setRoleCode(authUserResult.getRoleCode());
            resp.setSex(authUserResult.getSex());
            resp.setUpdateTime(authUserResult.getUpdateTime());
            resp.setUserName(authUserResult.getUserName());
            resp.setRealName(authUserResult.getRealName());
            resp.setPassword("it's a secret");
     }

    /**
     * 当前用户登出
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2019-08-21
     * @param authUserId 当前用户的ID
     */
    @Override
    public void authUserLogOut(String authUserId){
        handleLogOut(authUserId);
        //执行登出方法
        SecurityUtils.getSubject().logout();
    }


    /**
      * @description: 处理用户退出登陆
      * @auther: create by singer - Singer email:singer-coder@qq.com
      * @date 2022/5/9
      * @param authUserId 用户ID
      * @return
      */
    public void handleLogOut(String authUserId){
        log.info(">>>>>>>>>>>>删除登录信息参数authUserId:{}<<<<<<<<<<<",authUserId);
        String currentActiveUser = redisRepository.get(AuthConstants.AUTHENTICATED_ACTIVE_USER+authUserId);
        if(CheckParam.isNull(currentActiveUser)) {
            return;
        }
        ActiveUser activeUser = JSON.parseObject(currentActiveUser, ActiveUser.class);
        String userName = activeUser.getUserName();
        String token = activeUser.getToken();
        //删除TOKEN
        redisRepository.delete(AuthConstants.AUTHENTICATED_TOKEN + token);
        //缓存当前的用户信息 缓存的是JSON序列化之后的authUserResult
        redisRepository.delete(AuthConstants.AUTHENTICATED_USER_META_TOKEN + token);
        //在线人数 递减少
        redisRepository.decr(AuthConstants.AUTHENTICATED_ACTIVE_USERS_COUNT);
        //删除用户在线缓存
        redisRepository.delete(AuthConstants.AUTHENTICATED_ACTIVE_USER+authUserId);
        //删除用户配置
        redisRepository.delete(AuthConstants.AUTHENTICATED_USER_CONFIG+authUserId);
        //删除当前用户的权限
        redisRepository.delete(AuthConstants.AUTHENTICATED_USER_PERMISSION+userName);
        //删除当前用户的角色
        redisRepository.delete(AuthConstants.AUTHENTICATED_USER_ROLE+userName);
        //需要删除当前在线用户的集合
        String activeUserList = redisRepository.get(AuthConstants.AUTHENTICATED_ACTIVE_USERS);
        List<ActiveUser> activeList = JSON.parseArray(activeUserList, ActiveUser.class);
        activeList = activeList.stream().filter(item -> !StrUtil.equalsIgnoreCase(item.getAuthUserId(),authUserId)).collect(
                Collectors.toList());
        //此处不设置过期时间
        redisRepository.set(AuthConstants.AUTHENTICATED_ACTIVE_USERS,JSON.toJSONString(activeList));
    }

    /**
     * 查询当前所有在线的用户
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2019-08-23
     * @return List
     */
    @Override
    public List<ActiveUser> queryAllActiveUser(){
        String activeUserList = redisRepository.get(AuthConstants.AUTHENTICATED_ACTIVE_USERS);
        if(!CheckParam.isNull(activeUserList)){
            List<ActiveUser> activeList = JSON.parseArray(activeUserList, ActiveUser.class);
            activeList.forEach(item -> {
                try{
                    item.setToken(null);
                }catch (Exception e){
                    log.error(">>>>>>>>>>>>>>>>>>>>>查询在线用户处理数据出现异常:{},{}<<<<<<<<<<<<<<<<<<<",e.getMessage(),e);
                }
            });
            return activeList;
        }
        return new ArrayList<>();
    }

    /**
      * 根据当前用户名查询当前活跃的用户
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-23
      * @param userName 当前用户名
      * @return List
      */
    @Override
    public List<ActiveUser> queryActiveUserByUserName(String userName){
        HashSet<String> activeUserHashSet = redisRepository.keys(AuthConstants.AUTHENTICATED_ACTIVE_USERS);
        if(!CheckParam.isNull(activeUserHashSet) && !activeUserHashSet.isEmpty()){
            List<ActiveUser> activeUsers = new ArrayList<>();
            activeUserHashSet.forEach(u1 -> {
                try{
                    ActiveUser activeUser = JSON.parseObject(u1, ActiveUser.class);
                    activeUser.setToken(null);
                    if(!CheckParam.isNull(userName)){
                        if(activeUser.getUserName().equalsIgnoreCase(userName)){
                            activeUsers.add(activeUser);
                        }
                    }else{
                        activeUsers.add(activeUser);
                    }
                }catch (Exception e){
                    log.error(">>>>>>>>>>>>>>>>>>>>>查询在线用户处理数据出现异常:{},{}<<<<<<<<<<<<<<<<<<<",e.getMessage(),e);
                }
            });
            return activeUsers;
        }
        return new ArrayList<>();
    }

    /**
      * 根据用户名查询用户信息
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2019-08-21
      * @param userName 用户名
      * @return AuthUserResult
      */
    @Override
    public AuthUserResult queryAuthUserByName(String userName){
        //使用函数式查询方式查询数据，先查询缓存如果查询不到再查询数据库
        AuthUserResult authUserResult = BaseQueryRepository.queryByFunctional(
                () -> authCacheService.queryUserCacheByKey(userName,AuthUserResult.class),
                () -> authUserRepository.queryAuthUserSketchByUserName(baseConstant.getUnDeleteStatus(),
                        userName));
        return authUserResult;
    }

    /**
      * 查询所有用户的简略信息
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/4/6
      * @return List
      */
    @Override
    public List<AuthUserSketchResp> queryAllAuthUserSketch(){
        List<AuthUser> authUserList = authUserRepository.selectList(new LambdaQueryWrapper<>());
        if(CollectionUtils.isEmpty(authUserList)) {
            return Lists.newArrayList();
        }
        List<AuthUserSketchResp> respList = mapperFacade.mapAsList(authUserList, AuthUserSketchResp.class);
        return respList;
    }

    /**
      * 更新用户资料
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/4/18
      * @param updateReq 更新用户资料请求参数
      */
    @Override
    public void updateAuthUserProfile(UserProfileUpdateReq updateReq){
        log.info(">>>>>>>>>>>>>>>>>>>>>>用户资料修改请求参数:{}<<<<<<<<<<<<<<<<<<<<",updateReq.toString());
        //拿到当前用户
        AuthUserMeta authUserMeta = this.currentUserMeta(true);
        String authUserId = authUserMeta.getAuthUserId();
        AuthUser authUser = authUserRepository.selectOne(new LambdaQueryWrapper<AuthUser>()
                .eq(AuthUser::getAuthUserId, authUserId));
        if(CheckParam.isNull(authUser)){
            return;
        }
        setUpdateAuthUserProfile(updateReq,authUser);
        authUserRepository.updateById(authUser);
    }


    /**
      * 设置系统用户需要被更新的数据
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/4/17
      * @param userProfileUpdateReq 需要更新的数据
      * @param authUser 用户数据
      */
    public void setUpdateAuthUserProfile(UserProfileUpdateReq userProfileUpdateReq,AuthUser authUser){
        if(!CheckParam.isNull(userProfileUpdateReq.getDescription())){
            authUser.setDescription(userProfileUpdateReq.getDescription());
        }
        if(!CheckParam.isNull(userProfileUpdateReq.getPhoneNumber())){
            authUser.setPhoneNumber(userProfileUpdateReq.getPhoneNumber());
        }
        if(!CheckParam.isNull(userProfileUpdateReq.getAvatar())){
            authUser.setAvatar(userProfileUpdateReq.getAvatar());
        }
    }

    /**
      * 系统定制修改
      * @author: create by Singer email:singer-coder@qq.com
      * @date 2019/8/29
      * @param userConfig 系统定制修改参数
      */
    @Override
    public void updateUserConfig(AuthUserConfigUpdateReq userConfig) {
        log.info(">>>>>>>>>>>>>>>>>>>>>>系统定制修改参数:{}<<<<<<<<<<<<<<<<<<<<",userConfig.toString());
        //拿到当前用户
        AuthUserMeta authUserMeta = this.currentUserMeta(true);
        //查询
        PlatformUserConfig
                platformUserConfig = platformUserConfigRepository.selectOne(new LambdaQueryWrapper<PlatformUserConfig>()
                .eq(PlatformUserConfig::getAuthUserId, authUserMeta.getAuthUserId()));
        //设值
        if (!CheckParam.isNull(platformUserConfig)){
            if (StringUtils.isNotBlank(userConfig.getColor())){
                platformUserConfig.setColor(userConfig.getColor());
            }
            if (StringUtils.isNotBlank(userConfig.getLayout())){
                platformUserConfig.setLayout(userConfig.getLayout());
            }
            if (StringUtils.isNotBlank(userConfig.getMultiPage())){
                platformUserConfig.setMultiPage(userConfig.getMultiPage());
            }
            if (StringUtils.isNotBlank(userConfig.getTheme())){
                platformUserConfig.setTheme(userConfig.getTheme());
            }
            if (!CheckParam.isNull(userConfig.getFixHeader())){
                platformUserConfig.setFixHeader(userConfig.getFixHeader());
            }
            if (!CheckParam.isNull(userConfig.getFixSiderBar())){
                platformUserConfig.setFixSiderBar(userConfig.getFixSiderBar());
            }
            platformUserConfigRepository.updateById(platformUserConfig);
        }
    }

    /**
     * 更新密码
     * @author: create by Singer email:singer-coder@qq.com
     * @date 2019/8/29
     * @param userName 用户名
     * @param password 密码
     */
    @Override
    public void updatePassword(String userName, String password) {
        //根据userName查询数据
        AuthUser authUser = authUserRepository.selectOne(new LambdaQueryWrapper<AuthUser>()
                .eq(AuthUser::getUserName, userName));
        if (!CheckParam.isNull(authUser)){
            String authSalt = SnowflakeIdWorker.uniqueMainId();
            //生产新密码
            String encrypt = EncryptUtils.encrypt(password,authSalt);
            authUser.setAuthPassword(encrypt);
            authUser.setAuthSalt(authSalt);
            authUserRepository.updateById(authUser);
        }else {
            throw new BusinessException(ErrorCode.ERROR_UPDATE_PASSWORD.getCode(),
                    ErrorCode.ERROR_UPDATE_PASSWORD.getMessage());
        }
    }

    /**
      * 检验旧密码,如果输入的旧密码和原本设置的密码不同则不通过
      * @author: create by Singer email:singer-coder@qq.com
      * @date 2019/8/30
      * @param  userName 用户名
      * @param  password 密码
      */
    @Override
    public void checkOldPassword(String userName, String password) {
        //拿到当前用户
        AuthUserMeta authUserMeta = this.currentUserMeta(true);
        String authUserId = authUserMeta.getAuthUserId();
        String authPassword = authUserRepository.queryPasswordByUserId(authUserId, baseConstant.getUnDeleteStatus());
        //对旧密码进行加密
        String authSalt = authUserMeta.getAuthSalt();
        String encrypt = EncryptUtils.encrypt(password, authSalt);
        if (!StrUtil.equals(encrypt,authPassword,false)){
            throw new BusinessException(ErrorCode.OLD_PASSWORD_VERIFY_PASSWORD.getCode(),
                    ErrorCode.OLD_PASSWORD_VERIFY_PASSWORD.getMessage());
        }
    }

    /**
     * 检验用户名
     * @author: create by Singer email:singer-coder@qq.com
     * @date 2019/8/30
     * @param  userName 用户名
     */
    @Override
    public void checkUserName(String userName) {
        //查询出对应的用户
        AuthUser authUser = authUserRepository.selectOne(new LambdaQueryWrapper<AuthUser>()
                .eq(AuthUser::getUserName, userName));
        if (null != authUser){
            throw new BusinessException(ErrorCode.AUTH_USER_EXIST_CODE_ERROR.getCode(),
                    ErrorCode.AUTH_USER_EXIST_CODE_ERROR.getMessage());
        }
    }

    /**
      * 查询所有角色
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/4/6
      * @return List
      */
    @Override
    public List<AuthRoleResp> queryAllAuthRole(){
        List<AuthRole> authRoleList = authRoleRepository.selectList(new LambdaQueryWrapper<AuthRole>());
        if(CollectionUtils.isEmpty(authRoleList)) {
            return Lists.newArrayList();
        }
        List<AuthRoleResp> respList = mapperFacade.mapAsList(authRoleList, AuthRoleResp.class);
        return respList;
    }

    /**
      * 修改用户信息
      * @author: create by Singer email:singer-coder@qq.com
      * @date 2019/8/31
      * @param updateReq 修改用户信息请求参数
      */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateItem(AuthUserUpdateReq updateReq) {
        //查询出对应的用户
        AuthUser authUser = authUserRepository.selectOne(new LambdaQueryWrapper<AuthUser>()
                .eq(AuthUser::getAuthUserId, updateReq.getAuthUserId()));
        if(CheckParam.isNull(authUser)){
            return;
        }
        String authUserId = authUser.getAuthUserId();
        //设置更新内容
        if (StringUtils.isNotBlank(updateReq.getPhoneNumber()) &&
                !authUser.getPhoneNumber().equalsIgnoreCase(updateReq.getPhoneNumber())){
            authUser.setPhoneNumber(updateReq.getPhoneNumber());
        }
        if (!CheckParam.isNull(updateReq.getLockStatus())){
            authUser.setLockStatus(updateReq.getLockStatus());
        }
        if (!CheckParam.isNull(updateReq.getDepartmentId())){
            authUser.setDepartmentId(updateReq.getDepartmentId());
        }
        if (!CheckParam.isNull(updateReq.getRealName())){
            authUser.setRealName(updateReq.getRealName());
        }
        if (!CheckParam.isNull(updateReq.getUserName())){
            authUser.setUserName(updateReq.getUserName());
        }
        //设置密码的更新
        String password = updateReq.getPassword();
        if(!CheckParam.isNull(password)){
            //用户密码盐
            String authUserSalt = SnowflakeIdWorker.uniqueMainId();
            password = baseConstant.getDefaultPassword();
            String encryptedPassword = EncryptUtils.encrypt(password, authUserSalt);
            authUser.setAuthPassword(encryptedPassword);
            authUser.setAuthSalt(authUserSalt);
        }
        authUserRepository.updateById(authUser);
        //处理用户简略信息类型
        List<UserSketchReq> sketchList = updateReq.getSketchList();
        if(!CollectionUtils.isEmpty(sketchList)){
            insertUserSketchList(authUserId,sketchList);
        }
        //特殊处理角色
        if (StringUtils.isNotBlank(updateReq.getAuthRoleId())){
            //查询原有角色
            AuthUserRole authUserRole = authUserRoleRepository.selectOne(new LambdaQueryWrapper<AuthUserRole>()
                    .eq(AuthUserRole::getAuthUserId, updateReq.getAuthUserId()));
            //判断是否修改
            if (!authUserRole.getAuthRoleId().equalsIgnoreCase(updateReq.getAuthRoleId())){
                authUserRole.setAuthRoleId(updateReq.getAuthRoleId());
                //更新
                authUserRoleRepository.updateById(authUserRole);
            }
        }
    }

    /**
      * 用户注册
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/4/6
      * @param addReq 用户注册
      */
    @Override
    public void register(AuthUserAddReq addReq){
        addItem(addReq);
    }

    /**
     * 新增用户
     * @author: create by Singer email:singer-coder@qq.com
     * @date 2019/8/31
     * @param addReq
     * @param addReq 是否需要发送短信
     * @return AuthUser
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public AuthUser addItem(AuthUserAddReq addReq) {
        log.info(">>>>>>>>>>>>>>>>>>>>>>>新增用户,参数:{}",JSON.toJSONString(addReq));
        //用户已经存在了就不开户
        LambdaQueryWrapper<AuthUser> queryWrapper = new LambdaQueryWrapper<>();
        if(!CheckParam.isNull(addReq.getUserName())){
            queryWrapper.or(item -> item.eq(AuthUser::getUserName,addReq.getUserName()));
        }
        if(!CheckParam.isNull(addReq.getAuthUserId())){
            queryWrapper.or(item -> item.eq(AuthUser::getAuthUserId,addReq.getAuthUserId()));
        }
        AuthUser existedAuthUser = authUserRepository.selectOne(queryWrapper);
        if (!CheckParam.isNull(existedAuthUser)) {
            if(!CheckParam.isNull(addReq.getUserName())){
                existedAuthUser.setUserName(addReq.getUserName());
            }
            if(!CheckParam.isNull(addReq.getRealName())){
                existedAuthUser.setRealName(addReq.getRealName());
            }
            authUserRepository.updateById(existedAuthUser);
            return existedAuthUser;
        }
        AuthUser authUser = new AuthUser();
        String authUserId = authUser.getAuthUserId();
        addReq.setAuthUserId(authUserId);
        Integer lockStatus = addReq.getLockStatus();
        if(CheckParam.isNull(lockStatus)){
            authUser.setLockStatus(AuthUserLockStatus.LOCKED.getCode());
        }else{
            authUser.setLockStatus(lockStatus);
        }
        //用户密码盐
        String authUserSalt = SnowflakeIdWorker.uniqueMainId();
        String password = addReq.getPassword();
        if(CheckParam.isNull(password)){
            password = baseConstant.getDefaultPassword();
        }
        String encryptedPassword = EncryptUtils.encrypt(password, authUserSalt);
        authUser.setAuthPassword(encryptedPassword);
        authUser.setAuthSalt(authUserSalt);
        authUser.setUserName(addReq.getUserName());
        authUser.setDepartmentId(addReq.getDepartmentId());
        authUser.setRealName(addReq.getRealName());
        authUser.setPhoneNumber(addReq.getPhoneNumber());
        try {
            BaseUtil.setFieldValueNotNull(authUser);
        } catch (Exception e) {
            log.error("新增用户->设置用户为空的属性失败 {} , {} ",e.getMessage(),e);
        }
        authUserRepository.insert(authUser);
        //角色特殊处理
        AuthUserRole authUserRole = new AuthUserRole();
        authUserRole.setAuthRoleId(addReq.getAuthRoleId());
        authUserRole.setAuthUserId(authUser.getAuthUserId());
        authUserRoleRepository.insert(authUserRole);
        //初始化用户默认配置
        PlatformUserConfig platformUserConfig = new PlatformUserConfig();
        platformUserConfig.setAuthUserId(authUser.getAuthUserId());
        platformUserConfig.setColor(PlatformConstant.DEFAULT_USER_CONFIG_COLOR);
        platformUserConfig.setFixHeader(PlatformConstant.DEFAULT_USER_FIX_HEADER);
        platformUserConfig.setFixSiderBar(PlatformConstant.DEFAULT_USER_FIX_SIDE_BAR);
        platformUserConfig.setLayout(PlatformConstant.DEFAULT_USER_LAYOUT);
        platformUserConfig.setMultiPage(PlatformConstant.DEFAULT_USER_MULTI_PAGE);
        platformUserConfig.setTheme(PlatformConstant.DEFAULT_USER_THEME);
        platformUserConfigRepository.insert(platformUserConfig);
        List<UserSketchReq> sketchList = addReq.getSketchList();
        insertUserSketchList(authUserId,sketchList);
        return authUser;
    }


    /**
      * 插入用户的简略信息
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/5/25
      * @param authUserId 用户ID
      * @param sketchList 简略信息集合
      */
    @Override
    public void insertUserSketchList(String authUserId, List<UserSketchReq> sketchList){
        if(CollectionUtils.isEmpty(sketchList)){
            return;
        }
        sketchList.stream().forEach(item -> {
            insertUserSketch(authUserId,item);
        });
    }

    /**
      * 设置用户的简略信息
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2021/5/8
      * @param sketchReq
      */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void insertUserSketch(String authUserId,UserSketchReq sketchReq){
        log.info(">>>>>>>>>>>>>>>>>>>>>执行插入用户简略信息,当前线程ID:{}<<<<<<<<<<<<<<<<<<<",Thread.currentThread().getId());
        String sketchOtherId = sketchReq.getSketchOtherId();
        String sketchOtherType = sketchReq.getSketchOtherType().toString();
        AuthUserSketch authUserSketch = authUserSketchRepository.selectOne(
                new LambdaQueryWrapper<AuthUserSketch>()
                    .eq(AuthUserSketch::getAuthUserId, authUserId)
                    .eq(AuthUserSketch::getSketchOtherType, sketchOtherType)
        );
        if(!CheckParam.isNull(authUserSketch)){
            authUserSketchRepository.deleteById(authUserSketch);
        }
        //地区ID和用户ID都不为空的时候生成用户简略信息 生成的是地区信息
        if(!CheckParam.isNull(sketchOtherId)){
            //生成用户简略信息
            authUserSketch = new AuthUserSketch();
            authUserSketch.setAuthUserId(authUserId);
            authUserSketch.setSketchOtherId(sketchOtherId);
            authUserSketch.setSketchOtherType(sketchOtherType);
            try {
                BaseUtil.setFieldValueNotNull(authUserSketch);
            } catch (Exception e) {
                log.error("新增->设置设置用户的简略信息为空的属性失败 {} , {} ",e.getMessage(),e);
            }
            authUserSketchRepository.insert(authUserSketch);
        }
    }

    /**
     * 新增用户
     * @author: create by Singer email:singer-coder@qq.com
     * @date 2019/8/31
     * @param addReq
     * @return AuthUser
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public AuthUser addNewAuthUser(AuthUserAddReq addReq) {
        log.info(">>>>>>>>>>>>>>>>>>>>>>>新增用户,参数:{}",JSON.toJSONString(addReq));
        AuthUser authUser = addItem(addReq);
        log.info(">>>>>>>>>>>>>>>>>>>>>>>新增用户,结果:{}",JSON.toJSONString(authUser));
        return authUser;
    }

    /**
      * 删除用户
      * @author: create by Singer email:singer-coder@qq.com
      * @date 2019/9/2
      * @param mainIdList id集合
      */
    @Override
    public void batchDeleteItem(String mainIdList) {
        List<String> authUserIdList = Arrays.asList(mainIdList.split(","));
        if(!CollectionUtils.isEmpty(authUserIdList)){
            log.info(">>>>>>>>>>>>>>>>正在删除用户:{}<<<<<<<<<<<<<<<<<<<<",JSON.toJSONString(authUserIdList));
            authUserRepository.deleteByAuthUserIdList(authUserIdList);
            authUserRoleRepository.deleteByAuthUserIdList(authUserIdList);
            authUserSketchRepository.deleteByAuthUserIdList(authUserIdList);
            authUserIdList.forEach(userId->{
                authUserLogOut(userId); //执行登出
            });
        }
    }

    /**
     * 重置密码
     * @author: create by Singer email:singer-coder@qq.com
     * @date 2019/9/2
     * @param userNameList 用户名集合
     */
    @Override
    public void resetPassword(String userNameList) {
        String[] split = userNameList.split(",");
        Arrays.stream(split).forEach(userName->{
           log.info(">>>>>>>>>>>>>>>>正在重置密码:{}<<<<<<<<<<<<<<<<<<<<<",userName);
            String authSalt = SnowflakeIdWorker.uniqueMainId();
            authUserRepository.resetPassword(userName,EncryptUtils.encrypt(baseConstant.getDefaultPassword(),authSalt),authSalt);
        });
    }
}
