package com.yem.wm.im.giftapply.from;

import com.yem.wm.bc.common.CommonUtils;
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.entity.datamodel.IDataModel;
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.field.BasedataEdit;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.mvc.bill.BillView;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.EventObject;

/**
 * @author power
 * @ClassName
 * @description: 表单插件
 * @date 2023/7/28
 */
public class GiftApplyEdit extends AbstractBillPlugIn implements BeforeF7SelectListener {
    @Override
    public void afterCreateNewData(EventObject e) {

        setBillType();
        bringDept();
    }

    /**
     * @Description 根据布局给
     */
    public void setBillType() {

        IFormView view = this.getView();
        IDataModel model = this.getModel();
        String key = ((BillView) this.getView()).getRootControl().getKey();
        QFilter qFilter = null;
        if (key.equals("yem_im_giftapply_ed")) {
            qFilter = new QFilter("number", QCP.equals, "yem_im_giftapply_ED");
        } else if (key.equals("yem_im_giftapply_tk")) {
            qFilter = new QFilter("number", QCP.equals, "yem_im_giftapply_TK");
        } else if (key.equals("yem_im_giftapply_ly")) {
            qFilter = new QFilter("number", QCP.equals, "yem_im_giftapply_LY");
        }
        if (qFilter != null) {
            DynamicObject billType = BusinessDataServiceHelper.loadSingle("bos_billtype", "id", qFilter.toArray());
            model.setValue("yem_billtype", billType);
        }
    }

    @Override
    public void propertyChanged(PropertyChangedArgs e) {
        super.propertyChanged(e);
        String name = e.getProperty().getName();
        switch (name) {
            // 根据申请人携带部门
            case "yem_proposer":
                bringDept();
            default:
                break;
        }

    }

    /**
     * 根据申请人携带所属部门
     */
    private void bringDept() {
        DynamicObject dataEntity = this.getModel().getDataEntity();
        long applyUser = dataEntity.getLong("yem_proposer.id");
        if (applyUser > 0) {
            DynamicObject mainUserdep = CommonUtils.getMainUserdep(applyUser);
            if (mainUserdep != null) {
                this.getModel().setValue("yem_department", mainUserdep.getLong("id"));
            } else {
                this.getModel().setValue("yem_department", null);
            }
        } else {
            this.getModel().setValue("yem_department", null);
        }
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        BasedataEdit costitem = this.getControl("yem_costitem");
        BasedataEdit genera = this.getControl("yem_generalasstacttype");
        costitem.addBeforeF7SelectListener(this);
        genera.addBeforeF7SelectListener(this);
        BasedataEdit reqstorageorgunit = this.getControl("yem_reqstorageorgunit");
        BasedataEdit companyorg = this.getControl("yem_companyorg");
        BasedataEdit provideunit = this.getControl("yem_provideunit");
        reqstorageorgunit.addBeforeF7SelectListener(this);
        companyorg.addBeforeF7SelectListener(this);
        provideunit.addBeforeF7SelectListener(this);
    }

    @Override
    public void beforeF7Select(BeforeF7SelectEvent e) {
        String name = e.getProperty().getName();
        if ("yem_costitem".equals(name)) {
            DynamicObject object = BusinessDataServiceHelper.loadSingle("yem_genasstypegroup", new QFilter[]{new QFilter("number", QCP.equals, "H")});
            if (object != null) {
                long id = object.getLong("id");
                e.addCustomQFilter(new QFilter("yem_group.id", QCP.equals, String.valueOf(id)));
            }
        }

        if ("yem_generalasstacttype".equals(name)) {
            DynamicObject object = BusinessDataServiceHelper.loadSingle("yem_genasstypegroup", new QFilter[]{new QFilter("number", QCP.equals, "A")});
            if (object != null) {
                long id = object.getLong("id");
                e.addCustomQFilter(new QFilter("yem_group.id", QCP.equals, String.valueOf(id)));
            }
        }
        // 供应方库存组织                               供应方财务组织                      需求方库存组织
        if ("yem_reqstorageorgunit".equals(name) || "yem_companyorg".equals(name) || "yem_provideunit".equals(name)) {
            DynamicObject value = (DynamicObject) this.getModel().getValue("org");
            e.addCustomQFilter(OrgFilterUtils.getOrgFilter(value));
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        IFormView view = this.getView();
        String operateKey = e.getOperateKey();
        OperationResult operationResult = e.getOperationResult();
        switch (operateKey) {
            case "audit":
                if (YEM.isNotEmpty(operationResult)) {
                    view.invokeOperation("refresh");
                }
                break;
            case "unaudit":
                if (YEM.isNotEmpty(operationResult)) {
                    view.invokeOperation("refresh");
                }
                break;
        }
    }
}
