package com.yem.wm.es.storagetrans.form;

import com.yem.wm.utils.YEM;
import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.dataentity.metadata.IDataEntityProperty;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.IFormView;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * @Description: 订舱通知单_信保回填
 * @Date: 2024/7/15 14:17
 * @Created: by ZZSLL
 */

public class StorageTransEdit_xbs extends AbstractBillPlugIn {

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        ChangeData changeData = e.getChangeSet()[0];
        int rowIndex = changeData.getRowIndex();
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        if ("yem_preamt_cd".equals(name) || "yem_stcreditpreamt_cd".equals(name)) {//系统预收金额 || 订舱预收金额
            BigDecimal preamt_cd = (BigDecimal) model.getValue("yem_preamt_cd", rowIndex);
            preamt_cd = preamt_cd.setScale(2, RoundingMode.HALF_UP);
            BigDecimal stcreditpreamt_cd = (BigDecimal) model.getValue("yem_stcreditpreamt_cd", rowIndex);
            stcreditpreamt_cd = stcreditpreamt_cd.setScale(2, RoundingMode.HALF_UP);
            if (stcreditpreamt_cd.compareTo(preamt_cd) < 0) {
                view.showErrorNotification(String.format("信保明细第 %s 行，订舱预收金额 %s 不允许小于系统预收金额 %s ！！", rowIndex + 1, stcreditpreamt_cd.setScale(2, RoundingMode.HALF_UP), preamt_cd.setScale(2, RoundingMode.HALF_UP)));
            }
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        FormOperate source = (FormOperate) e.getSource();
        String operateKey = source.getOperateKey();
        IDataModel model = this.getModel();
        IFormView view = this.getView();
        if ("creditconfirm".equals(operateKey)) {
            int rowCount = model.getEntryRowCount("yem_creditdetail");
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < rowCount; i++) {
                BigDecimal preamt_cd = (BigDecimal) model.getValue("yem_preamt_cd", i);
                BigDecimal stcreditpreamt_cd = (BigDecimal) model.getValue("yem_stcreditpreamt_cd", i);
                preamt_cd = preamt_cd.setScale(2, RoundingMode.HALF_UP);
                stcreditpreamt_cd = stcreditpreamt_cd.setScale(2, RoundingMode.HALF_UP);
                if (stcreditpreamt_cd.compareTo(preamt_cd) < 0) {
                    sb.append(String.format("信保明细第 %s 行，订舱预收金额 %s 不允许小于系统预收金额 %s ！！", i + 1, stcreditpreamt_cd.setScale(2, RoundingMode.HALF_UP), preamt_cd.setScale(2, RoundingMode.HALF_UP)));
                    sb.append("\n\r");
                }
                int creditdate1_cd = (int) model.getValue("yem_creditdate1_cd", i);//信保天数1
                BigDecimal paymentamt1_cd = (BigDecimal) model.getValue("yem_paymentamt1_cd", i);//应收款金额1
                BigDecimal paymentamt1_cdbase = (BigDecimal) model.getValue("yem_paymentamt1_cdbase", i);//应收款金额1（本位币）
                if (creditdate1_cd == 0) {
                    sb.append(String.format("信保明细第 %s 行，信保天数1必填！！\n\r", i + 1));
                }
                if (YEM.isEmpty(paymentamt1_cd)) {
                    sb.append(String.format("信保明细第 %s 行，应收款金额1必填！！\n\r", i + 1));
                }
                if (YEM.isEmpty(paymentamt1_cdbase)) {
                    sb.append(String.format("信保明细第 %s 行，应收款金额1（本位币）必填！！\n\r", i + 1));
                }

            }
            if (sb.length() > 0) {
                e.setCancel(true);
                view.showErrorNotification(sb.toString());
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        OperationResult result = e.getOperationResult();
        String operateKey = e.getOperateKey();
        IFormView view = this.getView();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        if (result != null && result.isSuccess()) {
            if ("statusconvert_s".equals(operateKey)) {
                DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_creditdetail");
                for (DynamicObject dynamicObject : collection) {
                    dynamicObject.set("yem_creditstatus_e", "A");
                }
                SaveServiceHelper.update(dataEntity);
                view.invokeOperation("refresh");
            }

            if ("statusconvert_un_s".equals(operateKey)) {
                DynamicObjectCollection collection = dataEntity.getDynamicObjectCollection("yem_creditdetail");
                for (DynamicObject dynamicObject : collection) {
                    dynamicObject.set("yem_creditstatus_e", "B");
                }
                SaveServiceHelper.update(dataEntity);
                view.invokeOperation("refresh");
            }
        }
    }
}
