<?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.fs.store.mapper.FsPrescribeDrugMapper">
    
    <resultMap type="FsPrescribeDrug" id="FsPrescribeDrugResult">
        <result property="drugId"    column="drug_id"    />
        <result property="prescribeId"    column="prescribe_id"    />
        <result property="drugCommonName"    column="drug_common_name"    />
        <result property="drugSpecification"    column="drug_specification"    />
        <result property="usageMethod"    column="usage_method"    />
        <result property="usageFrequencyUnit"    column="usage_frequency_unit"    />
        <result property="usagePerUseCount"    column="usage_per_use_count"    />
        <result property="usagePerUseUnit"    column="usage_per_use_unit"    />
        <result property="usageDays"    column="usage_days"    />
        <result property="saleAmount"    column="sale_amount"    />
        <result property="saleUnit"    column="sale_unit"    />
        <result property="instructions"    column="instructions"    />
        <result property="approvalNumber"    column="approval_number"    />
        <result property="productId"    column="product_id"    />
    </resultMap>

    <sql id="selectFsPrescribeDrugVo">
        select drug_id, prescribe_id, drug_common_name, drug_specification, usage_method, usage_frequency_unit, usage_per_use_count, usage_per_use_unit, usage_days, sale_amount, sale_unit, instructions, approval_number, product_id from fs_prescribe_drug
    </sql>

    <select id="selectFsPrescribeDrugList" parameterType="FsPrescribeDrug" resultMap="FsPrescribeDrugResult">
        <include refid="selectFsPrescribeDrugVo"/>
        <where>  
            <if test="prescribeId != null "> and prescribe_id = #{prescribeId}</if>
            <if test="drugCommonName != null  and drugCommonName != ''"> and drug_common_name like concat('%', #{drugCommonName}, '%')</if>
            <if test="drugSpecification != null  and drugSpecification != ''"> and drug_specification = #{drugSpecification}</if>
            <if test="usageMethod != null  and usageMethod != ''"> and usage_method = #{usageMethod}</if>
            <if test="usageFrequencyUnit != null  and usageFrequencyUnit != ''"> and usage_frequency_unit = #{usageFrequencyUnit}</if>
            <if test="usagePerUseCount != null  and usagePerUseCount != ''"> and usage_per_use_count = #{usagePerUseCount}</if>
            <if test="usagePerUseUnit != null  and usagePerUseUnit != ''"> and usage_per_use_unit = #{usagePerUseUnit}</if>
            <if test="usageDays != null  and usageDays != ''"> and usage_days = #{usageDays}</if>
            <if test="saleAmount != null  and saleAmount != ''"> and sale_amount = #{saleAmount}</if>
            <if test="saleUnit != null  and saleUnit != ''"> and sale_unit = #{saleUnit}</if>
            <if test="instructions != null  and instructions != ''"> and instructions = #{instructions}</if>
            <if test="approvalNumber != null  and approvalNumber != ''"> and approval_number = #{approvalNumber}</if>
            <if test="productId != null "> and product_id = #{productId}</if>
        </where>
    </select>
    
    <select id="selectFsPrescribeDrugById" parameterType="Long" resultMap="FsPrescribeDrugResult">
        <include refid="selectFsPrescribeDrugVo"/>
        where drug_id = #{drugId}
    </select>
        
    <insert id="insertFsPrescribeDrug" parameterType="FsPrescribeDrug" useGeneratedKeys="true" keyProperty="drugId">
        insert into fs_prescribe_drug
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="prescribeId != null">prescribe_id,</if>
            <if test="drugCommonName != null">drug_common_name,</if>
            <if test="drugSpecification != null">drug_specification,</if>
            <if test="usageMethod != null">usage_method,</if>
            <if test="usageFrequencyUnit != null">usage_frequency_unit,</if>
            <if test="usagePerUseCount != null">usage_per_use_count,</if>
            <if test="usagePerUseUnit != null">usage_per_use_unit,</if>
            <if test="usageDays != null">usage_days,</if>
            <if test="saleAmount != null">sale_amount,</if>
            <if test="saleUnit != null">sale_unit,</if>
            <if test="instructions != null">instructions,</if>
            <if test="approvalNumber != null">approval_number,</if>
            <if test="productId != null">product_id,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="prescribeId != null">#{prescribeId},</if>
            <if test="drugCommonName != null">#{drugCommonName},</if>
            <if test="drugSpecification != null">#{drugSpecification},</if>
            <if test="usageMethod != null">#{usageMethod},</if>
            <if test="usageFrequencyUnit != null">#{usageFrequencyUnit},</if>
            <if test="usagePerUseCount != null">#{usagePerUseCount},</if>
            <if test="usagePerUseUnit != null">#{usagePerUseUnit},</if>
            <if test="usageDays != null">#{usageDays},</if>
            <if test="saleAmount != null">#{saleAmount},</if>
            <if test="saleUnit != null">#{saleUnit},</if>
            <if test="instructions != null">#{instructions},</if>
            <if test="approvalNumber != null">#{approvalNumber},</if>
            <if test="productId != null">#{productId},</if>
         </trim>
    </insert>

    <update id="updateFsPrescribeDrug" parameterType="FsPrescribeDrug">
        update fs_prescribe_drug
        <trim prefix="SET" suffixOverrides=",">
            <if test="prescribeId != null">prescribe_id = #{prescribeId},</if>
            <if test="drugCommonName != null">drug_common_name = #{drugCommonName},</if>
            <if test="drugSpecification != null">drug_specification = #{drugSpecification},</if>
            <if test="usageMethod != null">usage_method = #{usageMethod},</if>
            <if test="usageFrequencyUnit != null">usage_frequency_unit = #{usageFrequencyUnit},</if>
            <if test="usagePerUseCount != null">usage_per_use_count = #{usagePerUseCount},</if>
            <if test="usagePerUseUnit != null">usage_per_use_unit = #{usagePerUseUnit},</if>
            <if test="usageDays != null">usage_days = #{usageDays},</if>
            <if test="saleAmount != null">sale_amount = #{saleAmount},</if>
            <if test="saleUnit != null">sale_unit = #{saleUnit},</if>
            <if test="instructions != null">instructions = #{instructions},</if>
            <if test="approvalNumber != null">approval_number = #{approvalNumber},</if>
            <if test="productId != null">product_id = #{productId},</if>
        </trim>
        where drug_id = #{drugId}
    </update>

    <delete id="deleteFsPrescribeDrugById" parameterType="Long">
        delete from fs_prescribe_drug where drug_id = #{drugId}
    </delete>

    <delete id="deleteFsPrescribeDrugByIds" parameterType="String">
        delete from fs_prescribe_drug where drug_id in 
        <foreach item="drugId" collection="array" open="(" separator="," close=")">
            #{drugId}
        </foreach>
    </delete>
    
</mapper>