package com.tax.token.integration.validtoken;

import com.tax.token.constants.TokenConstants;
import com.tax.token.model.PushTokenReq;
import com.tax.token.model.ValidTokenInfo;
import com.yxcheng.token.utils.DateUtils;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.JSONHelper;
import com.yxcheng.token.utils.ResponseContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * Created by 曹小红 on 2019-03-20.
 */
public class ValidTokenClient {

    private static final Logger logger = LoggerFactory.getLogger(ValidTokenClient.class);

    public static ValidTokenInfo validTokenFrom(PushTokenReq pushTokenReq) {
        String timeFlag = DateUtils.getDataString(DateUtils.yyyymmddthhmmssSSS);
        logger.debug(timeFlag + "validTokenFrom is start...");
        ValidTokenInfo validTokenInfo = new ValidTokenInfo();
        try {
            String url = TokenConstants.TOKEN_VALID_URL;
            String bussiStr = "?recevDate=%s&taxno=%s&token=%s&zoneCode=%s";
            bussiStr = String.format(bussiStr, pushTokenReq.getRecevDate(), pushTokenReq.getTaxno(), pushTokenReq.getToken(), pushTokenReq.getZoneCode());
            url += bussiStr;
            ResponseContent ret = HttpHelper.getUrlRespContent(url);
            String retStr = ret.getContent();
            logger.info(timeFlag + "validTokenFrom request the url is : " + url + " the response is : " + retStr);
            Map<String, Object> retMap = JSONHelper.parserToMap(retStr);
            String retCode = String.valueOf(retMap.get("CODE"));
            String retMessage = String.valueOf(retMap.get("MESSAGE"));
            validTokenInfo.setCode(retCode);
            validTokenInfo.setMessage(retMessage);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error(timeFlag + "validTokenFrom is error!The error info is " + e.getMessage());
        }
        logger.debug(timeFlag + "validTokenFrom is end...");
        return validTokenInfo;
    }
}
