package com.yem.wm.es.contactbook.validator;

import com.yem.wm.es.contractamtlist.util.FunUtil;
import com.yem.wm.es.shippingdetails.util.PaymentControlUtil;
import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashSet;
import java.util.Set;

import static com.yem.wm.es.contractamtlist.util.FunUtil.getPayBills;
import static com.yem.wm.es.contractamtlist.util.FunUtil.getShipBills;

/**
 * @Description 提交时校验 对应发运明细是否有欠款
 * @Date 2024/1/25 10:34
 * @Created by ZZSLL
 */

public class ContactBookSubmitValidator extends AbstractValidator
{
    @Override
    public void validate()
    {
        ExtendedDataEntity[] entities = this.getDataEntities();
        for (ExtendedDataEntity entity : entities) {
            DynamicObject dataEntity = entity.getDataEntity();
            dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(), dataEntity.getDynamicObjectType().getName());
            String procbilltype = dataEntity.getString("yem_procbilltype");
            if ("Z".equals(procbilltype)) {
                DynamicObjectCollection materialinfos = dataEntity.getDynamicObjectCollection("yem_es_materialinfo");
                Set<String> set = new HashSet<>();
                for (DynamicObject materialinfo : materialinfos) {
                    String shipmentnumber = materialinfo.getString("yem_shipmentnumbers_m");
                    if (YEM.isNotEmpty(shipmentnumber)) {
                        set.add(shipmentnumber);
                    }
                }

                for (String shipmentnum : set) {
                    DynamicObject single = BusinessDataServiceHelper.loadSingle("yem_shippingdetails", new QFilter[]{new QFilter("billno", QCP.equals, shipmentnum)});
                    if (single == null) continue;

                    // 重新计算应申请金额
                    String sourceBillType = single.getString("yem_sourcebilltype");
                    long sourceBillId = single.getLong("yem_sourcebillid");
                    DynamicObject saleOrder = BusinessDataServiceHelper.loadSingle(sourceBillId, sourceBillType);
                    String saleorderno = single.getString("yem_saleorderno");
                    if (YEM.isEmpty(saleorderno)) return;
                    DynamicObject[] shipBills = getShipBills(saleOrder, single, "A");//出运
                    DynamicObject[] payBills = getPayBills(saleOrder, single, "B");//收款单
                    BigDecimal thisRealityAmt = PaymentControlUtil.getThisRealityAmt(saleOrder, shipBills, payBills, single);
                    // 应申请金额
                    BigDecimal abs = FunUtil.getAbs(thisRealityAmt);

                    if (abs.compareTo(new BigDecimal("0.1")) > 0) {

                        DynamicObject[] tradeapps = BusinessDataServiceHelper.load("yem_im_tradeapp", "id, billno, billstatus,yem_thisrealityamt, yem_im_tradeapp_em.yem_shippingnumber, yem_thisshipiouamt", new QFilter[]{new QFilter("yem_im_tradeapp_em.yem_shippingnumber", QCP.equals, single.getString("billno")), new QFilter("billstatus", QCP.equals, "C")});

                        if (tradeapps.length < 1) {
                            this.addErrorMessage(entity, String.format("发运单号为 %s 的发运明细不存在已审核的欠条申请单，请核对后重试！", shipmentnum));
                        }

                        BigDecimal arrearsAmt = BigDecimal.ZERO;

                        for (DynamicObject tradeapp : tradeapps) {
                            arrearsAmt = arrearsAmt.add(tradeapp.getBigDecimal("yem_thisshipiouamt"));
                        }

                        // 应申请金额 - 申请金额 > 0.1 提示不通过
                        if (abs.subtract(arrearsAmt).compareTo(new BigDecimal("0.1")) > 0) {

                            this.addErrorMessage(entity, String.format("%s 发运单号，本次发运应申请欠条金额 %s，请整机及配件货款延付申请审核后重试！", shipmentnum, abs.setScale(2, RoundingMode.HALF_UP)));
                        }
                    }
                }
            }
        }
    }
}
