package shkd.repc.recon.opplugin;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
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;

public class ContractOPPlugin extends AbstractOperationServicePlugIn {
    private static final Log logger = LogFactory.getLog(ContractOPPlugin.class);

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        super.onAddValidators(e);
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                String operateKey = this.getOperateKey();
                ExtendedDataEntity[] dataEntities1 = this.getDataEntities();
                switch (operateKey) {
                    case "submit":
                        for (ExtendedDataEntity extendedDataEntity : dataEntities1) {
                            //保证金明细
                            DynamicObject dataEntity = extendedDataEntity.getDataEntity();
                            DynamicObjectCollection qeugBondentrys;
                            try {
                                qeugBondentrys = dataEntity.getDynamicObjectCollection("qeug_bondentry");
                            }catch (Exception e){
                                dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(),"recon_contractbill");
                                qeugBondentrys = dataEntity.getDynamicObjectCollection("qeug_bondentry");
                            }
                            BigDecimal qeugMarginlevel = BigDecimal.ZERO;
                            if (!qeugBondentrys.isEmpty()) {
                                for (DynamicObject qeugBondentry : qeugBondentrys) {
                                    qeugMarginlevel = qeugMarginlevel.add(qeugBondentry.getBigDecimal("qeug_marginlevel"));
                                }
                            }
                            if (qeugMarginlevel.compareTo(BigDecimal.valueOf(100)) > 0) {
                                this.addErrorMessage(extendedDataEntity, "保证金明细:保证金比例(%)之和不能大于100");
                            }
                            //合同清单(定额)
                            DynamicObjectCollection invoiceentryinfos;
                            try {
                                //合同监控列表点击提交会提示找不到:qeug_invoiceentryinfo属性
                                invoiceentryinfos = dataEntity.getDynamicObjectCollection("qeug_invoiceentryinfo");
                                if (!invoiceentryinfos.isEmpty()) {
                                    for (DynamicObject invoiceentryinfo : invoiceentryinfos) {
                                        BigDecimal qeug_amount = invoiceentryinfo.getBigDecimal("qeug_amount");
                                    }
                                }
                            }catch (Exception e){
                                dataEntity = BusinessDataServiceHelper.loadSingle(dataEntity.getPkValue(),"recon_contractbill");
                                invoiceentryinfos = dataEntity.getDynamicObjectCollection("qeug_invoiceentryinfo");
                            }
                            BigDecimal qeug_amounts = BigDecimal.ZERO;//金额(元)_合计
                            if (!invoiceentryinfos.isEmpty()) {
                                for (DynamicObject invoiceentryinfo : invoiceentryinfos) {
                                    qeug_amounts = qeug_amounts.add(invoiceentryinfo.getBigDecimal("qeug_amount"));
                                }
                            }
                            BigDecimal amount = dataEntity.getBigDecimal("amount");//合同金额
                            // 检查 qeug_amounts 是否大于 amount
                            if (qeug_amounts.setScale(2, RoundingMode.HALF_UP).compareTo(amount) > 0) {
                                this.addErrorMessage(extendedDataEntity, "合同清单(定额)的金额之和不能大于合同金额");
                            }
                            DynamicObjectCollection qeug_orderformentry1 = dataEntity.getDynamicObjectCollection("qeug_orderformentry");
                            int qeug_orderformentry = qeug_orderformentry1==null?0:qeug_orderformentry1.size();
                            DynamicObjectCollection qeug_invoiceentryinfo1 = dataEntity.getDynamicObjectCollection("qeug_invoiceentryinfo");
                            int qeug_invoiceentryinfo = qeug_invoiceentryinfo1==null?0:qeug_invoiceentryinfo1.size();
                            if(qeug_orderformentry+qeug_invoiceentryinfo<1){
                                this.addErrorMessage(extendedDataEntity, "合同清单不允许为空!");
                            }
                            QFilter qFilter = new QFilter("billno", QCP.equals, dataEntity.getString("billno"));
                            DynamicObject[] load = BusinessDataServiceHelper.load("recon_conpayplan", "contractbill", qFilter.toArray());
                            if (load.length!=0) {
                                Object pkValue = load[0].getPkValue();
                                DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(pkValue, "recon_conpayplan");
                                DynamicObjectCollection conpayplanschedule = dynamicObject.getDynamicObjectCollection("conpayplanschedule");
                                if(conpayplanschedule==null||conpayplanschedule.size()<1){
                                    this.addErrorMessage(extendedDataEntity, "付款计划不能为空,若已存在付款计划,请先保存");
                                }
                            }else{
                                this.addErrorMessage(extendedDataEntity, "付款计划不能为空,若已存在付款计划,请先保存");
                            }
                        }
                        break;
                    case "qeug_attatchment":
                        for (ExtendedDataEntity extendedDataEntity : dataEntities1) {
                            //合同正文(盖章件)不能为空
                            DynamicObject dataEntity = extendedDataEntity.getDataEntity();
                            DynamicObjectCollection attachmentpanel = dataEntity.getDynamicObjectCollection("attachmentpanel");
                            if (attachmentpanel.isEmpty()) {
                                this.addMessage(extendedDataEntity, "合同正文(盖章件)不能为空");
                            }
                        }
                        break;
                    default:
                        break;

                }
            }
        });
    }
}