<?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.ruoyi.voluntary.mapper.BusiCardsOpenMapper">
    
    <resultMap type="BusiCardsOpen" id="BusiCardsOpenResult">
        <result property="id"    column="id"    />
        <result property="schoolId"    column="school_id"    />
        <result property="startNo"    column="start_no"    />
        <result property="endNo"    column="end_no"    />
        <result property="endDate"    column="end_date"    />
        <result property="sender"    column="sender"    />
        <result property="createTime"    column="create_time"    />
        <result property="isReopen"    column="is_reopen"    />
        <result property="cardType"    column="card_type"    />
        <result property="gradeId"    column="grade_id"    />
        <result property="year"    column="year"    />
        <result property="status"    column="status"    />
    </resultMap>

    <resultMap id="BusiCardsOpenBusiCardsOpenDetailResult" type="BusiCardsOpen" extends="BusiCardsOpenResult">
        <collection property="busiCardsOpenDetailList" notNullColumn="sub_id" javaType="java.util.List" resultMap="BusiCardsOpenDetailResult" />
    </resultMap>

    <resultMap type="BusiCardsOpenDetail" id="BusiCardsOpenDetailResult">
        <result property="id"    column="sub_id"    />
        <result property="openId"    column="sub_open_id"    />
        <result property="cardNo"    column="sub_card_no"    />
        <result property="createTime"    column="sub_create_time"    />
    </resultMap>

    <sql id="selectBusiCardsOpenVo">
        select id, school_id, start_no, end_no, end_date, sender, create_time, is_reopen, card_type, grade_id, year, status from b_busi_cards_open
    </sql>

    <select id="selectBusiCardsOpenList" parameterType="BusiCardsOpen" resultMap="BusiCardsOpenResult">
        <include refid="selectBusiCardsOpenVo"/>
        <where>  
            <if test="schoolId != null "> and school_id = #{schoolId}</if>
            <if test="startNo != null  and startNo != ''"> and start_no = #{startNo}</if>
            <if test="endNo != null  and endNo != ''"> and end_no = #{endNo}</if>
            <if test="endDate != null "> and end_date = #{endDate}</if>
            <if test="sender != null  and sender != ''"> and sender = #{sender}</if>
            <if test="isReopen != null "> and is_reopen = #{isReopen}</if>
            <if test="cardType != null "> and card_type = #{cardType}</if>
            <if test="gradeId != null "> and grade_id = #{gradeId}</if>
            <if test="year != null  and year != ''"> and year = #{year}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
        order by id desc
    </select>
    
    <select id="selectBusiCardsOpenById" parameterType="Long" resultMap="BusiCardsOpenBusiCardsOpenDetailResult">
        select a.id, a.school_id, a.start_no, a.end_no, a.end_date, a.sender, a.create_time, a.is_reopen, a.card_type, a.grade_id, a.year, a.status,
 b.id as sub_id, b.open_id as sub_open_id, b.card_no as sub_card_no, b.create_time as sub_create_time
        from b_busi_cards_open a
        left join b_busi_cards_open_detail b on b.open_id = a.id
        where a.id = #{id}
    </select>
        
    <insert id="insertBusiCardsOpen" parameterType="BusiCardsOpen" useGeneratedKeys="true" keyProperty="id">
        insert into b_busi_cards_open
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">school_id,</if>
            <if test="startNo != null">start_no,</if>
            <if test="endNo != null">end_no,</if>
            <if test="endDate != null">end_date,</if>
            <if test="sender != null">sender,</if>
            <if test="createTime != null">create_time,</if>
            <if test="isReopen != null">is_reopen,</if>
            <if test="cardType != null">card_type,</if>
            <if test="gradeId != null">grade_id,</if>
            <if test="year != null">year,</if>
            <if test="status != null">status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="schoolId != null">#{schoolId},</if>
            <if test="startNo != null">#{startNo},</if>
            <if test="endNo != null">#{endNo},</if>
            <if test="endDate != null">#{endDate},</if>
            <if test="sender != null">#{sender},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="isReopen != null">#{isReopen},</if>
            <if test="cardType != null">#{cardType},</if>
            <if test="gradeId != null">#{gradeId},</if>
            <if test="year != null">#{year},</if>
            <if test="status != null">#{status},</if>
         </trim>
    </insert>

    <update id="updateBusiCardsOpen" parameterType="BusiCardsOpen">
        update b_busi_cards_open
        <trim prefix="SET" suffixOverrides=",">
            <if test="schoolId != null">school_id = #{schoolId},</if>
            <if test="startNo != null">start_no = #{startNo},</if>
            <if test="endNo != null">end_no = #{endNo},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
            <if test="sender != null">sender = #{sender},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="isReopen != null">is_reopen = #{isReopen},</if>
            <if test="cardType != null">card_type = #{cardType},</if>
            <if test="gradeId != null">grade_id = #{gradeId},</if>
            <if test="year != null">year = #{year},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteBusiCardsOpenById" parameterType="Long">
        delete from b_busi_cards_open where id = #{id}
    </delete>

    <delete id="deleteBusiCardsOpenByIds" parameterType="String">
        delete from b_busi_cards_open where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteBusiCardsOpenDetailByOpenIds" parameterType="String">
        delete from b_busi_cards_open_detail where open_id in 
        <foreach item="openId" collection="array" open="(" separator="," close=")">
            #{openId}
        </foreach>
    </delete>

    <delete id="deleteBusiCardsOpenDetailByOpenId" parameterType="Long">
        delete from b_busi_cards_open_detail where open_id = #{openId}
    </delete>

    <insert id="batchBusiCardsOpenDetail">
        insert into b_busi_cards_open_detail( id, open_id, card_no, create_time) values
		<foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.openId}, #{item.cardNo}, #{item.createTime})
        </foreach>
    </insert>
</mapper>