package com.yem.wm.fm.gathering.validator;

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.servicehelper.BusinessDataServiceHelper;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: TODO
 * @Date: 2024/6/12 18:25
 * @Created: by ZZSLL
 */

public class GatheringAuditValidator extends AbstractValidator {
    @Override
    public void validate() {
        ExtendedDataEntity[] extendedDataEntities = this.getDataEntities();
        for (ExtendedDataEntity extendedDataEntity : extendedDataEntities) {

            sumReceivableAmtVal(extendedDataEntity);
        }
    }

    public void sumReceivableAmtVal(ExtendedDataEntity temp){
        DynamicObject bill = temp.getDataEntity();
//            BigDecimal claimamt = Bi  gDecimal.ZERO;
//            BigDecimal     salcontractamt = BigDecimal.ZERO;
        bill = BusinessDataServiceHelper.loadSingle(bill.getPkValue(), bill.getDynamicObjectType().getName());

        DynamicObject billtype = bill.getDynamicObject("yem_billtype");
        if (YEM.isNotEmpty(billtype)) {
            String billtypeString = billtype.getString("number");
            if ("yem_gathering_TZF".equals(billtypeString) || "yem_gathering_TZZ".equals(billtypeString)) {
                return;
            }
        }

        DynamicObjectCollection receiptdetails = bill.getDynamicObjectCollection("yem_receiptdetail");
        BigDecimal sumreceivableamt = bill.getBigDecimal("yem_sumreceivableamt");
        BigDecimal sumreceivableamtba = bill.getBigDecimal("yem_sumreceivableamtba");
        BigDecimal sumreceivableamts = BigDecimal.ZERO;
        BigDecimal sumreceivableamtbas = BigDecimal.ZERO;
        for (DynamicObject receiptdetail:receiptdetails) {
            //放合同金额
            Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
            //放认领金额
            Map<String, BigDecimal> mapa = new HashMap<String, BigDecimal>();
            DynamicObjectCollection claimdetails = receiptdetail.getDynamicObjectCollection("yem_claimdetail");
            BigDecimal sumclaimamt = BigDecimal.ZERO;
            BigDecimal receivableamt = receiptdetail.getBigDecimal("yem_receivableamt");
            sumreceivableamts=sumreceivableamts.add(receivableamt);
            BigDecimal receivableamtbase = receiptdetail.getBigDecimal("yem_receivableamtbase");
            sumreceivableamtbas=sumreceivableamtbas.add(receivableamtbase);
            for (DynamicObject claimdetail:claimdetails) {

                String noid = claimdetail.getString("yem_salcontractno.billno");
                BigDecimal claimamt = claimdetail.getBigDecimal("yem_claimamt");
                BigDecimal salcontractamt = claimdetail.getBigDecimal("yem_salcontractamt");
                sumclaimamt = claimamt.add(sumclaimamt);
                if (map.get(noid)!=null){
                    BigDecimal variable = map.get(noid);
                    claimamt = variable.add(claimamt);
                    map.put(noid,variable);
                }
                if (map.get(noid)==null){
                    map.put(noid,claimamt);
                    mapa.put(noid,salcontractamt);
                }
            }
            if (sumclaimamt.compareTo(receivableamt) == 1) {
                this.addErrorMessage(temp,"认领明细-认领金额合计值不允许超出收款明细的应收金额");
            }
//            for (Map.Entry<String, BigDecimal> entry : map.entrySet()) {
//                String mapKey = entry.getKey();
//                if (StringUtil.isNotEmpty(mapKey)){
//                    BigDecimal mapValue = entry.getValue();
//                    BigDecimal mapKeya = mapa.get(mapKey);
//                    if (mapValue.compareTo(mapKeya) == 1) {
//                        this.addErrorMessage(temp,mapKey+"同号下，认领明细-认领金额合计值不允许超出合同金额");
//                    }
//                }
//            }
        }
        if (sumreceivableamt.compareTo(sumreceivableamts) != 0) {
            this.addErrorMessage(temp,"财务信息应收金额合计{"+sumreceivableamt.stripTrailingZeros().toPlainString()+"}与收款明细应收金额汇总值{"+sumreceivableamts.stripTrailingZeros().toPlainString()+"}不一致，请调整数据重试！");
        }
        if (sumreceivableamtba.compareTo(sumreceivableamtbas) != 0) {
            this.addErrorMessage(temp,"财务信息应收金额合计{"+sumreceivableamtba.stripTrailingZeros().toPlainString()+"}与收款明细应收金额汇总值{"+sumreceivableamtbas.stripTrailingZeros().toPlainString()+"}不一致，请调整数据重试！");
        }

    }
}
