package com.persagy.adm.server.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.persagy.adm.server.algorithm.service.AdmProjectUserService;
import com.persagy.adm.server.auth.service.AdmAuthService;
import com.persagy.dmp.auth.client.BossAuthFacade;
import com.persagy.dmp.auth.config.AuthFeignInterceptor;
import com.persagy.dmp.auth.domain.BossAuthMenu;
import com.persagy.dmp.auth.domain.BossAuthUser;
import com.persagy.dmp.auth.domain.Project;
import com.persagy.dmp.common.constant.ResponseCode;
import com.persagy.dmp.common.exception.BusinessException;
import com.persagy.dmp.common.helper.SpringHelper;
import com.persagy.dmp.common.model.response.CommonResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ADM鉴权统一逻辑处理类
 * @author : lijie
 * Update By 2022/1/11 15:51
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class AdmAuthServiceImpl implements AdmAuthService {
    /**adm网站id的配置key,BOSS系统需要.值从BOSS权限系统中创建后获取*/
    private static final String ADM_WEB_SITE_ID="bdtp.adm.web.site.id";
    private final AdmProjectUserService admProjectUserService;

    /**
     * 获取当前登录人的用户信息
     * @return : com.persagy.dmp.auth.domain.BossAuthUser
     * @author : lijie
     * Update By 2022/1/11 15:52
     */
    @Override
    public BossAuthUser loginInfo() {
        // 1.获取用户的基本信息
        CommonResult<BossAuthUser> baseUserMesResult = BossAuthFacade.loginInnerLoginInfo();
        if (!ResponseCode.A00000.getDesc().equals(baseUserMesResult.getResult())){
            throw new BusinessException(baseUserMesResult.getResult(),baseUserMesResult.getMessage());
        }
        BossAuthUser authUser = baseUserMesResult.getData();
        if (null==authUser || StrUtil.isBlank(authUser.getId())){
            throw new BusinessException(ResponseCode.A10000.getDesc(),"用户不存在");
        }
        // 2.获得用户的菜单权限,略.调用BossAuthFacade.loginMenu(SpringHelper.getString(ADM_WEB_SITE_ID))即可
        // 3.获得用户的数据权限,略.调用BossAuthFacade.loginPermission(SpringHelper.getString(ADM_WEB_SITE_ID))即可
        // 4.获得用户的项目权限,暂时根据用户id从配置表中获取
        authUser.setProjects(admProjectUserService.getProjectsByUserId(authUser.getId()));
        return authUser;
    }
}
