package net.northking.cctp.platform.api.login.service;

import com.hzbank.testteam.autotest.dependencies.monsterDependency.dto.TestGroupDTO;
import com.hzbank.testteam.autotest.dependencies.monsterDependency.dto.UserDTO;
import net.northking.cctp.common.cache.EntityNameCache;
import net.northking.cctp.common.cache.EntityNameCatalog;
import net.northking.cctp.platform.bus.feign.HzBankFeign;
import net.northking.cctp.platform.dto.user.UserInfoFeignDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录服务
 *
 * @author xiusong.xie
 * @since 1.0
 */
@Service
@ConditionalOnMissingBean(name = "UserLoginServiceImpl")
public class UserLoginServiceImpl implements UserLoginService {
    private static final Logger logger = LoggerFactory.getLogger(UserLoginServiceImpl.class);

    /**
     * 登录成功后的默认权限
     */
    private static final String AUTHORITY_LOGIN = "authenticated";

    @Autowired
    private EntityNameCache entityNameCache;

    @Autowired
    private HzBankFeign hzBankFeign;

    @Override
    public UserInfoFeignDto getUserInfo(String userId) {
        UserInfoFeignDto userInfoFeignDto = new UserInfoFeignDto();
        Map<String, Object> params = new HashMap<>();
        params.put("id", Long.parseLong(userId));
        UserDTO userDTO = hzBankFeign.selectOneByMap(params);
        if (userDTO != null) {
            userInfoFeignDto.setScriptManagerOperCode(userDTO.getUserName());
            userInfoFeignDto.setScriptManagerCompany(userDTO.getCompanyName());
        }
        if (userInfoFeignDto != null) {
            TestGroupDTO cltTestTeamDto = this.getPlanTeamInfo(userId);
            if (cltTestTeamDto != null) {
                userInfoFeignDto.setTestTeam(cltTestTeamDto.getName());
                userInfoFeignDto.setTestTeamId(cltTestTeamDto.getId().toString());
                String userName = entityNameCache.translateById(EntityNameCatalog.MONSTER_USER, cltTestTeamDto.getManagerId().toString());
                userInfoFeignDto.setTestTeamManager(userName);
            }

            return userInfoFeignDto;
        }
        return new UserInfoFeignDto();

    }

    @Override
    public TestGroupDTO getPlanTeamInfo(String userId) {
        return hzBankFeign.getInfoByUserId(Long.parseLong(userId));
    }

}
