package com.yem.wm.im.clientdemand.validator;

import com.yem.wm.es.Util.TotalUtil;
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 java.math.BigDecimal;

/**
 * @author 张立攀
 * @version 1.0
 * @description 客户需求单提交校验器
 * @date 2023-07-22 14:56
 */
public class ClientSubmitValidator extends AbstractValidator {
    public static final String material = "yem_material";
    public static final String businesssort = "yem_businesssort";
    public static final String standardprice = "yem_standardprice";
    public static final String marketraiseprice = "yem_marketraiseprice";
    public final static String entry = "yem_im_detailedinfor";
    public final static String sonEntry = "yem_multiconfig";
    public final static String bigcabinetnumber = "yem_bigcabinetnumber";
    public final static String mincabinetnumber = "yem_mincabinetnumber";

    @Override
    public void validate() {
        ExtendedDataEntity[] datas = this.getDataEntities();
        StringBuffer info = new StringBuffer();
        StringBuffer infos = new StringBuffer();
        boolean flag = false;  boolean flags = false;
        for (ExtendedDataEntity temp : datas) {
            DynamicObject data = temp.getDataEntity();
            int bigcabinetnumber = data.getInt("yem_bigcabinetnumber");
            int mincabinetnumber = data.getInt("yem_mincabinetnumber");

            if ((bigcabinetnumber + mincabinetnumber) == 0) {
                info.append("基本信息中【大柜数量】、【小柜数量】不能全部为空! \r\n");
            }
            DynamicObjectCollection detailedinfor = data.getDynamicObjectCollection("yem_im_detailedinfor");
            int row = 1;
            if (detailedinfor != null && detailedinfor.size() > 0) {
                for (DynamicObject dy : detailedinfor) {

                    String materialclass = dy.getString("yem_materialclass.name");

                    if (materialclass != null && materialclass.equals("配件")) {
                        DynamicObject material = dy.getDynamicObject("yem_material");
                        if (material == null) {
                            this.addErrorMessage(temp, "明细信息中第" + row + "行物料分类为配件的数据、【物料编码】不能为空" + "\r\n");
                        }
                    }
                    String businesssort = dy.getString("yem_businesssort");
                    if ("Z".equals(businesssort)) {
                        BigDecimal standardprice = dy.getBigDecimal("yem_standardprice");
                        if (BigDecimal.ZERO.compareTo(standardprice) == 0) {
                            flag = true;
                            info.append("明细信息第" + row + "行对应 【标配FOB（青岛）价】为空 请获取标准价" + "\r\n");
                        }
                    }
                    int sonRow = 1;
                    DynamicObjectCollection dynamicObjects = dy.getDynamicObjectCollection("yem_multiconfig");
                    for (DynamicObject temp1 : dynamicObjects) {
                        BigDecimal marketraiseprice = temp1.getBigDecimal("yem_marketraisepribase");
                        if (BigDecimal.ZERO.compareTo(marketraiseprice) == 0) {
                            flags = true;
                            infos.append("明细信息第" + row + "行对应的多功能配置页签 第" + sonRow + "行的【标准销售加价】为空 请获取标准价" + "\r\n");
                        }
                        sonRow++;
                    }
                    row++;
                }
            }
            if (flag) {
                this.addErrorMessage(temp, info.toString());
            }
            if (flags) {
                this.addWarningMessage(temp,infos.toString());
            }

            DynamicObject billType = data.getDynamicObject("yem_billtype");
            String billTypeNumber = YEM.isNotEmpty(billType) ? billType.getString("number") : "";
            if ("yem_im_inclientdema_pj".equals(billTypeNumber) || "yem_im_clientdema_pj".equals(billTypeNumber)) {
                String s = TotalUtil.fundPoolAmtValidator(data, "yem_im_detailedinfor");
                if (YEM.isNotEmpty(s)) {
                    this.addErrorMessage(temp, s);
                }
            }

            // 外贸客户需求单_配件 不进行此校验
            if (!"yem_im_clientdema_pj".equals(billTypeNumber)) {
                //收汇金额校验
                rapAmtValidator(temp);
            }
            DynamicObject yem_currency = data.getDynamicObject("yem_currency");
            DynamicObject yem_currencybase = data.getDynamicObject("yem_currencybase");
            DynamicObject yem_internationalrate = data.getDynamicObject("yem_internationalrate");
            if (yem_currency != null && yem_currencybase != null) {
                if (yem_currency.getLong("id") != yem_currencybase.getLong("id")) {
                    if (yem_internationalrate == null) {
                        String msg = "结算币别不等于本位币，收款天数必录！";
                        this.addErrorMessage(temp, msg);
                    }
                }
            }
        }
    }


    /**
     * @description 收汇金额合计与表头金额（价格条款）是否一致校验
     * @author xwudd
     * @param[1] dataEntitie
     * return
     * @time 2023/11/9 16:14
     */

    public void rapAmtValidator(ExtendedDataEntity dataEntitie) {
        DynamicObject dataEntity = dataEntitie.getDataEntity();
        BigDecimal rapAmt = BigDecimal.ZERO;
        BigDecimal offerAmount = dataEntity.getBigDecimal("yem_offeramount");
        DynamicObjectCollection payPlans = dataEntity.getDynamicObjectCollection("yem_salescontrac_s");
        for (DynamicObject payPlan : payPlans) {
            rapAmt = rapAmt.add(payPlan.getBigDecimal("yem_rapamt"));
        }
        if (rapAmt.compareTo(offerAmount) != 0) {
            this.addErrorMessage(dataEntitie, String.format("客户需求单的报价金额{财务信息-报价金额（价格条款）{%s}跟其 收款计划-收汇金额合计{%s}不一致，请调整数据后重试", offerAmount, rapAmt));
        }
    }
}
