<?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.xinelu.manage.mapper.trainingitem.TrainingItemMapper">

    <resultMap type="TrainingItem" id="TrainingItemResult">
        <result property="id" column="id"/>
        <result property="trainingCategoryId" column="training_category_id"/>
        <result property="trainingParentId" column="training_parent_id"/>
        <result property="trainingItemTitle" column="training_item_title"/>
        <result property="trainingItemCode" column="training_item_code"/>
        <result property="trainingItemPrice" column="training_item_price"/>
        <result property="trainingItemCoverUrl" column="training_item_cover_url"/>
        <result property="trainingItemPosterUrl" column="training_item_poster_url"/>
        <result property="trainingItemType" column="training_item_type"/>
        <result property="trainingItemDetails" column="training_item_details"/>
        <result property="trainingItemContent" column="training_item_content"/>
        <result property="trainingItemDate" column="training_item_date"/>
        <result property="trainingItemStartTime" column="training_item_start_time"/>
        <result property="trainingItemEndTime" column="training_item_end_time"/>
        <result property="trainingItemAddress" column="training_item_address"/>
        <result property="trainingItemNotes" column="training_item_notes"/>
        <result property="trainingItemSort" column="training_item_sort"/>
        <result property="buyFlag" column="buy_flag"/>
        <result property="trainingShelfFlag" column="training_shelf_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>


    <resultMap type="com.xinelu.manage.vo.trainingitem.NurseAppletPersonTrainingItemVO"
               id="NurseAppletPersonTrainingItemVOResult">
        <result property="trainingItemId" column="trainingItemId"/>
        <result property="trainingCategoryId" column="training_category_id"/>
        <result property="trainingParentId" column="training_parent_id"/>
        <result property="trainingItemTitle" column="training_item_title"/>
        <result property="trainingItemCode" column="training_item_code"/>
        <result property="trainingItemPrice" column="training_item_price"/>
        <result property="trainingItemCoverUrl" column="training_item_cover_url"/>
        <result property="trainingItemPosterUrl" column="training_item_poster_url"/>
        <result property="trainingItemType" column="training_item_type"/>
        <result property="trainingItemDetails" column="training_item_details"/>
        <result property="trainingItemContent" column="training_item_content"/>
        <result property="trainingItemDate" column="training_item_date"/>
        <result property="trainingItemStartTime" column="training_item_start_time"/>
        <result property="trainingItemEndTime" column="training_item_end_time"/>
        <result property="trainingItemAddress" column="training_item_address"/>
        <result property="trainingItemNotes" column="training_item_notes"/>
        <result property="trainingItemSort" column="training_item_sort"/>
        <result property="buyFlag" column="buy_flag"/>
        <result property="trainingOrderDetailsBuy" column="trainingOrderDetailsBuy"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <collection property="trainingItemDirectoryList" javaType="java.util.List"
                    resultMap="TrainingItemDirectoryResult"/>
    </resultMap>

    <resultMap type="com.xinelu.manage.vo.trainingitemdirectory.TrainingItemDirectoryVO"
               id="TrainingItemDirectoryResult">
        <result property="trainingItemDirectoryId" column="trainingItemDirectoryId"/>
        <result property="trainingOrderItemDirectoryBuy" column="trainingOrderItemDirectoryBuy"/>
        <result property="trainingItemId" column="training_item_id"/>
        <result property="itemDirectoryName" column="item_directory_name"/>
        <result property="itemDirectoryCode" column="item_directory_code"/>
        <result property="itemDirectoryTitle" column="item_directory_title"/>
        <result property="itemDirectoryUrl" column="item_directory_url"/>
        <result property="itemDirectoryVideoDuration" column="item_directory_video_duration"/>
        <result property="itemDirectoryIntroduce" column="item_directory_introduce"/>
        <result property="itemDirectorySort" column="item_directory_sort"/>
        <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>

    <resultMap type="com.xinelu.manage.dto.trainingitem.TrainingItemAndDirectory" id="TrainingItemAndDirectoryResult">
        <result property="trainingItemId" column="trainingItemId"/>
        <result property="trainingCategoryId" column="training_category_id"/>
        <result property="trainingParentId" column="training_parent_id"/>
        <result property="trainingItemTitle" column="training_item_title"/>
        <result property="trainingItemCode" column="training_item_code"/>
        <result property="trainingItemPrice" column="training_item_price"/>
        <result property="trainingItemCoverUrl" column="training_item_cover_url"/>
        <result property="trainingItemPosterUrl" column="training_item_poster_url"/>
        <result property="trainingItemType" column="training_item_type"/>
        <result property="trainingItemDetails" column="training_item_details"/>
        <result property="trainingItemContent" column="training_item_content"/>
        <result property="trainingItemDate" column="training_item_date"/>
        <result property="trainingItemStartTime" column="training_item_start_time"/>
        <result property="trainingItemEndTime" column="training_item_end_time"/>
        <result property="trainingItemAddress" column="training_item_address"/>
        <result property="trainingItemNotes" column="training_item_notes"/>
        <result property="trainingItemSort" column="training_item_sort"/>
        <result property="buyFlag" column="buy_flag"/>
        <result property="trainingCategoryName" column="training_category_name"/>
        <result property="trainingParentName" column="trainingParentName"/>
        <collection property="trainingItemDirectoryList" javaType="java.util.List"
                    resultMap="TrainingItemDirectoryVOResult"/>
    </resultMap>

    <resultMap type="com.xinelu.manage.vo.trainingitemdirectory.TrainingItemDirectoryVO"
               id="TrainingItemDirectoryVOResult">
        <result property="trainingItemDirectoryId" column="trainingItemDirectoryId"/>
        <result property="trainingItemId" column="training_item_id"/>
        <result property="itemDirectoryName" column="item_directory_name"/>
        <result property="itemDirectoryCode" column="item_directory_code"/>
        <result property="itemDirectoryTitle" column="item_directory_title"/>
        <result property="itemDirectoryUrl" column="item_directory_url"/>
        <result property="itemDirectoryVideoDuration" column="item_directory_video_duration"/>
        <result property="itemDirectoryIntroduce" column="item_directory_introduce"/>
        <result property="itemDirectorySort" column="item_directory_sort"/>
    </resultMap>

    <sql id="selectTrainingItemVo">
        select id,
               training_category_id,
               training_parent_id,
               training_item_title,
               training_item_code,
               training_item_price,
               training_item_cover_url,
               training_item_poster_url,
               training_item_type,
               training_item_details,
               training_item_content,
               training_item_date,
               training_item_start_time,
               training_item_end_time,
               training_item_address,
               training_item_notes,
               training_item_sort,
               buy_flag,
               training_shelf_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from training_item
    </sql>

    <select id="selectTrainingItemList" parameterType="TrainingItem" resultMap="TrainingItemResult">
        <include refid="selectTrainingItemVo"/>
        <where>
            <if test="id != null ">
                and id = #{id}
            </if>
            <if test="trainingParentId != null ">
                and training_parent_id = #{trainingParentId}
            </if>
            <if test="trainingCategoryId != null ">
                and training_category_id = #{trainingCategoryId}
            </if>
            <if test="trainingItemTitle != null  and trainingItemTitle != ''">
                and training_item_title = #{trainingItemTitle}
            </if>
            <if test="trainingItemCode != null  and trainingItemCode != ''">
                and training_item_code = #{trainingItemCode}
            </if>
            <if test="trainingItemPrice != null ">
                and training_item_price = #{trainingItemPrice}
            </if>
            <if test="trainingItemCoverUrl != null  and trainingItemCoverUrl != ''">
                and training_item_cover_url = #{trainingItemCoverUrl}
            </if>
            <if test="trainingItemPosterUrl != null  and trainingItemPosterUrl != ''">
                and training_item_poster_url = #{trainingItemPosterUrl}
            </if>
            <if test="trainingItemType != null  and trainingItemType != ''">
                and training_item_type = #{trainingItemType}
            </if>
            <if test="trainingItemDetails != null  and trainingItemDetails != ''">
                and training_item_details = #{trainingItemDetails}
            </if>
            <if test="trainingItemContent != null  and trainingItemContent != ''">
                and training_item_content = #{trainingItemContent}
            </if>
            <if test="trainingItemDate != null ">
                and training_item_date = #{trainingItemDate}
            </if>
            <if test="trainingItemStartTime != null ">
                and training_item_start_time = #{trainingItemStartTime}
            </if>
            <if test="trainingItemEndTime != null ">
                and training_item_end_time = #{trainingItemEndTime}
            </if>
            <if test="trainingItemAddress != null  and trainingItemAddress != ''">
                and training_item_address = #{trainingItemAddress}
            </if>
            <if test="trainingItemNotes != null  and trainingItemNotes != ''">
                and training_item_notes = #{trainingItemNotes}
            </if>
            <if test="trainingItemSort != null ">
                and training_item_sort = #{trainingItemSort}
            </if>
            <if test="trainingShelfFlag != null ">
                and training_shelf_flag = #{trainingShelfFlag}
            </if>
        </where>
    </select>

    <select id="selectTrainingItemById" parameterType="Long"
            resultMap="TrainingItemResult">
        <include refid="selectTrainingItemVo"/>
        where id = #{id}
    </select>

    <insert id="insertTrainingItem" parameterType="TrainingItem" useGeneratedKeys="true"
            keyProperty="id">
        insert into training_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="trainingCategoryId != null">training_category_id,
            </if>
            <if test="trainingParentId != null">training_parent_id,
            </if>
            <if test="trainingItemTitle != null">training_item_title,
            </if>
            <if test="trainingItemCode != null">training_item_code,
            </if>
            <if test="trainingItemPrice != null">training_item_price,
            </if>
            <if test="trainingItemCoverUrl != null">training_item_cover_url,
            </if>
            <if test="trainingItemPosterUrl != null">training_item_poster_url,
            </if>
            <if test="trainingItemType != null">training_item_type,
            </if>
            <if test="trainingItemDetails != null">training_item_details,
            </if>
            <if test="trainingItemContent != null">training_item_content,
            </if>
            <if test="trainingItemDate != null">training_item_date,
            </if>
            <if test="trainingItemStartTime != null">training_item_start_time,
            </if>
            <if test="trainingItemEndTime != null">training_item_end_time,
            </if>
            <if test="trainingItemAddress != null">training_item_address,
            </if>
            <if test="trainingItemNotes != null">training_item_notes,
            </if>
            <if test="trainingItemSort != null">training_item_sort,
            </if>
            <if test="buyFlag != null">buy_flag,
            </if>
            <if test="trainingShelfFlag != null">training_shelf_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="trainingCategoryId != null">#{trainingCategoryId},
            </if>
            <if test="trainingParentId != null">#{trainingParentId},
            </if>
            <if test="trainingItemTitle != null">#{trainingItemTitle},
            </if>
            <if test="trainingItemCode != null">#{trainingItemCode},
            </if>
            <if test="trainingItemPrice != null">#{trainingItemPrice},
            </if>
            <if test="trainingItemCoverUrl != null">#{trainingItemCoverUrl},
            </if>
            <if test="trainingItemPosterUrl != null">#{trainingItemPosterUrl},
            </if>
            <if test="trainingItemType != null">#{trainingItemType},
            </if>
            <if test="trainingItemDetails != null">#{trainingItemDetails},
            </if>
            <if test="trainingItemContent != null">#{trainingItemContent},
            </if>
            <if test="trainingItemDate != null">#{trainingItemDate},
            </if>
            <if test="trainingItemStartTime != null">#{trainingItemStartTime},
            </if>
            <if test="trainingItemEndTime != null">#{trainingItemEndTime},
            </if>
            <if test="trainingItemAddress != null">#{trainingItemAddress},
            </if>
            <if test="trainingItemNotes != null">#{trainingItemNotes},
            </if>
            <if test="trainingItemSort != null">#{trainingItemSort},
            </if>
            <if test="buyFlag != null">#{buyFlag},
            </if>
            <if test="trainingShelfFlag != null">#{trainingShelfFlag},
            </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="updateTrainingItem" parameterType="TrainingItem">
        update training_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="trainingCategoryId != null">training_category_id =
                #{trainingCategoryId},
            </if>
            <if test="trainingParentId != null">training_parent_id =
                #{trainingParentId},
            </if>
            <if test="trainingItemTitle != null">training_item_title =
                #{trainingItemTitle},
            </if>
            <if test="trainingItemCode != null">training_item_code =
                #{trainingItemCode},
            </if>
            <if test="trainingItemPrice != null">training_item_price =
                #{trainingItemPrice},
            </if>
            <if test="trainingItemCoverUrl != null">training_item_cover_url =
                #{trainingItemCoverUrl},
            </if>
            <if test="trainingItemPosterUrl != null">training_item_poster_url =
                #{trainingItemPosterUrl},
            </if>
            <if test="trainingItemType != null">training_item_type =
                #{trainingItemType},
            </if>
            <if test="trainingItemDetails != null">training_item_details =
                #{trainingItemDetails},
            </if>
            <if test="trainingItemContent != null">training_item_content =
                #{trainingItemContent},
            </if>
            <if test="trainingItemDate != null">training_item_date =
                #{trainingItemDate},
            </if>
            <if test="trainingItemStartTime != null">training_item_start_time =
                #{trainingItemStartTime},
            </if>
            <if test="trainingItemEndTime != null">training_item_end_time =
                #{trainingItemEndTime},
            </if>
            <if test="trainingItemAddress != null">training_item_address =
                #{trainingItemAddress},
            </if>
            <if test="trainingItemNotes != null">training_item_notes =
                #{trainingItemNotes},
            </if>
            <if test="trainingItemSort != null">training_item_sort =
                #{trainingItemSort},
            </if>
            <if test="buyFlag != null">buy_flag =
                #{buyFlag},
            </if>
            <if test="trainingShelfFlag != null">training_shelf_flag =
                #{trainingShelfFlag},
            </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="deleteTrainingItemById" parameterType="Long">
        delete
        from training_item
        where id = #{id}
    </delete>

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

    <select id="selectTrainingItemAndCategoryList" parameterType="TrainingItem"
            resultType="com.xinelu.manage.vo.trainingitem.TrainingItemAndCategoryVO">
        select ti.id,
        ti.training_item_title,
        ti.training_parent_id,
        ti.training_item_code,
        ti.training_item_price,
        ti.training_item_type,
        ti.training_item_date,
        ti.training_item_start_time,
        ti.training_item_end_time,
        ti.training_item_sort,
        ti.training_shelf_flag,
        tc.training_category_name
        from training_item ti
        left join training_category tc ON tc.id = ti.training_category_id
        <where>
            <if test="trainingParentId != null">
                ti.training_parent_id = #{trainingParentId}
            </if>
            <if test="trainingCategoryId != null ">
                and ti.training_category_id = #{trainingCategoryId}
            </if>
            <if test="trainingItemTitle != null  and trainingItemTitle != ''">
                and ti.training_item_title like concat('%', #{trainingItemTitle}, '%')
            </if>
            <if test="trainingItemCode != null  and trainingItemCode != ''">
                and ti.training_item_code = #{trainingItemCode}
            </if>
            <if test="trainingItemType != null  and trainingItemType != ''">
                and ti.training_item_type = #{trainingItemType}
            </if>
            <if test="trainingItemDate != null ">
                and ti.training_item_date = #{trainingItemDate}
            </if>
        </where>
        order by ti.id desc
    </select>

    <select id="selectTrainingLearnItemList"
            parameterType="com.xinelu.manage.dto.trainingorder.NurseAppletPersonTrainingItemOrderDTO"
            resultMap="NurseAppletPersonTrainingItemVOResult">
        SELECT
        ti.id trainingItemId,
        ti.training_category_id,
        ti.training_parent_id,
        ti.training_item_title,
        ti.training_item_code,
        ti.training_item_price,
        ti.training_item_cover_url,
        ti.training_item_poster_url,
        ti.training_item_type,
        ti.training_item_details,
        ti.training_item_content,
        ti.training_item_date,
        ti.training_item_start_time,
        ti.training_item_end_time,
        ti.training_item_address,
        ti.training_item_notes,
        ti.training_item_sort,
        ti.buy_flag,
        tid.id trainingItemDirectoryId,
        tid.training_item_id,
        tid.item_directory_name,
        tid.item_directory_code,
        tid.item_directory_title,
        tid.item_directory_url,
        tid.item_directory_video_duration,
        tid.item_directory_introduce,
        tid.item_directory_sort
        FROM
        training_item ti
        LEFT JOIN training_item_directory tid ON tid.training_item_id = ti.id
        <where>
            <if test="trainingItemId != null ">
                and ti.id = #{trainingItemId}
            </if>
            <if test="trainingParentId != null ">
                OR ti.training_parent_id = #{trainingParentId}
            </if>
        </where>
    </select>

    <select id="selectTrainingLearnItemDetailsList"
            parameterType="com.xinelu.manage.dto.trainingitem.NurseAppletPersonTrainingItemDTO"
            resultMap="NurseAppletPersonTrainingItemVOResult">
        SELECT
        ti.id trainingItemId,
        ti.training_category_id,
        ti.training_parent_id,
        ti.training_item_title,
        ti.training_item_code,
        ti.training_item_price,
        ti.training_item_cover_url,
        ti.training_item_poster_url,
        ti.training_item_type,
        ti.training_item_details,
        ti.training_item_content,
        ti.training_item_sort,
        ti.buy_flag,
        tid.id trainingItemDirectoryId,
        tid.training_item_id,
        tid.item_directory_name,
        tid.item_directory_code,
        tid.item_directory_title,
        tid.item_directory_url,
        tid.item_directory_video_duration,
        tid.item_directory_introduce,
        tid.item_directory_sort,
        IF((SELECT COUNT( 1 ) FROM training_order_details tod
        INNER JOIN training_order t ON tod.training_order_no = t.training_order_no
        WHERE tod.training_item_id = ti.id
        AND t.del_flag = 0
        AND tod.del_flag = 0
        AND t.nurse_station_person_id = #{nurseStationPersonId}
        AND t.training_order_status = #{trainingOrderStatus}) > 0 , 'BUY' ,'NOT_BUY')
        trainingOrderDetailsBuy
        FROM
        training_item ti
        LEFT JOIN training_item_directory tid ON tid.training_item_id = ti.id
        <where>
            <if test="trainingItemId != null ">
                and ti.id = #{trainingItemId}
            </if>
            <if test="trainingCategoryId != null ">
                and ti.training_category_id = #{trainingCategoryId}
            </if>
            <if test="trainingParentId != null ">
                and ti.training_parent_id = #{trainingParentId}
            </if>
            <if test="trainingItemType != null  and trainingItemType != ''">
                and ti.training_item_type = #{trainingItemType}
            </if>
        </where>
    </select>

    <select id="selectTrainingItemAndDirectoryById"
            resultType="com.xinelu.manage.dto.trainingitem.TrainingItemAndDirectory"
            resultMap="TrainingItemAndDirectoryResult">
        select ti.id                                                                            trainingItemId,
               ti.training_category_id,
               ti.training_parent_id,
               ti.training_item_title,
               ti.training_item_code,
               ti.training_item_price,
               ti.training_item_cover_url,
               ti.training_item_poster_url,
               ti.training_item_type,
               ti.training_item_details,
               ti.training_item_content,
               ti.training_item_date,
               ti.training_item_start_time,
               ti.training_item_end_time,
               ti.training_item_address,
               ti.training_item_notes,
               ti.training_item_sort,
               ti.buy_flag,
               tc.training_category_name,
               tid.id                                                                           trainingItemDirectoryId,
               tid.item_directory_name,
               tid.item_directory_title,
               tid.item_directory_url,
               tid.item_directory_introduce,
               tid.item_directory_sort,
               (select training_item_title from training_item where id = ti.training_parent_id) trainingParentName
        from training_item ti
                 LEFT JOIN training_item_directory tid ON ti.id = tid.training_item_id
                 LEFT JOIN training_category tc ON tc.id = ti.training_category_id
        where ti.id = #{id}
    </select>

    <select id="selectTrainingItemByIdList" parameterType="Long"
            resultMap="TrainingItemResult">
        <include refid="selectTrainingItemVo"/>
        <where>
            <if test="trainingItemIdList != null and trainingItemIdList.size() > 0">
                and training_parent_id in
                <foreach item="trainingItemIdList" collection="trainingItemIdList" open="(" separator=","
                         close=")">
                    #{trainingItemIdList}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectTrainingItemAndDirectoryList"
            resultType="com.xinelu.manage.dto.trainingitem.TrainingItemAndDirectory"
            resultMap="TrainingItemAndDirectoryResult">
        select
        ti.id trainingItemId,
        ti.training_item_title,
        ti.training_item_cover_url,
        ti.training_item_poster_url,
        ti.training_item_content,
        tid.item_directory_url
        from training_item ti
        LEFT JOIN training_item_directory tid ON ti.id = tid.training_item_id
        <where>
            <if test="ids != null and ids.length > 0">
                ti.id in
                <foreach item="ids" collection="ids" open="(" separator="," close=")">
                    #{ids}
                </foreach>
            </if>
            <if test="trainingParentIds != null and trainingParentIds.length > 0">
                and ti.training_parent_id in
                <foreach item="trainingParentIds" collection="trainingParentIds" open="(" separator="," close=")">
                    #{trainingParentIds}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectTrainingItems" resultType="com.xinelu.manage.vo.trainingitem.TrainingItemAndCategoryVO">
        select id,
        training_parent_id,
        training_item_title trainingParentName,
        training_item_code,
        training_item_price,
        training_item_type,
        buy_flag
        from training_item
        <where>
            <if test="id != null">
                and id &lt;&gt; #{id}
            </if>
            <if test="trainingItemCode != null  and trainingItemCode != ''">
                and training_item_code = #{trainingItemCode}
            </if>
            <if test="trainingItemType != null  and trainingItemType != ''">
                and training_item_type = #{trainingItemType}
            </if>
            <if test="trainingParentName != null  and trainingParentName != ''">
                and training_item_title like concat('%', #{trainingParentName}, '%')
            </if>
            AND training_parent_id = 0
        </where>
    </select>

    <select id="getTrainingItemList" parameterType="com.xinelu.manage.dto.trainingitem.TrainingItemDTO"
            resultType="com.xinelu.manage.vo.trainingitem.TrainingItemVO">
        SELECT
        ti.id,
        ti.training_parent_id,
        ti.training_item_title,
        ti.training_item_code,
        ti.training_item_price,
        ti.training_item_cover_url,
        ti.training_item_poster_url,
        ti.training_item_type,
        ti.training_item_details,
        ti.training_item_content,
        ti.training_item_sort,
        ti.buy_flag,
        ti.training_shelf_flag,
        IF((SELECT COUNT( 1 ) FROM training_order_details tod
        INNER JOIN training_order t ON tod.training_order_no = t.training_order_no
        WHERE tod.training_item_id = ti.id
        AND t.del_flag = 0
        AND tod.del_flag = 0
        AND t.nurse_station_person_id = #{nurseStationPersonId}
        AND t.training_order_status = #{trainingOrderStatus}) > 0 , 'BUY' ,'NOT_BUY')
        trainingOrderDetailsBuy
        FROM
        training_item ti
        <where>
            <if test="id != null ">
                and ti.id = #{id}
            </if>
            <if test="trainingParentId != null ">
                and ti.training_parent_id = #{trainingParentId}
            </if>
            <if test="trainingShelfFlag != null ">
                and ti.training_shelf_flag = #{trainingShelfFlag}
            </if>
            <if test="buyFlag != null ">
                and ti.buy_flag = #{buyFlag}
            </if>
            <if test="trainingItemType != null  and trainingItemType != ''">
                and ti.training_item_type &lt;&gt; #{trainingItemType}
            </if>
        </where>
        ORDER BY
        ti.training_item_sort
    </select>

    <update id="updateTrainingItems" parameterType="TrainingItem">
        update training_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="trainingCategoryId != null">training_category_id =
                #{trainingCategoryId},
            </if>
            <if test="trainingParentId != null">training_parent_id =
                #{trainingParentId},
            </if>
            <if test="trainingItemTitle != null">training_item_title =
                #{trainingItemTitle},
            </if>
            <if test="trainingItemCode != null">training_item_code =
                #{trainingItemCode},
            </if>
            training_item_price = #{trainingItemPrice},
            <if test="trainingItemCoverUrl != null">training_item_cover_url =
                #{trainingItemCoverUrl},
            </if>
            <if test="trainingItemPosterUrl != null">training_item_poster_url =
                #{trainingItemPosterUrl},
            </if>
            <if test="trainingItemType != null">training_item_type =
                #{trainingItemType},
            </if>
            <if test="trainingItemDetails != null">training_item_details =
                #{trainingItemDetails},
            </if>
            <if test="trainingItemContent != null">training_item_content =
                #{trainingItemContent},
            </if>
            <if test="trainingItemDate != null">training_item_date =
                #{trainingItemDate},
            </if>
            <if test="trainingItemStartTime != null">training_item_start_time =
                #{trainingItemStartTime},
            </if>
            <if test="trainingItemEndTime != null">training_item_end_time =
                #{trainingItemEndTime},
            </if>
            <if test="trainingItemAddress != null">training_item_address =
                #{trainingItemAddress},
            </if>
            <if test="trainingItemNotes != null">training_item_notes =
                #{trainingItemNotes},
            </if>
            training_item_sort = #{trainingItemSort},
            buy_flag = #{buyFlag},
            <if test="trainingShelfFlag != null">training_shelf_flag =
                #{trainingShelfFlag},
            </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>

    <update id="updateTrainingItemList">
        update training_item
        set training_shelf_flag = #{trainingShelfFlag}
        where training_parent_id = #{trainingParentId}
    </update>
</mapper>