package com.sysu.admin.support.base;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManager;
import javax.validation.ConstraintViolation;
import javax.validation.Valid;
import javax.validation.Validation;
import java.util.Set;

public class BaseComponent {

    @Autowired
    public JPAQueryFactory mJPAQueryFactory;
    @Autowired
    public EntityManager mEntityManager;



    protected void validate(@Valid Object obj, Class... groups) {
        Set<ConstraintViolation<Object>> validateSet = Validation.buildDefaultValidatorFactory()
                .getValidator()
                .validate(obj, groups);
        if (!CollectionUtils.isEmpty(validateSet)) {
            String messages = validateSet.stream()
                    .map(ConstraintViolation::getMessage)
                    .reduce((m1, m2) -> m1 + "；" + m2)
                    .orElse("参数输入有误！");
            throw new IllegalArgumentException(messages);
        }
    }

}