<?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.DsBoatIllegalMapper">
    <select id="getBoatIllegalList" resultType="com.ltgk.boat.domain.entity.DsBoatIllegal">
        SELECT
        a.*,
        b.integral_value as integralValue
        FROM
        ds_boat_illegal a
        inner join ds_boat_data bd on bd.boat_name=a.boat_name
        LEFT JOIN ds_boat_credit_integral b ON a.illegal_type = b.integral_behavior
        WHERE a.del_flag = 0
        <if test="params.boatName != null and params.boatName != ''">
            AND a.boat_name like concat('%',#{params.boatName},'%')
        </if>
        <if test="params.illegalType != null and params.illegalType != ''">
            AND a.illegal_type = #{params.illegalType}
        </if>
        <if test="params.startTime != null and params.startTime != ''">
            <![CDATA[AND a.illegal_time >= #{params.startTime}]]>
        </if>
        <if test="params.endTime != null and params.endTime != ''">
            <![CDATA[AND a.illegal_time <= #{params.endTime}]]>
        </if>
        order by a.update_at desc
    </select>
    <select id="findBoatCreditList" resultType="com.ltgk.boat.domain.vo.DsBoatCreditVO">
        SELECT
        dbd.boat_id,
        dbd.boat_name,
        COALESCE(t.totalIllegalScore, 0) AS totalIllegalScore,
        IFNULL(bcg.credit_grade, '低风险等级') AS creditGrade
        FROM
        ds_boat_data dbd
        LEFT JOIN (SELECT boat_id, SUM(illegal_score) AS totalIllegalScore FROM ds_boat_illegal GROUP BY boat_id) t ON
        dbd.boat_id = t.boat_id
        LEFT JOIN ds_boat_credit_grade bcg ON t.totalIllegalScore BETWEEN bcg.integral_min AND bcg.integral_max
        <where>
            <if test="req.boatName != null and req.boatName != ''">
                and dbd.boat_name like concat('%',#{req.boatName},'%')
            </if>
        </where>
        ORDER BY totalIllegalScore DESC
    </select>
</mapper>