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

import kd.bos.dataentity.entity.DynamicObject;
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;

/**
 * @author power
 * @ClassName ReceiveSubmitOpValidator
 * @description: 年度预算单-自定义校验器
 * @date 2023年07月26日
 * @version: 1.0
 */
public class ReceiveSubmitOpValidator extends AbstractValidator
{

    @Override
    public void validate()
    {
        ExtendedDataEntity[] entities = this.getDataEntities();
        for (ExtendedDataEntity entity : entities) {
            DynamicObject bill = entity.getDataEntity();

            String billType = bill.getString("yem_billtype.number");
            if (billType.equals("yem_im_giftapply_LY")) {

//                Long customer = bill.getLong("yem_customer.id");

                DynamicObject saleorderCustomer = bill.getDynamicObject("yem_customer");
                Long customer = null;//
                if (saleorderCustomer != null) {
                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(saleorderCustomer.getPkValue(), saleorderCustomer.getDynamicObjectType().getName());
                    if (dynamicObject != null) {
                        DynamicObject groupcusfields = dynamicObject.getDynamicObject("yem_groupcustomers");
                        if (groupcusfields  != null) {
                            customer = groupcusfields.getLong("id");
                        }
                    }
                }

                Long org = bill.getLong("org.id");
                Long yem_bd_products = bill.getLong("yem_bd_products.id");
                // 礼品资金池
                DynamicObject giftAmountsPool = BusinessDataServiceHelper.loadSingle("yem_im_giftamount",
                        "yem_giftuseable",
                        new QFilter[]{new QFilter("yem_groupcusfields.id", QCP.equals, customer),
                                new QFilter("org", QCP.equals, org),
                                new QFilter("yem_bd_products", QCP.equals, yem_bd_products)});

                if (giftAmountsPool != null) {
                    // 剩余可用金额（人民币）
                    BigDecimal giftAmount = giftAmountsPool.getBigDecimal("yem_giftuseable");

                    String customerName = bill.getString("yem_customer.name");
                    String productsName = bill.getString("yem_bd_products.name");
                    if (giftAmount.compareTo(BigDecimal.ZERO) <= 0) {
                        // 如果该礼品领用申请单对应的礼品资金池的剩余可用金额<=0,不允许提交
                        this.addErrorMessage(entity, "客户：" + customerName + "，产品分类：" + productsName + "的礼品资金池当前剩余额度为" + giftAmount.setScale(4, RoundingMode.HALF_UP) + "，不允许超额领用。");
                    } else {
                        // 剩余可用金额 + 礼品申请单产品分类的额度控制字段是否大于等于本次领用金额（财务信息的金额）
                        // 产品分类的额度控制字段（人民币）
                        BigDecimal products = bill.getBigDecimal("yem_bd_products.yem_quotacontrol");
                        // 财务信息的金额（人民币）
                        BigDecimal amount = bill.getBigDecimal("yem_amount");
                        if (giftAmount.add(products).compareTo(amount) < 0) {
                            this.addErrorMessage(entity, "剩余额度：" + giftAmount.setScale(4, RoundingMode.HALF_UP) + "，本次领用额度：" + amount + "，已超出额度控制金额" + products.setScale(4, RoundingMode.HALF_UP) + "，请确认！");
                        }
                    }
                } else {
                    this.addErrorMessage(entity, "当前客户没有礼品池额度，请先维护礼品池额度！");
                }
            }
        }
    }
}




