package com.yem.em.bd.etcmanualim.from;

import com.yem.em.utils.CommonEmUtils;
import kd.bos.dataentity.resource.ResManager;

import java.util.EventObject;
import java.util.List;

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.form.events.AfterDoOperationEventArgs;
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.AbstractOperate;
import kd.bos.list.ListShowParameter;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * @author 夜子
 * @ClassName EtcmanualImportEdit
 * @description: 设备电子目录导入
 * @date 2023年07月12日
 * @version: 1.0
 */
public class EtcmanualImportEdit extends AbstractBillPlugIn implements BeforeF7SelectListener {

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit yem_basemodelib = this.getView().getControl("yem_basemodelib");
        if (yem_basemodelib != null) {
            yem_basemodelib.addBeforeF7SelectListener(this);
        }
    }


    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        AbstractOperate operate = (AbstractOperate) args.getSource();
        String key = operate.getOperateKey();
        if (key.equals("partsimport") || key.equals("detailimport") || key.equals("etcimport")) {
            if (this.getView().getModel().getDataChanged()) {
                this.getView().showErrorNotification(ResManager.loadKDString("请先保存单据！", "EtcmanualImportEdit_0", "yem_equipment"));
                args.cancel = true;
            }
        }
        if (key.equals("partsimport") || key.equals("detailimport") ) {
            DynamicObject bill=  this.getModel().getDataEntity();
            if(bill.getString("yem_importstatus").equals("S")){
                this.getView().showMessage(ResManager.loadKDString("已完成导入，重复导入！", "EtcmanualImportEdit_1", "yem_equipment"));
                args.cancel = true;
            }
        }
        if (key.equals("partsimport") ) {
            DynamicObject bill=  this.getModel().getDataEntity();
            if(bill.getString("yem_importstatus").equals("N")){
                this.getView().showMessage(ResManager.loadKDString("部件已导入完成！", "EtcmanualImportEdit_2", "yem_equipment"));
                args.cancel = true;
            }
        }
        if (key.equals("detailimport") ) {
            DynamicObject bill=  this.getModel().getDataEntity();
            if(bill.getString("yem_importstatus").equals("N")){
                this.getView().showMessage(ResManager.loadKDString("请先完成部件导入！", "EtcmanualImportEdit_3", "yem_equipment"));
                args.cancel = true;
            }
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        String operateKey = e.getOperateKey();
        if ("partsimport".equals(operateKey) || "detailimport".equals(operateKey)) {
            this.getView().invokeOperation("refresh");
        }
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String key = e.getProperty().getName();
        DynamicObject bill =   this.getModel().getDataEntity(true);
        ListShowParameter ListshowParameter = (ListShowParameter) e.getFormShowParameter();
        if (key.equals("yem_basemodelib")) {
            List<Long> UsebasemodelibIDl =  CommonEmUtils.getUseEtcmanualimport(bill.getLong("id"));
            UsebasemodelibIDl.addAll(CommonEmUtils.getUseEtcmanualist(0L,true)) ;
            QFilter q1 =  new QFilter("id", QFilter.not_in, UsebasemodelibIDl);
            ListshowParameter.getListFilterParameter().setFilter(q1);
        }
    }
}
