com.zys.mybatis.annotation.validate.DistinctClass Maven / Gradle / Ivy
package com.zys.mybatis.annotation.validate;
import com.zys.mybatis.condition.ConditionKey;
import com.zys.mybatis.constant.CharFinal;
import com.zys.mybatis.crud.Query;
import com.zys.mybatis.dao.base.BaseDao;
import com.zys.mybatis.utils.BeanUtil;
import com.zys.mybatis.utils.FieldUtils;
import com.zys.mybatis.utils.SpringUtil;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.lang.annotation.ElementType;
import java.util.HashMap;
import java.util.Map;
/**
* 去重校验规则
* @author zys
* @version 1.0
* @date 2020/12/28 17:54
*/
public class DistinctClass implements ConstraintValidator {
private Distinct distinct;
@Override
public void initialize(Distinct distinct) {
this.distinct = distinct;
}
@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
if (value == null || "".equals(value)) {
return true;
}
BaseDao baseDao = distinct.classes().equals(BaseDao.class) ? SpringUtil.getPollBean(distinct.classes()) : SpringUtil.getBean(distinct.classes());
Query query;
if (ElementType.TYPE.equals(distinct.type())) {
Map conditionMap = new HashMap<>();
for (String fieldName : distinct.columns()) {
conditionMap.put(new ConditionKey(fieldName, CharFinal.EQ) , BeanUtil.getValue(value, FieldUtils.toHump(fieldName)));
}
query = new Query<>(int.class,distinct.tableName(),"count(1)").setParams(conditionMap);
}else {
query = new Query<>(int.class,distinct.tableName(),"count(1)").eq(distinct.columns()[0],value);
}
if (!distinct.isAdd()) {
query.notIn(distinct.id(),BeanUtil.getValue(value, distinct.id()));
}
if (distinct.flag()) {
return baseDao.count(query) <= 0;
}
return baseDao.count(query) > 0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy