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

import com.yem.wm.utils.DynamicObjectUtil;
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.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.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

/**
 * @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 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) {
                    Date yemYearE = dynamicObject.getDate("yem_year_e");
                    if (yemYearE != null) {
                        extracted(dynamicObject, "A", dataEntity);
                    }
                }
                SaveServiceHelper.update(dataEntity);
                view.invokeOperation("refresh");
            }

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

    private static void extracted(DynamicObject dynamicObject, String B, DynamicObject dataEntity) {
        long id = dynamicObject.getLong("id");
        dynamicObject.set("yem_creditstatus_e", B);
        String billno = dataEntity.getString("billno");
        String selectfield = DynamicObjectUtil.getSelectfields("yem_im_csrcreditline", false);
        String selectfields = DynamicObjectUtil.getEntrySelectfields(selectfield, "yem_im_csrcreditline", "yem_im_info", false);
        DynamicObject yemImCsrcreditline = BusinessDataServiceHelper.loadSingle("yem_im_csrcreditline", selectfields
                , new QFilter[]{new QFilter("yem_im_info.yem_storagetransno.billno", QCP.equals, billno)});
        if (yemImCsrcreditline != null) {
            DynamicObjectCollection yemImInfo = yemImCsrcreditline.getDynamicObjectCollection("yem_im_info");
            if (YEM.isNotEmpty(yemImInfo)) {
                for (DynamicObject dynamicObjects : yemImInfo) {
                    Long yemStoragetransno = dynamicObjects.getLong("yem_storagetranentryid");
                    if (yemStoragetransno == id) {
                        dynamicObjects.set("yem_creditstatus_e", B);
                    }
                }
            }
            SaveServiceHelper.update(yemImCsrcreditline);
        }
    }

}
