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

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.dataentity.utils.StringUtils;
import kd.bos.entity.datamodel.ListSelectedRow;
import kd.bos.form.FormShowParameter;
import kd.bos.form.ShowType;
import kd.bos.form.events.HyperLinkClickArgs;
import kd.bos.list.IListView;
import kd.bos.list.ListShowParameter;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.sdk.plugin.Plugin;

import java.text.MessageFormat;

/**
 * Tyx 2025-10-21
 * 外部数据导入任务列表插件 nckd_sinsurimptask
 */
public class SinsurImpTaskListPlugin extends AbstractListPlugin implements Plugin {

    /**
     * 点击任务名称跳转到导入中间表：nckd_taskimpguide
     * @param args
     */
    public void billListHyperLinkClick(HyperLinkClickArgs args) {
        ListShowParameter listShowParameter = (ListShowParameter)this.getView().getFormShowParameter();
        if (!listShowParameter.isLookUp()) {
            String fieldName = args.getHyperLinkClickEvent().getFieldName();
            if (StringUtils.equals("name", fieldName)) {
                args.setCancel(true);
                IListView listView = (IListView)this.getView();
                ListSelectedRow selectRow = listView.getCurrentSelectedRowInfo();
                Long importTaskId = (Long)selectRow.getPrimaryKeyValue();
                this.openTempDataPage(importTaskId);
            }

        }
    }

    /**
     * 处理跳转参数并打开页面
     * @param importTaskId
     */
    private void openTempDataPage(Long importTaskId) {
        HRBaseServiceHelper helper = new HRBaseServiceHelper("nckd_sinsurimptask");
        DynamicObject data = helper.queryOne("id,name,nckd_org.id,nckd_welfarepayer.id,nckd_period.id,creator.id,nckd_sinsurtpl.id,nckd_taskstatus", importTaskId);
        String msg;
        if (data == null) {
            msg = ResManager.loadKDString("数据已不存在。", "ImportTaskList_0", "swc-hsas-formplugin", new Object[0]);
            this.getView().showErrorNotification(msg);
        } else if (StringUtils.equals("8", data.getString("nckd_taskstatus"))) {
            msg = ResManager.loadKDString("所选任务已清除中间表数据，无法查看。", "ImportTaskList_4", "swc-hsas-formplugin", new Object[0]);
            this.getView().showErrorNotification(msg);
        } else {
            FormShowParameter parameter = new FormShowParameter();
            parameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
            parameter.setFormId("nckd_taskimpguide");
            String caption = MessageFormat.format(ResManager.loadKDString("外部数据导入任务 - {0}", "ImportTaskList_1", "swc-hsas-formplugin", new Object[0]), data.getString("name"));
            parameter.setCaption(caption);
            parameter.setCustomParam("importTaskId", importTaskId);
            parameter.setCustomParam("importTaskName", data.getString("name"));
            parameter.setCustomParam("welfarepayerId", data.getLong("nckd_welfarepayer.id"));
            parameter.setCustomParam("periodId", data.getLong("nckd_period.id"));
            parameter.setCustomParam("periodNumber", data.getString("nckd_period.number"));
            parameter.setCustomParam("sinsurTplId", data.getLong("nckd_sinsurtpl.id"));
            parameter.setCustomParam("orgId", String.valueOf(data.getLong("nckd_org.id")));
            parameter.setCustomParam("creator", String.valueOf(data.getLong("creator.id")));
            String pageId = this.getView().getMainView() == null ? this.getView().getPageId() : this.getView().getMainView().getPageId();
            pageId = pageId + '_' + importTaskId;
            parameter.setPageId(pageId);
            this.getView().showForm(parameter);
        }
    }

}