<?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.DeliveryMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.aiotagro.cattletrade.business.entity.Delivery">
        <id column="id" property="id" />
        <result column="delivery_number" property="deliveryNumber" />
        <result column="buyer_price" property="buyerPrice" />
        <result column="sale_price" property="salePrice" />
        <result column="firm_price" property="firmPrice" />
        <result column="status" property="status" />
        <result column="license_plate" property="licensePlate" />
        <result column="car_front_photo" property="carFrontPhoto" />
        <result column="car_behind_photo" property="carBehindPhoto" />
        <result column="car_video" property="carVideo" />
        <result column="start_location" property="startLocation" />
        <result column="start_lat" property="startLat" />
        <result column="start_lon" property="startLon" />
        <result column="end_location" property="endLocation" />
        <result column="end_lat" property="endLat" />
        <result column="end_lon" property="endLon" />
        <result column="estimated_delivery_time" property="estimatedDeliveryTime" />
        <result column="registered_jbq_count" property="registeredJbqCount" />
        <result column="driver_name" property="driverName" />
        <result column="driver_mobile" property="driverMobile" />
        <result column="create_time" property="createTime" />
        <result column="created_by" property="createdBy" />
        <result column="check_by" property="checkBy" />
        <result column="check_time" property="checkTime" />
        <result column="check_video" property="checkVideo" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, delivery_number, buyer_price, sale_price, firm_price, status, license_plate, car_front_photo, car_behind_photo, car_video, start_location, start_lat, start_lon, end_location, end_lat, end_lon, estimated_delivery_time, registered_jbq_count, driver_name, driver_mobile, create_time, created_by, check_by, check_time, check_video
    </sql>

    <select id="getPageWarningLog" resultType="com.aiotagro.cattletrade.business.entity.Delivery">
        SELECT
        d.id,
        d.delivery_number,
        d.`status`,
        d.license_plate,
        d.car_front_photo,
        d.car_behind_photo,
        d.car_video,
        d.start_location,
        d.start_lat,
        d.start_lon,
        d.end_location,
        d.end_lat,
        d.end_lon,
        d.estimated_delivery_time,
        d.registered_jbq_count,
        d.driver_name,
        d.driver_mobile,
        d.create_time,
        d.created_by,
        d.check_by,
        d.check_time,
        d.check_video,
        wl.warning_type,
        wl.warning_time,
        wl.inventory_jbq_count,
        su.name as createByName
        FROM delivery d inner join warning_log wl on d.id = wl.delivery_id
        left join sys_user su on d.created_by = su.id
        <where>
                wl.warning_type in (2,3,4,5,6,7,8,9)
            and wl.id in (select max(id) from warning_log where delivery_id = d.id group by warning_type)
            <if test="dto.deliveryNumber != null and '' != dto.deliveryNumber">
                and d.delivery_number like concat('%', #{dto.deliveryNumber}, '%')
            </if>
            <if test="dto.licensePlate != null and '' != dto.licensePlate">
                and d.license_plate like concat('%', #{dto.licensePlate}, '%')
            </if>
            <if test="dto.startTime != null and '' != dto.startTime">
                and d.create_time <![CDATA[ >= ]]> #{dto.startTime}
            </if>
            <if test="dto.endTime != null and '' != dto.endTime">
                and d.create_time <![CDATA[ <= ]]> #{dto.endTime}
            </if>
            <if test="dto.warningType != null">
                and wl.warning_type = #{dto.warningType}
            </if>
            <if test="dto.status != null">
                and d.status = #{dto.status}
            </if>
        </where>
        order by wl.warning_time desc
    </select>

</mapper>
