<?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.DsBoatDataMapper">
    <select id="selectByPage" resultType="com.ltgk.boat.domain.entity.DsBoatData">
        SELECT boat.*,
        dbn.home_port as homePort,
        if(TIMESTAMPADD(HOUR,-2,now()) > dynamic_new.gps_time OR dynamic_new.gps_time IS NULL,'离线','在线') as
        lineType,
        dynamic_new.gps_time as gpsTime, dbh.port_id as homePortId, dbh.port_name as homePortName,dbh.create_at as
        homePortBuildTime,
        dbf.fishing_code as fishingCode,dbf.main_work_type as mainWorkType,dbf.double_work_type as doubleWorkType
        FROM
        ds_boat_data AS boat
        LEFT JOIN (SELECT a.* FROM (SELECT latitude,longitude,sog,cog,gps_time,boat_name FROM
        ds_dynamic_new ORDER BY gps_time DESC LIMIT 99999) a GROUP BY a.boat_name) dynamic_new ON
        dynamic_new.boat_name=boat.boat_name
        LEFT JOIN ds_boat_home_port as dbh ON dbh.boat_id=boat.boat_id
        LEFT JOIN ds_boat_fishing as dbf
        ON dbf.boat_id = boat.boat_id
        AND dbf.certificate_validity = (SELECT MAX(certificate_validity) FROM ds_boat_fishing WHERE boat_id =
        boat.boat_id ORDER BY certificate_validity DESC LIMIT 99999)
        LEFT JOIN (
        SELECT boat_id, home_port, update_at,
        ROW_NUMBER() OVER (PARTITION BY boat_id ORDER BY update_at DESC) AS rn
        FROM ds_boat_nationality
        ) dbn ON dbn.boat_id = boat.boat_id AND dbn.rn = 1
        WHERE
        boat.del_flag = 0
        <if test="params.boatName != null and params.boatName != ''">
            and boat.boat_name like concat('%',#{params.boatName},'%')
        </if>
        <if test="params.lineType != null and params.lineType != '' and params.lineType == '在线'">
            and TIMESTAMPADD(HOUR,-2,now()) &lt;= dynamic_new.gps_time
        </if>
        <if test="params.lineType != null and params.lineType != '' and params.lineType == '离线'">
            and (TIMESTAMPADD(HOUR,-2,now()) > dynamic_new.gps_time OR dynamic_new.gps_time IS NULL)
        </if>
        <if test="params.boatType != null and params.boatType != ''">
            and boat.boat_type =#{params.boatType}
        </if>
        <if test="params.holderName != null and params.holderName != ''">
            and boat.holder_name like concat('%',#{params.holderName},'%')
        </if>
        <if test="params.operatorUserId != null and params.operatorUserId != ''">
            and boat.operator_user_id = #{params.operatorUserId}
        </if>
        <if test="params.holderPhone != null and params.holderPhone != ''">
            and boat.holder_phone like concat('%',#{params.holderPhone},'%')
        </if>
        <if test="params.isLocal == '0'.toString()">
            and (boat.is_local is null or boat.is_local = '' or boat.is_local = '0')
        </if>
        <if test="params.isLocal == '1'.toString()">
            and boat.is_local = '1'
        </if>
        <if test="params.areaName != null and params.areaName != ''">
            and boat.area_name like concat('%',#{params.areaName},'%')
        </if>
        <if test="params.areaCode != null and params.areaCode != ''">
            and boat.area_code = #{params.areaCode}
        </if>
        <if test="params.minLength != null and params.minLength != ''">
            and boat.length &gt;= #{params.minLength}
        </if>
        <if test="params.maxLength != null and params.maxLength != ''">
            and boat.length &lt;= #{params.maxLength}
        </if>
        <if test="params.minPower != null and params.minPower != ''">
            and boat.main_power &gt;= #{params.minPower}
        </if>
        <if test="params.maxPower != null and params.maxPower != ''">
            and boat.main_power &lt;= #{params.maxPower}
        </if>
        <if test="params.startTime != null and params.startTime != ''">
            and boat.update_at &gt;= #{params.startTime}
        </if>
        <if test="params.endTime != null and params.endTime != ''">
            and boat.update_at &lt;= #{params.endTime}
        </if>
        <if test="params.boatCode != null and params.boatCode != ''">
            and boat.boat_code like concat('%',#{params.boatCode},'%')
        </if>

        <if test="params.queryStartPower != null and params.queryStartPower != ''">
            and boat.main_power &gt; #{params.queryStartPower}
        </if>
        <if test="params.queryEndPower != null and params.queryEndPower != ''">
            and boat.main_power &lt;= #{params.queryEndPower}
        </if>
        <if test="params.boatIdList != null and !params.boatIdList.isEmpty()">
            and boat.boat_id IN
            <foreach collection="params.boatIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>

        order by boat.create_at desc
    </select>

    <select id="selectBoatCount" resultType="java.lang.Long">
        select count(boat_id) from ds_boat_data
        where del_flag = 0
        <if test="isLocal != null">
            and is_local = #{isLocal}
        </if>
    </select>

    <select id="getBoatCredentialOverDueCount" resultType="java.lang.Long">
        select sum(a.number)
        from (
                 /** 捕捞许可证 **/
                 select count(id) as number
                 from ds_boat_fishing
                 where del_flag = 0
                   and certificate_validity &lt;= #{date}
                 union all
                 /** 渔船检验 **/
                 select count(id) as number
                 from ds_boat_examine
                 where del_flag = 0
                   and examine_validity &lt;= #{date}
                 union all
                 /** 船名登记 **/
                 select count(id) as number
                 from ds_boat_name
                 where del_flag = 0
                   and certificate_validity &lt;= #{date}
                 union all
                 /** 国籍所有权登记 **/
                 select count(id) as number
                 from ds_boat_nationality
                 where del_flag = 0
                   and certificate_validity &lt;= #{date}
                 union all
                 /** 渔业船网工具 **/
                 select count(id) as number
                 from ds_boat_net
                 where del_flag = 0
                   and certificate_validity &lt;= #{date}) as a
    </select>

    <select id="getBoatCredentialOverDueList" resultType="com.ltgk.boat.domain.vo.BoatCredentialOverDueVo">
        select * from (
        /** 捕捞许可证 **/
        select bf.boat_name as boatName, bf.certificate_validity as certificateValidity,
        '渔船捕捞许可证' as certificateType, bf.fishing_code as certificateCode,bf.sign_date as
        signDate,bf.sign_dept as signDept, bf.boat_id AS boatId, bf.create_at AS createAt,
        bd.code as deviceCode
        from ds_boat_fishing as bf left join ds_boat_device as bd on bf.boat_id = bd.boat_id and
        bd.device_type = 'AIS'
        where bf.del_flag = 0
        <if test="date != null">
            and bf.certificate_validity &lt;= #{date}
        </if>
        <if test="boatName != null and boatName != ''">
            and bf.boat_name like concat('%',#{boatName},'%')
        </if>
        <if test="deviceCode != null and deviceCode != ''">
            and bd.code like concat('%',#{deviceCode},'%')
        </if>
        <if test="boatIdList != null and !boatIdList.isEmpty()">
            and bf.boat_id IN
            <foreach collection="boatIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        union all
        /** 渔船检验 **/
        select bx.boat_name as boatName, bx.extension_validity as certificateValidity,
        '渔船检验证书' as certificateType, bx.examine_code as certificateCode,bx.examine_validity as
        signDate,bx.examine_dept as signDept, bx.boat_id AS boatId,bx.create_at AS createAt,
        bd.code as deviceCode from ds_boat_examine as bx left join ds_boat_device as bd on bx.boat_id = bd.boat_id and
        bd.device_type = 'AIS'
        where bx.del_flag = 0
        <if test="date != null">
            and bx.examine_validity &lt;= #{date}
        </if>
        <if test="boatName != null and boatName != ''">
            and bx.boat_name like concat('%',#{boatName},'%')
        </if>
        <if test="deviceCode != null and deviceCode != ''">
            and bd.code like concat('%',#{deviceCode},'%')
        </if>
        <if test="boatIdList != null and !boatIdList.isEmpty()">
            and bx.boat_id IN
            <foreach collection="boatIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        union all
        /** 船名登记 **/
        select bn.boat_name as boatName, bn.certificate_validity as certificateValidity,
        '渔船登记证书' as certificateType, bn.boat_name_approval_code as certificateCode,bn.apply_date as signDate,
        ''
        as signDept,bn.boat_id AS boatId,bn.create_at AS createAt,
        bd.code as deviceCode from ds_boat_name as bn left join ds_boat_device as bd on bn.boat_id = bd.boat_id and
        bd.device_type = 'AIS'
        where bn.del_flag = 0
        <if test="date != null">
            and bn.certificate_validity &lt;= #{date}
        </if>
        <if test="boatName != null and boatName != ''">
            and bn.boat_name like concat('%',#{boatName},'%')
        </if>
        <if test="deviceCode != null and deviceCode != ''">
            and bd.code like concat('%',#{deviceCode},'%')
        </if>
        <if test="boatIdList != null and !boatIdList.isEmpty()">
            and bn.boat_id IN
            <foreach collection="boatIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        union all
        /** 国籍所有权登记 **/
        select bn.boat_name as boatName, bn.certificate_validity as certificateValidity,
        '国籍所有权登记证书' as certificateType, bn.ownership_code as certificateCode,bn.approval_date as
        signDate,bn.approval_authority as signDept,bn.boat_id AS boatId,bn.create_at AS createAt,
        bd.code as deviceCode from ds_boat_nationality as bn left join ds_boat_device as bd on bn.boat_id =
        bd.boat_id and bd.device_type = 'AIS'
        where bn.del_flag = 0
        <if test="date != null">
            and bn.certificate_validity &lt;= #{date}
        </if>
        <if test="boatName != null and boatName != ''">
            and bn.boat_name like concat('%',#{boatName},'%')
        </if>
        <if test="deviceCode != null and deviceCode != ''">
            and bd.code like concat('%',#{deviceCode},'%')
        </if>
        <if test="boatIdList != null and !boatIdList.isEmpty()">
            and bn.boat_id IN
            <foreach collection="boatIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        union all
        /** 渔业船网工具 **/
        select bn.boat_name as boatName, bn.certificate_validity as certificateValidity,
        '渔业船网工具登记证书' as certificateType, bn.apply_net_code as certificateCode,bn.sign_date as
        signDate,bn.sign_dept as signDept,bn.boat_id AS boatId,bn.create_at AS createAt,
        bd.code as deviceCode from ds_boat_net as bn left join ds_boat_device as bd on bn.boat_id = bd.boat_id and
        bd.device_type = 'AIS'
        where bn.del_flag = 0
        <if test="date != null">
            and bn.certificate_validity &lt;= #{date}
        </if>
        <if test="boatName != null and boatName != ''">
            and bn.boat_name like concat('%',#{boatName},'%')
        </if>
        <if test="deviceCode != null and deviceCode != ''">
            and bd.code like concat('%',#{deviceCode},'%')
        </if>
        <if test="boatIdList != null and !boatIdList.isEmpty()">
            and bn.boat_id IN
            <foreach collection="boatIdList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        ) a
        <where>
            <if test="credentialType != null and credentialType != ''">
                and a.certificateType = #{credentialType}
            </if>
        </where>
    </select>

    <select id="getBoatInfoByCondition" resultType="com.ltgk.boat.domain.vo.BoatInfoByConditionVo">
        select dbd.boat_id as boatId , dbd.boat_name as boatName, dbd.type_id as typeId, dbd.device_type as deviceType,
        dbd.mmsi, dbd.code, dbd.type, dbd.cmd_sign as cmdSign, bd.boat_type as boatType, bd.boat_code as boatCode,
        bd.is_local as isLocal
        , bd.holder_name as holderName, bd.holder_address as holderAddress, bd.main_power as mainPower, bd.holder_phone
        as holderPhone,
        bn.home_port as homePort, bn.boat_image as boatImage, bd.length, bd.width, bd.height, bd.tonnage,
        bd.net_tonnage,bbw.type as isBlack
        from ds_boat_device as dbd
        left join ds_boat_data as bd on dbd.boat_id = bd.boat_id
        left join ds_boat_nationality as bn on bd.boat_id = bn.boat_id
        left join ds_boat_black_white as bbw on bd.boat_id = bbw.boat_id
        where dbd.del_flag = 0
        <if test="boatName != null and boatName != ''">
            and dbd.boat_name like concat('%',#{boatName},'%')
        </if>
        <if test="boatId != null and boatId != ''">
            and dbd.boat_id = #{boatId}
        </if>
        <if test="deviceCode != null and deviceCode != ''">
            and dbd.mmsi = #{deviceCode}
        </if>
        <if test="deviceType != null and deviceType != ''">
            and dbd.device_type = #{deviceType}
        </if>
        order by dbd.boat_id desc limit 1
    </select>
    <select id="getCameraCount" resultType="java.lang.Long">
        SELECT COUNT(vi.id)
        FROM video_identification AS vi
        WHERE vi.del_flag = 0
          AND vi.take_time &gt;= #{beginOfDay}
          AND vi.identification_type != '烟火事件'
          AND vi.boat_name != '未检测到船名'
          AND EXISTS(SELECT 1 FROM ds_video AS dv WHERE dv.video_ip = vi.video_ip AND dv.be_bayonet = 1)
    </select>
    <select id="getOnlineCount" resultType="java.lang.Long">
        select count(boat_id)
        from ds_dynamic_new
        where del_flag = 0
          and gps_time &gt;= #{beforeTwoHourDate}
    </select>

    <select id="getPortBerthList" resultType="com.ltgk.boat.domain.vo.PortBerthVo">
        select bp.port_name as portName, bp.berth_num as berthCount, bp.id as portId
        from ds_port as bp
        where bp.del_flag = 0
    </select>

    <select id="getBoatEnterOut" resultType="com.ltgk.boat.domain.vo.GetBoatEnterOutVo">
        select * from (
        select dep.boat_id as boatId, dep.boat_name as boatName, dep.port_id as portId, dep.port_name as portName,
        dep.enter_time as enterOutTime, dep.status as status, dep.review_user_name as reviewUserName,'进港' as
        enterOrOut,
        dep.review_time as reviewTime, bd.boat_material as boatMaterial, bd.length as length, bd.main_power as
        mainPower,bd.boat_type as boatType,bd.boat_code as boatCode,bd.is_local as isLocal,bd.holder_name as
        holderName, dep.ais_number AS terminalCode
        from ds_enter_port as dep left join ds_boat_data as bd on dep.boat_id = bd.boat_id
        where
        dep.del_flag = 0
        <if test="req.startTime != null and req.startTime != ''">
            and dep.enter_time &gt;= #{req.startTime}
        </if>
        <if test="req.endTime != null and req.endTime != ''">
            and dep.enter_time &lt;= #{req.endTime}
        </if>
        <if test="req.boatName != null and req.boatName != ''">
            and dep.boat_name like concat('%',#{req.boatName},'%')
        </if>
        <if test="req.portName != null and req.portName != ''">
            and dep.port_name like concat('%',#{req.portName},'%')
        </if>
        union all
        select dep.boat_id as boatId, dep.boat_name as boatName, dep.port_id as portId, dep.port_name as portName,
        dep.out_time as enterOutTime, dep.status as status, dep.review_user_name as reviewUserName,'出港' as
        enterOrOut,
        dep.review_time as reviewTime, bd.boat_material as boatMaterial, bd.length as length, bd.main_power as
        mainPower,bd.boat_type as boatType,bd.boat_code as boatCode,bd.is_local as isLocal,bd.holder_name as
        holderName, dep.ais_number AS terminalCode
        from ds_out_port as dep left join ds_boat_data as bd on dep.boat_id = bd.boat_id
        where dep.del_flag = 0
        <if test="req.startTime != null and req.startTime != ''">
            and dep.out_time &gt;= #{req.startTime}
        </if>
        <if test="req.endTime != null and req.endTime != ''">
            and dep.out_time &lt;= #{req.endTime}
        </if>
        <if test="req.boatName != null and req.boatName != ''">
            and dep.boat_name like concat('%',#{req.boatName},'%')
        </if>
        <if test="req.portName != null and req.portName != ''">
            and dep.port_name like concat('%',#{req.portName},'%')
        </if>
        ) as a
        <where>
            <if test="type != null and type != ''">
                and a.enterOrOut = #{type}
            </if>
        </where>
        order by a.enterOutTime DESC
    </select>
    <select id="getCertificateValidityList" resultType="com.ltgk.boat.domain.vo.CertificateValidityVO">
        SELECT boat_id                  AS boatId,
               MAx(fishingValidity)     AS fishingValidity,
               MAx(examineValidity)     AS examineValidity,
               MAx(nameValidity)        AS nameValidity,
               MAx(nationalityValidity) AS nationalityValidity,
               MAx(netValidity)         AS netValidity
        FROM (
                 /** 捕捞许可证 **/
                 SELECT boat_id,
                        certificate_validity AS fishingValidity,
                        NULL                 AS examineValidity,
                        NULL                 AS nameValidity,
                        NULL                 AS nationalityValidity,
                        NULL                 AS netValidity
                 FROM ds_boat_fishing
                 WHERE del_flag = 0
                 UNION ALL
                 /** 渔船检验 **/
                 SELECT boat_id,
                        NULL             AS fishingValidity,
                        examine_validity AS examineValidity,
                        NULL             AS nameValidity,
                        NULL             AS nationalityValidity,
                        NULL             AS netValidity
                 FROM ds_boat_examine
                 WHERE del_flag = 0
                 UNION ALL
                 /** 船名登记 **/
                 SELECT boat_id,
                        NULL                 AS fishingValidity,
                        NULL                 AS examineValidity,
                        certificate_validity AS nameValidity,
                        NULL                 AS nationalityValidity,
                        NULL                 AS netValidity
                 FROM ds_boat_name
                 WHERE del_flag = 0
                 UNION ALL
                 /** 国籍所有权登记 **/
                 SELECT boat_id,
                        NULL                 AS fishingValidity,
                        NULL                 AS examineValidity,
                        NULL                 AS nameValidity,
                        certificate_validity AS nationalityValidity,
                        NULL                 AS netValidity
                 FROM ds_boat_nationality
                 WHERE del_flag = 0
                 UNION ALL
                 /** 渔业船网工具 **/
                 SELECT boat_id,
                        NULL                 AS fishingValidity,
                        NULL                 AS examineValidity,
                        NULL                 AS nameValidity,
                        NULL                 AS nationalityValidity,
                        certificate_validity AS netValidity
                 FROM ds_boat_net
                 WHERE del_flag = 0) AS boatCertificate
        GROUP BY boat_id
        ORDER BY boat_id
    </select>
    <select id="findBoatAisListByPower" resultType="com.ltgk.system.domain.vo.DsDynamicRadarVO">
        SELECT boat.boat_id AS boatId,
        boat.boat_name AS boatName,
        boat.main_power AS mainPower,
        boat.length AS length,
        device.mmsi AS mmsi,
        device.device_type AS deviceType
        FROM ds_boat_data AS boat
        LEFT JOIN ds_boat_device AS device ON device.boat_id = boat.boat_id
        WHERE device.device_type = 'AIS'
        <if test="params.queryStartPower != null and params.queryStartPower != ''">
            and boat.main_power &gt; #{params.queryStartPower}
        </if>
        <if test="params.queryEndPower != null and params.queryEndPower != ''">
            and boat.main_power &lt;= #{params.queryEndPower}
        </if>
    </select>
</mapper>