<?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.ai.mapper.AiUserMessageMapper">
    
    <resultMap type="AiUserMessage" id="AiUserMessageResult">
        <result property="id"    column="id"    />
        <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"    />
        <result property="title"    column="title"    />
        <result property="sendType"    column="send_type"    />
        <result property="content"    column="content"    />
        <collection property="userIds" javaType="java.util.Set" ofType="java.lang.Long" select="selectMessageUserIds" column="id" />
    </resultMap>

    <resultMap id="MessageUserIdsResult" type="Long">
        <result column="user_id" />
    </resultMap>

    <select id="selectMessageUserIds" parameterType="Long" resultMap="MessageUserIdsResult">
        select user_id from user_message where message_id = #{id}
    </select>

    <sql id="selectAiUserMessageVo">
        select id, del_flag, create_by, create_time, update_by, update_time, remark, title, send_type, content from ai_user_message
    </sql>

    <select id="selectAiUserMessageList" parameterType="AiUserMessage" resultMap="AiUserMessageResult">
        <include refid="selectAiUserMessageVo"/>
        <where>
            <if test="title != null  and title != ''"> and title like concat('%', #{title}, '%')</if>
            <if test="sendType != null "> and send_type = #{sendType}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="createTime != null"><!-- 开始时间检索 -->
                AND date_format(create_time,'%Y%m%d') = date_format(#{createTime},'%Y%m%d')
            </if>
        </where>
        order by id desc
    </select>
    
    <select id="selectAiUserMessageById" parameterType="Long" resultMap="AiUserMessageResult">
        <include refid="selectAiUserMessageVo"/>
        where id = #{id}
    </select>

    <insert id="insertAiUserMessage" parameterType="AiUserMessage" useGeneratedKeys="true" keyProperty="id">
        insert into ai_user_message
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="delFlag != null and delFlag != ''">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="remark != null">remark,</if>
            <if test="title != null">title,</if>
            <if test="sendType != null">send_type,</if>
            <if test="content != null">content,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="delFlag != null and delFlag != ''">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="remark != null">#{remark},</if>
            <if test="title != null">#{title},</if>
            <if test="sendType != null">#{sendType},</if>
            <if test="content != null">#{content},</if>
         </trim>
    </insert>

    <insert id="addUserMessage">
        INSERT INTO user_message (message_id, user_id)
        VALUES
        <foreach collection="userIds" item="userId" separator=",">
            (#{messageId}, #{userId})
        </foreach>
    </insert>

    <update id="updateAiUserMessage" parameterType="AiUserMessage">
        update ai_user_message
        <trim prefix="SET" suffixOverrides=",">
            <if test="delFlag != null and delFlag != ''">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>
            <if test="title != null">title = #{title},</if>
            <if test="sendType != null">send_type = #{sendType},</if>
            <if test="content != null">content = #{content},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAiUserMessageById" parameterType="Long">
        delete from ai_user_message where id = #{id}
    </delete>

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