package nckd.jxccl.sit.hcsi.business.coordination.api;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.hr.hbp.business.coordination.api.ext.VerifBillBeforeSaveHandler;
import kd.hr.hbp.business.coordination.model.CoordContext;
import kd.hr.hbp.business.coordination.model.VerifBillStageEnum;
import nckd.jxccl.sit.hcsi.utils.SITCoordinationUtils;

import java.util.Map;

/**
 * @author: Tyx
 * @date: 2026/01/08
 * @description: 社保核定单保存前携带险种基数信息
 */
public class SinsurVerifBillBeforeSaveHandlerEx implements VerifBillBeforeSaveHandler {

    private static final Log log = LogFactory.getLog(SinsurVerifBillBeforeSaveHandlerEx.class);

    /**
     * 社保核定单保存前方法：<p>
     * 适用单据：核定单类型 = 社保 and 变动大类 = 调动 and 流动类型 = 流入 and 是否拆单 = 否
     * @param dynamicObjects
     * @param verifBillStageEnum
     * @param coordContext
     * @return
     */
    @Override
    public Map<String, Object> beforeSave(DynamicObject[] dynamicObjects, VerifBillStageEnum verifBillStageEnum, CoordContext coordContext) {
        // log.info("-------- 核定单 beforeSave 处理条数：" + dynamicObjects.length);

        for (DynamicObject bill : dynamicObjects) {
            if(checkIsNeedSiBaseInfo(bill)) {
                dealSiBaseInfo(bill);
            }
        }
        return null;
    }

    /**
     * 判断当前核定单是否需要处理
     * @param bill
     * @return
     */
    public boolean checkIsNeedSiBaseInfo (DynamicObject bill) {
        boolean isNeed = false;
        boolean isSplit = bill.getBoolean("nckd_issplit");

        // 协作业务对象
        String objectNumber = bill.getDynamicObject("coordbizobject").getString("number");

        DynamicObject ruleParam = bill.getDynamicObject("coordruleparam");
        // 获取到变动大类和变动类型
        String chgNumber = ruleParam.getString("chgevent_z.number");
        String flowType = ruleParam.getString("flowtype_y");
        if(objectNumber.equals("hcsi_sinsurfile") && !isSplit && chgNumber.equals("1020_S") && flowType.equals("1")) {
            return true;
        }
        return isNeed;
    }

    /**
     * 根据员工查询所有社保基数设置，并赋值到单据体中
     * @param bill
     */
    public void dealSiBaseInfo (DynamicObject bill) {
        DynamicObject ruleParam = bill.getDynamicObject("coordruleparam");
        DynamicObject employee = ruleParam.getDynamicObject("employee");
        DynamicObject[] sibaseCols = SITCoordinationUtils.getSiBaseByEmployeeId(employee.getLong("id"));
        if(sibaseCols.length == 0)
            return;
        Map<String, String> fieldsMappingMap = SITCoordinationUtils.getFieldsMappingMap();
        DynamicObjectCollection entryCols = bill.getDynamicObjectCollection("nckd_entryentity");
        for(DynamicObject sibase : sibaseCols) {
            DynamicObject entry = entryCols.addNew();
            for (String key : fieldsMappingMap.keySet()) {
                entry.set(fieldsMappingMap.get(key), sibase.get(key));
            }
        }
    }

}
