溯源码分组查询
This commit is contained in:
@@ -6,6 +6,12 @@ import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.farm.controller.admin.traceabilitycode.vo.TraceabilityCodePageReqVO;
|
||||
import cn.iocoder.yudao.module.farm.dal.dataobject.traceabilitycode.TraceabilityCodeDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
import org.apache.ibatis.annotations.Select;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 溯源-溯源码 Mapper
|
||||
@@ -60,50 +66,96 @@ public interface TraceabilityCodeMapper extends BaseMapperX<TraceabilityCodeDO>
|
||||
.orderByDesc(TraceabilityCodeDO::getCreateTime));
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据CodeOutBatch分组查询,获取每个批次的最新记录
|
||||
* 使用子查询方式避免GROUP BY的SQL兼容性问题
|
||||
*/
|
||||
@Select("<script>" +
|
||||
"SELECT t1.* FROM farm_traceability_code t1 " +
|
||||
"INNER JOIN ( " +
|
||||
" SELECT code_out_batch, MAX(create_time) as max_create_time " +
|
||||
" FROM farm_traceability_code " +
|
||||
" WHERE 1=1 " +
|
||||
"<if test='tenantId != null'> AND tenant_id = #{tenantId} </if>" +
|
||||
"<if test='speciesCode != null'> AND species_code = #{speciesCode} </if>" +
|
||||
"<if test='speciesName != null and speciesName != \"\"'> AND species_name LIKE CONCAT('%', #{speciesName}, '%') </if>" +
|
||||
"<if test='baseId != null'> AND base_id = #{baseId} </if>" +
|
||||
"<if test='baseName != null and baseName != \"\"'> AND base_name LIKE CONCAT('%', #{baseName}, '%') </if>" +
|
||||
"<if test='plotId != null'> AND plot_id = #{plotId} </if>" +
|
||||
"<if test='plotName != null and plotName != \"\"'> AND plot_name LIKE CONCAT('%', #{plotName}, '%') </if>" +
|
||||
"<if test='harvestDateStart != null'> AND harvest_date >= #{harvestDateStart} </if>" +
|
||||
"<if test='harvestDateEnd != null'> AND harvest_date <= #{harvestDateEnd} </if>" +
|
||||
"<if test='farmingInputCount != null'> AND farming_input_count = #{farmingInputCount} </if>" +
|
||||
"<if test='productId != null'> AND product_id = #{productId} </if>" +
|
||||
"<if test='productName != null and productName != \"\"'> AND product_name LIKE CONCAT('%', #{productName}, '%') </if>" +
|
||||
"<if test='codeOutBatch != null and codeOutBatch != \"\"'> AND code_out_batch = #{codeOutBatch} </if>" +
|
||||
"<if test='status != null'> AND status = #{status} </if>" +
|
||||
" GROUP BY code_out_batch " +
|
||||
") t2 ON t1.code_out_batch = t2.code_out_batch AND t1.create_time = t2.max_create_time " +
|
||||
"WHERE 1=1 " +
|
||||
"<if test='tenantId != null'> AND t1.tenant_id = #{tenantId} </if>" +
|
||||
"<if test='speciesCode != null'> AND t1.species_code = #{speciesCode} </if>" +
|
||||
"<if test='speciesName != null and speciesName != \"\"'> AND t1.species_name LIKE CONCAT('%', #{speciesName}, '%') </if>" +
|
||||
"<if test='baseId != null'> AND t1.base_id = #{baseId} </if>" +
|
||||
"<if test='baseName != null and baseName != \"\"'> AND t1.base_name LIKE CONCAT('%', #{baseName}, '%') </if>" +
|
||||
"<if test='plotId != null'> AND t1.plot_id = #{plotId} </if>" +
|
||||
"<if test='plotName != null and plotName != \"\"'> AND t1.plot_name LIKE CONCAT('%', #{plotName}, '%') </if>" +
|
||||
"<if test='harvestDateStart != null'> AND t1.harvest_date >= #{harvestDateStart} </if>" +
|
||||
"<if test='harvestDateEnd != null'> AND t1.harvest_date <= #{harvestDateEnd} </if>" +
|
||||
"<if test='farmingInputCount != null'> AND t1.farming_input_count = #{farmingInputCount} </if>" +
|
||||
"<if test='productId != null'> AND t1.product_id = #{productId} </if>" +
|
||||
"<if test='productName != null and productName != \"\"'> AND t1.product_name LIKE CONCAT('%', #{productName}, '%') </if>" +
|
||||
"<if test='codeOutBatch != null and codeOutBatch != \"\"'> AND t1.code_out_batch = #{codeOutBatch} </if>" +
|
||||
"<if test='status != null'> AND t1.status = #{status} </if>" +
|
||||
"ORDER BY t1.create_time DESC" +
|
||||
"</script>")
|
||||
List<TraceabilityCodeDO> selectLatestByCodeOutBatch(@Param("tenantId") String tenantId,
|
||||
@Param("speciesCode") String speciesCode,
|
||||
@Param("speciesName") String speciesName,
|
||||
@Param("baseId") String baseId,
|
||||
@Param("baseName") String baseName,
|
||||
@Param("plotId") String plotId,
|
||||
@Param("plotName") String plotName,
|
||||
@Param("harvestDateStart") String harvestDateStart,
|
||||
@Param("harvestDateEnd") String harvestDateEnd,
|
||||
@Param("farmingInputCount") Long farmingInputCount,
|
||||
@Param("productId") String productId,
|
||||
@Param("productName") String productName,
|
||||
@Param("codeOutBatch") String codeOutBatch,
|
||||
@Param("status") Long status);
|
||||
|
||||
default PageResult<TraceabilityCodeDO> selectPageList(TraceabilityCodePageReqVO reqVO) {
|
||||
// 先查询符合条件的最新记录ID列表
|
||||
List<TraceabilityCodeDO> latestRecords = selectLatestByCodeOutBatch(
|
||||
reqVO.getTenantId(),
|
||||
reqVO.getSpeciesCode(),
|
||||
reqVO.getSpeciesName(),
|
||||
reqVO.getBaseId(),
|
||||
reqVO.getBaseName(),
|
||||
reqVO.getPlotId(),
|
||||
reqVO.getPlotName(),
|
||||
reqVO.getHarvestDateStart(),
|
||||
reqVO.getHarvestDateEnd(),
|
||||
reqVO.getFarmingInputCount(),
|
||||
reqVO.getProductId(),
|
||||
reqVO.getProductName(),
|
||||
reqVO.getCodeOutBatch(),
|
||||
reqVO.getStatus()
|
||||
);
|
||||
|
||||
// 如果没有记录,直接返回空结果
|
||||
if (latestRecords.isEmpty()) {
|
||||
return new PageResult<>(Collections.emptyList(), 0L);
|
||||
}
|
||||
|
||||
// 提取ID列表
|
||||
List<String> ids = latestRecords.stream()
|
||||
.map(TraceabilityCodeDO::getId)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 根据ID列表进行分页查询,获取完整的记录信息
|
||||
return selectPage(reqVO, new LambdaQueryWrapperX<TraceabilityCodeDO>()
|
||||
.eqIfPresent(TraceabilityCodeDO::getSpeciesCode, reqVO.getSpeciesCode())
|
||||
.likeIfPresent(TraceabilityCodeDO::getSpeciesName, reqVO.getSpeciesName())
|
||||
.eqIfPresent(TraceabilityCodeDO::getBaseId, reqVO.getBaseId())
|
||||
.likeIfPresent(TraceabilityCodeDO::getBaseName, reqVO.getBaseName())
|
||||
.eqIfPresent(TraceabilityCodeDO::getPlotId, reqVO.getPlotId())
|
||||
.likeIfPresent(TraceabilityCodeDO::getPlotName, reqVO.getPlotName())
|
||||
.eqIfPresent(TraceabilityCodeDO::getTenantId, reqVO.getTenantId())
|
||||
// 采收时间范围查询
|
||||
.gtIfPresent(TraceabilityCodeDO::getHarvestDate, reqVO.getHarvestDateStart())
|
||||
.leIfPresent(TraceabilityCodeDO::getHarvestDate, reqVO.getHarvestDateEnd())
|
||||
.eqIfPresent(TraceabilityCodeDO::getFarmingInputCount, reqVO.getFarmingInputCount())
|
||||
.eqIfPresent(TraceabilityCodeDO::getProductId, reqVO.getProductId())
|
||||
.likeIfPresent(TraceabilityCodeDO::getProductName, reqVO.getProductName())
|
||||
.eqIfPresent(TraceabilityCodeDO::getPackingSpec, reqVO.getPackingSpec())
|
||||
.eqIfPresent(TraceabilityCodeDO::getProductImage, reqVO.getProductImage())
|
||||
.likeIfPresent(TraceabilityCodeDO::getBrandName, reqVO.getBrandName())
|
||||
.eqIfPresent(TraceabilityCodeDO::getIntroduce, reqVO.getIntroduce())
|
||||
.eqIfPresent(TraceabilityCodeDO::getBrandAuthenticationStr, reqVO.getBrandAuthentication())
|
||||
.eqIfPresent(TraceabilityCodeDO::getBusinessId, reqVO.getBusinessId())
|
||||
.likeIfPresent(TraceabilityCodeDO::getBusinessName, reqVO.getBusinessName())
|
||||
.eqIfPresent(TraceabilityCodeDO::getIsShowBusinessName, reqVO.getIsShowBusinessName())
|
||||
.eqIfPresent(TraceabilityCodeDO::getBusinessPhone, reqVO.getBusinessPhone())
|
||||
.eqIfPresent(TraceabilityCodeDO::getIsShowBusinessPhone, reqVO.getIsShowBusinessPhone())
|
||||
.likeIfPresent(TraceabilityCodeDO::getStoreName, reqVO.getStoreName())
|
||||
.eqIfPresent(TraceabilityCodeDO::getIsShowStoreName, reqVO.getIsShowStoreName())
|
||||
.eqIfPresent(TraceabilityCodeDO::getBusinessAddress, reqVO.getBusinessAddress())
|
||||
.eqIfPresent(TraceabilityCodeDO::getIsShowBusinessAddress, reqVO.getIsShowBusinessAddress())
|
||||
.eqIfPresent(TraceabilityCodeDO::getPublicityImage, reqVO.getPublicityImage())
|
||||
.eqIfPresent(TraceabilityCodeDO::getIsShowPublicityImage, reqVO.getIsShowPublicityImage())
|
||||
.eqIfPresent(TraceabilityCodeDO::getPublicityProfile, reqVO.getPublicityProfile())
|
||||
.eqIfPresent(TraceabilityCodeDO::getIsShowPublicityProfile, reqVO.getIsShowPublicityProfile())
|
||||
.eqIfPresent(TraceabilityCodeDO::getInspectionReport, reqVO.getInspectionReport())
|
||||
.eqIfPresent(TraceabilityCodeDO::getCollectImages, reqVO.getCollectImages())
|
||||
.eqIfPresent(TraceabilityCodeDO::getCameraInformation, reqVO.getCameraInformation())
|
||||
.eqIfPresent(TraceabilityCodeDO::getCodeOutCount, reqVO.getCodeOutCount())
|
||||
.eqIfPresent(TraceabilityCodeDO::getQrCode, reqVO.getQrCode())
|
||||
.eqIfPresent(TraceabilityCodeDO::getStatus, reqVO.getStatus())
|
||||
.eqIfPresent(TraceabilityCodeDO::getCodeOutBatch, reqVO.getCodeOutBatch())
|
||||
.eqIfPresent(TraceabilityCodeDO::getScanCodeCount, reqVO.getScanCodeCount())
|
||||
.betweenIfPresent(TraceabilityCodeDO::getCreateTime, reqVO.getCreateTime())
|
||||
// 按CodeOutBatch分组,选择每个分组中最新创建的记录
|
||||
.groupBy(TraceabilityCodeDO::getCodeOutBatch)
|
||||
.in(TraceabilityCodeDO::getId, ids)
|
||||
.orderByDesc(TraceabilityCodeDO::getCreateTime));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user