<?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.rocketmq.mapper.RmPolicyDeviceDetailsMapper">
    
    <resultMap type="RmPolicyDeviceDetails" id="RmPolicyDeviceDetailsResult">
        <result property="id"    column="id"    />
        <result property="clientId"    column="client_id"    />
        <result property="policyId"    column="policy_id"    />
        <result property="scriptId"    column="script_id"    />
        <result property="versionId"    column="version_id"    />
        <result property="policyStatus"    column="policy_status"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
    </resultMap>

    <sql id="selectRmPolicyDeviceDetailsVo">
        select id, client_id, policy_id, script_id, version_id, policy_status, create_time, update_time, create_by, update_by from rm_policy_device_details
    </sql>

    <select id="selectRmPolicyDeviceDetailsList" parameterType="RmPolicyDeviceDetails" resultMap="RmPolicyDeviceDetailsResult">
        <include refid="selectRmPolicyDeviceDetailsVo"/>
        <where>  
            <if test="clientId != null  and clientId != ''"> and client_id = #{clientId}</if>
            <if test="policyId != null  and policyId != ''"> and policy_id = #{policyId}</if>
            <if test="scriptId != null  and scriptId != ''"> and script_id = #{scriptId}</if>
            <if test="versionId != null  and versionId != ''"> and version_id = #{versionId}</if>
            <if test="policyStatus != null  and policyStatus != ''"> and policy_status = #{policyStatus}</if>
        </where>
    </select>
    
    <select id="selectRmPolicyDeviceDetailsById" parameterType="Long" resultMap="RmPolicyDeviceDetailsResult">
        <include refid="selectRmPolicyDeviceDetailsVo"/>
        where id = #{id}
    </select>

    <insert id="insertRmPolicyDeviceDetails" parameterType="RmPolicyDeviceDetails" useGeneratedKeys="true" keyProperty="id">
        insert into rm_policy_device_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="clientId != null and clientId != ''">client_id,</if>
            <if test="policyId != null and policyId != ''">policy_id,</if>
            <if test="scriptId != null">script_id,</if>
            <if test="versionId != null">version_id,</if>
            <if test="policyStatus != null and policyStatus != ''">policy_status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="clientId != null and clientId != ''">#{clientId},</if>
            <if test="policyId != null and policyId != ''">#{policyId},</if>
            <if test="scriptId != null">#{scriptId},</if>
            <if test="versionId != null">#{versionId},</if>
            <if test="policyStatus != null and policyStatus != ''">#{policyStatus},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
         </trim>
    </insert>

    <update id="updateRmPolicyDeviceDetails" parameterType="RmPolicyDeviceDetails">
        update rm_policy_device_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="policyStatus != null and policyStatus != ''">policy_status = #{policyStatus},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
        </trim>
        where
        <choose>
            <when test="id != null">
                id = #{id}
            </when>
            <when test="policyId != null and policyId != '' and clientId != null and clientId != ''">
                policy_id = #{policyId} and client_id = #{clientId}
            </when>
            <otherwise>
                1=0 <!-- 如果没有提供任何条件，则不更新任何记录 -->
            </otherwise>
        </choose>
    </update>

    <delete id="deleteRmPolicyDeviceDetailsById" parameterType="Long">
        delete from rm_policy_device_details where id = #{id}
    </delete>

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