<?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.xinyilu.system.mapper.testScoreBatchinfo.TestScoreBatchMapper">

    <resultMap type="TestScoreBatch" id="TestScoreBatchResult">
        <result property="id" column="id"/>
        <result property="batchCode" column="batch_code"/>
        <result property="batchName" column="batch_name"/>
        <result property="periodDictId" column="period_dict_id"/>
        <result property="period" column="period"/>
        <result property="remark" column="remark"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectTestScoreBatchVo">
        select id,
               batch_code,
               batch_name,
               period_dict_id,
               period,
               remark,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from test_score_batch
    </sql>

    <select id="selectTestScoreBatchList" parameterType="TestScoreBatch" resultMap="TestScoreBatchResult">
        <include refid="selectTestScoreBatchVo"/>
        <where>
            <if test="batchCode != null  and batchCode != ''">
                and batch_code = #{batchCode}
            </if>
            <if test="batchName != null  and batchName != ''">
                and batch_name like concat('%', #{batchName}, '%')
            </if>
            <if test="period != null  and period != ''">
                and period like concat('%', #{period}, '%')
            </if>
            and del_flag = 0
        </where>
        order by create_time desc
    </select>

    <select id="selectTestScoreBatchById" parameterType="Long"
            resultMap="TestScoreBatchResult">
        <include refid="selectTestScoreBatchVo"/>
        where id = #{id} and del_flag = 0
    </select>
    
    <insert id="insertTestScoreBatch" parameterType="TestScoreBatch" useGeneratedKeys="true"
            keyProperty="id">
        insert into test_score_batch
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="batchCode != null">batch_code,
            </if>
            <if test="batchName != null">batch_name,
            </if>
            <if test="periodDictId != null">period_dict_id,
            </if>
            <if test="period != null">period,
            </if>
            <if test="remark != null">remark,
            </if>
            <if test="delFlag != null">del_flag,
            </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>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="batchCode != null">#{batchCode},
            </if>
            <if test="batchName != null">#{batchName},
            </if>
            <if test="periodDictId != null">#{periodDictId},
            </if>
            <if test="period != null">#{period},
            </if>
            <if test="remark != null">#{remark},
            </if>
            <if test="delFlag != null">#{delFlag},
            </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>
        </trim>
    </insert>

    <update id="updateTestScoreBatch" parameterType="TestScoreBatch">
        update test_score_batch
        <trim prefix="SET" suffixOverrides=",">
            <if test="batchCode != null">batch_code =
                #{batchCode},
            </if>
            <if test="batchName != null">batch_name =
                #{batchName},
            </if>
            <if test="periodDictId != null">period_dict_id =
                #{periodDictId},
            </if>
            <if test="period != null">period =
                #{period},
            </if>
            <if test="remark != null">remark =
                #{remark},
            </if>
            <if test="delFlag != null">del_flag =
                #{delFlag},
            </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>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteTestScoreBatchById" parameterType="Long">
        delete
        from test_score_batch
        where id = #{id}
    </delete>

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

    <update id="updateTestScoreBatchByIds" parameterType="String">
        update test_score_batch set del_flag = 1 ,update_time = now() where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getItemBatchInfoList" parameterType="Long" resultType="testItems">
        SELECT
        ti.id,
        ti.item_name,
        ti.result_dict_type,
        ti.item_code
        FROM
        test_items ti
        INNER JOIN item_batch_info ibi ON ti.id = ibi.item_id
        <where>
            <if test="batchId != null ">
                and ibi.batch_id = #{batchId}
            </if>
        </where>
        AND ibi.del_flag =0
    </select>
</mapper>
