package com.tax.token.thirdService.impl;

import com.tax.token.model.TkServer;
import com.tax.token.model.TkToken;
import com.tax.token.model.TkZoneInfo;
import com.tax.token.thirdService.TokenThirdServiceI;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.JSONHelper;
import com.yxcheng.token.utils.ObjectUtil;
import com.yxcheng.token.utils.ResponseContent;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;

import java.io.IOException;
import java.util.Map;

public class GetTokenFromSzyh implements TokenThirdServiceI {

    @Override
    public Map<String, Object> invoke(String uuIdFlag, TkToken tokenEntity, TkZoneInfo zoneInfo, TkServer tkServer,boolean isFirst) throws IOException, HttpException {

        String taxno = tokenEntity.getTaxno();
        String zoneCode = tokenEntity.getZoneCode();

        String username = tkServer.getUserName();


        String url = tkServer.getReqUrl() + "?zoneCode="+zoneCode+ "&taxno=" + taxno +"&userCode="+username;
        System.out.println("url=" + url);
        ResponseContent ret  = null;
        if(isFirst){
            ret = HttpHelper.getUrlRespContent(url,30000);
        }else{
            ret = HttpHelper.getUrlRespContent(url);
        }
        Map<String, Object> retMap = null;
        if(ret!=null){
            String retStr = null;
            try {
                retStr = ret.getContent();
                if(StringUtils.isNotBlank(retStr)){
                    if(retStr.startsWith("<html>")){
                        return retMap;
                    }
                    retMap = JSONHelper.parserToMap(retStr);
                    if(StringUtils.isNotBlank(ObjectUtil.fixNull(retMap.get("data")))){
                        retMap.put("token",ObjectUtil.fixNull(retMap.get("data")));
                        retMap.put("code",ObjectUtil.fixNull(retMap.get("errorCode")));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return retMap;
    }


    public static void main(String[] args) throws IOException, HttpException {
        TkToken tokenEntity = new TkToken();
        TkZoneInfo zoneInfo=new TkZoneInfo(); TkServer tkServer=new TkServer();boolean isFirst = true;
        tokenEntity.setTaxno("91340200591406087P");
        tokenEntity.setZoneCode("3400");
        tokenEntity.setKeyCode("9382");
        tokenEntity.setSecert("");
        tokenEntity.setIpServerNo("T00005");
        zoneInfo.setYmbb("4.0.05");
        tkServer.setReqUrl("http://taxca-t.bigfintax.com:8762/client/getToken");
        tkServer.setUserName("szyh");


        GetTokenFromSzyh local = new GetTokenFromSzyh();
        System.out.println(local.invoke("aaa",tokenEntity,zoneInfo,tkServer,isFirst));

    }
}
