<?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.srs.comprehensive.mapper.QuestionnaireReleaseMapper">
    
    <resultMap type="QuestionnaireRelease" id="ReleaseResult">
        <result property="id"    column="id"    />
        <result property="title"    column="title"    />
        <result property="questions"    column="questions"    />

        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="status"    column="status"    />
        <result property="qId"    column="q_id"    />
        <result property="stuYearId"    column="stu_year_id"    />
        <result property="stuYearName"    column="stu_year_name"    />
        <result property="teacherId"    column="teacher_id"    />
        <result property="teacherName"    column="teacher_name"    />
        <result property="deptId"    column="dept_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="completed"    column="completed"    />
    </resultMap>

    <sql id="selectReleaseVo">
        select id, title, questions, start_time, end_time, status, q_id, stu_year_id, stu_year_name, teacher_id, teacher_name, dept_id, create_time, update_time from t_release
    </sql>

    <select id="selectReleaseList" parameterType="QuestionnaireRelease" resultMap="ReleaseResult">
        select * from (select
            r.*,
            th.name as teacher_name,
            th.teacher_id as teacher_id,
            (
                select count(id) from  t_answer a
                where
                    a.questionnaire_id  =  r.id
                    and a.target_id = th.teacher_id
                    and a.user_id = #{userName}
                    and a.status = '1'
            ) as completed
        from t_questionnaire r,cph_teacher th
        <where>
            <if test="title != null  and title != ''"> and r.title = #{title}</if>
            <if test="questions != null  and questions != ''"> and r.questions = #{questions}</if>
            <if test="status != null  and status != ''"> and r.status = #{status}</if>
            <if test="teacherId != null "> and th.teacher_id = #{teacherId}</if>
            <if test="teacherName != null  and teacherName != ''"> and th.name like concat('%', #{teacherName}, '%')</if>
            <if test="deptId != null "> and th.dept_id = #{deptId}</if>
            <if test="stuNo != null "> and  EXISTS(
                SELECT * from srs_student s LEFT JOIN srs_class c on c.class_id = s.class_id  where th.teacher_id = c.teacher_id and  s.stu_no = #{stuNo} )
            </if>
        </where>
        ) as temp
        <where>
            <if test="completed != null  and completed != ''"> and temp.completed = #{completed}</if>
        </where>
    </select>
    
    <select id="selectReleaseById" parameterType="Long" resultMap="ReleaseResult">
        <include refid="selectReleaseVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertRelease" parameterType="QuestionnaireRelease" useGeneratedKeys="true" keyProperty="id">
        insert into t_release
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="title != null">title,</if>
            <if test="questions != null">questions,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="status != null">status,</if>
            <if test="qId != null">q_id,</if>
            <if test="stuYearId != null">stu_year_id,</if>
            <if test="stuYearName != null">stu_year_name,</if>
            <if test="teacherId != null">teacher_id,</if>
            <if test="teacherName != null">teacher_name,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="title != null">#{title},</if>
            <if test="questions != null">#{questions},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="status != null">#{status},</if>
            <if test="qId != null">#{qId},</if>
            <if test="stuYearId != null">#{stuYearId},</if>
            <if test="stuYearName != null">#{stuYearName},</if>
            <if test="teacherId != null">#{teacherId},</if>
            <if test="teacherName != null">#{teacherName},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateRelease" parameterType="QuestionnaireRelease">
        update t_release
        <trim prefix="SET" suffixOverrides=",">
            <if test="title != null">title = #{title},</if>
            <if test="questions != null">questions = #{questions},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="status != null">status = #{status},</if>
            <if test="qId != null">q_id = #{qId},</if>
            <if test="stuYearId != null">stu_year_id = #{stuYearId},</if>
            <if test="stuYearName != null">stu_year_name = #{stuYearName},</if>
            <if test="teacherId != null">teacher_id = #{teacherId},</if>
            <if test="teacherName != null">teacher_name = #{teacherName},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteReleaseById" parameterType="Long">
        delete from t_release where id = #{id}
    </delete>

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


</mapper>