package nckd.jxccl.hr.psms.plugin.operate.contribution.validate;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import nckd.jxccl.base.common.constant.FormConstant;
import nckd.jxccl.base.common.utils.StrFormatter;
import nckd.jxccl.hr.psms.common.ContributionConstant;
import nckd.jxccl.hr.psms.helper.ContributionHelper;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * 动态表单插件
 */
/**
* 积分项目分数配置校验
* 实体标识：nckd_scoreitemconf
* @author W.Y.C
* @date 2025/11/2 13:02
* @version 1.0
*/
public class ScoreItemConfValidate extends AbstractValidator {

    @Override
    public void validate() {

        //查询其他有效日期内的数据

        for (ExtendedDataEntity dataEntity : this.getDataEntities()) {
            DynamicObject data = dataEntity.getDataEntity();
            long id = data.getLong(FormConstant.ID_KEY);
            Date startDate = data.getDate(FormConstant.NCKD_STARTDATE);
            Date endDate = data.getDate(FormConstant.NCKD_ENDDATE);

            QFilter filter = new QFilter(FormConstant.ID_KEY, QCP.not_equals,id);
            Map<String, BigDecimal> scoreConfMap = ContributionHelper.getScoreConf(filter);

            //判定是否在有效期内，endDate为空时，默认为不失效
            // if (startDate != null && (endDate == null || !endDate.before(new Date()))) {
                //校验有效时间范围内有没有重复配置
                DynamicObjectCollection dynamicObjectCollection = data.getDynamicObjectCollection(FormConstant.NCKD_ENTRYENTITY);
                Map<String, DynamicObject> scoreMap = dynamicObjectCollection.stream()
                .filter(Objects::nonNull)
                .collect(Collectors.toMap(
                        item -> {
                            DynamicObject scoreItem = item.getDynamicObject(ContributionConstant.NCKD_SCOREITEM);
                            DynamicObject scoreItemSub = item.getDynamicObject(ContributionConstant.NCKD_SCOREITEMSUB);
                            DynamicObject scoreItemRank = item.getDynamicObject(ContributionConstant.NCKD_SCOREITEMRANK);

                            long scoreItemId = (scoreItem != null) ? scoreItem.getLong(FormConstant.ID_KEY) : 0L;
                            long scoreItemSubId = (scoreItemSub != null) ? scoreItemSub.getLong(FormConstant.ID_KEY) : 0L;
                            long scoreItemRankId = (scoreItemRank != null) ? scoreItemRank.getLong(FormConstant.ID_KEY) : 0L;

                            if (scoreItemRankId > 0) {
                                return scoreItemId + "-" + scoreItemSubId + "-" + scoreItemRankId;
                            } else if (scoreItemSubId > 0) {
                                return scoreItemId + "-" + scoreItemSubId;
                            } else {
                                return String.valueOf(scoreItemId);
                            }
                        },
                        item -> item,
                        (existing, replacement) -> existing
                ));
                for (Map.Entry<String, DynamicObject> entry : scoreMap.entrySet()) {
                    String key = entry.getKey();
                    DynamicObject dynamicObject = entry.getValue();
                    if(scoreConfMap.get(key) != null){
                        DynamicObject scoreItem = dynamicObject.getDynamicObject(ContributionConstant.NCKD_SCOREITEM);
                        DynamicObject scoreItemSub = dynamicObject.getDynamicObject(ContributionConstant.NCKD_SCOREITEMSUB);
                        DynamicObject scoreItemRank = dynamicObject.getDynamicObject(ContributionConstant.NCKD_SCOREITEMRANK);

                        String scoreItemName = (scoreItem != null) ? scoreItem.getString(FormConstant.NAME_KEY) : null;
                        String scoreItemSubName = (scoreItemSub != null) ? scoreItemSub.getString(FormConstant.NAME_KEY) : null;
                        String scoreItemRankName = (scoreItemRank != null) ? scoreItemRank.getString(FormConstant.NAME_KEY) : null;
                        StringJoiner joiner = new StringJoiner("-");
                        if (StringUtils.isNotEmpty(scoreItemName)) {
                            joiner.add(scoreItemName);
                        }
                        if (StringUtils.isNotEmpty(scoreItemSubName)) {
                            joiner.add(scoreItemSubName);
                        }
                        if (StringUtils.isNotEmpty(scoreItemRankName)) {
                            joiner.add(scoreItemRankName);
                        }
                        this.addErrorMessage(dataEntity, StrFormatter.format("【{}】已存在生效中的配置。不能重复配置", joiner.toString()));
                    }
                }
            // }
        }
    }
}