package com.yem.wm.fm.gathering.from;

import com.yem.wm.utils.OrgFilterUtils;
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.form.control.events.RowClickEventListener;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.form.operate.FormOperate;

import java.math.BigDecimal;
import java.util.EventObject;

/**
 * @author power
 * @ClassName
 * @description: 服务表单插件
 * @date 2023/8/24
 */
public class GatheringsEdit extends AbstractBillPlugIn implements RowClickEventListener, BeforeF7SelectListener
{
    @Override
    public void registerListener(EventObject e)
    {
        // TODO Auto-generated method stub
        super.registerListener(e);
        this.addClickListeners("yem_financeyetaffirm", "yem_financeunaffirm");
        BasedataEdit yem_org = this.getControl("yem_org");
        yem_org.addBeforeF7SelectListener(this);
    }


    @Override
    public void beforeF7Select(BeforeF7SelectEvent e)
    {
        String key = e.getProperty().getName();
        // 财务组织
        if ("yem_org".equals(key)) {
            DynamicObject value = (DynamicObject) this.getModel().getValue("org");
            e.addCustomQFilter(OrgFilterUtils.getOrgFilter(value));
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e)
    {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        ChangeData cd = e.getChangeSet()[0];
        ChangeData[] changeData = e.getChangeSet();
        int row = changeData[0].getRowIndex();
        switch (name) {
            case "yem_comegoman":
                this.comegoManBand();
                break;
            case "yem_moneynature":
            case "yem_salcontractclaamt":
                checkFiledValue();
                break;
            case "yem_claimamt":
                checkInputOrder();
                break;
            default:
                break;
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args)
    {
        super.beforeDoOperation(args);
        String operateKey = ((FormOperate) args.getSource()).getOperateKey();
        if ("save".equals(operateKey)) {
            checkFiledValue();
        }
    }

    /**
     * 需要维护出运明细单号然后才能维护收款认领金额
     */
    private void checkInputOrder()
    {
        IDataModel model = this.getModel();
        DynamicObject dataEntity = model.getDataEntity(true);
        int rowIdx = model.getEntryCurrentRowIndex("yem_receiptdetail");
        DynamicObject entry = dataEntity.getDynamicObjectCollection("yem_receiptdetail").get(rowIdx);
        DynamicObjectCollection collection = entry.getDynamicObjectCollection("yem_claimdetail");
        int rowIndex = model.getEntryCurrentRowIndex("yem_claimdetail");
        DynamicObject dynamicObject = collection.get(rowIndex);

        String moneynature = dynamicObject.getString("yem_moneynature");

        if (!"D".equals(moneynature)) {
            // 出运明细单号
            DynamicObject shippingdetailsno = dynamicObject.getDynamicObject("yem_shippingdetailsno");

            if (shippingdetailsno == null) {
                this.getView().showErrorNotification("请先维护出运明细单号");
                model.setValue("yem_claimamt", BigDecimal.ZERO);
                model.setValue("yem_claimamtbase", BigDecimal.ZERO);
            }
        }
    }

    /**
     * 如果不等于下单预收款，，，t切合同认领金额不能超过出运金额
     */
    private void checkFiledValue()
    {
        IDataModel model = this.getModel();
        DynamicObject dataEntity = model.getDataEntity(true);
        int rowIdx = model.getEntryCurrentRowIndex("yem_receiptdetail");
        DynamicObjectCollection receiptdetail = dataEntity.getDynamicObjectCollection("yem_receiptdetail");
        if (receiptdetail.isEmpty()) return;
        DynamicObject entry = receiptdetail.get(rowIdx);
        DynamicObjectCollection collection = entry.getDynamicObjectCollection("yem_claimdetail");
        if (collection.isEmpty()) return;
        for (DynamicObject dynamicObject : collection) {
            int index = collection.indexOf(dynamicObject);
            String moneynature = dynamicObject.getString("yem_moneynature");
            if (!"D".equals(moneynature)) {
                // 合同认领金额
                BigDecimal salcontractclaamt = dynamicObject.getBigDecimal("yem_salcontractclaamt");
                // 出运金额
                BigDecimal shipamt = dynamicObject.getBigDecimal("yem_shipamt");

                if (YEM.isNotEmpty(shipamt)) {
                    if (salcontractclaamt.compareTo(shipamt) > 0) {
                        this.getView().showErrorNotification("认领明细第 " + (index + 1) + " 行,合同认领金额不能超过出运金额");
                    }
                }
            }
        }
    }

    @Override
    public void afterCreateNewData(EventObject e)
    {
        super.afterCreateNewData(e);
    }

    /**
     * 客户 带出 付款账户
     */
    private void comegoManBand()
    {
        //获取收款单 客户
        DynamicObject yem_comegoman = (DynamicObject) this.getModel().getValue("yem_comegoman");
        if (yem_comegoman != null) {
            DynamicObjectCollection entry = (DynamicObjectCollection) yem_comegoman.get("entry_bank");
            int i = 0;
            String bankaccount = null;
            String accountname = null;
            String bank = null;
            Boolean isdefault_bank = null;
            for (DynamicObject dynamicObject : entry) {
                //获取银行账号
                bankaccount = dynamicObject.getString("bankaccount");
                //获取账户名称
                accountname = dynamicObject.getString("accountname");
                //获取开户银行
                bank = dynamicObject.getString("bank.name");
                //获取币别
                isdefault_bank = dynamicObject.getBoolean("isdefault_bank");
                i = i + 1;
                if (isdefault_bank) {
                    this.getModel().setValue("yem_paymentact", bankaccount);
                    this.getModel().setValue("yem_paymentname", accountname);
                    this.getModel().setValue("yem_paymentbank", bank);
                    break;
                }
            }
            if (i == 1) {
                this.getModel().setValue("yem_paymentact", bankaccount);
                this.getModel().setValue("yem_paymentname", accountname);
                this.getModel().setValue("yem_paymentbank", bank);
            }
        }

    }
}
