<?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.nursestationitem.NurseStationItemMapper">

    <resultMap type="NurseStationItem" id="NurseStationItemResult">
        <result property="id" column="id"/>
        <result property="nurseStationId" column="nurse_station_id"/>
        <result property="nurseTypeId" column="nurse_type_id"/>
        <result property="userId" column="user_id"/>
        <result property="nurseItemCode" column="nurse_item_code"/>
        <result property="nurseItemName" column="nurse_item_name"/>
        <result property="nurseItemContent" column="nurse_item_content"/>
        <result property="advanceAppointDuration" column="advance_appoint_duration"/>
        <result property="itemPictureUrl" column="item_picture_url"/>
        <result property="sort" column="sort"/>
        <result property="nurseClassifyId" column="nurse_classify_id"/>
        <result property="appointmentLimitCount" column="appointment_limit_count"/>
        <result property="appointmentTimeInterval" column="appointment_time_interval"/>
        <result property="nurseClassifyName" column="nurse_classify_name"/>
        <result property="shelfStatus" column="shelf_status"/>
        <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.nursestationitem.NurseStationItemInfoDetailsVO" id="NurseStationItemInfoVO">
        <result property="id" column="id"/>
        <result property="nurseStationId" column="nurse_station_id"/>
        <result property="nurseTypeId" column="nurse_type_id"/>
        <result property="nurseItemCode" column="nurse_item_code"/>
        <result property="nurseItemName" column="nurse_item_name"/>
        <result property="itemPictureUrl" column="item_picture_url"/>
        <result property="advanceAppointDuration" column="advance_appoint_duration"/>
        <result property="nurseItemContent" column="nurse_item_content"/>
        <result property="sort" column="sort"/>
        <result property="appointmentLimitCount" column="appointment_limit_count"/>
        <result property="appointmentTimeInterval" column="appointment_time_interval"/>
        <result property="nurseClassifyId" column="nurse_classify_id"/>
        <result property="nurseClassifyName" column="nurse_classify_name"/>
        <result property="classifyName" column="classify_name"/>
        <result property="nurseStationName" column="nurse_station_name"/>
        <result property="shelfStatus" column="shelf_status"/>
        <collection property="nurseStationItemConsumables" javaType="java.util.List"
                    resultMap="NurseStationItemConsumableResult"/>
        <collection property="nurseStationItemPrices" javaType="java.util.List"
                    resultMap="NurseStationItemPriceResult"/>
    </resultMap>
    <!--护理站项目耗材关系表-->
    <resultMap type="com.xinelu.manage.vo.nursestationitemconsumable.NurseStationItemConsumableVO"
               id="NurseStationItemConsumableResult">
        <result property="consumableId" column="consumableId"/>
        <result property="nurseStationConsumableId" column="nurse_station_consumable_id"/>
        <result property="consumableCount" column="consumable_count"/>
        <result property="consumablePrice" column="consumable_price"/>
        <result property="consumableDetail" column="consumable_detail"/>
        <result property="consumableCode" column="consumable_code"/>
        <result property="consumableUnit" column="consumable_unit"/>
    </resultMap>
    <!--护理站价格-->
    <resultMap type="com.xinelu.manage.vo.nursestationitemprice.NurseStationItemPriceVO"
               id="NurseStationItemPriceResult">
        <result property="id" column="id"/>
        <result property="nurseItemPriceId" column="nurseItemPriceId"/>
        <result property="nurseStationItemId" column="nurse_station_item_id"/>
        <result property="serveDurationUnit" column="serve_duration_unit"/>
        <result property="price" column="price"/>
        <result property="description" column="description"/>
        <result property="commissionAmount" column="commission_amount"/>
        <result property="commissionRate" column="commission_rate"/>
    </resultMap>

    <select id="selectNurseStationItemInfoVO" resultMap="NurseStationItemInfoVO">
        select nst.id,
               nst.nurse_station_id,
               nst.nurse_type_id,
               nst.nurse_item_code,
               nst.nurse_item_name,
               nst.item_picture_url,
               nst.advance_appoint_duration,
               nst.nurse_item_content,
               nst.sort,
               nst.nurse_classify_id,
               nst.appointment_limit_count,
               nst.appointment_time_interval,
               nst.nurse_classify_name,
               nst.shelf_status,
               nci.classify_name,
               ns.nurse_station_name,
               nsip.id nurseItemPriceId,
               nsip.nurse_station_item_id,
               nsip.serve_duration_unit,
               nsip.price,
               nsip.description,
               nsip.commission_amount,
               nsip.commission_rate,
               nsic.id consumableId,
               nsic.nurse_station_consumable_id,
               nsic.consumable_detail,
               nsic.consumable_unit,
               nsic.consumable_count,
               nsic.consumable_price
        FROM nurse_station_item nst
                 LEFT JOIN nurse_station_item_price nsip ON nsip.nurse_station_item_id = nst.id
                 LEFT JOIN nurse_station_item_consumable nsic ON nsic.nurse_station_item_id = nst.id
                 LEFT JOIN nurse_station ns ON ns.id = nst.nurse_station_id
                 LEFT JOIN nurse_classify_info nci ON nci.id = nst.nurse_classify_id
        where nst.id = #{id}
    </select>

    <sql id="selectNurseStationItemVo">
        select id,
               nurse_station_id,
               nurse_type_id,
               user_id,
               item_picture_url,
               nurse_item_code,
               nurse_item_name,
               nurse_item_content,
               advance_appoint_duration,
               sort,
               nurse_classify_id,
               appointment_limit_count,
               appointment_time_interval,
               nurse_classify_name,
               shelf_status,
               create_by,
               create_time,
               update_by,
               update_time
        from nurse_station_item
    </sql>

    <select id="selectNurseStationItemList" parameterType="NurseStationItem"
            resultType="com.xinelu.manage.vo.nursestationitem.NurseStationItemVO">
        select nst.* ,ns.nurse_station_name, nci.classify_name
        from nurse_station_item nst
        LEFT JOIN nurse_station ns on ns.id=nst.nurse_station_id
        LEFT JOIN nurse_classify_info nci ON nci.id = nst.nurse_classify_id
        <where>
            <if test="nurseStationId != null ">
                and nst.nurse_station_id = #{nurseStationId}
            </if>
            <if test="nurseTypeId != null ">
                and nst.nurse_type_id = #{nurseTypeId}
            </if>
            <if test="userId != null ">
                and nst.user_id = #{userId}
            </if>
            <if test="nurseItemCode != null  and nurseItemCode != ''">
                and nst.nurse_item_code like concat('%', #{nurseItemCode}, '%')
            </if>
            <if test="nurseItemName != null  and nurseItemName != ''">
                and nst.nurse_item_name like concat('%', #{nurseItemName}, '%')
            </if>
            <if test="nurseItemContent != null  and nurseItemContent != ''">
                and nst.nurse_item_content = #{nurseItemContent}
            </if>
            <if test="advanceAppointDuration != null ">
                and nst.advance_appoint_duration = #{advanceAppointDuration}
            </if>
            <if test="sort != null ">
                and nst.sort = #{sort}
            </if>
            <if test="shelfStatus != null and shelfStatus != ''">
                and nst.shelf_status = #{shelfStatus}
            </if>
        </where>
        ORDER BY nst.create_time DESC
    </select>

    <select id="selectNurseStationItemById" parameterType="Long"
            resultType="com.xinelu.manage.vo.nursestationitem.NurseStationItemVO">
        select nst.id,
               nst.nurse_station_id,
               nst.nurse_type_id,
               nst.nurse_item_code,
               nst.nurse_item_name,
               nst.item_picture_url,
               nst.advance_appoint_duration,
               nst.nurse_item_content,
               nst.create_time,
               nst.create_by,
               nst.sort,
               nst.nurse_classify_id,
               nst.appointment_limit_count,
               nst.appointment_time_interval,
               nst.nurse_classify_name,
               nst.shelf_status,
               ns.nurse_station_name
        from nurse_station_item nst
                 LEFT JOIN nurse_station ns on ns.id = nst.nurse_station_id
        where nst.id = #{id}
    </select>

    <insert id="insertNurseStationItem" parameterType="NurseStationItem" useGeneratedKeys="true"
            keyProperty="id">
        insert into nurse_station_item
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="nurseStationId != null">nurse_station_id,
            </if>
            <if test="nurseTypeId != null">nurse_type_id,
            </if>
            <if test="userId != null">user_id,
            </if>
            <if test="nurseItemCode != null">nurse_item_code,
            </if>
            <if test="nurseItemName != null">nurse_item_name,
            </if>
            <if test="nurseItemContent != null">nurse_item_content,
            </if>
            <if test="advanceAppointDuration != null">advance_appoint_duration,
            </if>
            <if test="itemPictureUrl != null">item_picture_url,
            </if>
            <if test="sort != null">sort,
            </if>
            <if test="nurseClassifyId != null">nurse_classify_id,
            </if>
            <if test="appointmentLimitCount != null">appointment_limit_count,
            </if>
            <if test="appointmentTimeInterval != null">appointment_time_interval,
            </if>
            <if test="nurseClassifyName != null">nurse_classify_name,
            </if>
            <if test="shelfStatus != null">shelf_status,
            </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="nurseStationId != null">#{nurseStationId},
            </if>
            <if test="nurseTypeId != null">#{nurseTypeId},
            </if>
            <if test="userId != null">#{userId},
            </if>
            <if test="nurseItemCode != null">#{nurseItemCode},
            </if>
            <if test="nurseItemName != null">#{nurseItemName},
            </if>
            <if test="nurseItemContent != null">#{nurseItemContent},
            </if>
            <if test="advanceAppointDuration != null">#{advanceAppointDuration},
            </if>
            <if test="itemPictureUrl != null">#{itemPictureUrl},
            </if>
            <if test="sort != null">#{sort},
            </if>
            <if test="nurseClassifyId != null">#{nurseClassifyId},
            </if>
            <if test="appointmentLimitCount != null">#{appointmentLimitCount},
            </if>
            <if test="appointmentTimeInterval != null">#{appointmentTimeInterval},
            </if>
            <if test="nurseClassifyName != null">#{nurseClassifyName},
            </if>
            <if test="shelfStatus != null">#{shelfStatus},
            </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>

    <!--    <insert id="insertNurseStationItem" parameterType="java.util.List">
            INSERT INTO
            nurse_station_item(nurse_station_id, nurse_type_id, user_id, item_picture_url, nurse_item_code, nurse_item_name,nurse_item_content,advance_appoint_duration,sort,create_by, create_time, update_by, update_time)
            VALUES
            <foreach collection="list" item="productCategory" index="index"
                     separator=",">
                (
                #{productCategory.nurseStationId},
                #{productCategory.nurseTypeId},
                #{productCategory.userId},
                #{productCategory.itemPictureUrl},
                #{productCategory.itemPictureUrl},
                #{productCategory.nurseItemCode},
                #{productCategory.nurseItemName},
                #{productCategory.nurseItemContent},
                #{productCategory.advanceAppointDuration},
                #{productCategory.sort},
                #{productCategory.createBy},
                #{productCategory.createTime},
                #{productCategory.updateBy},
                #{productCategory.updateTime}
                )
            </foreach>
        </insert>-->

    <update id="updateNurseStationItem" parameterType="NurseStationItem">
        update nurse_station_item
        <trim prefix="SET" suffixOverrides=",">
            <if test="nurseStationId != null">nurse_station_id =
                #{nurseStationId},
            </if>
            <if test="nurseTypeId != null">nurse_type_id =
                #{nurseTypeId},
            </if>
            <if test="userId != null">user_id =
                #{userId},
            </if>
            <if test="nurseItemCode != null">nurse_item_code =
                #{nurseItemCode},
            </if>
            <if test="itemPictureUrl != null">item_picture_url =
                #{itemPictureUrl},
            </if>
            <if test="nurseItemName != null">nurse_item_name =
                #{nurseItemName},
            </if>
            <if test="nurseItemContent != null">nurse_item_content =
                #{nurseItemContent},
            </if>
            <if test="advanceAppointDuration != null">advance_appoint_duration =
                #{advanceAppointDuration},
            </if>
            <if test="sort != null">sort =
                #{sort},
            </if>
            nurse_classify_id = #{nurseClassifyId},
            appointment_limit_count = #{appointmentLimitCount},
            <if test="appointmentTimeInterval != null">appointment_time_interval =
                #{appointmentTimeInterval},
            </if>
            <if test="nurseClassifyName != null">nurse_classify_name =
                #{nurseClassifyName},
            </if>
            <if test="shelfStatus != null">shelf_status =
                #{shelfStatus},
            </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="deleteNurseStationItemById" parameterType="Long">
        delete
        from nurse_station_item
        where id = #{id}
    </delete>

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

    <select id="selectItemInfoList" parameterType="String"
            resultType="com.xinelu.manage.dto.nursestationitem.NurseStationItemImportDTO">
        select nst.nurse_item_name, ns.nurse_station_name,nst.advance_appoint_duration,
        nsip.price,nsip.serve_duration_unit,nst.id as nurseStationItemId
        from nurse_station_item nst
        LEFT JOIN nurse_station ns on ns.id = nst.nurse_station_id
        LEFT JOIN nurse_station_item_price nsip on nsip.nurse_station_item_id = nst.id
        <where>
            <if test="nurseStationIdList != null and nurseStationIdList.size() > 0">
                and ns.nurse_station_name in
                <foreach item="nurseStationName" collection="nurseStationIdList" open="(" separator="," close=")">
                    #{nurseStationName}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getNurseStationItemByIds" parameterType="NurseStationItem"
            resultType="NurseStationItem">
        select id,
        nurse_station_id,
        nurse_type_id,
        user_id,
        item_picture_url,
        nurse_item_code,
        nurse_item_name,
        nurse_item_content,
        advance_appoint_duration,
        nurse_classify_id,
        appointment_limit_count,
        appointment_time_interval,
        nurse_classify_name,
        shelf_status,
        sort,
        create_by,
        create_time,
        update_by,
        update_time
        from nurse_station_item
        <where>
            <if test="nurseStationItemIdsList != null  and nurseStationItemIdsList.size()>0 ">
                and id in
                <foreach item="nurseStationItemIds" collection="nurseStationItemIdsList" open="(" separator=","
                         close=")">
                    #{nurseStationItemIds}
                </foreach>
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="getNurseStationItemById" parameterType="long" resultType="nurseStationItem">
        <include refid="selectNurseStationItemVo"></include>
        where id = #{itemId}
    </select>

    <update id="updateShelfStatusById">
        update nurse_station_item
        set shelf_status = #{shelfStatus}
        where id = #{id}
    </update>
</mapper>