package com.yem.wm.im.debcrednote.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.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.List;

/**
 * @author 张立攀
 * @version 1.0
 * @description
 * @date 2023-08-09 14:17
 */
public class DebcrednoteValidator  extends AbstractValidator {

    @Override
    public void validate() {
        ExtendedDataEntity[] datas = this.getDataEntities();
        for (ExtendedDataEntity temp : datas) {
            DynamicObject dataEntity = temp.getDataEntity();
            DynamicObject yemCredittype = dataEntity.getDynamicObject("yem_credittype");
            DynamicObject sourcecreditno = dataEntity.getDynamicObject("yem_sourcecreditno");
                DynamicObject customer = dataEntity.getDynamicObject("yem_customer");
                DynamicObject products = dataEntity.getDynamicObject("yem_bd_products");
                BigDecimal amount_sub = dataEntity.getBigDecimal("yem_amount_sub");
                BigDecimal yem_amount = dataEntity.getBigDecimal("yem_amount");
            DynamicObjectCollection yem_creditnotesource =  dataEntity.getDynamicObjectCollection("yem_creditnotesource");
                if (customer != null && products != null) {
                    List<QFilter> list = new ArrayList<>();
                    list.add(new QFilter("yem_customer.id", QCP.equals, customer.getLong("id")));
                    list.add(new QFilter("billstatus", QCP.equals, "C"));
                    list.add(new QFilter("yem_bd_products.id", QCP.equals, products.getPkValue()));
                    DynamicObject loadColl = BusinessDataServiceHelper.loadSingle("yem_spcapitalpool", "yem_spcapitalpool_o.yem_origintype", list.toArray(new QFilter[0]));
                    if (loadColl != null) {
                        loadColl = BusinessDataServiceHelper.loadSingle(loadColl.getPkValue(), loadColl.getDynamicObjectType().getName());
                        DynamicObjectCollection spcapitalpool_o = loadColl.getDynamicObjectCollection("yem_spcapitalpool_o");
                        if(spcapitalpool_o.size() > 0){
                            if(sourcecreditno == null && yem_creditnotesource.size() ==0){
                                this.addMessage(temp,"来源贷记单号未维护");
                                break;
                            }
                        }
//                        if(amount_sub.compareTo(yem_amount) >0){
//                            this.addMessage(temp,"配件资金池扣减金额不允许大于配件资金池增加金额");
//                            break;
//                        }
                        for(DynamicObject col :spcapitalpool_o){
                            String sourcecreditno1 = col.getString("yem_originno");
//                            String sourcecreditno1 = "";
//                            if(sourcecreditnoDy != null){
//                                sourcecreditno1 = col.getString("billno");
//                            }

                            BigDecimal originno = col.getBigDecimal("yem_residueavaamt");
                        if(!YEM.isEmpty(originno) && YEM.isNotEmpty(sourcecreditno) && YEM.isNotEmpty(sourcecreditno1) && sourcecreditno1.equals(sourcecreditno.getString("billno"))){
//                                BigDecimal residueavaamt = loadColl.getBigDecimal("yem_amount_sub");
                                boolean flag = true;
                                if(yemCredittype != null){
                                    String string = yemCredittype.getString("name");
                                    if("服务维修".equals(string)){
                                        flag = false;
                                    }
                                }
                                if(amount_sub.compareTo(originno) > 0 && flag){
                                    this.addErrorMessage(temp,"资金池可用额度不足，请确认!");
                                }
                            }
                        }
                    }

            }
        }
    }}