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.mapper.DeptMapper;
import com.tuoheng.admin.mapper.RoleMapper;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.admin.vo.user.UserInfoVo;
import com.tuoheng.common.core.utils.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 获取部门树形列表
     *
     * @return
     */
    public JsonResult getUserInfo() {
        log.info("进入根据用户名查询用户信息业务");
        User user = CurrentUserUtil.getUserInfo();
        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);
    }
}
