<?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.SysFeedbackMapper">
    
    <resultMap type="SysFeedback" id="SysFeedbackResult">
        <result property="id"    column="id"    />
        <result property="cretim"    column="cretim"    />
        <result property="userId"    column="user_id"    />
        <result property="content"    column="content"    />
    </resultMap>

    <resultMap id="SysFeedbackSysResponseResult" type="SysFeedback" extends="SysFeedbackResult">
        <collection property="sysResponseList" notNullColumn="sub_id" javaType="java.util.List" resultMap="SysResponseResult" />
    </resultMap>

    <resultMap type="SysResponse" id="SysResponseResult">
        <result property="id"    column="sub_id"    />
        <result property="cretim"    column="sub_cretim"    />
        <result property="feId"    column="sub_fe_id"    />
        <result property="content"    column="sub_content"    />
    </resultMap>

    <sql id="selectSysFeedbackVo">
        select id, cretim, user_id, content from sys_feedback
    </sql>

    <select id="selectSysFeedbackList" parameterType="SysFeedback" resultMap="SysFeedbackResult">
        <include refid="selectSysFeedbackVo"/>
        <where>  
            <if test="cretim != null "> and cretim = #{cretim}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
        </where>
        order by id desc
    </select>
    
    <select id="selectSysFeedbackById" parameterType="Long" resultMap="SysFeedbackSysResponseResult">
        select a.id, a.cretim, a.user_id, a.content,
 b.id as sub_id, b.cretim as sub_cretim, b.fe_id as sub_fe_id, b.content as sub_content
        from sys_feedback a
        left join sys_response b on b.fe_id = a.id
        where a.id = #{id}
    </select>
        
    <insert id="insertSysFeedback" parameterType="SysFeedback" useGeneratedKeys="true" keyProperty="id">
        insert into sys_feedback
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cretim != null">cretim,</if>
            <if test="userId != null">user_id,</if>
            <if test="content != null and content != ''">content,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cretim != null">#{cretim},</if>
            <if test="userId != null">#{userId},</if>
            <if test="content != null and content != ''">#{content},</if>
         </trim>
    </insert>

    <update id="updateSysFeedback" parameterType="SysFeedback">
        update sys_feedback
        <trim prefix="SET" suffixOverrides=",">
            <if test="cretim != null">cretim = #{cretim},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="content != null and content != ''">content = #{content},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysFeedbackById" parameterType="Long">
        delete from sys_feedback where id = #{id}
    </delete>

    <delete id="deleteSysFeedbackByIds" parameterType="String">
        delete from sys_feedback where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteSysResponseByFeIds" parameterType="String">
        delete from sys_response where fe_id in 
        <foreach item="feId" collection="array" open="(" separator="," close=")">
            #{feId}
        </foreach>
    </delete>

    <delete id="deleteSysResponseByFeId" parameterType="Long">
        delete from sys_response where fe_id = #{feId}
    </delete>

    <insert id="batchSysResponse">
        insert into sys_response( id, cretim, fe_id, content) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.cretim}, #{item.feId}, #{item.content})
        </foreach>
    </insert>
</mapper>