package com.yem.wm.sys.formplugin.list;

import java.util.EventObject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yem.wm.sys.formplugin.form.AbstractVersionBillPlugIn;
import com.yem.wm.sys.utils.NewVersionHelper;

import com.yem.wm.utils.StringUtils;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.datamodel.IDataModel;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.FormShowParameter;
import kd.bos.form.IFormView;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

/**
 * 版本变更功能基类
 *
 * @author longh
 * @description:列表插件
 * @date 2021年11月17日 上午11:42:08
 */
public class AbstractVersionListPlugin extends AbstractListPlugin {

    //定义变更功能的列表可见控件标识
    private static final String[] VCTRLS = {"yem_versionhistory", "yem_version"};
    //定义变更功能的仅需求单据列表可见控件标识
    private static final String[] S = {"yem_newversion"};
    //定义变更功能的仅变更单据列表可见控件标识
    private static final String[] V = {"yem_effective"};
    //定义变更功能的仅变更单据列表不可见控件标识
    private static final String[] UNV = {"tblnew", "tblprint", "tblpush", "tbltrack", "yem_push", "yem_bizaction", "yem_track"};

    @Override
    public void afterBindData(EventObject e) {
        super.afterBindData(e);
        this.initNewVersionSetting();
    }

    /**
     * 设置新版本界面字段属性
     */
    private void initNewVersionSetting() {
        IFormView view = this.getView();
        FormShowParameter formShowParameter = view.getFormShowParameter();
        JSONObject formId = JSON.parseObject(formShowParameter.toString());
        String entityNumber = formId.getString("BillFormId");
        DynamicObject tplObj = NewVersionHelper.queryTemplate(entityNumber);
        //设置配置变更功能的单据列表控件可见性
        view.setVisible(false, "yem_effectchange");
        if (tplObj != null) {
            String yem_requirebill = tplObj.getString("yem_requirebill.number");
            String yem_changebill = tplObj.getString("yem_changebill.number");
            if (entityNumber.equals(yem_requirebill)) {
                //设置控件可见性（非变更单）
                view.setVisible(false, V);
            } else if (entityNumber.equals(yem_changebill)) {
                //设置控件可见性（变更单）
                view.setVisible(false, S);
                view.setVisible(false, UNV);
                String effectOption = tplObj.getString("yem_effectoption");
                if (!"audit".equals(effectOption)) {
                    view.setVisible(true, "yem_effectchange");
                }
            }
        }//设置未配置变更功能的单据列表控件可见性
        else {
            view.setVisible(false, S);
            view.setVisible(false, V);
            view.setVisible(false, VCTRLS);
        }
    }

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs e) {
        super.afterDoOperation(e);
        IFormView view = this.getView();
        String operateKey = e.getOperateKey();
        switch (operateKey) {
            case "newversion":
                AbstractVersionBillPlugIn.viewNewVersion(e, view);
                break;
            case "versionhistory":
                AbstractVersionBillPlugIn.viewVersionHistory(e, view);
                break;
            default:
                break;
        }
    }

    @Override
    public void beforeDoOperation(BeforeDoOperationEventArgs e) {
        super.beforeDoOperation(e);
        FormOperate operate = (FormOperate) e.getSource();
        String OperateKey = operate.getOperateKey();
        DynamicObject dataEntity = this.getModel().getDataEntity(true);
        IFormView view = this.getView();
        IDataModel model = this.getModel();
        ListSelectedRowCollection listSelectedData = e.getListSelectedData();
        if ("push".equals(OperateKey)) {
            FormShowParameter formShowParameter = view.getFormShowParameter();
            JSONObject formId = JSON.parseObject(formShowParameter.toString());
            String BillFormId = formId.getString("BillFormId");
            String entityNumber = "";
            if (BillFormId.equals("yem_es_salesorder_zj") || BillFormId.equals("yem_es_salesorder_pj")
                    || BillFormId.equals("yem_es_salesorder_nmzj") || BillFormId.equals("yem_es_salesorder_nmpj")) {
                entityNumber = "yem_es_salesorder";
            }
            if (BillFormId.equals("yem_shippingdetails_zj") || BillFormId.equals("yem_shippingdetails_pj")
                    || BillFormId.equals("yem_shippingdetails_nmzj") || BillFormId.equals("yem_shippingdetails_nmpj")) {
                entityNumber = "yem_shippingdetails";
            }
            if (BillFormId.equals("yem_es_storagetrans")) {
                entityNumber = BillFormId;
            }
            if (entityNumber.length() > 0) {
                for (ListSelectedRow SelectedData : listSelectedData) {
                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(SelectedData.getPrimaryKeyValue(), entityNumber);
                    String billNo = SelectedData.getBillNo();
                    boolean yem_changing = dynamicObject.getBoolean("yem_changing");
                    if (yem_changing) {
                        this.getView().showErrorNotification("单据【" + billNo + "】正在变更中，禁止下推！");
                        e.setCancel(true);
                    }
                }
            }
        }
    }
}
