QueyDSL+jpa 可以做到if判断成立后再对数据库某个字段进行去重吗,比如下面的实时与历史,如果是实时的话就对sn进行排序,selectDistinct的方式是不行的,而直接在紧接着的投影DTO对象加 distinct好像也不行
@Transactional(rollbackFor = Exception.class)
public Page<TAedWarn> findAll(TWarn dto, Pageable pageable, String city, Long userId) {
JPAQuery jpaQuery = jpaQueryFactory.selectDistinct(Projections.bean(TAedWarn.class,
QDevice.Device.sn,
QDevice.Device.model,
QWarn.Warn.id, //就是这里直接加distinct好像也不能去重
QWarn.Warn.type,
QWarn.Warn.readers))
.from(Warn.Warn).leftJoin(QDevice.Device).on(QWarn.Warn.deviceid.eq(QDevice.Device.id));
//判断是否进行条件查询
if (null != dto) {
//设备序列号
if (!StringUtils.isEmpty(dto.getSn()) && null != dto.getAccurateAndFuzzy()) {
//进行判断,是进行条件查询还是模糊查询
if (dto.getAccurateAndFuzzy() == AccurateAndFuzzy.ACCURATE.getCode()) {
jpaQuery.where(QDevice.Device.sn.eq(dto.getSn()));
}
if (dto.getAccurateAndFuzzy() == AccurateAndFuzzy.FUZZY.getCode()) {
jpaQuery.where(Device.Device.sn.contains(dto.getSn()));
}
}
//安装位置
if (null != dto.getSetupposition()) {
jpaQuery.where(QDevice.Device.setupposition.contains(dto.getSetupposition()));
}
//实时与历史 0:实时 1历史 可不可以加入如果是0的话就对 sn进行去重操作 1的话正常查询
if (null != dto.getTimeType() && TimeType.NOW.getCode() == dto.getTimeType()) {
jpaQuery.where(QWarn.Warn.handled.eq(TimeType.NOW.getCode()));
}
if (null != dto.getTimeType() && TimeType.HISTORY.getCode() == dto.getTimeType()) {
jpaQuery.where(QWarn.Warn.handled.eq(TimeType.HISTORY.getCode()));
}
}
//用户所处城市可见
jpaQuery.where(QAedDevice.aedDevice.setupcity.eq(city);
// 默认分页
jpaQuery.offset(pageable.getOffset()).limit(pageable.getPageSize());
QueryResults<TAedWarn> results = jpaQuery.fetchResults();