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");
            stringRedisTemplate.opsForValue().set(XlwConstants.REDIS_TOKEN_KEY, token, 6, TimeUnit.HOURS);
        }
        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;
    }


}
