package com.yem.wm.bd.companyinfo.from;

import kd.bos.bill.AbstractBillPlugIn;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.events.ChangeData;
import kd.bos.entity.datamodel.events.PropertyChangedArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * @author power
 * @ClassName Companyinfo
 * @description: 公司抬头表单插件
 * @date 2023年07月20日
 * @version: 1.0
 */
public class Companyinfo extends AbstractBillPlugIn {
    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        String propertyName = e.getProperty().getName();
        boolean stopClear;
        ChangeData cd = e.getChangeSet()[0];
        int rowIndex = cd.getRowIndex();
        Object newValue = cd.getNewValue();
        switch (propertyName) {
            case "yem_bd_accountbanks" :
                this.accountbanks(rowIndex);
                break;
            case "yem_default" :
                singleDefault(newValue,rowIndex);
                break;
            default:
                break;
        }
    }
    private void accountbanks(int row) {
        //银行账号
        DynamicObject yem_products = (DynamicObject)this.getModel().getValue("yem_bd_accountbanks");
        if (yem_products != null){
            long id =yem_products.getLong("bank.id");
            DynamicObject aCollection1 = BusinessDataServiceHelper.loadSingle("bd_finorginfo","address," +
                    "address_eng",new QFilter[] { new QFilter("id", QCP.equals, id)});
            this.getModel().setValue("yem_bankaddress",aCollection1.getString("address"),row);
            this.getModel().setValue("yem_bankaddressen",aCollection1.getString("address_eng"),row);
        }
        this.getModel().setValue("yem_bankaddress",null,row);
        this.getModel().setValue("yem_bankaddressen",null,row);
    }

    public void singleDefault(Object newValue,int rowIndex){
        IDataModel model = this.getModel();
        Boolean value = (Boolean)newValue;
        if(value){
            int bankInfoRows = model.getEntryRowCount("yem_bankinfo");
            for (int i = 0; i < bankInfoRows; i++) {
                if(i != rowIndex){
                    model.setValue("yem_default",false,i);
                }
            }
        }
    }

}
