package nckd.jxccl.sit.hcsi.formplugin.web.tp.guide;

import kd.bos.context.RequestContext;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.container.Tab;
import kd.bos.form.control.events.TabSelectEvent;
import kd.bos.form.control.events.TabSelectListener;
import kd.bos.form.events.BeforeClosedEvent;
import kd.bos.form.operate.MutexHelper;
import kd.bos.list.ListShowParameter;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.mutex.DataMutex;
import kd.bos.orm.query.QFilter;
import kd.hr.hbp.common.cache.HRAppCache;
import kd.hr.hbp.common.cache.HRPageCache;
import kd.hr.hbp.common.cache.IHRAppCache;

import java.io.IOException;
import java.util.EventObject;
import java.util.Map;

/**
 * Tyx 2025-10-21
 * 外部数据导入向导界面插件 nckd_taskimpguide
 */
public class TaskImpGuideListPlugin extends AbstractListPlugin implements TabSelectListener {

    @Override
    public void registerListener(EventObject e) {
        Tab tabSteps = (Tab)this.getControl("tabap");
        tabSteps.addTabSelectListener(this);
    }

    public void beforeBindData(EventObject arg) {
        this.applyMutexLock();
        this.clearAppCache();
        Tab tabSteps = (Tab)this.getControl("tabap");
        tabSteps.activeTab("dataimport");
    }

    private void applyMutexLock() {
        Long importTaskId = (Long)this.getView().getFormShowParameter().getCustomParam("importTaskId");
        StringBuilder msg = new StringBuilder();
        boolean require = MutexHelper.require("nckd_sinsurimptask", importTaskId, "modify", true, msg);
        HRPageCache cache = new HRPageCache(this.getView());
        if (require) {
            cache.put("currentPageLocked", Boolean.TRUE);
        } else {
            cache.put("isLocked", Boolean.TRUE);
            this.getView().showTipNotification(msg.toString());
        }
    }

    @Override
    public void tabSelected(TabSelectEvent tabSelectEvent) {
        String tabKey = tabSelectEvent.getTabKey();
        this.getPageCache().put("tabKey", tabKey);
        switch (tabKey) {
            case "dataimport":
                this.loadChildrenPage("dataimport", "dataimportflex", "nckd_sinsurtempdata");
                this.getPageCache().put("targetKey", "dataimportflex");
                break;
            case "datacompare":
                this.loadChildrenPage("datacompare", "dataimportflex1", "nckd_detailcomparebill");
                this.getPageCache().put("targetKey", "dataimportflex1");
                break;
        }
    }

    private void loadChildrenPage(String tabKey, String targetKey, String entityName) {
        HRPageCache pageCache = new HRPageCache(this.getView());
        Boolean isLocked = (Boolean)pageCache.get("isLocked", Boolean.class);
        FormShowParameter parameter = this.getView().getFormShowParameter();
        ListShowParameter showParameter = new ListShowParameter();
        showParameter.getOpenStyle().setShowType(ShowType.InContainer);
        showParameter.getOpenStyle().setTargetKey(targetKey);
        showParameter.setBillFormId(entityName);
        showParameter.setFormId("bos_list");
        showParameter.setLookUp(false);
        showParameter.setShowTitle(false);
        showParameter.setCustomParam("importTaskId", parameter.getCustomParam("importTaskId"));
        showParameter.setCustomParam("importTaskName", parameter.getCustomParam("importTaskName"));
        showParameter.setCustomParam("sinsurTplId", parameter.getCustomParam("sinsurTplId"));
        showParameter.setCustomParam("periodId", parameter.getCustomParam("periodId"));
        showParameter.setCustomParam("welfarepayerId", parameter.getCustomParam("welfarepayerId"));
        showParameter.setCustomParam("orgId", parameter.getCustomParam("orgId"));
        showParameter.setCustomParam("periodNumber", parameter.getCustomParam("periodNumber"));
        if (isLocked != null && isLocked) {
            showParameter.setCustomParam("isLocked", Boolean.TRUE);
        } else {
            showParameter.setCustomParam("isLocked", Boolean.FALSE);
        }

        String title = ResManager.loadKDString("数据导入", "ImportTaskGuideListPlugin_0", "swc-hsas-formplugin", new Object[0]);

        showParameter.setCaption(title);
        showParameter.setCustomParam("creator", parameter.getCustomParam("creator"));
        showParameter.setCustomParam("tabKey", tabKey);
        showParameter.setCustomParam("verifyRecordId", this.getPageCache().get("verifyRecordId"));
        this.getPageCache().put("verifyRecordId", (String)null);
        showParameter.setSendToClient(true);
        if("dataimport".equals(tabKey)) {
            showParameter.getListFilterParameter().getQFilters().add(new QFilter("nckd_importtask", "=", parameter.getCustomParam("importTaskId")));
        }
        else if("datacompare".equals(tabKey)) {
            showParameter.getListFilterParameter().getQFilters().add(new QFilter("nckd_period", "=", parameter.getCustomParam("periodId")));
            showParameter.getListFilterParameter().getQFilters().add(new QFilter("nckd_welfarepayer", "=", parameter.getCustomParam("welfarepayerId")));
        }
        this.getView().showForm(showParameter);
    }

    public void pageRelease(EventObject e) {
        String groupId = MutexHelper.getMutexGroupId("nckd_sinsurimptask", "modify");
        Long importTaskId = (Long)this.getView().getFormShowParameter().getCustomParam("importTaskId");
        DataMutex dataMutex = null;
        try {
            dataMutex = DataMutex.create();
            Map<String, String> mutex = dataMutex.getLockInfo(String.valueOf(importTaskId), groupId, "nckd_sinsurimptask");
            if (null != mutex && !mutex.isEmpty()) {
                String lockOwner = (String)mutex.get("GLOBALSESSION");
                String currentOwner = RequestContext.get().getGlobalSessionId();
                if (ObjectUtils.nullSafeEquals(lockOwner, currentOwner)) {
                    HRPageCache cache = new HRPageCache(this.getView());
                    Boolean isCurrentPageLocked = (Boolean)cache.get("currentPageLocked", Boolean.class);
                    if (isCurrentPageLocked == null) {
                        return;
                    }
                    MutexHelper.release("nckd_sinsurimptask", "modify", String.valueOf(importTaskId));
                    return;
                }
                return;
            }
        } finally {
            if (dataMutex != null) {
                try {
                    dataMutex.close();
                } catch (IOException e1) {
                }
            }

        }

    }

    @Override
    public void beforeClosed(BeforeClosedEvent e) {
        super.beforeClosed(e);
        this.clearAppCache();
    }

    private void clearAppCache() {
        FormShowParameter parameter = this.getView().getFormShowParameter();
        String cacheKey = parameter.getCustomParam("importTaskId") + "_" + parameter.getCustomParam("sinsurTplId");
        IHRAppCache appCache = HRAppCache.get(cacheKey);
        appCache.remove("columnHeadList");
    }
}