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


import java.util.List;

import com.yem.wm.bc.change.business.ChangeModelHelper;
import com.yem.wm.bc.change.common.consts.ChangeResumeFormConst;
import com.yem.wm.bc.common.enums.StatusEnum;
import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.RefObject;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.form.ConfirmCallBackListener;
import kd.bos.form.ConfirmTypes;
import kd.bos.form.FormShowParameter;
import kd.bos.form.MessageBoxOptions;
import kd.bos.form.MessageBoxResult;
import kd.bos.form.ShowType;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.form.events.BeforeDoOperationEventArgs;
import kd.bos.form.events.MessageBoxClosedEvent;
import kd.bos.form.operate.FormOperate;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.form.plugin.IFormPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.util.StringUtils;

/**
 * @author 夜子
 * @ClassName SrcBillChangePlugin
 * @description: 变更源单表单插件
 * @date 2023年08月03日
 * @version: 1.0
 */
public class SrcBillChangePlugin extends AbstractFormPlugin {
    public static Log logger = LogFactory.getLog(SrcBillChangePlugin.class);
    private static final String OPPARAM_AFTERCONFIRM = "afterconfirm";

    public void beforeDoOperation(BeforeDoOperationEventArgs args) {
        super.beforeDoOperation(args);
        FormOperate operate = (FormOperate) args.getSource();
        if (operate.getOperateKey().equals("bizchange")) {
            RefObject afterConfirm = new RefObject();
            if (!operate.getOption().tryGetVariableValue("afterconfirm", afterConfirm)) {
                this.getPageCache().put(ChangeResumeFormConst.ENABLE_CHANGERESUME, "true");
                ConfirmCallBackListener confirmCallBacks = new ConfirmCallBackListener("bizchange", (IFormPlugin) this);
                String confirmTip = ResManager.loadKDString((String) "是否确认要对当前单据进行变更?",
                        (String) "SrcBillChangePlugin_1", (String) "yem-yc-changemodel", (Object[]) new Object[0]);
                this.getView().showConfirm(confirmTip, MessageBoxOptions.YesNo, ConfirmTypes.Default, confirmCallBacks);
                args.setCancel(true);
            }
        } else if (operate.getOperateKey().equals("bizchangeresume")) {
            QFilter qFilter2;
            long srcbillid = (Long) this.getModel().getValue("id");
            QFilter qFilter = new QFilter("srcbillid", "=", (Object) srcbillid);
            DynamicObject changerecord = BusinessDataServiceHelper.loadSingleFromCache((String) "yem_changeresume",
                    (QFilter[]) new QFilter[]{qFilter,
                            qFilter2 = new QFilter("currentchangenumber", "!=", (Object) 0)});
            if (changerecord != null) {
                FormShowParameter changeResume = new FormShowParameter();
                changeResume.setFormId("yem_changeresumeform");
                changeResume.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                changeResume.setStatus(OperationStatus.EDIT);
                changeResume.setCustomParam("srcbillid", (Object) srcbillid);
                changeResume.setCustomParam("srcbillno", this.getModel().getValue("billno"));
                this.getView().showForm(changeResume);
            } else {
                this.getView()
                        .showTipNotification(ResManager.loadKDString((String) "没有实际录入变更数据，暂未记录变更履历信息。",
                                (String) "XBillChangeListPlugin_2", (String) "yem-yc-changemodel",
                                (Object[]) new Object[0]));
                args.setCancel(true);
            }
        }
    }

    public void confirmCallBack(MessageBoxClosedEvent evt) {
        super.confirmCallBack(evt);
        if (evt.getCallBackId().equals("bizchange") && evt.getResult() == MessageBoxResult.Yes) {
            OperateOption operateOption = OperateOption.create();
            operateOption.setVariableValue("afterconfirm", "true");
            operateOption.setVariableValue(ChangeResumeFormConst.ENABLE_CHANGERESUME,
                    this.getPageCache().get(ChangeResumeFormConst.ENABLE_CHANGERESUME));
            this.getView().invokeOperation("bizchange", operateOption);
        }
    }

    public void afterDoOperation(AfterDoOperationEventArgs args) {
        super.afterDoOperation(args);
        FormOperate operate = (FormOperate) args.getSource();
        if (args.getOperationResult() != null && args.getOperationResult().isSuccess()
                && operate.getOperateKey().equals("bizchange")) {
            OperationResult opRes = args.getOperationResult();
            List billPks = opRes.getSuccessPkIds();
            String srcBillEntity = this.getModel().getDataEntityType().getName();
            if (!billPks.isEmpty()) {
                DynamicObject changeModel;
                String xBillEntityID = operate.getOption().getVariableValue("xBillEntityID", "");
                if (StringUtils.isEmpty((String) xBillEntityID)
                        && (changeModel = ChangeModelHelper.getChangeModel4SrcBill((String) srcBillEntity)) != null
                        && changeModel.getDynamicObject("xbill") != null) {
                    xBillEntityID = changeModel.getDynamicObject("xbill").getString("id");
                }
                for (Object billPk : billPks) {
                    QFilter q3;
                    QFilter q2;
                    QFilter q1 = new QFilter("sourcebillid", "=", billPk);
                    DynamicObject xbill = QueryServiceHelper.queryOne((String) xBillEntityID, (String) "id",
                            (QFilter[]) new QFilter[]{q1,
                                    q2 = new QFilter("billstatus", "=", (Object) StatusEnum.SAVE.getValue()),
                                    q3 = new QFilter("changestatus", "!=", (Object) "D")});
                    if (xbill == null)
                        continue;
                    BillShowParameter fsp = new BillShowParameter();
                    fsp.setFormId(xBillEntityID);
                    fsp.getOpenStyle().setShowType(ShowType.MainNewTabPage);
                    fsp.setPkId(xbill.get("id"));
                    fsp.setCustomParam(ChangeResumeFormConst.ENABLE_CHANGERESUME,
                            (Object) this.getPageCache().get(ChangeResumeFormConst.ENABLE_CHANGERESUME));
                    this.getView().showForm((FormShowParameter) fsp);
                }
            }
        }
    }
}