package cn.common.common.utils;

import cn.common.common.enums.ErrorCode;
import cn.common.common.exception.BusinessException;
import cn.common.common.utils.collection.CollectionUtils;
import org.hibernate.validator.HibernateValidator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Singer create by Singer email:singer-coder@qq.com
 * @packageName cn.common.common.validation
 * @Description: 数据校验工具类
 * @date 2022-03-23
 */
public class ValidationUtil {

    private ValidationUtil() {


    }

    // 线程安全的，直接构建也可以，这里使用静态代码块一样的效果
    private static Validator validator;

    static {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
    }

    /**
     * 字段校验 校验集合
     * @author: create by singer - Singer email:singer-coder@qq.com
     * @date 2022/3/23
     * @param dataList 参数
     * @return
     */
    public static <T> void validate(List<T> dataList) {
        Set<ConstraintViolation<T>> constraintViolations = new HashSet<>();
        dataList.forEach(item -> {
            Set<ConstraintViolation<T>> validateSet = validator.validate(item);
            constraintViolations.addAll(validateSet);
        });
        if (!CollectionUtils.isEmpty(constraintViolations)) {
            StringBuilder validateError = new StringBuilder();
            for (ConstraintViolation<T> constraintViolation : constraintViolations) {
                validateError.append(constraintViolation.getMessage()).append(";");
            }
            throw new BusinessException(ErrorCode.PARAM_ERROR.getCode(), validateError.toString());
        }
    }

    /**
      * 字段校验
      * @author: create by singer - Singer email:singer-coder@qq.com
      * @date 2022/3/23
      * @param t 参数
      * @return
      */
    public static <T> void validate(T t) {
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(t);
        if (!CollectionUtils.isEmpty(constraintViolations)) {
            StringBuilder validateError = new StringBuilder();
            for (ConstraintViolation<T> constraintViolation : constraintViolations) {
                validateError.append(constraintViolation.getMessage()).append(";");
            }
            throw new BusinessException(ErrorCode.PARAM_ERROR.getCode(), validateError.toString());
        }
    }

    public static <T> void validate1(T t) {
        Validator validator = Validation.byProvider(HibernateValidator.class).configure().failFast(false) // 为true，则有一个错误就结束校验
                .buildValidatorFactory().getValidator();
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(t);
        StringBuilder validateError = new StringBuilder();
        for (ConstraintViolation<T> constraintViolation : constraintViolations) {
            validateError.append(constraintViolation.getMessage()).append(";");
        }

    }

}
