package org.xyzh.system.user.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.common.core.page.PageDomain;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.dto.user.TbSysUser;
import org.xyzh.common.dto.user.TbSysUserDeptRole;
import org.xyzh.common.dto.user.TbSysUserInfo;
import org.xyzh.common.utils.IDUtils;
import org.xyzh.common.vo.UserDeptRoleVO;
import org.xyzh.common.vo.UserVO;
import org.xyzh.system.mapper.UserMapper;
import org.xyzh.system.mapper.UserInfoMapper;
import org.xyzh.system.mapper.UserDeptRoleMapper;
import org.xyzh.system.user.service.SysUserService;
import org.xyzh.system.utils.LoginUtil;

import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * @description SysUserServiceImpl.java文件描述 系统用户服务实现类
 * @filename SysUserServiceImpl.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-09-28
 */
@Service
public class SysUserServiceImpl implements SysUserService {

    private static final Logger logger = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private UserDeptRoleMapper userDeptRoleMapper;

    @Transactional
    @Override
    public ResultDomain<TbSysUser> registerUser(TbSysUser user) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();

        try {
            logger.info("开始注册用户：{}", user.getUsername());
            // 检查用户是否已存在
            ResultDomain<Boolean> checkResult = checkUserExists(user);
            if (checkResult.isSuccess()) {
                resultDomain.fail("用户已存在");
                return resultDomain;
            }
            Date now = new Date();
            user.setID(IDUtils.generateID());
            user.setCreateTime(now);
            user.setDeleted(false);
            // 创建用户信息
            TbSysUserInfo userInfo = new TbSysUserInfo();
            userInfo.setUserID(user.getID());
            userInfo.setCreateTime(now);
            userInfo.setAvatar("default");
            
            TbSysUserDeptRole userDeptRole = new TbSysUserDeptRole();
            userDeptRole.setUserID(user.getID());
            userDeptRole.setDeptID("-1");
            userDeptRole.setRoleID("freedom");
            userDeptRole.setCreateTime(now);
            
            userMapper.insertUser(user);
            userInfoMapper.insertUserInfo(userInfo);
            userDeptRoleMapper.bindUser(Arrays.asList(userDeptRole));
            resultDomain.success("注册用户成功", user);
            return resultDomain;            
        } catch (Exception e) {
            logger.error("注册用户失败：{}", user.getUsername(), e);
            resultDomain.fail("注册用户失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<Boolean> checkUserExists(TbSysUser user) {
        ResultDomain<Boolean> resultDomain = new ResultDomain<>();

        try {
            logger.info("开始检查用户是否已存在：{}", user.getUsername());
            
        } catch (Exception e) {
            logger.error("检查用户是否已存在失败：{}", user.getUsername(), e);
            resultDomain.fail("检查用户是否已存在失败：" + e.getMessage());
            return resultDomain;
        }
        return resultDomain;
    }

    @Override
    public ResultDomain<TbSysUser> getAllUsers() {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();

        try {
            logger.info("开始查询所有用户");
            
            TbSysUser filter = new TbSysUser();
            List<TbSysUser> users = userMapper.selectByFilter(filter);
            
            logger.info("查询所有用户完成，共找到{}个用户", users.size());
            resultDomain.success("查询成功", users);
            return resultDomain;
            
        } catch (Exception e) {
            logger.error("查询所有用户失败", e);
            resultDomain.fail("查询用户失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUser> getUserById(String userId) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始根据ID查询用户：{}", userId);
            
            if (!StringUtils.hasText(userId)) {
                resultDomain.fail("用户ID不能为空");
                return resultDomain;
            }
            
            TbSysUser filter = new TbSysUser();
            filter.setID(userId);
            filter.setDeleted(false);
            
            List<TbSysUser> users = userMapper.selectByFilter(filter);
            
            if (users.isEmpty()) {
                logger.warn("未找到用户：{}", userId);
                resultDomain.fail("未找到指定用户");
                return resultDomain;
            }
            
            logger.info("根据ID查询用户完成：{}", userId);
            resultDomain.success("查询成功", users.get(0));
            return resultDomain;
            
        } catch (Exception e) {
            logger.error("根据ID查询用户失败：{}", userId, e);
            resultDomain.fail("查询用户失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUser> getUserByUsername(String username) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始根据用户名查询用户：{}", username);
            
            if (!StringUtils.hasText(username)) {
                resultDomain.fail("用户名不能为空");
                return resultDomain;
            }
            
            TbSysUser user = userMapper.selectByUsername(username);
            
            if (user == null) {
                logger.warn("未找到用户：{}", username);
                resultDomain.fail("未找到指定用户");
                return resultDomain;
            }
            
            logger.info("根据用户名查询用户完成：{}", username);
            resultDomain.success("查询成功", user);
            return resultDomain;
            
        } catch (Exception e) {
            logger.error("根据用户名查询用户失败：{}", username, e);
            resultDomain.fail("查询用户失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUser> getUserByFilter(TbSysUser filter) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始根据过滤条件查询用户：{}", filter);
            
            if (filter == null) {
                resultDomain.fail("过滤条件不能为空");
                return resultDomain;
            }
            
            // 检查至少有一个查询条件
            // boolean hasFilter = StringUtils.hasText(filter.getID()) ||
            //                   StringUtils.hasText(filter.getUsername()) ||
            //                   StringUtils.hasText(filter.getEmail()) ||
            //                   StringUtils.hasText(filter.getPhone());
            
            // if (!hasFilter) {
            //     resultDomain.fail("至少需要提供一个查询条件");
            //     return resultDomain;
            // }
            
            List<TbSysUser> users = userMapper.selectByFilter(filter);
            
            if (users.isEmpty()) {
                logger.warn("未找到符合条件的用户：{}", filter);
                resultDomain.fail("未找到指定用户");
                return resultDomain;
            }
            
            logger.info("根据过滤条件查询用户完成：{}", filter);
            resultDomain.success("查询成功", users);
            return resultDomain;
            
        } catch (Exception e) {
            logger.error("根据过滤条件查询用户失败：{}", filter, e);
            resultDomain.fail("查询用户失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUser> getUserPage(TbSysUser filter, PageParam pageParam) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        List<TbSysUser> users = userMapper.selectUserPage(filter, pageParam);
        PageDomain<TbSysUser> pageDomain = new PageDomain<>();
        pageDomain.setDataList(users);
        pageDomain.setPageParam(pageParam);
        resultDomain.success("查询成功", pageDomain);
        return resultDomain;
    }

    @Transactional
    @Override
    public ResultDomain<TbSysUser> createUser(TbSysUser user) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始创建用户：{}", user.getUsername());
            
            // 参数校验
            if (!StringUtils.hasText(user.getUsername())) {
                resultDomain.fail("用户名不能为空");
                return resultDomain;
            }
            
            // 检查用户名是否已存在
            ResultDomain<Boolean> checkResult = checkUsernameExists(user.getUsername(), null);
            if (!checkResult.isSuccess()) {
                resultDomain.fail(checkResult.getMessage());
                return resultDomain;
            }
            if (checkResult.getData()) {
                resultDomain.fail("用户名已存在");
                return resultDomain;
            }
            
            // 检查邮箱是否已存在
            if (StringUtils.hasText(user.getEmail())) {
                ResultDomain<Boolean> emailCheckResult = checkEmailExists(user.getEmail(), null);
                if (!emailCheckResult.isSuccess()) {
                    resultDomain.fail(emailCheckResult.getMessage());
                    return resultDomain;
                }
                if (emailCheckResult.getData()) {
                    resultDomain.fail("邮箱已存在");
                    return resultDomain;
                }
            }
            
            // 设置基础信息
            user.setID(IDUtils.generateID());
            user.setCreateTime(new Date());
            user.setDeleted(false);
            if (user.getStatus() == null) {
                user.setStatus(1); // 默认启用状态
            }
            
            // 插入数据库
            ResultDomain<TbSysUser> result = registerUser(user);
            
            if (result.isSuccess()) {
                logger.info("创建用户成功：{}", user.getUsername());
                resultDomain.success("创建用户成功", result.getData());
                return resultDomain;
            } else {
                logger.warn("创建用户失败：{}", user.getUsername());
                resultDomain.fail("创建用户失败：" + result.getMessage());
            }
            
            return resultDomain;
        } catch (Exception e) {
            logger.error("创建用户异常：{}", user.getUsername(), e);
            resultDomain.fail("创建用户失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUser> updateUser(TbSysUser user) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始更新用户：{}", user.getID());
            
            // 参数校验
            if (!StringUtils.hasText(user.getID())) {
                resultDomain.fail("用户ID不能为空");
                return resultDomain;
            }
            if (!StringUtils.hasText(user.getUsername())) {
                resultDomain.fail("用户名不能为空");
                return resultDomain;
            }
            
            // 检查用户是否存在
            ResultDomain<TbSysUser> existResult = getUserById(user.getID());
            if (!existResult.isSuccess()) {
                resultDomain.fail(existResult.getMessage());
                return resultDomain;
            }
            
            // 检查用户名是否已存在（排除自身）
            ResultDomain<Boolean> checkResult = checkUsernameExists(user.getUsername(), user.getID());
            if (!checkResult.isSuccess()) {
                resultDomain.fail(checkResult.getMessage());
                return resultDomain;
            }
            if (checkResult.getData()) {
                resultDomain.fail("用户名已存在");
                return resultDomain;
            }
            
            // 检查邮箱是否已存在（排除自身）
            if (StringUtils.hasText(user.getEmail())) {
                ResultDomain<Boolean> emailCheckResult = checkEmailExists(user.getEmail(), user.getID());
                if (!emailCheckResult.isSuccess()) {
                    resultDomain.fail(emailCheckResult.getMessage());
                    return resultDomain;
                }
                if (emailCheckResult.getData()) {
                    resultDomain.fail("邮箱已存在");
                    return resultDomain;
                }
            }
            
            // 设置更新时间
            user.setUpdateTime(new Date());
            
            // 更新数据库
            int result = userMapper.updateUser(user);
            
            if (result > 0) {
                logger.info("更新用户成功：{}", user.getID());
                resultDomain.success("更新用户成功", user);
                return resultDomain;
            } else {
                logger.warn("更新用户失败：{}", user.getID());
                resultDomain.fail("更新用户失败");
                return resultDomain;
            }
            
        } catch (Exception e) {
            logger.error("更新用户异常：{}", user.getID(), e);
            resultDomain.fail("更新用户失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUser> deleteUser(String userId) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始删除用户：{}", userId);
            
            if (!StringUtils.hasText(userId)) {
                resultDomain.fail("用户ID不能为空");
                return resultDomain;
            }
            
            // 检查用户是否存在
            ResultDomain<TbSysUser> existResult = getUserById(userId);
            if (!existResult.isSuccess()) {
                resultDomain.fail(existResult.getMessage());
                return resultDomain;
            }
            
            // 逻辑删除
            TbSysUser user = existResult.getData();
            user.setDeleted(true);
            user.setDeleteTime(new Date());
            
            int result = userMapper.updateUser(user);
            
            if (result > 0) {
                logger.info("删除用户成功：{}", userId);
                resultDomain.success("删除用户成功", user);
                return resultDomain;
            } else {
                logger.warn("删除用户失败：{}", userId);
                resultDomain.fail("删除用户失败");
                return resultDomain;
            }
            
        } catch (Exception e) {
            logger.error("删除用户异常：{}", userId, e);
            resultDomain.fail("删除用户失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUserDeptRole> getBindUserDeptRoleList(TbSysUserDeptRole filter) {
        ResultDomain<TbSysUserDeptRole> resultDomain = new ResultDomain<>();
        List<TbSysUserDeptRole> userDeptRoles = userDeptRoleMapper.selectByFilter(filter);
        resultDomain.success("查询成功", userDeptRoles);
        return resultDomain;
    }

    @Transactional
    @Override
    public ResultDomain<UserDeptRoleVO> bindUserDeptRole(UserDeptRoleVO userDeptRoleVO) {
        ResultDomain<UserDeptRoleVO> resultDomain = new ResultDomain<>();
        TbSysUser currentUser = LoginUtil.getCurrentUser();
        try {
            List<TbSysUserDeptRole> userDeptRoles = new ArrayList<>();
            logger.info("开始绑定用户部门角色：{}", userDeptRoleVO.getID());
            Date now = new Date();
            for (TbSysUser user : userDeptRoleVO.getUsers()) {
                for(TbSysUserDeptRole userDeptRole : userDeptRoleVO.getUserDeptRoles()) {
                    userDeptRole.setID(IDUtils.generateID());
                    userDeptRole.setUserID(user.getID());
                    userDeptRole.setCreateTime(now);
                    userDeptRole.setCreator(currentUser.getID());
                    userDeptRoles.add(userDeptRole);
                }
            }

            userDeptRoleMapper.bindUser(userDeptRoles);

        } catch (Exception e) {
            logger.error("绑定用户部门角色异常：{}", userDeptRoleVO.getID(), e);
            resultDomain.fail("绑定用户部门角色失败：" + e.getMessage());
            return resultDomain;
        }
        return resultDomain;
    }

    @Transactional
    @Override
    public ResultDomain<UserDeptRoleVO> unbindUserDeptRole(UserDeptRoleVO userDeptRoleVO) {
        ResultDomain<UserDeptRoleVO> resultDomain = new ResultDomain<>();
        TbSysUser currentUser = LoginUtil.getCurrentUser();
        try {
            List<TbSysUserDeptRole> userDeptRoles = new ArrayList<>();
            logger.info("开始解绑用户部门角色：{}", userDeptRoleVO.getID());
            Date now = new Date();
            for(TbSysUser user:userDeptRoleVO.getUsers()) {
                for(TbSysUserDeptRole userDeptRole : userDeptRoleVO.getUserDeptRoles()) {
                    userDeptRole.setUserID(user.getID());
                    userDeptRoles.add(userDeptRole);
                }
            }
            int result = userDeptRoleMapper.unbindUser(userDeptRoles);
            if(result > 0) {
                logger.info("解绑用户部门角色成功：{}", userDeptRoleVO.getID());
                resultDomain.success("解绑用户部门角色成功", userDeptRoleVO);
            } else {
                logger.warn("解绑用户部门角色失败：{}", userDeptRoleVO.getID());
                resultDomain.fail("解绑用户部门角色失败");
            }
            return resultDomain;
        } catch (Exception e) {
            logger.error("解绑用户部门角色异常：{}", userDeptRoleVO.getID(), e);
            resultDomain.fail("解绑用户部门角色失败：" + e.getMessage());
        }
        return resultDomain;
    }

    @Override
    public ResultDomain<Boolean> checkUsernameExists(String username, String excludeId) {
        ResultDomain<Boolean> resultDomain = new ResultDomain<>();
        try {
            logger.info("检查用户名是否存在：{}", username);
            
            if (!StringUtils.hasText(username)) {
                resultDomain.fail("用户名不能为空");
                return resultDomain;
            }

            TbSysUser filter = new TbSysUser();
            if (StringUtils.hasText(excludeId)) {
                filter.setID(excludeId);
            }
            filter.setUsername(username);
            filter.setDeleted(false);
            
            long count = userMapper.selectByFilter(filter).size();
            boolean exists = count > 0;
            
            logger.info("用户名存在性检查完成：{}，存在：{}", username, exists);
            resultDomain.success("检查完成", exists);
            return resultDomain;
            
        } catch (Exception e) {
            logger.error("检查用户名存在性失败：{}", username, e);
            resultDomain.fail("检查失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<Boolean> checkEmailExists(String email, String excludeId) {
        ResultDomain<Boolean> resultDomain = new ResultDomain<>();
        try {
            logger.info("检查邮箱是否存在：{}", email);
            
            if (!StringUtils.hasText(email)) {
                resultDomain.fail("邮箱不能为空");
                return resultDomain;
            }
            
            TbSysUser filter = new TbSysUser();
            filter.setEmail(email);
            filter.setDeleted(false);
            
            if (StringUtils.hasText(excludeId)) {
                filter.setID(excludeId);
            }
            
            long count = userMapper.selectByFilter(filter).size();
            boolean exists = count > 0;
            
            logger.info("邮箱存在性检查完成：{}，存在：{}", email, exists);
            resultDomain.success("检查完成", exists);
            return resultDomain;
            
        } catch (Exception e) {
            logger.error("检查邮箱存在性失败：{}", email, e);
            resultDomain.fail("检查失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUser> searchUsers(String username, String email, String status) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始搜索用户，用户名：{}，邮箱：{}，状态：{}", username, email, status);
            
            List<TbSysUser> users = userMapper.selectUserList(username, email, status);
            
            logger.info("搜索用户完成，共找到{}个用户", users.size());
            resultDomain.success("搜索成功", users);
            return resultDomain;
            
        } catch (Exception e) {
            logger.error("搜索用户失败", e);
            resultDomain.fail("搜索用户失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUser> changeUserStatus(String userId, Integer status) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始修改用户状态：{}，状态：{}", userId, status);
            
            if (!StringUtils.hasText(userId)) {
                resultDomain.fail("用户ID不能为空");
                return resultDomain;
            }
            
            if (status == null) {
                resultDomain.fail("用户状态不能为空");
                return resultDomain;
            }
            
            // 检查用户是否存在
            ResultDomain<TbSysUser> existResult = getUserById(userId);
            if (!existResult.isSuccess()) {
                resultDomain.fail(existResult.getMessage());
                return resultDomain;
            }
            
            TbSysUser user = existResult.getData();
            user.setStatus(status);
            user.setUpdateTime(new Date());
            
            int result = userMapper.updateUser(user);
            
            if (result > 0) {
                logger.info("修改用户状态成功：{}", userId);
                resultDomain.success("修改用户状态成功", user);
                return resultDomain;
            } else {
                logger.warn("修改用户状态失败：{}", userId);
                resultDomain.fail("修改用户状态失败");
                return resultDomain;
            }
            
        } catch (Exception e) {
            logger.error("修改用户状态异常：{}", userId, e);
            resultDomain.fail("修改用户状态失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUser> resetPassword(String userId, String newPassword) {
        ResultDomain<TbSysUser> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始重置用户密码：{}", userId);
            
            if (!StringUtils.hasText(userId)) {
                resultDomain.fail("用户ID不能为空");
                return resultDomain;
            }
            
            if (!StringUtils.hasText(newPassword)) {
                resultDomain.fail("新密码不能为空");
                return resultDomain;
            }
            
            // 检查用户是否存在
            ResultDomain<TbSysUser> existResult = getUserById(userId);
            if (!existResult.isSuccess()) {
                resultDomain.fail(existResult.getMessage());
                return resultDomain;
            }
            
            TbSysUser user = existResult.getData();
            // TODO: 这里应该对密码进行加密处理
            user.setPassword(newPassword);
            user.setUpdateTime(new Date());
            
            int result = userMapper.updateUser(user);
            
            if (result > 0) {
                logger.info("重置用户密码成功：{}", userId);
                resultDomain.success("重置密码成功", user);
                return resultDomain;
            } else {
                logger.warn("重置用户密码失败：{}", userId);
                resultDomain.fail("重置密码失败");
                return resultDomain;
            }
            
        } catch (Exception e) {
            logger.error("重置用户密码异常：{}", userId, e);
            resultDomain.fail("重置密码失败：" + e.getMessage());
            return resultDomain;
        }
    }


    // ----------------用户信息相关--------------------------------

    @Override
    public ResultDomain<TbSysUserInfo> getUserInfoById(String userId) {
        ResultDomain<TbSysUserInfo> resultDomain = new ResultDomain<>();

        try {
            logger.info("开始根据ID查询用户信息：{}", userId);
            if (!StringUtils.hasText(userId)) {
                resultDomain.fail("用户ID不能为空");
                return resultDomain;
            }

            TbSysUserInfo userInfo = userMapper.selectUserInfoById(userId);
            if (userInfo == null) {
                logger.warn("未找到用户信息：{}", userId);
                resultDomain.fail("未找到指定用户信息");
                return resultDomain;
            }
            
            logger.info("根据ID查询用户信息完成：{}", userId);
            resultDomain.success("查询成功", userInfo);
            return resultDomain;
            
        } catch (Exception e) {
            logger.error("根据ID查询用户信息失败：{}", userId, e);
            resultDomain.fail("查询用户信息失败：" + e.getMessage());
            return resultDomain;
        }
    }

    @Override
    public ResultDomain<TbSysUserInfo> updateUserInfo(TbSysUserInfo userInfo) {
        ResultDomain<TbSysUserInfo> resultDomain = new ResultDomain<>();

        try {
            logger.info("开始更新用户信息：{}", userInfo.getUserID());
            if (!StringUtils.hasText(userInfo.getUserID())) {
                resultDomain.fail("用户ID不能为空");
                return resultDomain;
            }
            userInfo.setUpdateTime(new Date());
            
            int result = userMapper.updateUserInfo(userInfo);
            
            if (result > 0) {
                logger.info("更新用户信息成功：{}", userInfo.getUserID());
                TbSysUserInfo newUserInfo = userMapper.selectUserInfoById(userInfo.getUserID());
                resultDomain.success("更新用户信息成功", newUserInfo);
                return resultDomain;
            } else {
                logger.warn("更新用户信息失败：{}", userInfo.getUserID());
                resultDomain.fail("更新用户信息失败");
                return resultDomain;
            }
            
        } catch (Exception e) {
            logger.error("更新用户信息异常：{}", userInfo.getUserID(), e);
            resultDomain.fail("更新用户信息失败：" + e.getMessage());
            return resultDomain;
        }
    }


    @Override
    public ResultDomain<UserVO> getUserInfoTotal(String userId) {
        ResultDomain<UserVO> resultDomain = new ResultDomain<>();
        try {
            logger.info("开始获取用户信息总览：{}", userId);
            String deptPath = userMapper.getUserDeptPath(userId);
            UserVO userVO = userMapper.selectUserInfoTotal(userId);
            userVO.setDeptName(deptPath);
            resultDomain.success("获取用户信息总览成功", userVO);
            return resultDomain;
        } catch (Exception e) {
            logger.error("获取用户信息总览失败：{}", userId, e);
            resultDomain.fail("获取用户信息总览失败：" + e.getMessage());
            return resultDomain;
        }
    }
}