package nc.pubimpl.so.rule;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import nc.impl.pubapp.pattern.rule.IRule;
import nc.vo.pub.lang.UFDouble;
import nc.vo.so.m30.entity.SaleOrderBVO;
import nc.vo.so.m30.entity.SaleOrderHVO;
import nc.vo.so.m30.entity.SaleOrderViewVO;
import nc.vo.so.m30.util.HttpClmUtil;
import nccloud.commons.lang.StringUtils;

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

/**
 * 同步合同销售订单的数量信息
 */
public class SyncClmNumRule implements IRule<SaleOrderViewVO> {
    private String flag = "";

    public SyncClmNumRule() {
    }

    public SyncClmNumRule(String flag) {
        this.flag = flag;
    }

    public void process(SaleOrderViewVO[] vos) {
        JSONArray array = new JSONArray();
        for (SaleOrderViewVO vo : vos) {
            SaleOrderHVO head = vo.getHead();
            SaleOrderBVO body = vo.getBody();
            String vdef9 = head.getVdef9();// BIP合同销售订单id
            String vbdef11 = body.getVbdef11();// BIP合同销售订单更新 子表id
            if (StringUtils.isEmpty(vdef9) || StringUtils.isEmpty(vbdef11)) {
                continue;
            }
            // 累计开票主数量
            UFDouble ntotalinvoicenum = body.getNtotalinvoicenum();
            if ("ntotalinvoicenum".equals(flag) && ntotalinvoicenum == null) {
                continue;
            }
            // 累计确认应收金额
            UFDouble ntotalarmny = body.getNtotalarmny();
            if ("ntotalarmny".equals(flag) && ntotalarmny == null) {
                continue;
            }
            // 累计出库数量
            UFDouble ntotaloutnum = body.getNtotaloutnum();
            if ("ntotaloutnum".equals(flag) && ntotaloutnum == null) {
                continue;
            }
            // 累计安排生产订单主数量
            UFDouble narrangemonum = body.getNarrangemonum();
            if ("narrangemonum".equals(flag) && narrangemonum == null) {
                continue;
            }
            // 累计退货主数量
            UFDouble ntotalreturnnum = body.getNtotalreturnnum();
            if ("ntotalreturnnum".equals(flag) && ntotalreturnnum == null) {
                continue;
            }

            JSONObject oneJson = new JSONObject();
            List<Map<String, Object>> bodyList = new ArrayList<>();
            Map<String, Object> bodyMap = new HashMap<>();

            bodyMap.put("id", vbdef11);

            if (ntotalinvoicenum != null) {
                bodyMap.put("billQty", ntotalinvoicenum.toString());
            }
            if (ntotalarmny != null) {
                bodyMap.put("totalAccrued", ntotalarmny.toString());
            }
            if (ntotaloutnum != null) {
                bodyMap.put("totalOutQty", ntotaloutnum.toString());
            }
            if (narrangemonum != null) {
                bodyMap.put("narrangemonum", narrangemonum.toString());
            }
            if (ntotalreturnnum != null) {
                bodyMap.put("ntotalreturnnum", ntotalreturnnum.toString());
            }

            bodyList.add(bodyMap);

            oneJson.put("contractOrdersList", bodyList);

            oneJson.put("id", vdef9);
            array.add(oneJson);
        }
        if (!array.isEmpty()) {
            JSONObject reqData = new JSONObject();
            reqData.put("HTXSDD", array);
            HttpClmUtil httpClmUtil = new HttpClmUtil();
            httpClmUtil.reqSaleOrder(reqData);
        }
    }
}
