<?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="cn.iocoder.yudao.module.jx.mapper.DriveSchoolFeedbackMapper">

    <resultMap type="cn.iocoder.yudao.module.jx.domain.DriveSchoolFeedback" id="DriveSchoolFeedbackResult">
        <result property="id"    column="id"    />
        <result property="userId"    column="userId"    />
        <result property="content"    column="content"    />
        <result property="jlContent"    column="jlContent"    />
        <result property="jlId"    column="jlId"    />
        <result property="jlName"    column="jlName"    />
        <result property="jxId"    column="jxId"    />
        <result property="jxName"    column="jxName"    />
        <result property="createTime"    column="create_time"    />
        <result property="creator"    column="create_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updater"    column="update_by"    />
        <result property="evaluateType"    column="evaluateType"    />
        <result property="userName"    column="userName"    />
        <result property="likes"    column="likes"    />
    </resultMap>

    <sql id="selectDriveSchoolFeedbackVo">
        select * from drive_school_feedback
    </sql>

    <select id="selectDriveSchoolFeedbackList" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolFeedback" resultType="cn.iocoder.yudao.module.base.vo.SchoolFeedBackVO">
        SELECT
        f.id,
        f.user_id,
        f.user_name,
        f.busi_id,
        f.evaluate_type,
        f.rate,
        f.content,
        f.teach_content,
        f.service_content,
        f.tenant_id,
        f.deleted,
        f.creator,
        f.create_time,
        f.updater,
        f.update_time,
        -- 以下根据 evaluate_type 选择连接不同的表
        CASE
        WHEN f.evaluate_type = '0' THEN t.course_name -- 对应训练评价
        WHEN f.evaluate_type = '1' THEN e.batch_id -- 对应考试评价
        ELSE NULL
        END AS related_info,
        -- 获取教练信息
        CASE
        WHEN f.evaluate_type = '0' THEN t.coach_name -- 训练评价时获取教练名称
        WHEN f.evaluate_type = '1' THEN eb.coach_name -- 考试评价时获取教练名称
        ELSE NULL
        END AS coach_name
        FROM
        drive_school_feedback f
        LEFT JOIN drive_school_train t ON f.busi_id = t.id AND f.evaluate_type = '0'
        LEFT JOIN drive_school_exam_batch_item e ON f.busi_id = e.id AND f.evaluate_type = '1'
        LEFT JOIN drive_school_exam_batch eb ON e.batch_id = eb.id AND f.evaluate_type = '1' -- 连接考试批次表

        <where>
            f.deleted = b'0' -- 只查询未删除的记录
            <if test="entity.jlName != null and entity.jlName != ''">
                AND (
                t.coach_name LIKE CONCAT('%', #{entity.jlName}, '%')
                OR eb.coach_name LIKE CONCAT('%', #{entity.jlName}, '%')
                )
            </if>
            <if test="entity.evaluateType != null and  entity.evaluateType != ''">
                AND f.evaluate_type = #{entity.evaluateType}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectDriveSchoolFeedbackListAll" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolFeedback" resultType="cn.iocoder.yudao.module.base.vo.SchoolFeedBackVO">
        <include refid="selectDriveSchoolFeedbackVo"/>
        <where>
            deleted = 0
            <if test="entity.userId != null "> and userId = #{entity.userId}</if>
            <if test="entity.jxName != null  and entity.jxName != ''"> and jxName like concat('%',#{entity.jxName},'%') </if>
            <if test="entity.jlName != null  and entity.jlName != ''"> and jlName like concat('%',#{entity.jlName},'%')</if>
            <if test="entity.evaluateType != null "> and evaluate_type = #{entity.evaluateType}</if>
        </where>
        order by create_time desc
    </select>

    <select id="likeOne" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolFeedback" resultType="Integer">
        select count(1) total from drive_school_feedback
        <where>
            deleted = 0 and
            likes = '1'
            <if test="jxId != null "> and jxId = #{jxId}</if>
        </where>
        order by create_time desc
    </select>

    <select id="likeTwo" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolFeedback" resultType="Integer">
        select count(1) total from drive_school_feedback
        <where>
            deleted = 0 and
            likes = '2'
            <if test="jxId != null "> and jxId = #{jxId}</if>
        </where>
    </select>

    <select id="likeThree" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolFeedback" resultType="Integer">
        <include refid="selectDriveSchoolFeedbackVo"/>
        select count(1) total from drive_school_feedback
        <where>
            deleted = 0 and
            likes = '3'
            <if test="jxId != null "> and jxId = #{jxId}</if>
        </where>
    </select>

    <select id="selectDriveSchoolFeedbackById" parameterType="Long" resultMap="DriveSchoolFeedbackResult">
        <include refid="selectDriveSchoolFeedbackVo"/>
        where id = #{id}
    </select>

    <insert id="insertDriveSchoolFeedback" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolFeedback">
        insert into drive_school_feedback
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="userId != null">userId,</if>
            <if test="content != null">content,</if>
            <if test="jlContent != null">jlContent,</if>
            <if test="jlId != null">jlId,</if>
            <if test="jlName != null">jlName,</if>
            <if test="jxId != null">jxId,</if>
            <if test="jxName != null">jxName,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="evaluateType != null">evaluateType,</if>
            <if test="userName != null">userName,</if>
            <if test="likes!= null">likes,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="userId != null">#{userId},</if>
            <if test="content != null">#{content},</if>
            <if test="jlContent != null">#{jlContent},</if>
            <if test="jlId != null">#{jlId},</if>
            <if test="jlName != null">#{jlName},</if>
            <if test="jxId != null">#{jxId},</if>
            <if test="jxName != null">#{jxName},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="evaluateType != null">#{evaluateType},</if>
            <if test="userName != null">#{userName},</if>
            <if test="likes != null">#{likes},</if>
        </trim>
    </insert>
    <insert id="insertData">
        insert into drive_school_feedback (userId, content, create_time) values (#{userId}, #{content}, now())
    </insert>

    <update id="updateDriveSchoolFeedback" parameterType="cn.iocoder.yudao.module.jx.domain.DriveSchoolFeedback">
        update drive_school_feedback
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">userId = #{userId},</if>
            <if test="content != null">content = #{content},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="evaluateType != null">evaluateType = #{evaluateType},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteDriveSchoolFeedbackById" parameterType="Long">
        delete from drive_school_feedback where id = #{id}
    </delete>

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