package com.tax.token.thirdService.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.*;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

public class GetTokenFromEleJsonTax implements TokenThirdServiceI {

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

    @Override
    public Map<String, Object> invoke(String uuIdFlag, TkToken tokenEntity, TkZoneInfo zoneInfo, TkServer tkServer, boolean isFirst) throws IOException, HttpException {
        Map<String,Object> resultMap = new HashMap<>();
        String taxno = tokenEntity.getTaxno();

        String zoneCode = tokenEntity.getZoneCode();
        String type = "0";
        //用户类型为2时，不传用户名密码
        if(StringUtil.isNotEmpty(tokenEntity.getUserType()) && tokenEntity.getUserType().equals("2")) {
            type = "1";
        }
        String url = tkServer.getReqUrl();
        JSONObject json = new JSONObject();
        json.put("userCode", tokenEntity.getUserCode());
        json.put("taxno", taxno);
        json.put("type", type);
        json.put("dqdm", zoneCode);
        json.put("site", tokenEntity.getSite());
        json.put("target", tokenEntity.getTarget());
        json.put("requestId", uuIdFlag);
        if(null!=tokenEntity.getTaxUserInfo()) {
            json.put("loginInfoJson", JSON.toJSON(tokenEntity.getTaxUserInfo()));
        }
        json.put("loginInfoMd5", StringUtil.transNullToEmpty(tokenEntity.getSecretMd5()));

        logger.info(uuIdFlag + " GetTokenFromEleJsonTax taxno is " + taxno + " url is " + url + ",param is " + json.toString());
        ResponseContent ret = HttpHelper.postJsonEntity(url,json.toString(), 30000, 90000);
        String resultStr = ret.getContent();
        logger.info(uuIdFlag + " GetTokenFromEleJsonTax taxno is " + taxno + ",result is " + resultStr);
        if(StringUtils.isNotEmpty(resultStr)) {
            Map<String, Object> resultStrMap = JSONHelper.parserToMap(resultStr);
            resultMap.put("code", ObjectUtil.fixNull(resultStrMap.get("code")));
            resultMap.put("message", ObjectUtil.fixNull(resultStrMap.get("message")));
            resultMap.put("times", ObjectUtil.fixNull(resultStrMap.get("time")));
            resultMap.put("requestId", ObjectUtil.fixNull(resultStrMap.get("requestId")));
            resultMap.put("proxyip", ObjectUtil.fixNull(resultStrMap.get("proxyip")));
            resultMap.put("userName", ObjectUtil.fixNull(resultStrMap.get("userName")));
            resultMap.put("session", ObjectUtil.fixNull(resultStrMap.get("session")));
            if("0".equals(resultStrMap.get("code"))){
                resultMap.put("token",resultStrMap.get("token"));
            }
            resultMap.put("resContext", resultStr);
        }

        return resultMap;
    }


    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("1");
//        tokenEntity.setZoneCode("2");
//        tokenEntity.setSecert("3");
//        tokenEntity.setUserCode("ediliankeji");
//
//        tkServer.setReqUrl("http://123.56.20.54:8182/TokenManage/digitalcookie");
//        tkServer.setUserName("ediliankeji");
//
//        GetTokenFromEleJsonTax local = new GetTokenFromEleJsonTax();
//        System.out.println(local.invoke("aaa", tokenEntity,zoneInfo,tkServer,isFirst));
        String resultStr = "{\"code\":\"8000\",\"message\":\"用户访问频繁，请180秒后再试：3502:913502037692676604:15280312366\",\"time1\":126}";
        Map<String, Object> resultStrMap = JSONHelper.parserToMap(resultStr);
        System.out.println(ObjectUtil.fixNull(resultStrMap.get("time")));
    }
}
