package com.malk.siku.utils;

import com.alibaba.fastjson.JSONObject;
import com.malk.utils.UtilHttp;
import com.malk.utils.UtilMap;
import com.malk.utils.UtilToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;

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

/**
 * 每刻云票
 */
@Slf4j
public class MkYpUtil {
    //测试环境
    /*private final static String appCode = "AP53EP1SVDS1N9";
    private final static String appSecret = "qF4nm3nPnyXYcrWcr5jl";
    private final static String host = "pms-uat.maycur.com";*/

    //生产环境
    private final static String appCode = "AP565SLVOI76UA";
    private final static String appSecret = "sGoMNMSbaw4MWLLbUTtPye2k6trtfIs1";
    private final static String host = "pms.maycur.com";

    private final Object $lock = new Object[0];

    private static final Long EXPIRES_IN = 1800000L;

    public static Map initTokenHeader(){

            Map header = new HashMap();

            String tokenId = UtilToken.get("invalid-tokenId-mkyp");
            String entCode = UtilToken.get("invalid-entCode-mkyp");

            if (StringUtils.isNotBlank(tokenId) && StringUtils.isNotBlank(entCode)) {
                header.put("tokenId",tokenId);
                header.put("entCode",entCode);

                return header;
            } else {
                //每刻云票登录认证
                Map body = new HashMap();

                long timeMillis = System.currentTimeMillis();
                body.put("appCode",appCode);
                body.put("secret",getSecret(appCode,appSecret,timeMillis));
                body.put("timestamp",timeMillis);

                Map result = (Map) JSONObject.parse(UtilHttp.doPost("https://" + host + "/api/common/openapi/auth/login", null, null, body));

                Map data = UtilMap.getMap(result, "data");

                tokenId = UtilMap.getString(data, "tokenId");
                entCode = UtilMap.getString(data, "entCode");

                log.info("每刻云票tokenId, {}", tokenId);
                log.info("每刻云票entCode, {}", entCode);

                UtilToken.put("invalid-tokenId-mkyp", tokenId, EXPIRES_IN);
                UtilToken.put("invalid-entCode-mkyp", entCode, EXPIRES_IN);

                header.put("tokenId",tokenId);
                header.put("entCode",entCode);

                return header;
            }

    }

    //开票通知单导入
    public static Map application(Object body){
        Map result = (Map) JSONObject.parse(UtilHttp.doPost("https://" + host + "/api/ar/openapi/application_oa/batch", MkYpUtil.initTokenHeader(), null, body,(Map) null));
//        Map result = (Map) JSONObject.parse(UtilHttp.doRequest(UtilHttp.METHOD.POST, "https://" + host + "/api/ar/openapi/application_oa/batch", MkYpUtil.initTokenHeader(), null, body));

        log.info("Result:{}",result);

        return result;
    }

    //保存云票客户
    public static Map saveClient(Object body){
        Map result = (Map) JSONObject.parse(UtilHttp.doPost("https://" + host + "/api/ar/openapi/client/batch", MkYpUtil.initTokenHeader(), null, body,(Map) null));

        log.info("Result:{}",result);

        return result;
    }

    //应收单查询
    public static List<Map> searchReceivable(Object body){
        Map result = (Map) JSONObject.parse(UtilHttp.doPost("https://" + host + "/api/ar/openapi/receivable_oa/search", MkYpUtil.initTokenHeader(), null, body,(Map) null));

        log.info("Result:{}",result);

        Map data = UtilMap.getMap(result, "data");

        List<Map> data2 = UtilMap.getList(data, "data");

        return data2;
    }

    //员工批量查询接口
    public static List<Map> getAllEmployee(){
        Map result = (Map) JSONObject.parse(UtilHttp.doPost("https://" + host + "/api/openapi/employee/getAllEmployee", MkYpUtil.initTokenHeader(), null, null,(Map) null));

        log.info("Result:{}",result);

        List<Map> data = UtilMap.getList(result, "data");

        return data;
    }

    //已完成发票数据查询接口V2
    public static Map getInvoiceInfo(String invoiceNumber){
        Map body = new HashMap();
        body.put("searchType","INVOICE_VAT");
        body.put("invoiceNumber",invoiceNumber);
        body.put("pageSize",1);
        body.put("pageNum",1);

        Map result = (Map) JSONObject.parse(UtilHttp.doPost("https://" + host + "/api/ar/openapi/invoice/v2/search", MkYpUtil.initTokenHeader(), null, body,(Map) null));

        log.info("Result:{}",result);

        Map data = UtilMap.getMap(result, "data");

        List<Map> list = UtilMap.getList(data, "list");

        if (!list.isEmpty()){
            return list.get(0);
        }else {
            return null;
        }
    }

    //应收单作废接口
    public static void receivableObsolete(Map body){
        Map result = (Map) JSONObject.parse(UtilHttp.doPost("https://" + host + "/api/ar/openapi/receivable_oa/obsolete", MkYpUtil.initTokenHeader(), null, body,(Map) null));

        log.info("Result:{}",result);
    }

    private static String getSecret(String appCode, String appSecret, long timeMillis){
        log.info("time：{}",timeMillis);

        String s = DigestUtils.sha256Hex(appSecret + ":" + appCode + ":" + timeMillis);
        log.info("s:{}",s);

        return s;
    }


}
