<?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="cn.com.taiji.beidou.ship.warning.record.mapper.WarningRecordMapper">
    <resultMap id="byRule" type="statisticsByRuleResult">
        <id property="ruleId" column="rule_id"/>
        <result property="ruleName" column="rule_name"/>
        <result property="count" column="count_num"/>
    </resultMap>

    <select id="statisticsByRule" resultMap="byRule">
        select rule.id as rule_id,rule.`name` as rule_name,count(record.id) as count_num
        from ax_beidou_warning_rule rule
        LEFT JOIN ax_beidou_warning_record record on record.rule_id = rule.id
        where rule.del_flag = '0'
        GROUP BY rule.id,rule_name ORDER BY rule.id
    </select>

    <select id="warningRecordList" parameterType="string" resultType="warningRecordListResult">
        SELECT record.id, archives.devide_no AS devideNo, archives.ship_id AS shipId,
        archives.owner_name AS ownerName, archives.owner_tel_no AS ownerTelNo,
        record.model_code as modelCode,area.name as warningDetail,
        record.warning_time AS warningTime, dis.id AS disposeId ,temp.num as num,
        location.location
        FROM ax_beidou_warning_record record
        INNER JOIN (select rec.device_id,MAX(rec.id) as id,count(rec.device_id) AS num
                    from ax_beidou_warning_record rec
                    where rec.rule_id = #{ruleId}
                    <if test="startTime != null and startTime != ''">and rec.warning_time &gt; #{startTime}</if>
                    <if test="endTime != null and endTime != ''">and rec.warning_time &lt; #{endTime}</if>
                     GROUP BY rec.device_id) as temp on record.id = temp.id
        INNER JOIN ax_beidou_ship_archives archives ON archives.devide_no = record.device_id
        LEFT JOIN ax_beidou_ship_dispose dis ON record.id = dis.warning_id
        LEFT JOIN ax_beidou_warning_area area ON area.id = record.area_id
        LEFT JOIN ax_beidou_ship_location location ON location.device_id = record.device_id
        <if test="deptId != null and deptId != ''">
        INNER JOIN ax_beidou_dept dept ON archives.police_station_id = dept.pscbh
        </if>
        <where>
            record.rule_id = #{ruleId}
            <if test="deptId != null and deptId != ''">
                and dept.xw_dept_id like concat('%',#{deptId},'%')
            </if>
            <if test="shipId != null and shipId != ''">
                and archives.ship_id like concat('%',#{shipId},'%')
            </if>
            <if test="startTime != null and startTime != ''">and record.warning_time &gt; #{startTime}</if>
            <if test="endTime != null and endTime != ''">and record.warning_time &lt; #{endTime}</if>
        </where>
        ORDER BY  record.warning_time DESC
    </select>

    <select id="getWarningRecordByDeviceId" parameterType="string" resultType="simpleWarningRecordResult">
       select record.id,warning_time as warningTime,dis.id as disposeId
        from ax_beidou_warning_record record
        LEFT JOIN ax_beidou_ship_dispose dis ON dis.warning_id = record.id
        where record.device_id = #{devideNo} and record.rule_id = #{ruleId} ORDER BY warning_time DESC
    </select>

    <select id="warningRecordExcel" parameterType="string" resultType="warningRecordExcel">
        select archives.devide_no as devideNo,archives.ship_id as shipId,archives.owner_name as ownerName,archives.owner_tel_no as ownerTelNo,
        record.warning_time as warningTime,location.longitude,location.latitude,dept.xw_dept_name as deptName
        from ax_beidou_warning_record record
        INNER JOIN ax_beidou_ship_archives archives ON archives.devide_no = record.device_id
        INNER JOIN ax_beidou_dept dept ON archives.police_station_id = dept.pscbh
        LEFT JOIN ax_beidou_ship_location location ON location.device_id = record.device_id
        <where>
            record.rule_id = #{ruleId}
            <if test="deptId != null and deptId != ''">
                and dept.xw_dept_id = #{deptId}
            </if>
            <if test="startTime != null and startTime != ''">and record.warning_time &gt; #{startTime}</if>
            <if test="endTime != null and endTime != ''">and record.warning_time &lt; #{endTime}</if>
        </where>
    </select>
    

    <insert id="insertDispose" parameterType="shipDisposeData">
        insert into ax_beidou_ship_dispose(
        id,
        warning_id,
        dispose_type,
        <if test="devideNo != null and devideNo != ''">devide_no,</if>
        dispose_suggestion,
        <if test="disposePicture != null and disposePicture != ''">dispose_picture,</if>
        dispose_user_id,
        dispose_time
        ) values(
        #{id},
        #{warningId},
        #{disposeType},
        <if test="devideNo != null and devideNo != ''">#{devideNo},</if>
        #{disposeSuggestion},
        <if test="disposePicture != null and disposePicture != ''">#{disposePicture},</if>
        #{disposeUserId},
        now()
        )
    </insert>

    <select id="getDisposeStatus" resultType="map">
        select dict_value as dictValue,dict_label as dictLabel from sys_dict_data where dict_type = 'ship_dispose_status' order by dict_sort ASC
    </select>

    <select id="getRecordById" parameterType="string" resultType="warningRecordData">
        select id,rule_id as ruleId, model_id as modelId,warning_time as warningTime,device_id as deviceId from ax_beidou_warning_record where id = #{id}
    </select>


</mapper>