|
|
|
@ -0,0 +1,164 @@
|
|
|
|
|
package com.docus.server.sys.common;
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.annotation.TableField;
|
|
|
|
|
import com.baomidou.mybatisplus.annotation.TableId;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
|
|
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Collection;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public abstract class BaseService<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements IBaseService<T> {
|
|
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(BaseService.class);
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public T findById(String id) {
|
|
|
|
|
if (id == null) {
|
|
|
|
|
throw new RuntimeException("param id is required");
|
|
|
|
|
}
|
|
|
|
|
return baseMapper.selectById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<T> findByIds(Collection<String> ids) {
|
|
|
|
|
if (ids == null || ids.size() == 0) {
|
|
|
|
|
return new ArrayList<>();
|
|
|
|
|
}
|
|
|
|
|
return baseMapper.selectBatchIds(ids);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 按字段查询,返回所有匹配的记录
|
|
|
|
|
*/
|
|
|
|
|
public List<T> findBy(String propertyName, Object propertyValue) {
|
|
|
|
|
String columnName = getColumnName(propertyName);
|
|
|
|
|
return baseMapper.selectList(new QueryWrapper<T>().eq(columnName, propertyValue));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<T> findActiveBy(String propertyName, Object propertyValue) {
|
|
|
|
|
String columnName = getColumnName(propertyName);
|
|
|
|
|
QueryWrapper<T> queryWrapper = new QueryWrapper<T>().eq(columnName, propertyValue);
|
|
|
|
|
queryWrapper.ne("state", 1);
|
|
|
|
|
return baseMapper.selectList(queryWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//返回所有匹配的记录并排序
|
|
|
|
|
public List<T> findBy(String propertyName, Object propertyValue, Sort sort) {
|
|
|
|
|
String columnName = getColumnName(propertyName);
|
|
|
|
|
QueryWrapper<T> query = new QueryWrapper<T>().eq(columnName, propertyValue);
|
|
|
|
|
buildSort(sort, query);
|
|
|
|
|
return baseMapper.selectList(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 返回所有匹配的Active记录并排序
|
|
|
|
|
*/
|
|
|
|
|
public List<T> findActiveBy(String propertyName, Object propertyValue, Sort sort) {
|
|
|
|
|
String columnName = getColumnName(propertyName);
|
|
|
|
|
QueryWrapper<T> query = new QueryWrapper<T>().eq(columnName, propertyValue);
|
|
|
|
|
query.ne("state", 1);
|
|
|
|
|
buildSort(sort, query);
|
|
|
|
|
return baseMapper.selectList(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//按字段查询,返回第-条记录
|
|
|
|
|
public T findOneBy(String propertyName, Object propertyValue) {
|
|
|
|
|
String columnName = getColumnName(propertyName);
|
|
|
|
|
return baseMapper.selectOne(new QueryWrapper<T>().eq(columnName, propertyValue));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//按字段查询,返回第一条Active 记录
|
|
|
|
|
public T findActiveOneBy(String propertyName, Object propertyValue) {
|
|
|
|
|
String columnName = getColumnName(propertyName);
|
|
|
|
|
QueryWrapper<T> queryWrapper = new QueryWrapper<T>().eq(columnName, propertyValue);
|
|
|
|
|
queryWrapper.ne("state", 1);
|
|
|
|
|
return baseMapper.selectOne(queryWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//* L ambda方式自由组合查询条件
|
|
|
|
|
public List<T> find(LambdaQueryWrapper<T> queryWrapper) {
|
|
|
|
|
return baseMapper.selectList(queryWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//L ambda方式自由组合查询条件,返回第一条记录
|
|
|
|
|
public T findone(LambdaQueryWrapper<T> queryWrapper) {
|
|
|
|
|
return baseMapper.selectOne(queryWrapper);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//返回表所有记录
|
|
|
|
|
public List<T> findAll() {
|
|
|
|
|
return baseMapper.selectList(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//返回表所有Active的记录
|
|
|
|
|
public List<T> findA1lActive() {
|
|
|
|
|
return baseMapper.selectList(Wrappers.<T>query().ne("state", 1));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//返回表所有记录并排序
|
|
|
|
|
public List<T> findA1l(Sort sort) {
|
|
|
|
|
QueryWrapper<T> query = new QueryWrapper<T>();
|
|
|
|
|
buildSort(sort, query);
|
|
|
|
|
return baseMapper.selectList(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//返回表所有Active记录并排序
|
|
|
|
|
public List<T> findAllActive(Sort sort) {
|
|
|
|
|
QueryWrapper<T> query = new QueryWrapper<T>();
|
|
|
|
|
query.ne("state", 1);
|
|
|
|
|
buildSort(sort, query);
|
|
|
|
|
return baseMapper.selectList(query);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//组装排序语句
|
|
|
|
|
private void buildSort(Sort sort, QueryWrapper<T> query) {
|
|
|
|
|
if (sort != null) {
|
|
|
|
|
List<Sort.SortItem> sortList = sort.getSortList();
|
|
|
|
|
|
|
|
|
|
for (Sort.SortItem sortItem : sortList) {
|
|
|
|
|
if (sortItem.getDirection() == Sort.Direction.ASC) {
|
|
|
|
|
query.orderByAsc(getColumnName(sortItem.getProperty()));
|
|
|
|
|
} else {
|
|
|
|
|
query.orderByDesc(getColumnName(sortItem.getProperty()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int deleteByIdList(List<String> idList) {
|
|
|
|
|
if (idList == null || idList.size() == 0) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return baseMapper.deleteBatchIds(idList);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//通过实体属性获取对应的数据库字段名
|
|
|
|
|
public String getColumnName(String propertyName) {
|
|
|
|
|
Map<String, Field> fieldMap = ReflectionKit.getFieldMap(getEntityClass());
|
|
|
|
|
if (fieldMap != null) {
|
|
|
|
|
Field field = fieldMap.get(propertyName);
|
|
|
|
|
|
|
|
|
|
if (field != null) {
|
|
|
|
|
TableField annotation = field.getAnnotation(TableField.class);
|
|
|
|
|
if (annotation != null) {
|
|
|
|
|
return annotation.value();
|
|
|
|
|
}
|
|
|
|
|
TableId idAnnotation = field.getAnnotation(TableId.class);
|
|
|
|
|
if (idAnnotation != null) {
|
|
|
|
|
return idAnnotation.value();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
throw new RuntimeException("获取不到属性" + propertyName + "的列名");
|
|
|
|
|
}
|
|
|
|
|
}
|