<?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.BusExamInfoMapper">
    
    <resultMap type="BusExamInfo" id="BusExamInfoResult">
        <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="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="singleMark"    column="single_mark"    />
        <result property="multipleNum"    column="multiple_num"    />
        <result property="multipleMark"    column="multiple_mark"    />
        <result property="estimateNum"    column="estimate_num"    />
        <result property="estimateMark"    column="estimate_mark"    />
        <result property="fullMark"    column="full_mark"    />
        <result property="passMark"    column="pass_mark"    />
        <result property="useNumber"    column="use_number"    />
        <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="BusExamInfoBusExamInfoQuesResult" type="BusExamInfo" extends="BusExamInfoResult">
        <collection property="busExamInfoQuesList" notNullColumn="sub_exam_id" javaType="java.util.List" resultMap="BusExamInfoQuesResult" />
    </resultMap>

    <resultMap type="BusExamInfoQues" id="BusExamInfoQuesResult">
        <result property="examId"    column="sub_exam_id"    />
        <result property="questionId"    column="sub_question_id"    />
        <result property="marks"    column="sub_marks"    />
        <result property="sortBy"    column="sub_sort_by"    />
    </resultMap>

    <sql id="selectBusExamInfoVo">
        select bei.id, bei.com_id, sd.dept_name as com_name, bei.project_id, pi.project_name, bei.exam_title, bei.exam_type, bei.craft_type, bei.craft_post, bei.single_num, bei.single_mark, bei.multiple_num, bei.multiple_mark, bei.estimate_num, bei.estimate_mark, bei.full_mark, bei.pass_mark, bei.use_number, bei.is_del, bei.create_by, bei.create_time, bei.update_by, bei.update_time, bei.remark from bus_exam_info bei
        left join pro_project_info pi on pi.id = bei.project_id
        left join sys_dept sd on sd.dept_id = pi.com_id
    </sql>

    <select id="selectBusExamInfoList" parameterType="BusExamInfo" resultMap="BusExamInfoResult">
        <include refid="selectBusExamInfoVo"/>
        <where>
            bei.is_del != 2
            <if test="comId != null "> and bei.com_id = #{comId}</if>
            <if test="projectId != null "> and bei.project_id = #{projectId}</if>
            <if test="activeComId != null "> and bei.com_id = #{activeComId}</if>
            <if test="activeProjectId != null "> and bei.project_id = #{activeProjectId}</if>
            <if test="projectName != null  and projectName != ''"> and pi.project_name like concat('%', #{projectName}, '%')</if>
            <if test="examTitle != null  and examTitle != ''"> and bei.exam_title = #{examTitle}</if>
            <if test="examType != null  and examType != ''"> and bei.exam_type = #{examType}</if>
            <if test="craftType != null  and craftType != ''"> and bei.craft_type = #{craftType}</if>
            <if test="craftPost != null  and craftPost != ''"> and bei.craft_post = #{craftPost}</if>
            <if test="isDel != null "> and bei.is_del = #{isDel}</if>
        </where>
        order by bei.id desc
    </select>
    
    <select id="selectBusExamInfoById" parameterType="Long" resultMap="BusExamInfoBusExamInfoQuesResult">
        select a.id, a.com_id, sd.dept_name as com_name, a.project_id, pi.project_name, a.exam_title, a.exam_type, a.craft_type, a.craft_post, a.single_num, a.single_mark, a.multiple_num, a.multiple_mark, a.estimate_num, a.estimate_mark, a.full_mark, a.pass_mark, a.use_number, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,
 b.exam_id as sub_exam_id, b.question_id as sub_question_id, b.marks as sub_marks, b.sort_by as sub_sort_by
        from bus_exam_info a
        left join pro_project_info pi on pi.id = a.project_id
        left join sys_dept sd on sd.dept_id = pi.com_id
        left join bus_exam_info_ques b on b.exam_id = a.id
        where a.id = #{id}
    </select>
        
    <insert id="insertBusExamInfo" parameterType="BusExamInfo" useGeneratedKeys="true" keyProperty="id">
        insert into bus_exam_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="comId != null">com_id,</if>
            <if test="projectId != null">project_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="singleMark != null">single_mark,</if>
            <if test="multipleNum != null">multiple_num,</if>
            <if test="multipleMark != null">multiple_mark,</if>
            <if test="estimateNum != null">estimate_num,</if>
            <if test="estimateMark != null">estimate_mark,</if>
            <if test="fullMark != null">full_mark,</if>
            <if test="passMark != null">pass_mark,</if>
            <if test="useNumber != null">use_number,</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="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="singleMark != null">#{singleMark},</if>
            <if test="multipleNum != null">#{multipleNum},</if>
            <if test="multipleMark != null">#{multipleMark},</if>
            <if test="estimateNum != null">#{estimateNum},</if>
            <if test="estimateMark != null">#{estimateMark},</if>
            <if test="fullMark != null">#{fullMark},</if>
            <if test="passMark != null">#{passMark},</if>
            <if test="useNumber != null">#{useNumber},</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="updateBusExamInfo" parameterType="BusExamInfo">
        update bus_exam_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="comId != null">com_id = #{comId},</if>
            <if test="projectId != null">project_id = #{projectId},</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="singleMark != null">single_mark = #{singleMark},</if>
            <if test="multipleNum != null">multiple_num = #{multipleNum},</if>
            <if test="multipleMark != null">multiple_mark = #{multipleMark},</if>
            <if test="estimateNum != null">estimate_num = #{estimateNum},</if>
            <if test="estimateMark != null">estimate_mark = #{estimateMark},</if>
            <if test="fullMark != null">full_mark = #{fullMark},</if>
            <if test="passMark != null">pass_mark = #{passMark},</if>
            <if test="useNumber != null">use_number = #{useNumber},</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="updateExamUseNumber" parameterType="Long">
        update bus_exam_info set use_number = use_number+1 where id = #{id}
    </update>

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

    <update id="deleteBusExamInfoByIds" parameterType="String">
        update bus_exam_info set is_del=2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    
    <delete id="deleteBusExamInfoQuesByExamIds" parameterType="String">
        delete from bus_exam_info_ques where exam_id in 
        <foreach item="examId" collection="array" open="(" separator="," close=")">
            #{examId}
        </foreach>
    </delete>

    <delete id="deleteBusExamInfoQuesByExamId" parameterType="Long">
        delete from bus_exam_info_ques where exam_id = #{examId}
    </delete>

    <insert id="batchBusExamInfoQues">
        insert into bus_exam_info_ques( exam_id, question_id, marks, sort_by) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.examId}, #{item.questionId}, #{item.marks}, #{item.sortBy})
        </foreach>
    </insert>
</mapper>