<?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.ltgk.boat.mapper.DsEnterOutPortMapper">
    <resultMap type="com.ltgk.boat.domain.entity.DsEnterOutPort" id="DsEnterOutPortResult">
        <result property="id" column="id"/>
        <result property="boatName" column="boat_name"/>
        <result property="entryDir" column="entry_dir"/>
        <result property="enterOutTime" column="enter_out_time"/>
        <result property="portName" column="port_name"/>
        <result property="shipFlag" column="ship_flag"/>
        <result property="deviceType" column="device_type"/>
        <result property="deviceCode" column="device_code"/>
        <result property="createAt" column="create_at"/>
        <result property="areaCoed" column="area_coed"/>
        <result property="areaName" column="area_name"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <select id="selectBoatGoSea" resultType="com.ltgk.boat.domain.vo.BoatGoSeaVo">
        select DATE(enter_out_time) as date, count(id) as num
        from ds_enter_out_port
        where enter_out_time &gt;= #{startTime}
          and enter_out_time &lt;= #{endTime}
          and entry_dir = 1
        group by date
        order by date asc
    </select>
    <select id="findAutomaticIdentificationList" resultType="com.ltgk.boat.domain.vo.AutomaticIdentificationVO">
        select * from (
        SELECT deop.enter_out_time AS time,
        deop.boat_id as boatId,
        dbd.boat_type AS boatType,
        deop.boat_name AS boatName,
        deop.port_name AS portName,
        IF(deop.entry_dir = 0, '进港', IF(deop.entry_dir = 1, '出港', '未知')) AS entryOrOutType,
        "终端识别" AS recognitionType,
        "" AS pictureUrl,
        "" AS videoUrl,
        deop.device_code AS terminalCode
        FROM ds_enter_out_port AS deop
        left join ds_boat_data AS dbd ON deop.boat_id = dbd.boat_id
        <where>
            <if test="req.boatName != null and req.boatName != ''">
                AND deop.boat_name LIKE concat('%', #{req.boatName}, '%')
            </if>
            <if test="req.startTime != null and req.endTime != null">
                AND deop.enter_out_time BETWEEN #{req.startTime} AND #{req.endTime}
            </if>
            <if test="req.entryOrOutType != null and req.entryOrOutType == 3">
                AND (deop.entry_dir = 0 OR deop.entry_dir = 1)
            </if>
            <if test="req.entryOrOutType != null and req.entryOrOutType != 3">
                AND deop.entry_dir = #{req.entryOrOutType}
            </if>
        </where>
        UNION ALL
        SELECT vi.take_time AS time,
        dbd.boat_id as boatId,
        dbd.boat_type AS boatType,
        vi.boat_name AS boatName,
        vi.port_name AS portName,
        vi.entry_out AS entryOrOutType,
        "卡口识别" AS recognitionType,
        vi.picture_url AS pictureUrl,
        vi.video_url AS videoUrl,
        device.mmsi AS terminalCode
        FROM video_identification AS vi
        left join ds_boat_data AS dbd ON vi.boat_name = dbd.boat_name
        LEFT JOIN ds_boat_device AS device ON vi.boat_name = device.boat_name AND device.device_type = "AIS"
        <where>
            <if test="req.boatName != null and req.boatName != ''">
                AND vi.boat_name LIKE concat('%', #{req.boatName}, '%')
            </if>
            <if test="req.startTime != null and req.endTime != null">
                AND vi.take_time BETWEEN #{req.startTime} AND #{req.endTime}
            </if>
            <if test="req.entryOrOutType != null and req.entryOrOutType == 3">
                AND vi.entry_out != '无路径'
            </if>
            <if test="req.entryOrOutType != null and req.entryOrOutType == 0">
                AND vi.entry_out = '进港'
            </if>
            <if test="req.entryOrOutType != null and req.entryOrOutType == 1">
                AND vi.entry_out = '出港'
            </if>
        </where>
        ) as a ORDER BY a.time DESC
    </select>

    <select id="findNotReportBoatList" resultType="com.ltgk.boat.domain.vo.NotReportBoatVO">
        SELECT boat.holder_name AS holderName,
        boat.holder_phone AS holderPhone,
        deop.boat_name AS boatName,
        deop.port_name AS portName,
        deop.device_type AS deviceType,
        deop.device_code AS deviceCode,
        deop.enter_out_time AS enterOutTime,
        deop.entry_dir AS entryDir
        FROM ds_enter_out_port deop
        LEFT JOIN ds_boat_data boat ON boat.boat_id = deop.boat_id
        <where>
            <if test="req.boatName != null and req.boatName != ''">
                AND deop.boat_name LIKE concat('%', #{req.boatName}, '%')
            </if>
            <if test="req.portName != null and req.portName != ''">
                AND deop.port_name LIKE concat('%', #{req.portName}, '%')
            </if>
            <if test="req.deviceType != null and req.deviceType != ''">
                AND deop.device_type = #{req.deviceType}
            </if>
            <if test="req.deviceCode != null and req.deviceCode != ''">
                AND deop.device_code LIKE concat('%', #{req.deviceCode}, '%')
            </if>
            <if test="req.entryDir != null">
                AND deop.entry_dir = #{req.entryDir}
            </if>
            <if test="req.startTime != null and req.endTime != null">
                AND deop.enter_out_time BETWEEN #{req.startTime} AND #{req.endTime}
            </if>
        </where>
        ORDER BY enter_out_time DESC
    </select>
</mapper>