<?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.shzg.project.worn.mapper.MqttTopicConfigMapper">

    <!-- ================== ResultMap ================== -->
    <resultMap type="MqttTopicConfig" id="MqttTopicConfigResult">
        <result property="id" column="id" />
        <result property="project" column="project" />
        <result property="warehouse" column="warehouse" />
        <result property="deptId" column="dept_id" />
        <result property="topicUp" column="topic_up" />
        <result property="topicDownPrefix" column="topic_down_prefix" />
        <result property="status" column="status" />
        <result property="remark" column="remark" />
        <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="isDelete" column="is_delete" />
    </resultMap>

    <!-- ================== 基础查询SQL ================== -->
    <sql id="selectMqttTopicConfigVo">
        select id, project, warehouse, dept_id, topic_up, topic_down_prefix,
               status, remark, create_by, create_time, update_by, update_time, is_delete
        from mqtt_topic_config
    </sql>

    <!-- ================== 列表查询（支持数据隔离） ================== -->
    <select id="selectMqttTopicConfigList" parameterType="MqttTopicConfig" resultMap="MqttTopicConfigResult">
        <include refid="selectMqttTopicConfigVo"/>
        <where>
            <if test="project != null and project != ''">and project = #{project}</if>
            <if test="warehouse != null and warehouse != ''">and warehouse = #{warehouse}</if>
            <if test="topicUp != null and topicUp != ''">and topic_up = #{topicUp}</if>
            <if test="topicDownPrefix != null and topicDownPrefix != ''">and topic_down_prefix = #{topicDownPrefix}</if>
            <if test="status != null and status != ''">and status = #{status}</if>
            <if test="isDelete != null and isDelete != ''">and is_delete = #{isDelete}</if>

            <if test="deptIds != null and deptIds.size() > 0">
                and dept_id in
                <foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
                    #{deptId}
                </foreach>
            </if>
        </where>
    </select>

    <!-- ================== 根据ID查询 ================== -->
    <select id="selectMqttTopicConfigById" parameterType="Long" resultMap="MqttTopicConfigResult">
        <include refid="selectMqttTopicConfigVo"/>
        where id = #{id}
    </select>

    <!-- ================== 查询启用的Topic（MQTT用） ================== -->
    <select id="selectEnabledMqttTopicConfigList" resultMap="MqttTopicConfigResult">
        <include refid="selectMqttTopicConfigVo"/>
        where status = '0'
        and is_delete = '0'
        and topic_up is not null
        and topic_up != ''

        <!-- 🔥 防止跨项目订阅 -->
        <if test="deptIds != null and deptIds.size() > 0">
            and dept_id in
            <foreach collection="deptIds" item="deptId" open="(" separator="," close=")">
                #{deptId}
            </foreach>
        </if>
    </select>

    <!-- ================== 新增 ================== -->
    <insert id="insertMqttTopicConfig" parameterType="MqttTopicConfig" useGeneratedKeys="true" keyProperty="id">
        insert into mqtt_topic_config
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="project != null and project != ''">project,</if>
            <if test="warehouse != null and warehouse != ''">warehouse,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="topicUp != null and topicUp != ''">topic_up,</if>
            <if test="topicDownPrefix != null and topicDownPrefix != ''">topic_down_prefix,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDelete != null and isDelete != ''">is_delete,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="project != null and project != ''">#{project},</if>
            <if test="warehouse != null and warehouse != ''">#{warehouse},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="topicUp != null and topicUp != ''">#{topicUp},</if>
            <if test="topicDownPrefix != null and topicDownPrefix != ''">#{topicDownPrefix},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDelete != null and isDelete != ''">#{isDelete},</if>
        </trim>
    </insert>

    <!-- ================== 修改 ================== -->
    <update id="updateMqttTopicConfig" parameterType="MqttTopicConfig">
        update mqtt_topic_config
        <trim prefix="SET" suffixOverrides=",">
            <if test="project != null and project != ''">project = #{project},</if>
            <if test="warehouse != null and warehouse != ''">warehouse = #{warehouse},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="topicUp != null and topicUp != ''">topic_up = #{topicUp},</if>
            <if test="topicDownPrefix != null and topicDownPrefix != ''">topic_down_prefix = #{topicDownPrefix},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDelete != null">is_delete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <!-- ================== 删除（单个） ================== -->
    <delete id="deleteMqttTopicConfigById" parameterType="Long">
        delete from mqtt_topic_config where id = #{id}
    </delete>

    <!-- ================== 批量删除================== -->
    <delete id="deleteMqttTopicConfigByIds">
        delete from mqtt_topic_config where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <select id="selectByDeptId" resultMap="MqttTopicConfigResult">
        <include refid="selectMqttTopicConfigVo"/>
        where dept_id = #{deptId}
        and status = '0'
        and is_delete = '0'
        limit 1
    </select>

</mapper>