package com.tax.sign.utils;

import com.google.common.collect.Lists;
import com.yxcheng.token.utils.DateUtils;
import com.yxcheng.token.utils.HttpHelper;
import com.yxcheng.token.utils.MD5Util;
import com.yxcheng.token.utils.ResponseContent;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 曹小红 on 2018-03-23.
 */
public class TaxnoUtil {

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

    public static List<String> getTaxnoList(String url){
        List<String> taxnoList = Lists.newArrayList();
        logger.info(" taxutil url is : " + url);

        Map<String, Object> paramsMap = new HashMap<String, Object>();
        paramsMap.put("data", "{'user':'signuser'}");
        paramsMap.put("username", "signuser");
        paramsMap.put("timestamp", DateUtils.getDataString(DateUtils.yyyymmddhhmmss));
        paramsMap.put("sign", MD5Util.MD5Encode((String)paramsMap.get("data") + (String)paramsMap.get("username") + "!@#123456$%^" + paramsMap.get("timestamp")));
        try {
            ResponseContent responseContent = HttpHelper.postEntity(url, paramsMap);
            if(responseContent!=null){
                String content = responseContent.getContent();

                JSONObject contentObj = JSONObject.fromObject(content);
                String resultCode = contentObj.getString("resultCode");
                if(resultCode.equals("00")) {
                    JSONArray dataArr = contentObj.getJSONArray("data");
                    for (int i = 0; i < dataArr.size(); i++) {
                        JSONObject taxnoObj = dataArr.getJSONObject(i);
                        String taxno = taxnoObj.getString("taxno");
                        taxnoList.add(taxno);
                    }
                }
                logger.info(" taxutil content is : " + taxnoList.size());
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getMessage());
        }
        return taxnoList;
    }

    public static void main(String[] args) {
        TaxnoUtil.getTaxnoList("http://10.25.143.82:8181/fpcj/client/flushTaxUser/flushTaxno.do");
    }
}
