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.ResponseContent;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;

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

public class GetTokenFromXForce 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 secert = tkServer.getPassword();
        String url = tkServer.getReqUrl() + "?companyTaxNo="+taxno+ "&secretKey=" + secert;
        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)){
                    retMap = JSONHelper.parserToMap(retStr);
                    if("TXWRAU0001".equals(retMap.get("code"))){
                        retMap.put("token",retMap.get("result"));
                        retMap.put("code","0");
                    }


                }
            } 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("91540124321390417T");

        //tkServer.setReqUrl("http://fat-taxware-mammoth-service.phoenix-t.xforceplus.com/global/taxware/v1/input/mammoth/company/token");
        tkServer.setReqUrl("http://pro-taxware-mammoth-service.phoenix.xforceplus.com/global/taxware/v1/input/mammoth/company/token");
        tkServer.setPassword("077390f914c6241c262d8b54c676ab0d");

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

    }
}
