<?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">
<!-- namespace必须指向DAO接口 -->
<mapper namespace="com.jpsoft.shinestar.modules.base.dao.MedicationInfoDAO">
    <resultMap id="MedicationInfoMap" type="com.jpsoft.shinestar.modules.base.entity.MedicationInfo">
        <id property="id" column="id_"/>
        <result property="createPopedomId" column="create_popedom_id"/>
        <result property="beRemindPopedomId" column="be_remind_popedom_id"/>
        <result property="medicationName" column="medication_name"/>
        <result property="startTime" column="start_time"/>
        <result property="endTime" column="end_time"/>
        <result property="medicationDays" column="medication_days"/>
        <result property="nextDays" column="next_days"/>
        <result property="medicationFrequency" column="medication_frequency"/>
        <result property="usage" column="usage_"/>
        <result property="remindTime" column="remind_time"/>
        <result property="needRemind" column="need_remind"/>
        <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"/>
        <result property="remark" column="remark_"/>
    </resultMap>
    <insert id="insert" parameterType="com.jpsoft.shinestar.modules.base.entity.MedicationInfo">
        <!--
        <selectKey resultType="java.lang.String" order="BEFORE" keyProperty="id">
            select sys_guid() from dual
        </selectKey>
        -->
        <![CDATA[
		insert into base_medication_info
	    (id_,create_popedom_id,be_remind_popedom_id,medication_name,start_time,end_time,medication_days,next_days,medication_frequency,usage_,remind_time,need_remind,del_flag,create_by,create_time,update_by,update_time,remark_)
		values
		(
			#{id,jdbcType=VARCHAR}
			,#{createPopedomId,jdbcType=VARCHAR}
			,#{beRemindPopedomId,jdbcType=VARCHAR}
			,#{medicationName,jdbcType=VARCHAR }
			,#{startTime,jdbcType=TIMESTAMP}
            ,#{endTime,jdbcType=TIMESTAMP}
            ,#{medicationDays,jdbcType=INTEGER}
            ,#{nextDays,jdbcType=INTEGER}
            ,#{medicationFrequency,jdbcType=INTEGER}
            ,#{usage,jdbcType=VARCHAR}
            ,#{remindTime,jdbcType=VARCHAR}
            ,#{needRemind,jdbcType=BOOLEAN}
			,#{delFlag,jdbcType= NUMERIC }
			,#{createBy,jdbcType=VARCHAR}
			,#{createTime,jdbcType= TIMESTAMP }
			,#{updateBy,jdbcType=VARCHAR}
			,#{updateTime,jdbcType= TIMESTAMP }
            ,#{remark,jdbcType=VARCHAR}
		)
	]]>
    </insert>
    <delete id="delete" parameterType="string">
        delete from base_medication_info where id_=#{id,jdbcType=VARCHAR}
    </delete>
    <update id="update" parameterType="com.jpsoft.shinestar.modules.base.entity.MedicationInfo">
        update base_medication_info
        <set>
            <if test="createPopedomId!=null">
                create_popedom_id = #{createPopedomId,jdbcType=VARCHAR},
            </if>
            <if test="beRemindPopedomId!=null">
                be_remind_popedom_id=#{beRemindPopedomId,jdbcType=VARCHAR},
            </if>
            <if test="medicationName!=null">
                medication_name = #{medicationName,jdbcType=VARCHAR },
            </if>
            <if test="startTime!=null">
                start_time=#{startTime,jdbcType=TIMESTAMP },
            </if>
            <if test="endTime!=null">
                end_time=#{endTime,jdbcType=TIMESTAMP },
            </if>
            <if test="medicationDays!=null">
                medication_days=#{medicationDays,jdbcType=INTEGER },
            </if>
            <if test="nextDays!=null">
                next_days = #{nextDays,jdbcType=INTEGER },
            </if>
            <if test="medicationFrequency!=null">
                medication_frequency = #{medicationFrequency,jdbcType=INTEGER },
            </if>
            <if test="usage!=null">
                usage_ = #{usage,jdbcType=VARCHAR },
            </if>
            <if test="remindTime!=null">
                remind_time=#{remindTime,jdbcType=VARCHAR },
            </if>
            <if test="needRemind!=null">
                need_remind=#{needRemind,jdbcType=BOOLEAN },
            </if>
            <if test="delFlag!=null">
                del_flag=#{delFlag,jdbcType= NUMERIC },
            </if>
            <if test="createBy!=null">
                create_by=#{createBy,jdbcType=VARCHAR},
            </if>
            <if test="createTime!=null">
                create_time=#{createTime,jdbcType= TIMESTAMP },
            </if>
            <if test="updateBy!=null">
                update_by=#{updateBy,jdbcType=VARCHAR},
            </if>
            <if test="updateTime!=null">
                update_time=#{updateTime,jdbcType= TIMESTAMP },
            </if>
            <if test="remark!=null">
                remark_ = #{remark,jdbcType=VARCHAR },
            </if>
        </set>
        where id_=#{id}
    </update>
    <select id="get" parameterType="string" resultMap="MedicationInfoMap">
        select * from base_medication_info
        where id_=#{0}
        and del_flag = 0
    </select>
    <select id="exist" parameterType="string" resultType="int">
        select count(*) from base_medication_info where id_=#{0}
    </select>
    <select id="list" resultMap="MedicationInfoMap">
        select * from base_medication_info where del_flag = 0
    </select>
    <select id="search" parameterType="hashmap" resultMap="MedicationInfoMap">
        <![CDATA[
			select *
			from base_medication_info
		]]>
        <where>
            del_flag=0
            <if test="searchParams.id != null">
                and id_ = #{searchParams.id}
            </if>
            <if test="searchParams.beRemindPopedomId != null">
                and be_remind_popedom_id = #{searchParams.beRemindPopedomId}
            </if>
            <if test="searchParams.startTime != null">
                <![CDATA[
                and start_time <= #{searchParams.startTime}
                ]]>
            </if>
            <if test="searchParams.endTime != null">
                <![CDATA[
                and end_time >= #{searchParams.endTime}
                 ]]>

            </if>
        </where>
        <foreach item="sort" collection="sortList" open="order by" separator=",">
            ${sort.name} ${sort.order}
        </foreach>
    </select>

    <select id="findByBeRemindPopedomId" resultMap="MedicationInfoMap">
        <![CDATA[
        select * from base_medication_info
        where del_flag = 0
        and be_remind_popedom_id = #{beRemindPopedomId}
        ]]>
    </select>
</mapper>