package com.tuoheng.admin.service.user.query;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tuoheng.admin.conver.UserConverMapper;
import com.tuoheng.admin.entity.Dept;
import com.tuoheng.admin.entity.Role;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.code.user.QueryUserInfoByIdCodeEnum;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.RoleMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.user.UserInfoVo;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 根据用户Id查询用户信息业务层处理
 *
 * @author wanjing
 * @team tuoheng
 * @date 2022-12-16
 */
@Slf4j
@Service
public class QueryUserInfoByIdService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 获取部门树形列表
     *
     * @return
     */
    public JsonResult getUserInfo(String id) {
        log.info("进入根据用户Id查询用户信息业务");
        JsonResult result = this.check(id);
        if (0 != result.getCode()) {
            log.info("根据用户Id查询用户信息业务：校验失败：{}", result.getMsg());
            return result;
        }

        User user = (User) result.getData();

        UserInfoVo userInfoVo = UserConverMapper.INSTANCE.fromUserToUserInfoVo(user);
        Dept dept = deptMapper.selectOne(new LambdaQueryWrapper<Dept>()
                .eq(Dept::getId, userInfoVo.getDeptId())
                .eq(Dept::getMark, 1));
        if (ObjectUtil.isNotNull(dept)) {
            userInfoVo.setDeptName(dept.getName());
        }

        Role role = roleMapper.selectOne(new LambdaQueryWrapper<Role>()
                .eq(Role::getId, userInfoVo.getRoleId())
                .eq(Role::getMark, 1));
        if (ObjectUtil.isNotNull(role)) {
            userInfoVo.setRoleName(role.getRoleName());
        }
        return JsonResult.success(userInfoVo);
    }

    /**
     * 检查参数
     *
     * @param id
     * @return
     */
    private JsonResult check(String id) {
        if (StringUtils.isEmpty(id)) {
            return JsonResult.error(QueryUserInfoByIdCodeEnum.USER_ID_IS_NULL.getCode(), QueryUserInfoByIdCodeEnum.USER_ID_IS_NULL.getMsg());
        }
        User user = userMapper.selectOne(new LambdaQueryWrapper<User>()
                .eq(User::getId, id)
                .eq(User::getMark, 1));

        if (ObjectUtil.isNull(user)) {
            return JsonResult.error(QueryUserInfoByIdCodeEnum.USER_IS_NOT_EXIST.getCode(), QueryUserInfoByIdCodeEnum.USER_IS_NOT_EXIST.getMsg());
        }

        if (!user.getTenantId().equals(CurrentUserUtil.getTenantId())) {
            return JsonResult.error(QueryUserInfoByIdCodeEnum.NON_TENANT_USER.getCode(), QueryUserInfoByIdCodeEnum.NON_TENANT_USER.getMsg());
        }
        return JsonResult.success(user);
    }

}
