package nckd.jxccl.swc.hsas.formplugin.web.outdata.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.bos.orm.query.QFilter;
import kd.hr.hbp.business.servicehelper.HRBaseServiceHelper;
import kd.sdk.plugin.Plugin;

import java.text.MessageFormat;

/**
 * Tyx 2025-12-09
 * 外单位数据导入任务列表插件 【nckd_outimptask】
 */
public class OutImpTaskListPlugin extends AbstractListPlugin implements Plugin {

    @Override
    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);
            }
        }
    }

    private void openTempDataPage(Long importTaskId) {
        HRBaseServiceHelper helper = new HRBaseServiceHelper("nckd_outimptask");
        DynamicObject data = helper.queryOne("id,name,nckd_org.id,nckd_period.id,creator.id,nckd_outimptpl.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 {
            ListShowParameter parameter = new ListShowParameter();
            parameter.getOpenStyle().setShowType(ShowType.MainNewTabPage);
            parameter.setBillFormId("nckd_outtempdata");
            parameter.setFormId("bos_list");
            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("periodId", data.getLong("nckd_period.id"));
            parameter.setCustomParam("periodNumber", data.getString("nckd_period.number"));
            parameter.setCustomParam("outimpTplId", data.getLong("nckd_outimptpl.id"));
            parameter.setCustomParam("orgId", String.valueOf(data.getLong("nckd_org.id")));
            parameter.setCustomParam("creator", String.valueOf(data.getLong("creator.id")));
            parameter.getListFilterParameter().getQFilters().add(new QFilter("nckd_importtask", "=", parameter.getCustomParam("importTaskId")));
            String pageId = this.getView().getMainView() == null ? this.getView().getPageId() : this.getView().getMainView().getPageId();
            pageId = pageId + '_' + importTaskId;
            parameter.setPageId(pageId);
            this.getView().showForm(parameter);
        }
    }

}