<?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.ruoyi.system.mapper.TEarlyWarningMapper">

    <resultMap type="TEarlyWarning" id="TEarlyWarningResult">
        <result property="id" column="id"/>
        <result property="userId" column="user_id"/>
        <result property="state" column="state"/>
        <result property="type" column="type"/>
        <result property="content" column="content"/>
        <result property="eventRecord" column="event_record"/>
        <result property="location" column="location"/>
        <result property="lng" column="lng"/>
        <result property="lat" column="lat"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="facilityId" column="facility_id"/>
        <result property="deviceIdCode" column="device_id_code"/>
        <result property="alarmId" column="alarm_id"/>
        <result property="typeDesc"    column="type_desc"    />
    </resultMap>

    <sql id="selectTEarlyWarningVo">
        select id,
               user_id,
               state,
               type,
               content,
               event_record,
               location,
               lng,
               lat,
               create_time,
               update_time,
               facility_id,
               device_id_code,
               alarm_id,
               type_desc
        from t_early_warning
    </sql>

    <select id="selectTEarlyWarningByAlarmId" parameterType="long" resultMap="TEarlyWarningResult">
        <include refid="selectTEarlyWarningVo"/>
        where alarm_id = #{alarmId}
    </select>
    <select id="selectTEarlyWarningList" parameterType="TEarlyWarning" resultMap="TEarlyWarningResult">
        <include refid="selectTEarlyWarningVo"/>
        <where>
            <if test="userId != null ">and user_id = #{userId}</if>
            <if test="state != null ">and state = #{state}</if>
            <if test="type != null ">and type = #{type}</if>
            <if test="content != null  and content != ''">and content = #{content}</if>
            <if test="eventRecord != null  and eventRecord != ''">and event_record = #{eventRecord}</if>
            <if test="location != null  and location != ''">and location = #{location}</if>
            <if test="lng != null ">and lng = #{lng}</if>
            <if test="lat != null ">and lat = #{lat}</if>
            <if test="facilityId != null ">and facility_id = #{facilityId}</if>
            <if test="deviceIdCode != null  and deviceIdCode != ''">and device_id_code = #{deviceIdCode}</if>
            <if test="alarmId != null ">and alarm_id = #{alarmId}</if>
            <if test="typeDesc != null  and typeDesc != ''"> and type_desc = #{typeDesc}</if>
        </where>
    </select>

    <select id="selectTEarlyWarningById" parameterType="Long" resultMap="TEarlyWarningResult">
        <include refid="selectTEarlyWarningVo"/>
        where id = #{id}
    </select>

    <select id="selectTEarlyWarningList_SOS" resultType="com.ruoyi.system.domain.TEarlyWarning">
        select a.id,
        a.user_id,
        a.state,
        a.state as solve_status,
        a.type,
        a.type as alarm_type,
        a.content ,
        a.event_record,
        a.event_record as msg,
        a.location,
        a.location as address_desc,
        a.lng,
        a.lat,
        a.lng as gps_long,
        a.lat as gps_lat,
        a.create_time as createTime,
        a.update_time,
        a.facility_id,
        a.device_id_code,
        a.device_id_code as deviceIdCode,
        a.alarm_id,
        a.type_desc as typeDesc,
        a.type_desc as alarmMsg,
        b.name,
        b.tel_one,
        c.id as service_id,
        c.store_name,
        d.device_model
        from t_early_warning a
        left join t_user_profile b on a.user_id = b.id
        left join t_service_manage c on b.serviceid = c.id
        left join t_device_list d on a.device_id_code = d.device_id
        <where>
            <if test="type != null ">and a.type = #{type}</if>
            <if test="name != null ">and b.name = #{name}</if>
            <if test="solve_status != null ">and a.state = #{solve_status}</if>
        </where>
    </select>
    <select id="selectTEarlyWarningList_HEALTHSOLVE" resultType="com.ruoyi.system.domain.TEarlyWarning">
        select a.id,
        a.user_id,
        a.state,
        a.state as solve_status,
        a.type,
        a.type as alarm_type,
        a.content as alarmMsg,
        a.event_record,
        a.event_record as msg,
        a.location,
        a.location as address_desc,
        a.lng,
        a.lat,
        a.lng as gps_long,
        a.lat as gps_lat,
        a.create_time as createTime,
        a.update_time,
        a.facility_id,
        a.device_id_code,
        a.device_id_code as deviceIdCode,
        a.alarm_id,
        a.type_desc,
        b.name,
        b.tel_one,
        c.id as service_id,
        c.store_name,
        d.device_model
        from t_early_warning a
        left join t_user_profile b on a.user_id = b.id
        left join t_service_manage c on b.serviceid = c.id
        left join t_device_list d on a.device_id_code = d.device_id
        <where>
            and a.type &gt;= 22
            and a.type &lt;= 25
            <if test="name != null ">and b.name = #{name}</if>
            <if test="solve_status != null ">and a.state = #{solve_status}</if>
        </where>
    </select>
    <select id="selectTEarlyWarningList_OTHERSOLVE" resultType="com.ruoyi.system.domain.TEarlyWarning">
        select a.id,
        a.user_id,
        a.state,
        a.state as solve_status,
        a.type,
        a.type as alarm_type,
        a.content as alarmMsg,
        a.event_record,
        a.event_record as msg,
        a.location,
        a.location as address_desc,
        a.lng,
        a.lat,
        a.lng as gps_long,
        a.lat as gps_lat,
        a.create_time as createTime,
        a.update_time,
        a.facility_id,
        a.device_id_code,
        a.device_id_code as deviceIdCode,
        a.alarm_id,
        a.type_desc,
        b.name,
        b.tel_one,
        c.id as service_id,
        c.store_name,
        d.device_model
        from t_early_warning a
        left join t_user_profile b on a.user_id = b.id
        left join t_service_manage c on b.serviceid = c.id
        left join t_device_list d on a.device_id_code = d.device_id
        <where>
            and a.type != 16
            and a.type != 21
            and a.type != 22
            and a.type != 23
            and a.type != 24
            and a.type != 25
            <if test="name != null ">and b.name = #{name}</if>
            <if test="solve_status != null ">and a.state = #{solve_status}</if>
        </where>
    </select>
    <select id="selectTEarlyWarningCompletedCountByType" parameterType="Integer" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM t_early_warning
        WHERE type = #{type}
          AND state = 1;
    </select>
    <select id="selectTEarlyWarningUnCompletedCountByType" parameterType="Integer" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM t_early_warning
        WHERE type = #{type}
          AND state = 0;
    </select>
    <select id="selectTEarlyWarningUnCompletedAllCount" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM t_early_warning
        WHERE state = 0;
    </select>
    <select id="selectTEarlyWarningUnCompletedPercentageByType" parameterType="Integer" resultType="java.lang.Double">
        SELECT (COUNT(CASE WHEN state = 1 THEN 0 END) / COUNT(*) * 100)
        FROM t_early_warning
        WHERE type = #{type}
    </select>
    <select id="selectTEarlyWarningUnCompletedPercentageByAllType" resultType="java.lang.Double">
        SELECT (COUNT(CASE WHEN state = 1 THEN 0 END) / COUNT(*) * 100)
        FROM t_early_warning
    </select>
    <insert id="insertTEarlyWarning" parameterType="TEarlyWarning" useGeneratedKeys="true" keyProperty="id">
        insert into t_early_warning
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="state != null">state,</if>
            <if test="type != null">type,</if>
            <if test="content != null">content,</if>
            <if test="eventRecord != null">event_record,</if>
            <if test="location != null">location,</if>
            <if test="lng != null">lng,</if>
            <if test="lat != null">lat,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="facilityId != null">facility_id,</if>
            <if test="deviceIdCode != null">device_id_code,</if>
            <if test="alarmId != null">alarm_id,</if>
            <if test="typeDesc != null">type_desc,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="state != null">#{state},</if>
            <if test="type != null">#{type},</if>
            <if test="content != null">#{content},</if>
            <if test="eventRecord != null">#{eventRecord},</if>
            <if test="location != null">#{location},</if>
            <if test="lng != null">#{lng},</if>
            <if test="lat != null">#{lat},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="facilityId != null">#{facilityId},</if>
            <if test="deviceIdCode != null">#{deviceIdCode},</if>
            <if test="alarmId != null">#{alarmId},</if>
            <if test="typeDesc != null">#{typeDesc},</if>
        </trim>
    </insert>

    <update id="updateTEarlyWarning" parameterType="TEarlyWarning">
        update t_early_warning
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="state != null">state = #{state},</if>
            <if test="type != null">type = #{type},</if>
            <if test="content != null">content = #{content},</if>
            <if test="eventRecord != null">event_record = #{eventRecord},</if>
            <if test="location != null">location = #{location},</if>
            <if test="lng != null">lng = #{lng},</if>
            <if test="lat != null">lat = #{lat},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="facilityId != null">facility_id = #{facilityId},</if>
            <if test="deviceIdCode != null">device_id_code = #{deviceIdCode},</if>
            <if test="alarmId != null">alarm_id = #{alarmId},</if>
            <if test="typeDesc != null">type_desc = #{typeDesc},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTEarlyWarningById" parameterType="Long">
        delete
        from t_early_warning
        where id = #{id}
    </delete>

    <delete id="deleteTEarlyWarningByIds" parameterType="String">
        delete from t_early_warning where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>