溯源码分组查询

This commit is contained in:
顾君禹
2025-09-14 20:19:37 +08:00
parent 0dadcf2aa9
commit 1c55502da5
2 changed files with 97 additions and 40 deletions

View File

@@ -7,7 +7,6 @@ import cn.iocoder.yudao.module.farm.controller.admin.traceabilitycode.vo.Traceab
import cn.iocoder.yudao.module.farm.dal.dataobject.traceabilitycode.TraceabilityCodeDO; import cn.iocoder.yudao.module.farm.dal.dataobject.traceabilitycode.TraceabilityCodeDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -70,45 +69,6 @@ public interface TraceabilityCodeMapper extends BaseMapperX<TraceabilityCodeDO>
* 根据CodeOutBatch分组查询,获取每个批次的最新记录 * 根据CodeOutBatch分组查询,获取每个批次的最新记录
* 使用子查询方式避免GROUP BY的SQL兼容性问题 * 使用子查询方式避免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 &gt;= #{harvestDateStart} </if>" +
"<if test='harvestDateEnd != null'> AND harvest_date &lt;= #{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 &gt;= #{harvestDateStart} </if>" +
"<if test='harvestDateEnd != null'> AND t1.harvest_date &lt;= #{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, List<TraceabilityCodeDO> selectLatestByCodeOutBatch(@Param("tenantId") String tenantId,
@Param("speciesCode") String speciesCode, @Param("speciesCode") String speciesCode,
@Param("speciesName") String speciesName, @Param("speciesName") String speciesName,

View File

@@ -9,4 +9,101 @@
文档可见:https://www.iocoder.cn/MyBatis/x-plugins/ 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/
--> -->
<!-- 根据CodeOutBatch分组查询,获取每个批次的最新记录 -->
<select id="selectLatestByCodeOutBatch" resultType="cn.iocoder.yudao.module.farm.dal.dataobject.traceabilitycode.TraceabilityCodeDO">
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 &gt;= #{harvestDateStart}
</if>
<if test="harvestDateEnd != null">
AND harvest_date &lt;= #{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 &gt;= #{harvestDateStart}
</if>
<if test="harvestDateEnd != null">
AND t1.harvest_date &lt;= #{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
</select>
</mapper> </mapper>