<?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.applet.mapper.trainingorderdetails.TrainingOrderDetailsMapper">

    <resultMap type="TrainingOrderDetails" id="TrainingOrderDetailsResult">
        <result property="id" column="id"/>
        <result property="trainingOrderId" column="training_order_id"/>
        <result property="trainingOrderNo" column="training_order_no"/>
        <result property="trainingItemId" column="training_item_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="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="selectTrainingOrderDetailsVo">
        select id,
               training_order_id,
               training_order_no,
               training_item_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,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time
        from training_order_details
    </sql>

    <select id="selectTrainingOrderDetailsList" parameterType="TrainingOrderDetails"
            resultMap="TrainingOrderDetailsResult">
        <include refid="selectTrainingOrderDetailsVo"/>
        <where>
            <if test="trainingOrderId != null ">
                and training_order_id = #{trainingOrderId}
            </if>
            <if test="trainingOrderNo != null  and trainingOrderNo != ''">
                and training_order_no = #{trainingOrderNo}
            </if>
            <if test="trainingItemId != null ">
                and training_item_id = #{trainingItemId}
            </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>
        </where>
    </select>

    <select id="selectTrainingOrderDetailsById" parameterType="Long"
            resultMap="TrainingOrderDetailsResult">
        <include refid="selectTrainingOrderDetailsVo"/>
        where id = #{id}
    </select>

    <insert id="insertTrainingOrderDetails" parameterType="TrainingOrderDetails" useGeneratedKeys="true"
            keyProperty="id">
        insert into training_order_details
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="trainingOrderId != null">training_order_id,
            </if>
            <if test="trainingOrderNo != null">training_order_no,
            </if>
            <if test="trainingItemId != null">training_item_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="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="trainingOrderId != null">#{trainingOrderId},
            </if>
            <if test="trainingOrderNo != null">#{trainingOrderNo},
            </if>
            <if test="trainingItemId != null">#{trainingItemId},
            </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="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="updateTrainingOrderDetails" parameterType="TrainingOrderDetails">
        update training_order_details
        <trim prefix="SET" suffixOverrides=",">
            <if test="trainingOrderId != null">training_order_id =
                #{trainingOrderId},
            </if>
            <if test="trainingOrderNo != null">training_order_no =
                #{trainingOrderNo},
            </if>
            <if test="trainingItemId != null">training_item_id =
                #{trainingItemId},
            </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="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="deleteTrainingOrderDetailsById" parameterType="Long">
        delete
        from training_order_details
        where id = #{id}
    </delete>

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

    <insert id="insertTrainingOrderDetailsList" parameterType="java.util.List">
        insert into training_order_details(
        training_order_id,
        training_order_no,
        training_item_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,
        del_flag,
        create_by,
        create_time,
        update_by,
        update_time
        ) values
        <foreach item="trainingOrderDetails" index="index" collection="list" separator=",">
            (
            #{trainingOrderDetails.trainingOrderId},
            #{trainingOrderDetails.trainingOrderNo},
            #{trainingOrderDetails.trainingItemId},
            #{trainingOrderDetails.trainingParentId},
            #{trainingOrderDetails.trainingItemTitle},
            #{trainingOrderDetails.trainingItemCode},
            #{trainingOrderDetails.trainingItemPrice},
            #{trainingOrderDetails.trainingItemCoverUrl},
            #{trainingOrderDetails.trainingItemPosterUrl},
            #{trainingOrderDetails.trainingItemType},
            #{trainingOrderDetails.trainingItemDetails},
            #{trainingOrderDetails.trainingItemContent},
            #{trainingOrderDetails.trainingItemDate},
            #{trainingOrderDetails.trainingItemStartTime},
            #{trainingOrderDetails.trainingItemEndTime},
            #{trainingOrderDetails.trainingItemAddress},
            #{trainingOrderDetails.trainingItemNotes},
            #{trainingOrderDetails.trainingItemSort},
            #{trainingOrderDetails.delFlag},
            #{trainingOrderDetails.createBy},
            #{trainingOrderDetails.createTime},
            #{trainingOrderDetails.updateBy},
            #{trainingOrderDetails.updateTime}
            )
        </foreach>
    </insert>

    <select id="selectTrainingOrderDetailsByIdCount" resultType="java.lang.Integer">
        select COUNT(1)
        from training_order_details
        <where>
            <if test="trainingItemId != null ">
                and training_item_id = #{trainingItemId}
            </if>
        </where>
    </select>

    <select id="selectTrainingOrderDetailsByOrderId" parameterType="Long"
            resultType="com.xinelu.manage.vo.trainingorderdetails.TrainingDetailsByOrderVO">
        select training_order_id,
               training_order_no
        from training_order_details
        where del_flag = 0
          AND training_order_id = #{trainingOrderId} limit 1
    </select>

    <select id="getBuyTrainingOrderInfo" resultType="int">
        select
        count( 1 ) trainingCount
        from
        training_order tr
        inner join training_order_details tod on tr.training_order_no = tod.training_order_no
        <where>
            <if test="nurseStationPersonId != null ">
                and tr.nurse_station_person_id = #{nurseStationPersonId}
            </if>
            <if test="trainingItemId != null ">
                and tod.training_item_id = #{trainingItemId}
            </if>
            <if test="trainingOrderStatusList != null and trainingOrderStatusList.size() > 0">
                and tr.training_order_status in
                <foreach item="trainingOrderStatus" collection="trainingOrderStatusList" open="(" separator=","
                         close=")">
                    #{trainingOrderStatus}
                </foreach>
            </if>
            AND tr.del_flag = 0
            AND tod.del_flag = 0
        </where>
    </select>

    <select id="getWaitPayTrainingOrderDetailsInfo" parameterType="string"
            resultType="com.xinelu.manage.vo.trainingorderdetails.WaitPayTrainingOrderDetailsVO">
        SELECT tr.nurse_station_person_id,
               tr.nurse_person_name,
               ts.training_item_cover_url,
               ts.training_item_title,
               tr.training_order_no,
               tr.training_order_amount,
               tr.training_order_status,
               tr.training_order_time
        FROM training_order tr
                 INNER JOIN training_order_details ts ON tr.training_order_no = ts.training_order_no
        where tr.del_flag = 0
          AND ts.del_flag = 0
          AND tr.training_order_no = #{trainingOrderNo}
          AND tr.training_order_status = #{trainingOrderStatus}
        GROUP BY ts.training_order_no
    </select>
</mapper>
