//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nc.pubimpl.ic.m45.m25;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import nc.bs.ic.pub.rewrite.RewriteBPTemplate;
import nc.bs.logging.Log;
import nc.bs.logging.Logger;
import nc.bs.uapbd.util.MyHelper;
import nc.bs.uapbd.util.ThirdPartyPostRequestUtil;
import nc.vo.ic.m45.entity.PurchaseInBodyVO;
import nc.vo.ic.pub.util.NCBaseTypeUtils;
import nc.vo.ml.NCLangRes4VoTransl;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nccloud.baseapp.core.log.NCCForUAPLogger;

import java.util.Map;

public class Bill45For25Rewrite extends RewriteBPTemplate<PurchaseInBodyVO> {
    private static final Log logger = Log.getInstance("qyMesLog");
    private static final String reqUrl = "/IF_QyErpApi.ashx?action=addRkdFpxx";

    public Bill45For25Rewrite() {
        super(PurchaseInBodyVO.class);
        this.additionalFields(new String[]{"nnum"});
        this.addUpdateField("nsignnum");
    }

    protected void reWriteAfterProcess(PurchaseInBodyVO[] vos) throws BusinessException {
        UFDouble dvalue = null;
        UFDouble num = null;
        UFDouble nsignnum = null;

        for (PurchaseInBodyVO vo : vos) {
            num = NCBaseTypeUtils.abs(vo.getNnum());
            nsignnum = NCBaseTypeUtils.abs(vo.getNsignnum());
            dvalue = NCBaseTypeUtils.sub(num, new UFDouble[]{nsignnum});
            if (NCBaseTypeUtils.isLtZero(dvalue)) {
                throw new BusinessException(NCLangRes4VoTransl.getNCLangRes().getStrByID("4008002_0", "04008002-0092"));
            }
        }

        super.reWriteAfterProcess(vos);
        this.syncQyMes(vos);
    }

    /**
     * 同步启源的累计开票数量
     *
     * @param vos
     * @throws BusinessException
     */
    protected void syncQyMes(PurchaseInBodyVO[] vos) throws BusinessException {
        JSONArray contentParam = new JSONArray();
        for (PurchaseInBodyVO vo : vos) {
            // 启源入库单行主键
            String vbdef20 = vo.getVbdef20();
            if (vbdef20 == null || vbdef20.isEmpty() || "~".equals(vbdef20)) {
                continue;
            }
            String nsignnum = vo.getNsignnum().toString();
            JSONObject param = new JSONObject();
            param.put("rkqdid", vbdef20); // 启源入库单行主键
            param.put("ydyfpsl", nsignnum);// 累计开票数量
            contentParam.add(param);
        }
        if (!contentParam.isEmpty()) {
            JSONObject data = new JSONObject();
            data.put("batchid", System.currentTimeMillis());
            data.put("Content", contentParam);
            pushData(data);
        }
    }

    /**
     * 推送同步数据
     */
    private void pushData(JSONObject param) throws BusinessException {
        Map<String, String> configParams = MyHelper.getConfigParams("jm-config", null);
        String jsonStr = JSON.toJSONString(param);
        logger.error("qyMes-purchasein-param = " + jsonStr);
        NCCForUAPLogger.debug("qyMes-purchasein-param = " + jsonStr);
        String baseUrl = configParams.get("qmsBaseUrl");
        String requestUrl = baseUrl + reqUrl;
        logger.error("qyMes-purchasein-url = " + requestUrl);
        String result = ThirdPartyPostRequestUtil.sendPostRequest(requestUrl, jsonStr);
        NCCForUAPLogger.debug("qyMes-purchasein-result = " + result);
        JSONObject resultObj = JSONObject.parseObject(result);
        logger.error("qyMes-purchasein-res = " + result);

        if (!"true".equals(resultObj.getString("success"))) {
            Logger.error("qyMes-purchasein-error,result[" + resultObj.toJSONString() + "]");
        }
    }
}
