<?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.fs.course.mapper.FsUserCourseComplaintRecordMapper">

    <resultMap type="FsUserCourseComplaintRecord" id="FsUserCourseComplaintRecordResult">
        <result property="recordId"    column="record_id"    />
        <result property="userId"    column="user_id"    />
        <result property="complaintTypeId"    column="complaint_type_id"    />
        <result property="complaintContent"    column="complaint_content"    />
        <result property="courseId"    column="course_id"    />
        <result property="videoId"    column="video_id"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <sql id="selectFsUserCourseComplaintRecordVo">
        select record_id, user_id, complaint_type_id, complaint_content, course_id, video_id, create_time from fs_user_course_complaint_record
    </sql>

    <select id="selectFsUserCourseComplaintRecordList" parameterType="FsUserCourseComplaintRecord" resultMap="FsUserCourseComplaintRecordResult">
        <include refid="selectFsUserCourseComplaintRecordVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="complaintTypeId != null "> and complaint_type_id = #{complaintTypeId}</if>
            <if test="complaintContent != null  and complaintContent != ''"> and complaint_content = #{complaintContent}</if>
            <if test="courseId != null "> and course_id = #{courseId}</if>
            <if test="videoId != null "> and video_id = #{videoId}</if>
        </where>
    </select>

    <select id="selectFsUserCourseComplaintRecordPageList" resultType="com.fs.course.vo.FsUserCourseComplaintRecordPageListVO">
        SELECT
        cr.*,
        ct.complaint_type_name,
        uc.course_name,
        ucv.title,
        fs_user.nick_name,
        if(ec.comment_status = 1,'已拉黑','正常') as status
        FROM
        fs_user_course_complaint_record cr
        LEFT JOIN fs_user_course_complaint_type ct ON ct.complaint_type_id = cr.complaint_type_id
        LEFT JOIN fs_user_course uc ON uc.course_id = cr.course_id
        LEFT JOIN fs_user_course_video ucv ON ucv.video_id = cr.video_id
        LEFT JOIN fs_user ON fs_user.user_id = cr.user_id
        left join qw_external_contact ec on cr.user_id = ec.fs_user_id
        <where>
            <if test="nickName != null and nickName != '' ">
                and fs_user.nick_name like concat('%', #{nickName}, '%')
            </if>
        </where>
    </select>

    <select id="selectFsUserCourseComplaintRecordByRecordId" parameterType="Long" resultMap="FsUserCourseComplaintRecordResult">
        <include refid="selectFsUserCourseComplaintRecordVo"/>
        where record_id = #{recordId}
    </select>

    <insert id="insertFsUserCourseComplaintRecord" parameterType="FsUserCourseComplaintRecord" useGeneratedKeys="true" keyProperty="recordId">
        insert into fs_user_course_complaint_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="complaintTypeId != null">complaint_type_id,</if>
            <if test="complaintContent != null">complaint_content,</if>
            <if test="courseId != null">course_id,</if>
            <if test="videoId != null">video_id,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="complaintTypeId != null">#{complaintTypeId},</if>
            <if test="complaintContent != null">#{complaintContent},</if>
            <if test="courseId != null">#{courseId},</if>
            <if test="videoId != null">#{videoId},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateFsUserCourseComplaintRecord" parameterType="FsUserCourseComplaintRecord">
        update fs_user_course_complaint_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="complaintTypeId != null">complaint_type_id = #{complaintTypeId},</if>
            <if test="complaintContent != null">complaint_content = #{complaintContent},</if>
            <if test="courseId != null">course_id = #{courseId},</if>
            <if test="videoId != null">video_id = #{videoId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deleteFsUserCourseComplaintRecordByRecordId" parameterType="Long">
        delete from fs_user_course_complaint_record where record_id = #{recordId}
    </delete>

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