package nckd.jimin.jyyy.hr.hom.print;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.print.core.data.DataRowSet;
import kd.bos.print.core.data.field.TextField;
import kd.bos.print.core.plugin.AbstractPrintPlugin;
import kd.bos.print.core.plugin.event.AfterLoadDataEvent;
import kd.bos.print.core.plugin.event.BeforeLoadDataEvent;
import kd.bos.print.core.plugin.event.CustomDataLoadEvent;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jimin.jyyy.hr.plugin.workflow.HRWorkflowUtils;

/**
 * 工牌打印插件 Tyx 2025-07-03
 * 获取一级部门负责人
 * 数据源主表是通用协作详情，取通用协作详情上的入职单
 */
public class EmployeeCardPrintPlugin extends AbstractPrintPlugin implements Plugin {

    @Override
    public void beforeLoadData(BeforeLoadDataEvent evt) {
        super.beforeLoadData(evt);
    }

    @Override
    public void loadCustomData(CustomDataLoadEvent evt) {
        super.loadCustomData(evt);
        String queryName = evt.getDataSource().getDsName();
        //获取到入职办理单PK，去找一级部门
        if("nckd_customquery".equals(queryName)) {
            //获取通用协作详情ID
            Object pkId = evt.getDataSource().getPkId();
            String entityId = evt.getDataSource().getFormId();
            DynamicObject handleDyn = BusinessDataServiceHelper.loadSingle(pkId, entityId);
            //获取入职单
            DynamicObject onBrdDyn = handleDyn.getDynamicObject("onbrdid");
            Long adminOrgId = onBrdDyn.getLong("aadminorg.id");
            Long firstDeptId = HRWorkflowUtils.getFirstDeptId(adminOrgId);
            if(firstDeptId != 0L) {
                DynamicObject firstDept = BusinessDataServiceHelper.loadSingle(firstDeptId, "haos_adminorghr");
                //一级部门名称
                String firstDeptName = firstDept.getString("name");
                evt.getCustomDataRows().clear();
                DataRowSet dataRowSet = new DataRowSet();
                dataRowSet.put("nckd_firstdept", new TextField(firstDeptName));
                evt.getCustomDataRows().add(dataRowSet);
            }


        }
    }

    @Override
    public void afterLoadData(AfterLoadDataEvent evt) {
        super.afterLoadData(evt);
    }
}