<?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.oa.mapper.CmcTrainMapper">
    
    <resultMap type="CmcTrain" id="CmcTrainResult">
        <result property="trainId"    column="train_id"    />
        <result property="name"    column="name"    />
        <result property="place"    column="place"    />
        <result property="organizer"    column="organizer"    />
        <result property="num"    column="num"    />
        <result property="participates"    column="participates"    />
        <result property="beginDate"    column="begin_date"    />
        <result property="endDate"    column="end_date"    />
    </resultMap>

    <sql id="selectCmcTrainVo">
        select train_id, name, place, organizer, num, participates, begin_date, end_date from cmc_train
    </sql>

    <select id="selectCmcTrainList" parameterType="CmcTrain" resultMap="CmcTrainResult">
        <include refid="selectCmcTrainVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="place != null  and place != ''"> and place = #{place}</if>
            <if test="organizer != null  and organizer != ''"> and organizer = #{organizer}</if>
            <if test="num != null "> and num = #{num}</if>
            <if test="participates != null  and participates != ''"> and participates = #{participates}</if>
            <if test="beginDate != null "> and begin_date = #{beginDate}</if>
            <if test="endDate != null "> and end_date = #{endDate}</if>
        </where>
        order by begin_date desc
    </select>
    
    <select id="selectCmcTrainByTrainId" parameterType="Integer" resultMap="CmcTrainResult">
        <include refid="selectCmcTrainVo"/>
        where train_id = #{trainId}
    </select>
        
    <insert id="insertCmcTrain" parameterType="CmcTrain" useGeneratedKeys="true" keyProperty="trainId">
        insert into cmc_train
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="place != null">place,</if>
            <if test="organizer != null">organizer,</if>
            <if test="num != null">num,</if>
            <if test="participates != null">participates,</if>
            <if test="beginDate != null">begin_date,</if>
            <if test="endDate != null">end_date,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="place != null">#{place},</if>
            <if test="organizer != null">#{organizer},</if>
            <if test="num != null">#{num},</if>
            <if test="participates != null">#{participates},</if>
            <if test="beginDate != null">#{beginDate},</if>
            <if test="endDate != null">#{endDate},</if>
         </trim>
    </insert>

    <update id="updateCmcTrain" parameterType="CmcTrain">
        update cmc_train
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="place != null">place = #{place},</if>
            <if test="organizer != null">organizer = #{organizer},</if>
            <if test="num != null">num = #{num},</if>
            <if test="participates != null">participates = #{participates},</if>
            <if test="beginDate != null">begin_date = #{beginDate},</if>
            <if test="endDate != null">end_date = #{endDate},</if>
        </trim>
        where train_id = #{trainId}
    </update>

    <delete id="deleteCmcTrainByTrainId" parameterType="Integer">
        delete from cmc_train where train_id = #{trainId}
    </delete>

    <delete id="deleteCmcTrainByTrainIds" parameterType="String">
        delete from cmc_train where train_id in 
        <foreach item="trainId" collection="array" open="(" separator="," close=")">
            #{trainId}
        </foreach>
    </delete>
</mapper>