<?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.zhyc.module.biosafety.mapper.SwPrescriptionMapper">
    
    <resultMap type="SwPrescription" id="SwPrescriptionResult">
        <result property="id"    column="id"    />
        <result property="no"    column="no"    />
        <result property="name"    column="name"    />
        <result property="persType"    column="pers_type"    />
        <result property="comment"    column="comment"    />
        <result property="status"    column="status"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
    </resultMap>

    <resultMap id="SwPrescriptionSwPresDetailResult" type="SwPrescription" extends="SwPrescriptionResult">
        <collection property="swPresDetailList" ofType="SwPresDetail" column="id" select="selectSwPresDetailList" />
    </resultMap>

    <resultMap type="SwPresDetail" id="SwPresDetailResult">
        <result property="id"    column="id"    />
        <result property="persId"    column="pers_id"    />
        <result property="mediId"    column="medi_id"    />
        <result property="dosage"    column="dosage"    />
        <result property="unitId"    column="unit_id"    />
        <result property="usageId"    column="usage_id"    />
    </resultMap>

    <sql id="selectSwPrescriptionVo">
        select id, no, name, pers_type, comment, status, update_by, update_time, create_by, create_time from sw_prescription
    </sql>

    <select id="selectSwPrescriptionList" parameterType="SwPrescription" resultMap="SwPrescriptionResult">
        <include refid="selectSwPrescriptionVo"/>
        <where>  
            <if test="no != null  and no != ''"> and no like concat('%', #{no}, '%')</if>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="persType != null "> and pers_type = #{persType}</if>
            <if test="status != null "> and status = #{status}</if>
        </where>
    </select>
    
    <select id="selectSwPrescriptionById" parameterType="Long" resultMap="SwPrescriptionSwPresDetailResult">
        select id, no, name, pers_type, comment, status, update_by, update_time, create_by, create_time
        from sw_prescription
        where id = #{id}
    </select>

    <select id="selectSwPresDetailList" resultMap="SwPresDetailResult">
        select id, pers_id, medi_id, dosage, unit_id, usage_id
        from sw_pres_detail
        where pers_id = #{pers_id}
    </select>

    <insert id="insertSwPrescription" parameterType="SwPrescription" useGeneratedKeys="true" keyProperty="id">
        insert into sw_prescription
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="no != null">no,</if>
            <if test="name != null">name,</if>
            <if test="persType != null">pers_type,</if>
            <if test="comment != null">comment,</if>
            <if test="status != null">status,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="no != null">#{no},</if>
            <if test="name != null">#{name},</if>
            <if test="persType != null">#{persType},</if>
            <if test="comment != null">#{comment},</if>
            <if test="status != null">#{status},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
         </trim>
    </insert>

    <update id="updateSwPrescription" parameterType="SwPrescription">
        update sw_prescription
        <trim prefix="SET" suffixOverrides=",">
            <if test="no != null">no = #{no},</if>
            <if test="name != null">name = #{name},</if>
            <if test="persType != null">pers_type = #{persType},</if>
            <if test="comment != null">comment = #{comment},</if>
            <if test="status != null">status = #{status},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSwPrescriptionById" parameterType="Long">
        delete from sw_prescription where id = #{id}
    </delete>

    <delete id="deleteSwPrescriptionByIds" parameterType="String">
        delete from sw_prescription where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    
    <delete id="deleteSwPresDetailByPersIds" parameterType="String">
        delete from sw_pres_detail where pers_id in 
        <foreach item="persId" collection="array" open="(" separator="," close=")">
            #{persId}
        </foreach>
    </delete>

    <delete id="deleteSwPresDetailByPersId" parameterType="Long">
        delete from sw_pres_detail where pers_id = #{persId}
    </delete>

    <insert id="batchSwPresDetail">
        insert into sw_pres_detail( id, pers_id, medi_id, dosage, unit_id, usage_id) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.persId}, #{item.mediId}, #{item.dosage}, #{item.unitId}, #{item.usageId})
        </foreach>
    </insert>
</mapper>