package net.northking.cctp.scriptcase.api.ats.service;

import com.alibaba.fastjson.JSONObject;
import net.northking.cctp.common.db.annotation.DatabaseReadOnly;
import net.northking.cctp.scriptcase.constants.ScriptConstant;
import net.northking.cctp.scriptcase.dto.ats.AtsQueryDto;
import net.northking.cctp.scriptcase.dto.ats.ResultResponse;
import net.northking.cctp.scriptcase.tools.HttpClientUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

@Service
@RefreshScope
@ConditionalOnMissingBean(name = "AtsTestElementApiServiceImpl")
public class AtsTestElementApiServiceImpl implements AtsTestElementApiService {

    @Value("${ats.url}")
    private String atsUrl;

    @Value("${ats.ak}")
    private String ak;

    /**
     * 日志
     */
    private final static Logger logger = LoggerFactory.getLogger(AtsTestElementApiServiceImpl.class);


    @Override
    @DatabaseReadOnly
    public ResultResponse getScriptList(AtsQueryDto query) {
        cn.hutool.json.JSONObject jsonObject = new cn.hutool.json.JSONObject(query);
        String doPost = HttpClientUtils.doPost(atsUrl + ScriptConstant.GET_SCRIPT_PRO_URL, jsonObject.toString(), ak);

        JSONObject parseObject = JSONObject.parseObject(doPost);
        ResultResponse resultResponse = parseObject.toJavaObject(ResultResponse.class);
        resultResponse.setAk(ak);
        resultResponse.setAtsUrl(atsUrl);
        return resultResponse;
    }

    @Override
    public ResultResponse getScenarios(AtsQueryDto query) {

        cn.hutool.json.JSONObject jsonObject = new cn.hutool.json.JSONObject(query);
        String doPost = HttpClientUtils.doPost(atsUrl + ScriptConstant.GET_SCENAR_PRO_URL, jsonObject.toString(), ak);
        JSONObject parseObject = JSONObject.parseObject(doPost);
        ResultResponse resultResponse = parseObject.toJavaObject(ResultResponse.class);
        resultResponse.setAk(ak);
        resultResponse.setAtsUrl(atsUrl);
        return resultResponse;
    }

    /**
     * 获取ATS地址以及ak配置
     * @return
     */
    @Override
    public ResultResponse getAtsConfig() {
        ResultResponse resultResponse = new ResultResponse();
        resultResponse.setAk(ak);
        resultResponse.setAtsUrl(atsUrl);
        return resultResponse;
    }

}
