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

    <resultMap type="com.ltgk.boat.domain.entity.BoatAlarmInfo" id="BoatAlarmInfoMap">
        <result property="id" column="id"/>
        <result property="alarmType" column="alarm_type"/>
        <result property="alarmNature" column="alarm_nature"/>
        <result property="alarmArea" column="alarm_area"/>
        <result property="mmsi" column="mmsi"/>
        <result property="targetSourceId" column="target_source_id"/>
        <result property="areaCode" column="area_code"/>
        <result property="boatName" column="boat_name"/>
        <result property="areaNumber" column="area_number"/>
        <result property="lastLongitude" column="last_longitude"/>
        <result property="lastLatitude" column="last_latitude"/>
        <result property="lastLocationTime" column="last_location_time"/>
        <result property="lastBwlx" column="last_bwlx"/>
        <result property="speed" column="speed"/>
        <result property="course" column="course"/>
        <result property="alarmTime" column="alarm_time"/>
        <result property="acceptState" column="accept_state"/>
        <result property="imgUrl" column="img_url"/>
        <result property="imgTime" column="img_time"/>
        <result property="videoUrl" column="video_url"/>
        <result property="createBy" column="create_by"/>
        <result property="createAt" column="create_at"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateAt" column="update_at"/>
        <result property="delFlag" column="del_flag"/>
        <result property="acceptUserId" column="accept_user_id"/>
        <result property="acceptUserName" column="accept_user_name"/>
        <result property="acceptTime" column="accept_time"/>
        <result property="verifySituation" column="verify_situation"/>
        <result property="participants" column="participants"/>
        <result property="processResults" column="process_results"/>
        <result property="processUserName" column="process_user_name"/>
        <result property="processUserId" column="process_user_id"/>
        <result property="remark" column="remark"/>
        <result property="length" column="length"/>
    </resultMap>

    <select id="page" resultType="com.ltgk.boat.domain.vo.BoatAlarmInfoVo">
        select
        ba.id, ba.alarm_type as alarmType, ba.alarm_nature as alarmNature, ba.alarm_area as alarmArea,
        ba.mmsi, b.area_code as areaCode, b.area_name as areaName, ba.boat_name as boatName, ba.area_number as
        areaNumber,
        ba.last_longitude as lastLongitude, ba.last_latitude as lastLatitude, ba.last_location_time as lastLocationTime,
        ba.last_bwlx as lastBwlx, ba.alarm_time as alarmTime, ba.accept_state as acceptState, ba.img_url as imgUrl,
        ba.video_url as videoUrl, b.length, b.width, b.height, b.tonnage, b.net_tonnage as netTonnage,
        b.main_power as mainPower, b.holder_name as holderName, b.holder_address as holderAddress, b.holder_phone as
        holderPhone,b.boat_id as boatId,
        b.boat_type as boatType, ba.mmsi AS terminalCode,
        ba.accept_user_id AS acceptUserId, ba.accept_user_name AS acceptUserName, ba.accept_time AS acceptTime,
        ba.verify_situation AS verifySituation, ba.participants AS participants, ba.process_results AS processResults,
        ba.process_user_name AS processUserName, ba.process_user_id AS processUserId, ba.target_source_id AS
        targetSourceId,
        ba.img_time AS imgTime, ba.remark AS remark, ba.speed AS speed, ba.course AS course
        from boat_alarm_info ba
        left join ds_boat_data b on ba.boat_name = b.boat_name
        where ba.del_flag = 0
        <if test="boatName != null and boatName != ''">
            and ba.boat_name like concat('%',#{boatName},'%')
        </if>
        <if test="mmsi != null and mmsi != ''">
            and ba.mmsi like concat('%',#{mmsi},'%')
        </if>
        <if test="targetSourceId != null and targetSourceId != ''">
            and ba.target_source_id like concat('%',#{targetSourceId},'%')
        </if>
        <if test="alarmNature != null and alarmNature != ''">
            and ba.alarm_nature = #{alarmNature}
        </if>
        <if test="alarmArea != null and alarmArea != ''">
            and ba.alarm_area = #{alarmArea}
        </if>
        <if test="areaNumber != null and areaNumber != ''">
            and ba.area_number = #{areaNumber}
        </if>
        <if test="startTime != null and startTime != ''">
            and ba.last_location_time &gt;= #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
            and ba.last_location_time &lt;= #{endTime}
        </if>
        <if test="acceptState != null">
            and ba.accept_state = #{acceptState}
        </if>
        <if test="appQueryParams != null and appQueryParams != ''">
            and (
                ba.boat_name like concat('%',#{appQueryParams},'%')
                OR ba.process_user_name like concat('%',#{appQueryParams},'%')
                OR ba.process_results like concat('%',#{appQueryParams},'%')
            )
        </if>
        <if test="noThreeNoBoat != null and noThreeNoBoat">
            <if test="alarmTypeList != null and !alarmTypeList.isEmpty()">
                AND ba.alarm_type IN
                <foreach collection="alarmTypeList" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </if>
        <if test="noThreeNoBoat != null and !noThreeNoBoat">
            <if test="onlyThreeNoBoat != null and onlyThreeNoBoat">
                AND ba.alarm_type = '疑似三无船只'
                AND ba.update_at &gt;= DATE_SUB(NOW(), INTERVAL 30 MINUTE)
                AND ba.img_url IS NOT NULL
            </if>
            <if test="onlyThreeNoBoat != null and !onlyThreeNoBoat">
                AND (
                <!-- 条件1：查询所有非"疑似三无船只"类型 -->
                ba.alarm_type != '疑似三无船只'
                <!-- 当有alarmTypeList时，限制类型 -->
                <if test="alarmTypeList != null and !alarmTypeList.isEmpty()">
                    AND ba.alarm_type IN
                    <foreach collection="alarmTypeList" item="item" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
                OR
                <!-- 条件2：查询"疑似三无船只"类型且更新时间在30分钟内 -->
                (
                ba.alarm_type = '疑似三无船只'
                AND ba.update_at &gt;= DATE_SUB(NOW(), INTERVAL 30 MINUTE)
                AND ba.img_url IS NOT NULL
                )
                )
            </if>
        </if>
        ORDER BY ba.alarm_time DESC
    </select>
</mapper>

