<?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.emr.dao.templateSearch.Emr_ModleMapper" >
  <resultMap id="BaseResultMap" type="com.emr.entity.templateSearch.Emr_Modle" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="modle_flag" property="modleFlag" jdbcType="NVARCHAR" />
    <result column="modle_name" property="modleName" jdbcType="NVARCHAR" />
    <result column="effe_time" property="effeTime" jdbcType="NVARCHAR" />
    <result column="select_flag" property="selectFlag" jdbcType="INTEGER" />
    <result column="effective" property="effective" jdbcType="INTEGER" />
    <result column="modle_sort" property="modleSort" jdbcType="INTEGER" />
    <result column="remark" property="remark" jdbcType="NVARCHAR" />
    <result column="creater" property="creater" jdbcType="NVARCHAR" />
    <result column="create_time" property="createTime" jdbcType="NCHAR" />
    <result column="updater" property="updater" jdbcType="NVARCHAR" />
    <result column="update_time" property="updateTime" jdbcType="NCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, modle_flag, modle_name, effe_time, select_flag, effective, modle_sort, remark, 
    creater, create_time, updater, update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from emr_modle
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from emr_modle
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.emr.entity.templateSearch.Emr_Modle" useGeneratedKeys="true" keyProperty="id">
    insert into emr_modle (modle_flag, modle_name,
      effe_time, select_flag, effective, 
      modle_sort, remark, creater, 
      create_time, updater, update_time
      )
    values (#{modleFlag,jdbcType=NVARCHAR}, #{modleName,jdbcType=NVARCHAR},
      #{effeTime,jdbcType=NVARCHAR}, #{selectFlag,jdbcType=INTEGER}, #{effective,jdbcType=INTEGER}, 
      #{modleSort,jdbcType=INTEGER}, #{remark,jdbcType=NVARCHAR}, #{creater,jdbcType=NVARCHAR}, 
      #{createTime,jdbcType=NCHAR}, #{updater,jdbcType=NVARCHAR}, #{updateTime,jdbcType=NCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.emr.entity.templateSearch.Emr_Modle" >
    insert into emr_modle
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="modleFlag != null" >
        modle_flag,
      </if>
      <if test="modleName != null" >
        modle_name,
      </if>
      <if test="effeTime != null" >
        effe_time,
      </if>
      <if test="selectFlag != null" >
        select_flag,
      </if>
      <if test="effective != null" >
        effective,
      </if>
      <if test="modleSort != null" >
        modle_sort,
      </if>
      <if test="remark != null" >
        remark,
      </if>
      <if test="creater != null" >
        creater,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="updater != null" >
        updater,
      </if>
      <if test="updateTime != null" >
        update_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="modleFlag != null" >
        #{modleFlag,jdbcType=NVARCHAR},
      </if>
      <if test="modleName != null" >
        #{modleName,jdbcType=NVARCHAR},
      </if>
      <if test="effeTime != null" >
        #{effeTime,jdbcType=NVARCHAR},
      </if>
      <if test="selectFlag != null" >
        #{selectFlag,jdbcType=INTEGER},
      </if>
      <if test="effective != null" >
        #{effective,jdbcType=INTEGER},
      </if>
      <if test="modleSort != null" >
        #{modleSort,jdbcType=INTEGER},
      </if>
      <if test="remark != null" >
        #{remark,jdbcType=NVARCHAR},
      </if>
      <if test="creater != null" >
        #{creater,jdbcType=NVARCHAR},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=NCHAR},
      </if>
      <if test="updater != null" >
        #{updater,jdbcType=NVARCHAR},
      </if>
      <if test="updateTime != null" >
        #{updateTime,jdbcType=NCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.emr.entity.templateSearch.Emr_Modle" >
    update emr_modle
    <set >
      <if test="modleFlag != null" >
        modle_flag = #{modleFlag,jdbcType=NVARCHAR},
      </if>
      <if test="modleName != null" >
        modle_name = #{modleName,jdbcType=NVARCHAR},
      </if>
      <if test="effeTime != null" >
        effe_time = #{effeTime,jdbcType=NVARCHAR},
      </if>
      <if test="selectFlag != null" >
        select_flag = #{selectFlag,jdbcType=INTEGER},
      </if>
      <if test="effective != null" >
        effective = #{effective,jdbcType=INTEGER},
      </if>
      <if test="modleSort != null" >
        modle_sort = #{modleSort,jdbcType=INTEGER},
      </if>
      <if test="remark != null" >
        remark = #{remark,jdbcType=NVARCHAR},
      </if>
      <if test="creater != null" >
        creater = #{creater,jdbcType=NVARCHAR},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=NCHAR},
      </if>
      <if test="updater != null" >
        updater = #{updater,jdbcType=NVARCHAR},
      </if>
      <if test="updateTime != null" >
        update_time = #{updateTime,jdbcType=NCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.emr.entity.templateSearch.Emr_Modle" >
    update emr_modle
    set modle_flag = #{modleFlag,jdbcType=NVARCHAR},
      modle_name = #{modleName,jdbcType=NVARCHAR},
      effe_time = #{effeTime,jdbcType=NVARCHAR},
      select_flag = #{selectFlag,jdbcType=INTEGER},
      effective = #{effective,jdbcType=INTEGER},
      modle_sort = #{modleSort,jdbcType=INTEGER},
      remark = #{remark,jdbcType=NVARCHAR},
      creater = #{creater,jdbcType=NVARCHAR},
      create_time = #{createTime,jdbcType=NCHAR},
      updater = #{updater,jdbcType=NVARCHAR},
      update_time = #{updateTime,jdbcType=NCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <!--验证模板标识是否存在-->
  <select id="checkModleFlag" resultType="com.emr.entity.templateSearch.Emr_Modle" parameterType="java.lang.String" >
    SELECT
        id
    FROM
        emr_modle
    WHERE
        creater = #{creater}
    AND
        modle_flag = #{modleFlag}
  </select>
  <!--验证模板名称是否存在-->
  <select id="checkModleName" resultType="com.emr.entity.templateSearch.Emr_Modle" parameterType="java.lang.String" >
    SELECT
        id
    FROM
        emr_modle
    WHERE
        creater = #{creater}
    AND
        modle_name = #{modleName}
  </select>
  <!--根据创建用户查询默认模板对象-->
  <select id="selectFlagExist" resultType="com.emr.entity.templateSearch.Emr_Modle" parameterType="java.lang.String" >
    SELECT
        TOP 1 *
    FROM
        emr_modle
    WHERE
        select_flag = 1
        <if test="creater != null">
            AND  creater = #{creater}
        </if>
  </select>
  <!--根据创建用户查询模板集合-->
  <select id="selectAllByCreater" resultMap="BaseResultMap" parameterType="java.lang.String" >
    SELECT
        *
    FROM
        (
            SELECT
                *, modle_name newName,
                isOper = 1
            FROM
                emr_modle
            WHERE
                id = 1
            UNION ALL
                SELECT
                    *, modle_name newName,
                    isOper = 1
                FROM
                    emr_modle
                <where>
                    id != 1
                    <if test="creater != null and creater != ''">
                        AND creater IN (${creater})
                    </if>

                </where>

        ) a
    ORDER BY
        select_flag,
        modle_sort
  </select>
  <!--根据创建用户查询可用模板集合-->
  <select id="selectAllByCreaterOrderSelect" resultMap="BaseResultMap" parameterType="java.lang.String" >
    SELECT
        id,
        modle_name,
        select_flag
    FROM
        emr_modle
    WHERE
        effective = 1
    AND
        effe_time >= CONVERT(varchar(10),GETDATE(),120)
    AND
        creater = #{creater}
    ORDER BY
        select_flag desc,modle_sort
  </select>
</mapper>