<?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.yanzhu.manage.mapper.BusExamUserMapper">
    
    <resultMap type="BusExamUser" id="BusExamUserResult">
        <result property="id"    column="id"    />
        <result property="comId"    column="com_id"    />
        <result property="comName"    column="com_name"    />
        <result property="projectId"    column="project_id"    />
        <result property="projectName"    column="project_name"    />
        <result property="userId"    column="user_id"    />
        <result property="userNick"    column="user_nick"    />
        <result property="userName"    column="user_name"    />
        <result property="examTitle"    column="exam_title"    />
        <result property="examType"    column="exam_type"    />
        <result property="craftType"    column="craft_type"    />
        <result property="craftPost"    column="craft_post"    />
        <result property="singleNum"    column="single_num"    />
        <result property="multipleNum"    column="multiple_num"    />
        <result property="estimateNum"    column="estimate_num"    />
        <result property="userMark"    column="user_mark"    />
        <result property="resStatus"    column="res_status"    />
        <result property="useTimes"    column="use_times"    />
        <result property="isDel"    column="is_del"    />
        <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"    />
    </resultMap>

    <resultMap id="BusExamUserBusExamUserResultResult" type="BusExamUser" extends="BusExamUserResult">
        <collection property="busExamUserResultList" notNullColumn="sub_id" javaType="java.util.List" resultMap="BusExamUserResultResult" />
    </resultMap>

    <resultMap type="BusExamUserResult" id="BusExamUserResultResult">
        <result property="id"    column="sub_id"    />
        <result property="examUserId"    column="sub_exam_user_id"    />
        <result property="questionId"    column="sub_question_id"    />
        <result property="questionTitle"    column="sub_question_title"    />
        <result property="questionType"    column="sub_question_type"    />
        <result property="questionOption"    column="sub_question_option"    />
        <result property="userAnswer"    column="sub_user_answer"    />
        <result property="answer"    column="sub_answer"    />
        <result property="useRes"    column="sub_use_res"    />
        <result property="useMark"    column="sub_use_mark"    />
    </resultMap>

    <sql id="selectBusExamUserVo">
        select beu.id, beu.com_id, sd.dept_name as com_name, beu.project_id, pi.project_name, beu.user_id, su.nick_name as user_nick, su.user_name, beu.exam_title, beu.exam_type, beu.craft_type, beu.craft_post, beu.single_num, beu.multiple_num, beu.estimate_num, beu.user_mark, beu.res_status, beu.use_times, beu.is_del, beu.create_by, beu.create_time, beu.update_by, beu.update_time, beu.remark from bus_exam_user beu
        left join pro_project_info pi on pi.id = beu.project_id
        left join sys_dept sd on sd.dept_id = pi.com_id
        left join sys_user su on su.user_id = beu.user_id
    </sql>

    <select id="selectBusExamUserList" parameterType="BusExamUser" resultMap="BusExamUserResult">
        <include refid="selectBusExamUserVo"/>
        <where>
            and beu.is_del = 0
            <if test="comId != null "> and beu.com_id = #{comId}</if>
            <if test="projectId != null "> and beu.project_id = #{projectId}</if>
            <if test="activeComId != null "> and beu.com_id = #{activeComId}</if>
            <if test="activeProjectId != null "> and beu.project_id = #{activeProjectId}</if>
            <if test="userId != null "> and beu.user_id = #{userId}</if>
            <if test="projectName != null  and projectName != ''"> and pi.project_name like concat('%', #{projectName}, '%')</if>
            <if test="userName != null  and userName != ''"> and (su.nick_name like concat('%', #{userName}, '%') or su.user_name like concat('%', #{userName}, '%') )</if>
            <if test="examTitle != null  and examTitle != ''"> and beu.exam_title = #{examTitle}</if>
            <if test="examType != null  and examType != ''"> and beu.exam_type = #{examType}</if>
            <if test="craftType != null  and craftType != ''"> and beu.craft_type = #{craftType}</if>
            <if test="craftPost != null  and craftPost != ''"> and beu.craft_post = #{craftPost}</if>
            <if test="resStatus != null "> and beu.res_status = #{resStatus}</if>
            <if test="useTimes != null  and useTimes != ''"> and beu.use_times = #{useTimes}</if>
        </where>
        order by beu.id desc
    </select>
    
    <select id="selectBusExamUserById" parameterType="Long" resultMap="BusExamUserBusExamUserResultResult">
        select a.id, a.com_id, sd.dept_name as com_name, a.project_id, pi.project_name, a.user_id, su.nick_name as user_nick, su.user_name, a.exam_title, a.exam_type, a.craft_type, a.craft_post, a.single_num, a.multiple_num, a.estimate_num, a.user_mark, a.res_status, a.use_times, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,
        b.id as sub_id, b.exam_user_id as sub_exam_user_id, b.question_id as sub_question_id, b.question_title as sub_question_title, b.question_type as sub_question_type, b.question_option as sub_question_option, b.user_answer as sub_user_answer, b.answer as sub_answer, b.use_res as sub_use_res, b.use_mark as sub_use_mark
        from bus_exam_user a
        left join pro_project_info pi on pi.id = a.project_id
        left join sys_dept sd on sd.dept_id = a.com_id
        left join sys_user su on su.user_id = a.user_id
        left join bus_exam_user_result b on b.exam_user_id = a.id
        where a.id = #{id}
    </select>
        
    <insert id="insertBusExamUser" parameterType="BusExamUser" useGeneratedKeys="true" keyProperty="id">
        insert into bus_exam_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="comId != null">com_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="examTitle != null">exam_title,</if>
            <if test="examType != null">exam_type,</if>
            <if test="craftType != null">craft_type,</if>
            <if test="craftPost != null">craft_post,</if>
            <if test="singleNum != null">single_num,</if>
            <if test="multipleNum != null">multiple_num,</if>
            <if test="estimateNum != null">estimate_num,</if>
            <if test="userMark != null">user_mark,</if>
            <if test="resStatus != null">res_status,</if>
            <if test="useTimes != null">use_times,</if>
            <if test="isDel != null">is_del,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="comId != null">#{comId},</if>
            <if test="projectId != null">#{projectId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="examTitle != null">#{examTitle},</if>
            <if test="examType != null">#{examType},</if>
            <if test="craftType != null">#{craftType},</if>
            <if test="craftPost != null">#{craftPost},</if>
            <if test="singleNum != null">#{singleNum},</if>
            <if test="multipleNum != null">#{multipleNum},</if>
            <if test="estimateNum != null">#{estimateNum},</if>
            <if test="userMark != null">#{userMark},</if>
            <if test="resStatus != null">#{resStatus},</if>
            <if test="useTimes != null">#{useTimes},</if>
            <if test="isDel != null">#{isDel},</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>
         </trim>
    </insert>

    <update id="updateBusExamUser" parameterType="BusExamUser">
        update bus_exam_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="comId != null">com_id = #{comId},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="examTitle != null">exam_title = #{examTitle},</if>
            <if test="examType != null">exam_type = #{examType},</if>
            <if test="craftType != null">craft_type = #{craftType},</if>
            <if test="craftPost != null">craft_post = #{craftPost},</if>
            <if test="singleNum != null">single_num = #{singleNum},</if>
            <if test="multipleNum != null">multiple_num = #{multipleNum},</if>
            <if test="estimateNum != null">estimate_num = #{estimateNum},</if>
            <if test="userMark != null">user_mark = #{userMark},</if>
            <if test="resStatus != null">res_status = #{resStatus},</if>
            <if test="useTimes != null">use_times = #{useTimes},</if>
            <if test="isDel != null">is_del = #{isDel},</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>
        </trim>
        where id = #{id}
    </update>

    <update id="deleteBusExamUserById" parameterType="Long">
        update bus_exam_user set is_del=2 where id = #{id}
    </update>

    <update id="deleteBusExamUserByIds" parameterType="String">
        update bus_exam_user set is_del=2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

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

    <delete id="deleteBusExamUserResultByExamUserId" parameterType="Long">
        delete from bus_exam_user_result where exam_user_id = #{examUserId}
    </delete>

    <insert id="batchBusExamUserResult">
        insert into bus_exam_user_result( id, exam_user_id, question_id, question_title, question_type, question_option, user_answer, answer, use_res, use_mark) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.examUserId}, #{item.questionId}, #{item.questionTitle}, #{item.questionType}, #{item.questionOption}, #{item.userAnswer}, #{item.answer}, #{item.useRes}, #{item.useMark})
        </foreach>
    </insert>

</mapper>