package nckd.jxccl.hrmp.hbpm.business.service.hr;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.hr.hbp.common.util.HRStringUtils;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionBillConstant;
import nckd.jxccl.hrmp.hbpm.common.hr.PositionChangeTypeEnum;

/**
 * 岗位申请单保存服务
 * @from: kd.hr.homs.business.domain.orgbatch.service.impl.OrgBatchBillSaveHelper
 * @author: jtd
 * @date: 2025/12/27 15:04
 */
public class PositionBillSaveHelper {
    private static PositionBillSaveHelper INSTANCE = new PositionBillSaveHelper();
    private static final Log logger = LogFactory.getLog(PositionBillSaveHelper.class);

    public static PositionBillSaveHelper getInstance() {
        return INSTANCE;
    }

    public Long getPositionId(DynamicObject entryEntityDynFromPage, String suffix) {
        String idFieldName = String.join(".", PositionBillConstant.NCKD_POSITION_KEY, PositionBillConstant.ID_KEY);
        if (HRStringUtils.equals(entryEntityDynFromPage.getString(spliceTwoStringByUnderLine(suffix, String.join(".", PositionBillConstant.NCKD_CHANGETYPE, PositionBillConstant.NUMBER_KEY))), PositionChangeTypeEnum.ADD.getNumber())) {
            idFieldName = PositionBillConstant.NCKD_POSITION_KEY;
        }

        String positionFiledName = spliceTwoStringByUnderLine(suffix, idFieldName);
        return entryEntityDynFromPage.getLong(positionFiledName);
    }

    public String getSuffixFromEntryEntityDyn(DynamicObject entryEntityDynFromPage) {
        String entryEntityName = entryEntityDynFromPage.getDynamicObjectType().getName();
        int lastUnderLineIndex = entryEntityName.lastIndexOf("_");
        return entryEntityName.substring(lastUnderLineIndex + 1);
    }

    public String spliceTwoStringByUnderLine(String oneString, String otherString) {
        String[] str = otherString.split("\\.");
        return str.length > 1 ? str[0] + "_" + oneString + "." + str[1] : otherString + "_" + oneString;
    }

}
