package com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw;

import cn.hutool.core.util.StrUtil;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.constatnt.SdgkConstants;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.constatnt.XlwConstants;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.common.result.XlwResultData;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.AuthMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.sdgk.SdgkAuthMapper;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.sdgk.SdgkClientFeign;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.sdgk.dto.SdgkBaseResult;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.sdgk.dto.SdgkLoginDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.dto.XlwLoginDTO;
import com.cico.nest.lcdp.boot.core.qxsd.infrastructure.gatewayimpl.http.xlw.feign.XlwClientFeign;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author zws
 * @date 2024/8/7
 */
@Component
public class XlwAuthMapper implements AuthMapper {

    @Resource
    private XlwClientFeign xlwClientFeign;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Value("${qxsd.xlw.loginName}")
    private String loginName;
    @Value("${qxsd.xlw.loginPwd}")
    private String loginPwd;

    @Resource
    private SdgkClientFeign sdgkClientFeign;
    @Resource
    private SdgkAuthMapper sdgkAuthMapper;
    @Value("${qxsd.sdgk.loginName}")
    private String sdgkLoginName;
    @Value("${qxsd.sdgk.password}")
    private String sdgkLoginPwd;

    @Override
    public String getXlwLoginToken() {
        String token = stringRedisTemplate.opsForValue().get(XlwConstants.REDIS_TOKEN_KEY);
        if (StrUtil.isEmpty(token)) {
            XlwLoginDTO xlwLoginDTO = new XlwLoginDTO();
            xlwLoginDTO.setLoginName(loginName);
            xlwLoginDTO.setLoginPwd(loginPwd);
            XlwResultData<Map<String, Object>> loginResult = xlwClientFeign.login(xlwLoginDTO);
            Map<String, Object> resultData = loginResult.getData();
            token = (String) resultData.get("sessionId");
            // token 这里的有效期要小于新路威token 的有效期，不让就会  redis 中还存在,其实登陆失败了
//            stringRedisTemplate.opsForValue().set(XlwConstants.REDIS_TOKEN_KEY, token, 6, TimeUnit.HOURS);
            stringRedisTemplate.opsForValue().set(XlwConstants.REDIS_TOKEN_KEY, token, 5, TimeUnit.MINUTES);
        }
        return token;
    }


    @Override
    public String getSdgkLoginToken() {
        String token = stringRedisTemplate.opsForValue().get(SdgkConstants.REDIS_TOKEN_KEY);
        if(StrUtil.isEmpty(token)){
            token = sdgkAuthMapper.getLoginToken();
            stringRedisTemplate.opsForValue().set(SdgkConstants.REDIS_TOKEN_KEY,token,10, TimeUnit.MINUTES);
        }
        return token;
    }


}
