package nckd.jxccl.sit.hcsi.opplugin.web.file.attach;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.exception.KDBizException;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.plugin.Plugin;
import nckd.jxccl.sit.hcsi.common.constant.SitConstant;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Tyx 2025-12-05
 * 险种基数设置保存插件 【nckd_hcsi_sibase_ext】
 */
public class SIBaseSaveOpEx extends AbstractOperationServicePlugIn implements Plugin {

    private static final Log logger = LogFactory.getLog(SIBaseSaveOpEx.class);

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        super.onPreparePropertys(e);
        //e.getFieldKeys().add("welfaretype.nckd_sinsurfilefields");
    }

    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] bills = e.getDataEntities();
        // 根据社保档案ID进行分组：
        Map<Long, List<DynamicObject>> billsMap = Arrays.stream(bills)
                .collect(Collectors.groupingBy(bill -> {
                    Long sinsurfileId = bill.getLong("sinsurfile.id");
                    return sinsurfileId != null ? sinsurfileId : 0L;
                }));
        for(Long sinsurFileId : billsMap.keySet()) {
            if(sinsurFileId == 0L)
                continue;
            DynamicObject sinsurFileDyn = SitConstant.SINSURFILE_HELPER.loadOne(sinsurFileId);
            List<DynamicObject> siBaseList = billsMap.get(sinsurFileId);
            for (DynamicObject siBase : siBaseList) {
                // 险种上新增了一个字段nckd_sinsurfilefields, 根据这个字段的值判断把险种维度写回档案上的哪个字段
                Long welfAreTypeId = siBase.getLong("welfaretype.id");
                String selectFields = "nckd_sinsurfilefields";
                DynamicObject welfAreTypeDyn = SitConstant.WELFARETYPE_HELPER.queryOne(selectFields, welfAreTypeId);
                String fieldKey = welfAreTypeDyn.getString("nckd_sinsurfilefields");
                sinsurFileDyn.set(fieldKey, siBase.getLong("nckd_sinsurdimension.id"));
            }
            OperationResult result = SaveServiceHelper.saveOperate("hcsi_sinsurfile", new DynamicObject[]{sinsurFileDyn}, OperateOption.create());
            if(!result.isSuccess()) {
                logger.info("-------- 保存基数设置结果：" + result.getMessage());
                logger.info("-------- 保存基数设置错误：" + result.getAllErrorOrValidateInfo());
                throw new KDBizException("-------- 保存基数设置失败：" + result.getMessage());
            }
        }
    }

    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        super.afterExecuteOperationTransaction(e);
    }
}