<?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.BusExamQuestionMapper">
    
    <resultMap type="BusExamQuestion" id="BusExamQuestionResult">
        <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="craftType"    column="craft_type"    />
        <result property="craftPost"    column="craft_post"    />
        <result property="questionDiff"    column="question_diff"    />
        <result property="questionType"    column="question_type"    />
        <result property="questionScope"    column="question_scope"    />
        <result property="questionDesc"    column="question_desc"    />
        <result property="questionAnswer"    column="question_answer"    />
        <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="BusExamQuestionBusExamQuestionResultResult" type="BusExamQuestion" extends="BusExamQuestionResult">
        <collection property="busExamQuestionResultList" notNullColumn="sub_id" javaType="java.util.List" resultMap="BusExamQuestionResultResult" />
    </resultMap>

    <resultMap type="BusExamQuestionResult" id="BusExamQuestionResultResult">
        <result property="id"    column="sub_id"    />
        <result property="questionId"    column="sub_question_id"    />
        <result property="opt"    column="sub_opt"    />
        <result property="result"    column="sub_result"    />
        <result property="isOk"    column="sub_is_ok"    />
        <result property="createBy"    column="sub_create_by"    />
        <result property="createTime"    column="sub_create_time"    />
        <result property="updateBy"    column="sub_update_by"    />
        <result property="updateTime"    column="sub_update_time"    />
        <result property="remark"    column="sub_remark"    />
    </resultMap>

    <sql id="selectBusExamQuestionVo">
        select beq.id, beq.com_id, sd.dept_name as com_name, beq.project_id, pi.project_name, beq.craft_type, beq.craft_post, beq.question_diff, beq.question_type, beq.question_scope, beq.question_desc, beq.question_answer, beq.is_del, beq.create_by, beq.create_time, beq.update_by, beq.update_time, beq.remark from bus_exam_question beq
        left join pro_project_info pi on pi.id = beq.project_id
        left join sys_dept sd on sd.dept_id = pi.com_id
    </sql>

    <select id="selectBusExamQuestionList" parameterType="BusExamQuestion" resultMap="BusExamQuestionResult">
        <include refid="selectBusExamQuestionVo"/>
        <where>
            and beq.is_del != 2
            <if test="comId != null "> and (beq.com_id = #{comId} or beq.com_id is null)</if>
            <if test="projectId != null "> and (beq.project_id = #{projectId} or beq.project_id is null)</if>
            <if test="activeComId != null "> and (beq.com_id = #{activeComId} or beq.com_id is null)</if>
            <if test="activeProjectId != null "> and (beq.project_id = #{activeProjectId} or beq.project_id is null)</if>
            <if test="projectName != null  and projectName != ''"> and pi.project_name like concat('%', #{projectName}, '%')</if>
            <if test="craftType != null  and craftType != ''"> and find_in_set(#{craftType}, beq.craft_type)</if>
            <if test="craftPost != null and craftPost != ''"> and find_in_set(#{craftPost}, beq.craft_post)</if>
            <if test="questionDiff != null "> and beq.question_diff = #{questionDiff} </if>
            <if test="questionType != null "> and beq.question_type = #{questionType} </if>
            <if test="questionScope != null and questionScope != ''"> and beq.question_scope = #{questionScope}</if>
            <if test="questionDesc != null and questionDesc != ''"> and beq.question_desc = #{questionDesc}</if>
            <if test="isDel != null"> and beq.is_del = #{isDel}</if>
        </where>
        order by beq.id desc
    </select>
    
    <select id="selectBusExamQuestionById" parameterType="Long" resultMap="BusExamQuestionBusExamQuestionResultResult">
        select a.id, a.com_id, sd.dept_name as com_name, a.project_id, pi.project_name, a.craft_type, a.craft_post, a.question_diff, a.question_type, a.question_scope, a.question_desc, a.question_answer, a.is_del, a.create_by, a.create_time, a.update_by, a.update_time, a.remark,
 b.id as sub_id, b.question_id as sub_question_id, b.opt as sub_opt, b.result as sub_result, b.is_ok as sub_is_ok, b.create_by as sub_create_by, b.create_time as sub_create_time, b.update_by as sub_update_by, b.update_time as sub_update_time, b.remark as sub_remark
        from bus_exam_question 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_question_result b on b.question_id = a.id
        where a.id = #{id}
    </select>
        
    <insert id="insertBusExamQuestion" parameterType="BusExamQuestion" useGeneratedKeys="true" keyProperty="id">
        insert into bus_exam_question
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="comId != null">com_id,</if>
            <if test="projectId != null">project_id,</if>
            <if test="craftType != null">craft_type,</if>
            <if test="craftPost != null">craft_post,</if>
            <if test="questionDiff != null">question_diff,</if>
            <if test="questionType != null">question_type,</if>
            <if test="questionScope != null">question_scope,</if>
            <if test="questionDesc != null">question_desc,</if>
            <if test="questionAnswer != null">question_answer,</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="craftType != null">#{craftType},</if>
            <if test="craftPost != null">#{craftPost},</if>
            <if test="questionDiff != null">#{questionDiff},</if>
            <if test="questionType != null">#{questionType},</if>
            <if test="questionScope != null">#{questionScope},</if>
            <if test="questionDesc != null">#{questionDesc},</if>
            <if test="questionAnswer != null">#{questionAnswer},</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="updateBusExamQuestion" parameterType="BusExamQuestion">
        update bus_exam_question
        <trim prefix="SET" suffixOverrides=",">
            <if test="comId != null">com_id = #{comId},</if>
            <if test="projectId != null">project_id = #{projectId},</if>
            <if test="craftType != null">craft_type = #{craftType},</if>
            <if test="craftPost != null">craft_post = #{craftPost},</if>
            <if test="questionDiff != null">question_diff = #{questionDiff},</if>
            <if test="questionType != null">question_type = #{questionType},</if>
            <if test="questionScope != null">question_scope = #{questionScope},</if>
            <if test="questionDesc != null">question_desc = #{questionDesc},</if>
            <if test="questionAnswer != null">question_answer = #{questionAnswer},</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="deleteBusExamQuestionById" parameterType="Long">
        update bus_exam_question set is_del=2 where id = #{id}
    </update>

    <delete id="deleteBusExamQuestionByIds" parameterType="String">
        update bus_exam_question set is_del=2 where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteBusExamQuestionResultByQuestionIds" parameterType="String">
        delete from bus_exam_question_result where question_id in 
        <foreach item="questionId" collection="array" open="(" separator="," close=")">
            #{questionId}
        </foreach>
    </delete>

    <delete id="deleteBusExamQuestionResultByQuestionId" parameterType="Long">
        delete from bus_exam_question_result where question_id = #{questionId}
    </delete>

    <insert id="batchBusExamQuestionResult">
        insert into bus_exam_question_result( id, question_id, opt, result, is_ok, create_by, create_time, update_by, update_time) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.questionId}, #{item.opt}, #{item.result}, #{item.isOk}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>

</mapper>