<?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.zehong.system.mapper.TTrainPlanMapper">
    
    <resultMap type="TTrainPlan" id="TTrainPlanResult">
        <result property="planId"    column="plan_id"    />
        <result property="planName"    column="plan_name"    />
        <result property="sort"    column="sort"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUser"    column="create_user"    />
    </resultMap>

    <sql id="selectTTrainPlanVo">
        select plan_id, plan_name, sort, create_time, create_user from t_train_plan
    </sql>

    <select id="selectTTrainPlanList" parameterType="TTrainPlan" resultMap="TTrainPlanResult">
        <include refid="selectTTrainPlanVo"/>
        <where>  
            <if test="planName != null  and planName != ''"> and plan_name like concat('%', #{planName}, '%')</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="createUser != null  and createUser != ''"> and create_user = #{createUser}</if>
        </where>
    </select>
    
    <select id="selectTTrainPlanById" parameterType="Long" resultMap="TTrainPlanResult">
        <include refid="selectTTrainPlanVo"/>
        where plan_id = #{planId}
    </select>
        
    <insert id="insertTTrainPlan" parameterType="TTrainPlan" useGeneratedKeys="true" keyProperty="planId">
        insert into t_train_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planId != null">plan_id,</if>
            <if test="planName != null">plan_name,</if>
            <if test="sort != null">sort,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUser != null">create_user,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planId != null">#{planId},</if>
            <if test="planName != null">#{planName},</if>
            <if test="sort != null">#{sort},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUser != null">#{createUser},</if>
         </trim>
    </insert>
    <insert id="insetsPlanPost">
        INSERT INTO t_train_plan_post(plan_id,post_id) VALUES
        <foreach collection="postIds" separator="," item="item">
            (#{planId},#{item})
        </foreach>

    </insert>
    <delete id="deletePlanPost" >
        DELETE FROM t_train_plan_post WHERE plan_id = #{planId}
    </delete>
    <update id="updateTTrainPlan" parameterType="TTrainPlan">
        update t_train_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="planName != null">plan_name = #{planName},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUser != null">create_user = #{createUser},</if>
        </trim>
        where plan_id = #{planId}
    </update>

    <delete id="deleteTTrainPlanById" parameterType="Long">
        delete from t_train_plan where plan_id = #{planId}
    </delete>

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

    <select id="selectTrainPostByPlanId" parameterType="Long" resultType="com.zehong.system.domain.vo.PlanPostVo">
        SELECT  p.`post_id` as postId,p.`post_name` as postName,IF(pp.`post_id` IS NULL,FALSE,TRUE) AS ischeck  FROM sys_post p
	    LEFT JOIN t_train_plan_post pp ON ( p.`post_id` = pp.`post_id` AND pp.`plan_id` = #{planId} )
    </select>
    <select id="selectAlluserByplanId" resultType="java.lang.String">
        SELECT user_id FROM sys_user_post
        WHERE post_id IN (SELECT post_id FROM  t_train_plan_post  WHERE plan_id =#{planId})
        GROUP BY user_id
    </select>
</mapper>