<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.aiotagro.cattletrade.business.mapper.WarningLogMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.aiotagro.cattletrade.business.entity.WarningLog">
        <id column="id" property="id" />
        <result column="delivery_id" property="deliveryId" />
        <result column="warning_type" property="warningType" />
        <result column="inventory_jbq_count" property="inventoryJbqCount" />
        <result column="expected_distance" property="expectedDistance" />
        <result column="actual_distance" property="actualDistance" />
        <result column="warning_time" property="warningTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, delivery_id, warning_type, inventory_jbq_count, expected_distance, actual_distance, warning_time
    </sql>
    <select id="getNewWarningLog" resultType="com.aiotagro.cattletrade.business.entity.WarningLog">

        SELECT
            <include refid="Base_Column_List"/>
        from warning_log
        <where>
            id in(select  max(wl.id) from warning_log wl
            left join delivery d on d.id = wl.delivery_id
            <where>
                (d.created_by = #{dto.userId} or d.check_by = #{dto.userId})
            </where>
            group by wl.delivery_id,wl.warning_type)
            <if test="dto.deliveryNumber != null and '' != dto.deliveryNumber">
               and delivery_id = (select id from delivery where delivery_number concat('%',#{dto.deliveryNumber},'%') and status = 1 limit 1)
            </if>
            <if test="dto.warningType != null">
                and warning_type = #{dto.warningType}
            </if>
        </where>

    </select>


</mapper>
