package nckd.jxccl.sit.hcsi.validate.web;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.util.StringUtils;
import kd.sdk.sit.sitbp.business.helper.SITBPServiceHelper;
import kd.sdk.sit.sitbp.common.StdDimDTO;
import kd.sdk.sit.sitbp.common.StdResultInfoDTO;
import nckd.jxccl.sit.hcsi.utils.SITCoordinationUtils;

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


/**
 * 社保核定单确认生效/拆单校验器
 * 核定单中提单生效时，增加险种维度的校验，参保标准启用了险种维度的，校验提醒险种维度必填，参保标准未启用险种维度的，险种维度可以非必填
 */
public class SinsurEmpCoordConfirmEffectValidator extends AbstractValidator {

    private SITCoordinationUtils SITCoordinationUtils = new SITCoordinationUtils();
    @Override
    public void validate() {
        for (ExtendedDataEntity dataEntity : dataEntities) {
            DynamicObject bill = dataEntity.getDataEntity();
            DynamicObject sinsurStd = bill.getDynamicObject("sinsurstd_r31");
            StdResultInfoDTO stdResultInfoDTO = SITBPServiceHelper.getStdInfoAndStdDim(Arrays.asList(sinsurStd.getLong("id"))).get(sinsurStd.getLong("id"));
            List<StdResultInfoDTO.StdInsurTypeResultInfoDTO> StdInsurTypeResultInfoDTOs = stdResultInfoDTO.getStdInsurTypeResultInfoDTOS();
            Map<Long, StdResultInfoDTO.StdInsurTypeResultInfoDTO> map = StdInsurTypeResultInfoDTOs.stream().collect(Collectors.toMap((dyx) -> {
                return dyx.getInsurTypeId();
            }, (dyx) -> {
                return dyx;
            }, (key1, key2) -> {
                return key2;
            }));
            // 判断各险种是否启用险种维度，如果启用则再判断险种维度是否有选，没选的话添加错误信息
            for(DynamicObject entry: bill.getDynamicObjectCollection("nckd_entryentity")) {
                DynamicObject welfAreType = entry.getDynamicObject("nckd_welfaretype");
                boolean isEnable = SITCoordinationUtils.getEnableDimByInsurTypeId(map, welfAreType.getLong("id"));
                // 如果启用了险种维度，再判断你险种维度是否选择
                if(isEnable) {
                    DynamicObject dimensionDyn = entry.getDynamicObject("nckd_sinsurdimension");
                    if(ObjectUtils.isEmpty(dimensionDyn)) {
                        this.addFatalErrorMessage(dataEntity, "第" + entry.getInt("seq") + "行，请选择险种维度！");
                    }
                }
            }
        }
    }
}
