package nckd.jimin.jyyy.fi.business;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.fi.er.business.trip.exception.TripSyncLogParam;
import kd.hrmp.hrpi.business.infrastructure.utils.QFilterUtil;

import java.util.Map;

/**
 * 商旅字段映射插件，同程人员同步，获取职等
 * @author wanghaiwu_kd
 * @date 2025/06/17
 */
public class DtgTripCusPlugin {
    private static final Log logger = LogFactory.getLog(DtgTripCusPlugin.class);

    public DtgTripCusPlugin() {
    }
    public Object getPositionLevelName(Map<String, Object> map, TripSyncLogParam tripSyncLogParam) {
        Map<String, Object> context = (Map)map.get("context");
        if (context == null) {
            return "";
        } else {
            Object employeeNumber = context.get("param.outEmployeeId");
            //人员基本信息hrpi_person
            //职等基础hrpi_empjobrel
            if (employeeNumber != null) {
                QFilter statusFilter = QFilterUtil.getCurrentQf();
                statusFilter.and(QFilterUtil.getDataStatusFilter());

                QFilter qFilter = new QFilter("number", "=", employeeNumber);
                qFilter.and(statusFilter);

                DynamicObject hrPerson = BusinessDataServiceHelper.loadSingleFromCache("hrpi_person", "id", qFilter.toArray());
                if (hrPerson == null) {
                    logger.info("根据员工number未获取到HR人员基本信息{}", employeeNumber);
                    return "";
                }

                qFilter = new QFilter("person.id", "=", hrPerson.getLong("id"));
                statusFilter.and(QFilterUtil.getStatusFilters());
                qFilter.and(statusFilter);

                DynamicObject hrJobrel = BusinessDataServiceHelper.loadSingleFromCache("hrpi_empjobrel", "id, joblevel", qFilter.toArray());
                if (hrJobrel == null) {
                    logger.info("根据员工number未获取到HR人员职等信息{}", employeeNumber);
                    return "";
                }

                DynamicObject jobLevel = hrJobrel.getDynamicObject("joblevel");
                if(jobLevel == null){
                    logger.info("根据员工number未获取到HR人员职级{}", employeeNumber);
                    return "";
                }

                String result = jobLevel.getString("name");

                return result;
            } else {
                logger.info("未获取到员工{}", employeeNumber);
                return "";
            }
        }
    }
}
