<?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.nursestationconsumable.NurseStationConsumableMapper">

    <resultMap type="NurseStationConsumable" id="NurseStationConsumableResult">
        <result property="id" column="id"/>
        <result property="nurseStationId" column="nurse_station_id"/>
        <result property="userId" column="user_id"/>
        <result property="consumableCode" column="consumable_code"/>
        <result property="consumableDetail" column="consumable_detail"/>
        <result property="consumableUnit" column="consumable_unit"/>
        <result property="consumablePrice" column="consumable_price"/>
        <result property="sort" column="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>

    <sql id="selectNurseStationConsumableVo">
        select id,
               nurse_station_id,
               user_id,
               consumable_code,
               consumable_detail,
               consumable_unit,
               consumable_price,
               sort,
               create_by,
               create_time,
               update_by,
               update_time
        from nurse_station_consumable
    </sql>

    <select id="selectNurseStationConsumableList" parameterType="NurseStationConsumable"
            resultType="com.xinelu.manage.vo.nursestationconsumable.NurseStationConsumableVO">
        select
        nsc.id,
        nsc.nurse_station_id,
        nsc.user_id,
        nsc.consumable_code,
        nsc.consumable_detail,
        nsc.consumable_unit,
        nsc.consumable_price,
        nsc.sort,
        nsc.create_by,
        nsc.create_time,
        ns.nurse_station_name
        FROM nurse_station_consumable nsc
        LEFT JOIN nurse_station ns on ns.id = nsc.nurse_station_id
        <where>
            <if test="nurseStationId != null ">
                and nsc.nurse_station_id = #{nurseStationId}
            </if>
            <if test="userId != null ">
                and nsc.user_id = #{dto.userId}
            </if>
            <if test="consumableCode != null  and consumableCode != ''">
                and nsc.consumable_code like concat('%', #{consumableCode}, '%')
            </if>
            <if test="consumableDetail != null  and consumableDetail != ''">
                and nsc.consumable_detail like concat('%', #{consumableDetail}, '%')
            </if>
            <if test="consumableUnit != null  and consumableUnit != ''">
                and nsc.consumable_unit = #{consumableUnit}
            </if>
            <if test="consumablePrice != null ">
                and nsc.consumable_price = #{consumablePrice}
            </if>
            <if test="sort != null ">
                and nsc.sort = #{dto.sort}
            </if>
        </where>
        ORDER BY nsc.create_time DESC
    </select>

    <select id="selectNurseStationConsumableById" parameterType="Long"
            resultType="com.xinelu.manage.vo.nursestationconsumable.NurseStationConsumableVO">
        SELECT nsc.id,
               nsc.consumable_detail,
               nsc.consumable_unit,
               nsc.consumable_price,
               nsc.sort,
               ns.id nurseStationId,
               ns.nurse_station_name
        FROM nurse_station_consumable nsc
                 LEFT JOIN nurse_station ns on ns.id = nsc.nurse_station_id
        where nsc.id = #{id}
    </select>

    <insert id="insertNurseStationConsumable" parameterType="java.util.List">
        INSERT INTO
        nurse_station_consumable(nurse_station_id, user_id, consumable_code,
        consumable_detail,consumable_unit,consumable_price, sort, create_by, create_time, update_by, update_time)
        VALUES
        <foreach collection="list" item="productCategory" index="index"
                 separator=",">
            (
            #{productCategory.nurseStationId},
            #{productCategory.userId},
            #{productCategory.consumableCode},
            #{productCategory.consumableDetail},
            #{productCategory.consumableUnit},
            #{productCategory.consumablePrice},
            #{productCategory.sort},
            #{productCategory.createBy},
            #{productCategory.createTime},
            #{productCategory.updateBy},
            #{productCategory.updateTime}
            )
        </foreach>
    </insert>

    <update id="updateNurseStationConsumable" parameterType="NurseStationConsumable">
        update nurse_station_consumable
        <trim prefix="SET" suffixOverrides=",">
            <if test="nurseStationId != null">nurse_station_id =
                #{nurseStationId},
            </if>
            <if test="userId != null">user_id =
                #{userId},
            </if>
            <if test="consumableCode != null">consumable_code =
                #{consumableCode},
            </if>
            <if test="consumableDetail != null">consumable_detail =
                #{consumableDetail},
            </if>
            <if test="consumableUnit != null">consumable_unit =
                #{consumableUnit},
            </if>
            <if test="consumablePrice != null">consumable_price =
                #{consumablePrice},
            </if>
            <if test="sort != null">sort =
                #{sort},
            </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="deleteNurseStationConsumableById" parameterType="Long">
        delete
        from nurse_station_consumable
        where id = #{id}
    </delete>

    <delete id="deleteNurseStationConsumableByIds" parameterType="String">
        delete from nurse_station_consumable where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <select id="selectConsumableInfoList" parameterType="String"
            resultType="com.xinelu.manage.dto.nursestationconsumable.NurseStationConsumableImportlDTO">
        SELECT ns.nurse_station_name,
        nsc.consumable_detail
        FROM nurse_station_consumable nsc
        LEFT JOIN nurse_station ns ON ns.id = nsc.nurse_station_id
        <where>
            <if test="nurseStationNameList != null and nurseStationNameList.size() > 0">
                and ns.nurse_station_name in
                <foreach item="nurseStationName" collection="nurseStationNameList" open="(" separator="," close=")">
                    #{nurseStationName}
                </foreach>
            </if>
        </where>
    </select>
</mapper>
