package cn.stylefeng.team.meta.modular.factory;

import cn.stylefeng.team.meta.api.entity.MetaField;
import cn.stylefeng.team.meta.api.exception.MetaException;

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

import static cn.stylefeng.team.meta.modular.enums.MetaFieldExceptionEnum.REPEAT_FIELD;

public class ValidateFieldFactory {

    public static void validateRepeatField(List<MetaField> metaFieldList) {

        Map<String, List<MetaField>> map = metaFieldList.stream().collect(Collectors.groupingBy(MetaField::getFieldCode));
        List<String> list = new ArrayList<>();
        for (Map.Entry<String, List<MetaField>> entry : map.entrySet()) {
            if (entry.getValue().size() > 1) {
                list.add(entry.getKey());
            }
        }
        if (!list.isEmpty()) {
            throw new MetaException(REPEAT_FIELD, String.join(",", list));
        }

    }

}
