package nckd.jimin.jyyy.hr.swc.hsas.formplugin.web.basedata;


import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.exception.KDBizException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.sdk.db.KDB;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.swc.hsbp.common.vo.AbstractCloudCollaExecutePlugin;
import nckd.jimin.jyyy.hr.swc.hsas.formplugin.web.AdjSalFileService;
import nckd.jimin.jyyy.hr.swc.hsas.formplugin.web.helper.AdjSalFileHelper;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * description: 新增薪酬档案 协作扩展插件
 * 根据入职单上二开字段生成定调薪信息：
 * @author Tyx
 * @date  2025/6/4
 */
public class AdjSalFileCloudCollaExecutePluginEx implements AbstractCloudCollaExecutePlugin {

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

    private static AdjSalFileHelper helper = new AdjSalFileHelper();

    /**
     * 对协作数据赋值或修改，
     * @param fieldMapResult
     * @param fieldRuleResult
     * @param assembleSaveData 协作后数据结果，会作为execute的入参，
     * @param commonParams
     * @param ruleEngineData
     * @param currentTriggerObjectName
     * @return
     */
    @Override
    public List<String> afterAssembleData(Map<String, List<DynamicObject>> fieldMapResult, Map<String, Object> fieldRuleResult, Map<String, List<DynamicObject>> assembleSaveData, Map<String, Object> commonParams, Map<String, Object> ruleEngineData, String currentTriggerObjectName) {
        log.info(" inter afterAssembleData ");
        Map executeInfoMap = (Map) commonParams.get("executeInfoMap");
        Map msgEntity = (Map) executeInfoMap.get("msgEntity");
        // 获取变动记录ID
        Long chgRecordId = getChgrecordId(msgEntity);
        // 获取入职ID
        Long onBrdBillId = AdjSalFileHelper.getOnBrdBillId(chgRecordId);
        DynamicObject onBrdDyn = AdjSalFileHelper.getOnBrdBillDyn(onBrdBillId);
        Map<String, Object> map = AdjSalFileHelper.execute(onBrdDyn, commonParams);
        if(!Boolean.valueOf(map.get("success").toString())) {
            throw new KDBizException(map.get("msg").toString());
        }
        else {
            //更新下N薪系数
            Long personId = AdjSalFileHelper.getPersonId(commonParams);
            DynamicObject salAdjFile = AdjSalFileHelper.getSalaryAdjFile(personId);
            BigDecimal ncoe = onBrdDyn.getBigDecimal("nckd_ncoefficient");
            salAdjFile.set("nckd_ncoefficient", ncoe);
            SaveServiceHelper.save(new DynamicObject[]{salAdjFile});
        }
        return AbstractCloudCollaExecutePlugin.super.afterAssembleData(fieldMapResult, fieldRuleResult, assembleSaveData, commonParams, ruleEngineData, currentTriggerObjectName);
    }

    @Override
    public List<String> execute(List<DynamicObject> executeData, Map<String, Object> ruleEngineData) {
        log.info(" inter execute ");
        //throw new KDBizException("终止执行");
        return AbstractCloudCollaExecutePlugin.super.execute(executeData, ruleEngineData);
    }

    /**
     * 获取变动记录ID
     * @param contentMap
     * @return
     */
    private Long getChgrecordId(Map<String, Object> contentMap) {
        Long chgrecordId = (Long)contentMap.get("chgrecordId");
        if (chgrecordId != null && chgrecordId > 0L) {
            return chgrecordId;
        } else {
            Long recordId = (Long)contentMap.get("recordId");
            return recordId != null && recordId > 0L ? recordId : null;
        }
    }



}
