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

import com.yem.wm.utils.DynamicObjectUtil;
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.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ljw
 * @date 2024/9/24 19:04
 * @description ExportPresentSubmitsValidator
 */
public class ExportPresentSubmitsValidator extends AbstractValidator {

    @Override
    public void validate() {
        ExtendedDataEntity[] dataEntities = this.getDataEntities();
        for (ExtendedDataEntity data : dataEntities) {
            DynamicObject dataEntity = data.getDataEntity();
            // 款项类型
            String paymenttype = dataEntity.getString("yem_paymenttype");
            if ("A".equals(paymenttype)) {
                arrearsVerification(dataEntity, data);
            }
        }
    }

    private void arrearsVerification(DynamicObject dataEntity, ExtendedDataEntity data) {
        DynamicObject yemCustomer = dataEntity.getDynamicObject("yem_customer");
        if (yemCustomer != null) {
            long aLong = yemCustomer.getLong("id");
            String selectfield = DynamicObjectUtil.getSelectfields("yem_es_storagetrans_xbs", false);
            String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_es_storagetrans_xbs", "yem_creditdetail", false);
            DynamicObject[] load = BusinessDataServiceHelper.load("yem_es_storagetrans_xbs", selectfields
                    , new QFilter[]{new QFilter("yem_customer.id", QCP.equals, aLong)});
            if (load.length > 0) {
                List<String> list = new ArrayList<>();
                for (DynamicObject dynamicObject : load) {
                    DynamicObjectCollection yemCreditdetail = dynamicObject.getDynamicObjectCollection("yem_creditdetail");
                    String dynamicObjectString = dynamicObject.getString("billno");
                    for (DynamicObject object : yemCreditdetail) {
                        BigDecimal yemCountpayback = object.getBigDecimal("yem_countpayback");
                        BigDecimal yemActualsinkamtCd = object.getBigDecimal("yem_actualsinkamt_cd");
                        BigDecimal yemPaymentamt1Cd = object.getBigDecimal("yem_paymentamt1_cd");
                        Date paydate1 = object.getDate("yem_paydate1");//应收款日期1
                        if (YEM.isNotEmpty(paydate1) && paydate1.before(new Date()) && yemActualsinkamtCd.compareTo(yemPaymentamt1Cd) < 0) {
                            DynamicObject yemSalecontractno = object.getDynamicObject("yem_salecontractno");
                            if (yemSalecontractno != null) {
                                String billno = yemSalecontractno.getString("billno");
                                list.add("当前寄单放单客户存在历史合同：[" + billno + "]，订舱通知单：[" + dynamicObjectString + "]，逾期未收款，请确认！！！\n");

                            }
                        }
                    }
                }
                if (list.size() > 0) {
                    this.addMessage(data, list.toString());
                }
            }

        }
    }

}
