<?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.manage.mapper.WarnManageMapper">

    <resultMap type="WarnManage" id="WarnManageResult">
        <result property="warnId" column="warn_id"/>
        <result property="equipmentId" column="equipment_id"/>
        <result property="equipmentIp" column="equipment_ip"/>
        <result property="equipmentName" column="equipment_name"/>
        <result property="channelId" column="channel_id"/>
        <result property="channelNum" column="channel_num"/>
        <result property="algorithmType" column="algorithm_type"/>
        <result property="parameterSet" column="parameter_set"/>
        <result property="videoAddress" column="video_address"/>
        <result property="warnImage" column="warn_image"/>
        <result property="reportStatus" column="report_status"/>
        <result property="warnTime" column="warn_time"/>
        <result property="rangName" column="rang_name"/>
        <result property="deptId" column="dept_id"/>
        <result property="deptName" column="dept_name"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectWarnManageVo">
        select warn_id,
               equipment_id,
               equipment_ip,
               equipment_name,
               channel_id,
               channel_num,
               algorithm_type,
               parameter_set,
               video_address,
               warn_image,
               report_status,
               warn_time,
               rang_name,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               remark
        from warn_manage
    </sql>

    <select id="selectWarnManageList" parameterType="WarnManage" resultMap="WarnManageResult">
        select w.warn_id, w.equipment_ip, w.equipment_name, w.channel_id, w.channel_num, w.algorithm_type,
        w.parameter_set, w.video_address, w.warn_image, w.report_status, w.warn_time,w.rang_name, w.del_flag, w.create_time,
        e.dept_name,e.dept_id
        from warn_manage w left join equipment_manage e on e.equipment_id = w.equipment_id
        <where>
            w.del_flag = '0'
            <if test="equipmentIp != null  and equipmentIp != ''">and w.equipment_ip = #{equipmentIp}</if>
            <if test="equipmentName != null  and equipmentName != ''">and w.equipment_name like concat('%',
                #{equipmentName}, '%')
            </if>
            <if test="channelId != null ">and w.channel_id = #{channelId}</if>
            <if test="channelNum != null  and channelNum != ''">and w.channel_num = #{channelNum}</if>
            <if test="algorithmType != null  and algorithmType != ''">and w.algorithm_type = #{algorithmType}</if>
            <if test="parameterSet != null  and parameterSet != ''">and w.parameter_set = #{parameterSet}</if>
            <if test="videoAddress != null  and videoAddress != ''">and w.video_address = #{videoAddress}</if>
            <if test="warnImage != null  and warnImage != ''">and w.warn_image = #{warnImage}</if>
            <if test="reportStatus != null  and reportStatus != ''">and w.report_status = #{reportStatus}</if>
            <if test="rangName != null  and rangName != ''">and w.rang_name like concat('%',#{rangName}, '%') </if>
            <if test="deptId != null  ">and e.dept_id = #{deptId}</if>
            <if test="warnTime != null ">and date_format(w.warn_time,'%Y%m%d') =date_format(#{warnTime},'%Y%m%d')</if>
            <if test="params.month != null ">and date_format(w.warn_time,'%Y%m') =date_format(#{warnTime},'%Y%m')</if>
            <if test="params.year != null and params.year != ''"><!-- 年份检索 -->
                AND date_format(w.warn_time,'%Y') = #{params.year}
            </if>
        </where>
        order by w.create_time DESC
    </select>

    <select id="selectWarnManageDeptListByTime" parameterType="WarnManage" resultMap="WarnManageResult">
        select w.warn_id, w.equipment_ip, w.equipment_name, w.channel_id, w.channel_num, w.algorithm_type,
        w.parameter_set, w.video_address, w.warn_image, w.report_status, w.warn_time,w.rang_name, w.del_flag, w.create_time,
        e.dept_name,e.dept_id
        from warn_manage w left join equipment_manage e on e.equipment_id = w.equipment_id
        <where>
            w.del_flag = '0'
            <if test="warnTime != null ">and date_format(warn_time,'%Y%m') =date_format(#{warnTime},'%Y%m')</if>
        </where>
    </select>

    <select id="selectWarnManageByWarnId" parameterType="Long" resultMap="WarnManageResult">
        <include refid="selectWarnManageVo"/>
        where warn_id = #{warnId}
    </select>

    <insert id="insertWarnManage" parameterType="WarnManage" useGeneratedKeys="true" keyProperty="warnId">
        insert into warn_manage
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null and equipmentId != ''">equipment_id,</if>
            <if test="equipmentIp != null and equipmentIp != ''">equipment_ip,</if>
            <if test="equipmentName != null and equipmentName != ''">equipment_name,</if>
            <if test="channelId != null">channel_id,</if>
            <if test="channelNum != null and channelNum != ''">channel_num,</if>
            <if test="algorithmType != null and algorithmType != ''">algorithm_type,</if>
            <if test="parameterSet != null and parameterSet != ''">parameter_set,</if>
            <if test="videoAddress != null">video_address,</if>
            <if test="warnImage != null">warn_image,</if>
            <if test="reportStatus != null">report_status,</if>
            <if test="warnTime != null">warn_time,</if>
            <if test="rangName != null">rang_name,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="equipmentId != null and equipmentId != ''">#{equipmentId},</if>
            <if test="equipmentIp != null and equipmentIp != ''">#{equipmentIp},</if>
            <if test="equipmentName != null and equipmentName != ''">#{equipmentName},</if>
            <if test="channelId != null">#{channelId},</if>
            <if test="channelNum != null and channelNum != ''">#{channelNum},</if>
            <if test="algorithmType != null and algorithmType != ''">#{algorithmType},</if>
            <if test="parameterSet != null and parameterSet != ''">#{parameterSet},</if>
            <if test="videoAddress != null">#{videoAddress},</if>
            <if test="warnImage != null">#{warnImage},</if>
            <if test="reportStatus != null">#{reportStatus},</if>
            <if test="warnTime != null">#{warnTime},</if>
            <if test="rangName != null">#{rangName},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateWarnManage" parameterType="WarnManage">
        update warn_manage
        <trim prefix="SET" suffixOverrides=",">
            <if test="equipmentId != null and equipmentId != ''">equipment_id = #{equipmentId},</if>
            <if test="equipmentIp != null and equipmentIp != ''">equipment_ip = #{equipmentIp},</if>
            <if test="equipmentName != null and equipmentName != ''">equipment_name = #{equipmentName},</if>
            <if test="channelId != null">channel_id = #{channelId},</if>
            <if test="channelNum != null and channelNum != ''">channel_num = #{channelNum},</if>
            <if test="algorithmType != null and algorithmType != ''">algorithm_type = #{algorithmType},</if>
            <if test="parameterSet != null and parameterSet != ''">parameter_set = #{parameterSet},</if>
            <if test="videoAddress != null">video_address = #{videoAddress},</if>
            <if test="warnImage != null">warn_image = #{warnImage},</if>
            <if test="reportStatus != null">report_status = #{reportStatus},</if>
            <if test="warnTime != null">warn_time = #{warnTime},</if>
            <if test="rangName != null">rang_name = #{rangName},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where warn_id = #{warnId}
    </update>

    <delete id="deleteWarnManageByWarnId" parameterType="Long">
        delete
        from warn_manage
        where warn_id = #{warnId}
    </delete>

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