<?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.DsBoatSuspiciousMapper">
    <resultMap id="BaseResultMap" type="com.ltgk.boat.domain.entity.DsBoatSuspicious">
        <!--@mbg.generated-->
        <!--@Table ds_boat_suspicious-->
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="boat_id" jdbcType="VARCHAR" property="boatId"/>
        <result column="boat_name" jdbcType="VARCHAR" property="boatName"/>
        <result column="del_flag" jdbcType="INTEGER" property="delFlag"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_at" jdbcType="TIMESTAMP" property="createAt"/>
    </resultMap>
    <sql id="Base_Column_List">
        <!--@mbg.generated-->
        id, boat_id, boat_name, del_flag, create_by, create_at
    </sql>

    <select id="getBoatSuspiciousList" resultType="com.ltgk.boat.domain.vo.DsSuspiciousVo">
        SELECT
        dbs.id,
        dbs.boat_id AS boatId,
        dba.boat_name AS boatName,
        dba.boat_type AS boatType,
        dbd.deviceType AS deviceType,
        dbd.deviceCode AS deviceCode,
        ddn.gps_time AS gpsTime,
        COALESCE(dbi.totalIllegalScore, 0) AS totalIllegalScore,
        <!-- 新增信用等级字段 -->
        IFNULL(bcg.credit_grade, '低风险等级') AS creditGrade
        FROM ds_boat_suspicious AS dbs
        LEFT JOIN ds_boat_data AS dba ON dbs.boat_id = dba.boat_id
        LEFT JOIN (
        SELECT
        boat_id,
        GROUP_CONCAT(device_type) AS deviceType,
        GROUP_CONCAT(mmsi) AS deviceCode
        FROM ds_boat_device
        GROUP BY boat_id
        ) dbd ON dbs.boat_id = dbd.boat_id
        LEFT JOIN ds_dynamic_new AS ddn ON dbs.boat_id = ddn.boat_id
        LEFT JOIN (
        SELECT
        boat_id,
        SUM(illegal_score) AS totalIllegalScore
        FROM ds_boat_illegal
        GROUP BY boat_id
        ) dbi ON dbs.boat_id = dbi.boat_id
        <!-- 新增信用等级表关联 -->
        LEFT JOIN ds_boat_credit_grade bcg
        ON COALESCE(dbi.totalIllegalScore, 0) BETWEEN bcg.integral_min AND bcg.integral_max
        <where>
            <if test="boatName != null and boatName != ''">
                AND dba.boat_name LIKE concat('%',#{boatName},'%')
            </if>
            <if test="boatType != null and boatType != ''">
                AND dba.boat_type = #{boatType}
            </if>
            <if test="integralMin != null">
                AND COALESCE(dbi.totalIllegalScore, 0) &gt;= #{integralMin}
            </if>
            <if test="integralMax != null">
                AND COALESCE(dbi.totalIllegalScore, 0) &lt;= #{integralMax}
            </if>
        </where>
    </select>
</mapper>