package com.yem.wm.bc.change.list;

import com.yem.wm.bc.change.business.ChangeModelHelper;
import com.yem.wm.bc.change.common.consts.ChangeResumeFormConst;
import com.yem.wm.bc.common.CommonUtils;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.datamodel.ListSelectedRowCollection;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.SetFilterEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;

import java.util.List;

/**
 * @author 夜子
 * @ClassName XBillChangeListPlugin
 * @description: TODO
 * @date 2023年08月03日
 * @version: 1.0
 */
public class XBillChangeListPlugin extends AbstractListPlugin {
    public void setFilter(SetFilterEvent e) {
        super.setFilter(e);
        List<QFilter> listFilter = e.getQFilters();
        QFilter f = new QFilter("changestatus", "!=", "D");
        listFilter.add(f);
    }

    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate operate = (FormOperate)args.getSource();
        ListSelectedRowCollection listSelectRows = args.getListSelectedData();
        if (!this.isListSelectedDataNull(listSelectRows)) {
            if (listSelectRows.size() > 0) {
                switch (operate.getOperateKey()) {
                    case "submit":
                    case "unsubmit":
                    case "audit":
                    case "unaudit":
                    case "bizvalid":
                         {
                            if (!CommonUtils.isNull(operate.getEntityId())) {
                                DynamicObject changeModel = ChangeModelHelper.getChangeModel4XBill(operate.getEntityId());
                                if (changeModel == null) {
                                    this.getView().showTipNotification(ResManager.loadKDString("当前单据没有启用的变更模型，请联系开发服务人员维护变更模型数据。", "SrcBillBizChangeOpValidator_2", "yem-yc-changemodel"));
                                    args.setCancel(true);
                                    break ;
                                }
                            }

                            FormShowParameter showParameter = this.getView().getFormShowParameter();
                            String enableChangeResume = (String)showParameter.getCustomParam(ChangeResumeFormConst.ENABLE_CHANGERESUME);
                            if (CommonUtils.isNull(enableChangeResume)) {
                                enableChangeResume = this.getPageCache().get(ChangeResumeFormConst.ENABLE_CHANGERESUME);
                                if (CommonUtils.isNull(enableChangeResume)) {
                                    enableChangeResume = "true";
                                    this.getPageCache().put(ChangeResumeFormConst.ENABLE_CHANGERESUME, enableChangeResume);
                                }
                            }

                            operate.getOption().setVariableValue(ChangeResumeFormConst.ENABLE_CHANGERESUME, enableChangeResume);
                        }
                }
            }

            if (operate.getOperateKey().equals("audit") && !this.isListSelectedDataNull(listSelectRows) && listSelectRows.getPrimaryKeyValues().length > 1) {
                DynamicObject changeModel = ChangeModelHelper.getChangeModel4XBill(operate.getEntityId());
                if (changeModel != null && "auto".equals(changeModel.getString("updatetype"))) {
                    this.getView().showTipNotification(ResManager.loadKDString("当前变更单据使用的变更模型设置为“审核即生效”，只能选择一条记录进行审核，请重新选择。", "XBillChangeListPlugin_0", "yem-yc-changemodel"));
                    args.setCancel(true);
                }
            }

        }
    }

    public void afterDoOperation(AfterDoOperationEventArgs args) {
        super.afterDoOperation(args);
        FormOperate operate = (FormOperate)args.getSource();
        if ("audit".equalsIgnoreCase(args.getOperateKey()) && args.getOperationResult().isSuccess()) {
            DynamicObject changeModel = ChangeModelHelper.getChangeModel4XBill(operate.getEntityId());
            if (changeModel != null && "auto".equals(changeModel.getString("updatetype"))) {
                this.getView().invokeOperation("bizvalid");
            }
        } else {
            List<Object> successbillids;
            if (operate.getOperateKey().equals("bizchangeresume")) {
                successbillids = args.getOperationResult().getSuccessPkIds();
                if (successbillids.size() > 1) {
                    this.getView().showTipNotification(ResManager.loadKDString("仅支持单张单据查看变更履历，请选择1张单据。", "SrcBillChangeListPlugin_3", "yem-yc-changemodel"));
                } else if (successbillids.size() == 1) {
                    long xBillID = (Long)successbillids.get(0);
                    QFilter qFilter = new QFilter("xbillid", "=", xBillID);
                    QFilter qFilter2 = new QFilter("currentchangenumber", "!=", 0);
                    DynamicObject changerecord = BusinessDataServiceHelper.loadSingleFromCache("yem_changeresume", new QFilter[]{qFilter, qFilter2});
                    if (changerecord != null) {
                        FormShowParameter changeResume = new FormShowParameter();
                        changeResume.setFormId("yem_changeresumeform");
                        changeResume.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                        changeResume.setStatus(OperationStatus.EDIT);
                        changeResume.setCustomParam("srcbillid", changerecord.get("srcbillid"));
                        changeResume.setCustomParam("srcbillno", changerecord.get("srcbillno"));
                        changeResume.setCustomParam("xbillid", xBillID);
                        this.getView().showForm(changeResume);
                    } else {
                        this.getView().showTipNotification(ResManager.loadKDString("没有实际录入变更数据，暂未记录变更履历信息。", "XBillChangeListPlugin_2", "yem-yc-changemodel"));
                    }
                }
            }
        }

    }

    private boolean isListSelectedDataNull(ListSelectedRowCollection listSelectRows) {
        return listSelectRows == null || listSelectRows.size() < 1;
    }
}
