<?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.ItemBatchInfo.ItemBatchInfoMapper">

    <resultMap type="ItemBatchInfo" id="ItemBatchInfoResult">
            <result property="id" column="id"/>
            <result property="batchId" column="batch_id"/>
            <result property="itemId" column="item_id"/>
            <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="selectItemBatchInfoVo">
        select id, batch_id, item_id, del_flag, create_by, create_time, update_by, update_time from item_batch_info
    </sql>

    <select id="selectItemBatchInfoList" parameterType="ItemBatchInfo" resultMap="ItemBatchInfoResult">
        <include refid="selectItemBatchInfoVo"/>
        <where>
                        <if test="batchId != null ">
                            and batch_id = #{batchId}
                        </if>
                        <if test="itemId != null ">
                            and item_id = #{itemId}
                        </if>
        </where>
    </select>

    <select id="selectItemBatchInfoById" parameterType="Long"
            resultMap="ItemBatchInfoResult">
            <include refid="selectItemBatchInfoVo"/>
            where id = #{id}
    </select>

    <insert id="insertItemBatchInfo" parameterType="ItemBatchInfo">
        insert into item_batch_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="id != null">id,
                    </if>
                    <if test="batchId != null">batch_id,
                    </if>
                    <if test="itemId != null">item_id,
                    </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="id != null">#{id},
                    </if>
                    <if test="batchId != null">#{batchId},
                    </if>
                    <if test="itemId != null">#{itemId},
                    </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="updateItemBatchInfo" parameterType="ItemBatchInfo">
        update item_batch_info
        <trim prefix="SET" suffixOverrides=",">
                    <if test="batchId != null">batch_id =
                        #{batchId},
                    </if>
                    <if test="itemId != null">item_id =
                        #{itemId},
                    </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="deleteItemBatchInfoById" parameterType="Long">
        delete from item_batch_info where batch_id = #{id}
    </delete>

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

    <select id="selectItemBatchInfoCount" parameterType="long" resultType="int">
        SELECT
            count( 1 ) batchCount
        FROM
            test_score_batch tc
            INNER JOIN item_batch_info bi ON tc.id = bi.batch_id
        <where>
            <if test="batchId != null">
                tc.id = #{batchId}
            </if>
        </where>
    </select>

    <insert id="insertItemBatchInfoList">
        insert into item_batch_info(
        batch_id,
        item_id,
        del_flag,
        create_by,
        create_time
        ) values
        <foreach item="itemBatch" index="index" collection="list" separator=",">
            (
            #{itemBatch.batchId},
            #{itemBatch.itemId},
            #{itemBatch.delFlag},
            #{itemBatch.createBy},
            #{itemBatch.createTime}
            )
        </foreach>
    </insert>

</mapper>
