<?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.emrCollect.Emr_CollectMapper" >
  <resultMap id="BaseResultMap" type="com.emr.entity.emrCollect.Emr_Collect" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="title" property="title" jdbcType="NVARCHAR" />
    <result column="content" property="content" jdbcType="NVARCHAR" />
    <result column="collect_time" property="collectTime" jdbcType="NCHAR" />
    <result column="patient_id" property="patientId" jdbcType="NVARCHAR" />
    <result column="admiss_id" property="admissId" jdbcType="NCHAR" />
    <result column="inpatient_no" property="inpatientNo" jdbcType="NCHAR" />
    <result column="admiss_times" property="admissTimes" jdbcType="SMALLINT" />
    <result column="collect_type_flag" property="collectTypeFlag" jdbcType="NVARCHAR" />
    <result column="collect_type_name" property="collectTypeName" jdbcType="NVARCHAR" />
    <result column="parent_id" property="parentId" jdbcType="INTEGER" />
    <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, user_id, title, content, collect_time, patient_id, admiss_id, inpatient_no, admiss_times, 
    collect_type_flag, collect_type_name, parent_id, creater, create_time, updater, update_time
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from emr_collect
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from emr_collect
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.emr.entity.emrCollect.Emr_Collect" >
    insert into emr_collect (user_id, title,
      content, collect_time, patient_id, 
      admiss_id, inpatient_no, admiss_times, 
      collect_type_flag, collect_type_name, parent_id, 
      creater, create_time, updater, 
      update_time)
    values (#{userId,jdbcType=INTEGER}, #{title,jdbcType=NVARCHAR},
      #{content,jdbcType=NVARCHAR}, #{collectTime,jdbcType=NCHAR}, #{patientId,jdbcType=NVARCHAR}, 
      #{admissId,jdbcType=NCHAR}, #{inpatientNo,jdbcType=NCHAR}, #{admissTimes,jdbcType=SMALLINT}, 
      #{collectTypeFlag,jdbcType=NVARCHAR}, #{collectTypeName,jdbcType=NVARCHAR}, #{parentId,jdbcType=INTEGER}, 
      #{creater,jdbcType=NVARCHAR}, #{createTime,jdbcType=NCHAR}, #{updater,jdbcType=NVARCHAR}, 
      #{updateTime,jdbcType=NCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.emr.entity.emrCollect.Emr_Collect" >
    insert into emr_collect
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="title != null" >
        title,
      </if>
      <if test="content != null" >
        content,
      </if>
      <if test="collectTime != null" >
        collect_time,
      </if>
      <if test="patientId != null" >
        patient_id,
      </if>
      <if test="admissId != null" >
        admiss_id,
      </if>
      <if test="inpatientNo != null" >
        inpatient_no,
      </if>
      <if test="admissTimes != null" >
        admiss_times,
      </if>
      <if test="collectTypeFlag != null" >
        collect_type_flag,
      </if>
      <if test="collectTypeName != null" >
        collect_type_name,
      </if>
      <if test="parentId != null" >
        parent_id,
      </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="userId != null" >
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="title != null" >
        #{title,jdbcType=NVARCHAR},
      </if>
      <if test="content != null" >
        #{content,jdbcType=NVARCHAR},
      </if>
      <if test="collectTime != null" >
        #{collectTime,jdbcType=NCHAR},
      </if>
      <if test="patientId != null" >
        #{patientId,jdbcType=NVARCHAR},
      </if>
      <if test="admissId != null" >
        #{admissId,jdbcType=NCHAR},
      </if>
      <if test="inpatientNo != null" >
        #{inpatientNo,jdbcType=NCHAR},
      </if>
      <if test="admissTimes != null" >
        #{admissTimes,jdbcType=SMALLINT},
      </if>
      <if test="collectTypeFlag != null" >
        #{collectTypeFlag,jdbcType=NVARCHAR},
      </if>
      <if test="collectTypeName != null" >
        #{collectTypeName,jdbcType=NVARCHAR},
      </if>
      <if test="parentId != null" >
        #{parentId,jdbcType=INTEGER},
      </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.emrCollect.Emr_Collect" >
    update emr_collect
    <set >
      <if test="userId != null" >
        user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="title != null" >
        title = #{title,jdbcType=NVARCHAR},
      </if>
      <if test="content != null" >
        content = #{content,jdbcType=NVARCHAR},
      </if>
      <if test="collectTime != null" >
        collect_time = #{collectTime,jdbcType=NCHAR},
      </if>
      <if test="patientId != null" >
        patient_id = #{patientId,jdbcType=NVARCHAR},
      </if>
      <if test="admissId != null" >
        admiss_id = #{admissId,jdbcType=NCHAR},
      </if>
      <if test="inpatientNo != null" >
        inpatient_no = #{inpatientNo,jdbcType=NCHAR},
      </if>
      <if test="admissTimes != null" >
        admiss_times = #{admissTimes,jdbcType=SMALLINT},
      </if>
      <if test="collectTypeFlag != null" >
        collect_type_flag = #{collectTypeFlag,jdbcType=NVARCHAR},
      </if>
      <if test="collectTypeName != null" >
        collect_type_name = #{collectTypeName,jdbcType=NVARCHAR},
      </if>
      <if test="parentId != null" >
        parent_id = #{parentId,jdbcType=INTEGER},
      </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.emrCollect.Emr_Collect" >
    update emr_collect
    set user_id = #{userId,jdbcType=INTEGER},
      title = #{title,jdbcType=NVARCHAR},
      content = #{content,jdbcType=NVARCHAR},
      collect_time = #{collectTime,jdbcType=NCHAR},
      patient_id = #{patientId,jdbcType=NVARCHAR},
      admiss_id = #{admissId,jdbcType=NCHAR},
      inpatient_no = #{inpatientNo,jdbcType=NCHAR},
      admiss_times = #{admissTimes,jdbcType=SMALLINT},
      collect_type_flag = #{collectTypeFlag,jdbcType=NVARCHAR},
      collect_type_name = #{collectTypeName,jdbcType=NVARCHAR},
      parent_id = #{parentId,jdbcType=INTEGER},
      creater = #{creater,jdbcType=NVARCHAR},
      create_time = #{createTime,jdbcType=NCHAR},
      updater = #{updater,jdbcType=NVARCHAR},
      update_time = #{updateTime,jdbcType=NCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>
  <!--根据用户id查询收藏分类-->
  <select id="selectAllCollectTypeByCreater" resultMap="BaseResultMap">
      SELECT
          id,
          title,
          content,
          collect_time,
          patient_id,
          admiss_id,
          inpatient_no,
          admiss_times,
          collect_type_flag,
          collect_type_name,
          parent_id,
          collect_type_name newName
      FROM
          emr_collect
      <where>
          <if test="userIds != null">
              user_id IN (${userIds})
          </if>
          <if test="collectTypeFlag != null">
            AND collect_type_flag != ''
          </if>
      </where>
  </select>

  <!--根据用户id查询收藏集合-->
  <select id="selectAllCollectByCreater" resultMap="BaseResultMap">
      SELECT
          a.id,
          a.title,
          a.content,
          a.collect_time,
          a.inpatient_no,
          a.admiss_times,
          b.collect_type_name,
          a.creater,
          a.parent_id,
          c.name,
          c.dis_date disTime
      FROM
          emr_collect a
      LEFT JOIN
          emr_collect b
      ON
          a.parent_id = b.id
      LEFT JOIN
          commomtable c
      ON
          a.patient_id = c.patient_id
      WHERE
          a.collect_type_flag is null
          <if test="userIds != null">
              AND a.user_id IN (${userIds})
          </if>
          <if test="record.creater != '' and record.creater != null">
              AND a.creater LIKE '%${record.creater}%'
          </if>
          <if test="record.inpatientNo != '' and record.inpatientNo != null">
              AND a.inpatient_no LIKE '%${record.inpatientNo}%'
          </if>
          <if test="record.admissTimes != '' and record.admissTimes != null">
              AND a.admiss_times = ${record.admissTimes}
          </if>
          <if test="startTime != '' and startTime != null">
              AND a.collect_time &gt;=  #{startTime}
              <if test="endTime != '' and endTime != null">
                  AND a.collect_time  &lt;= #{endTime}
              </if>
          </if>
          <if test="record.name != '' and record.name != null">
              AND c.name LIKE '%${record.name}%'
          </if>
          <if test="record.parentId != null">
              AND a.parent_id = ${record.parentId}
          </if>
      ORDER BY a.collect_time DESC,a.id DESC
  </select>
  <!--根据用户id和patientId查询收藏对象-->
  <select id="selectCollectByUserIdAndPatientId" resultType="com.emr.entity.emrCollect.Emr_Collect" >
      SELECT
          id,
          title,
          content,
          collect_time,
          patient_id,
          admiss_id,
          inpatient_no,
          admiss_times,
          collect_type_flag,
          collect_type_name,
          parent_id
      FROM
          emr_collect
      <where>
          user_id = ${userId}
        AND
          patient_id = #{patientId}
      </where>
  </select>

  <!--根据用户Id和收藏类别名称查验类别名称是否存在-->
  <select id="checkCollectTypeNameByCreaterAndTypeName" resultType="com.emr.entity.emrCollect.Emr_Collect">
      SELECT
          id
      FROM
          emr_collect
      WHERE
          user_id = ${userId}
      AND
          collect_type_name = #{collectTypeName}
  </select>

  <!--根据id查询对象，包含是否可查看0不可查看，1可查看，是否被锁0未锁，1被锁-->
  <select id="getCollectAndShowAndLockById" resultType="com.emr.entity.emrCollect.Emr_Collect">
      select * from (SELECT
          CASE WHEN emr_apply_approve.id is null THEN 0 ELSE 1 END isShowDetail,
          CASE WHEN emr_lock.id is null THEN 0 ELSE 1 END lockStatus,
          emr_collect.*,commomtable.name newName,
          commomtable.dis_date
        FROM
            emr_collect
        LEFT JOIN
            emr_apply_approve
        ON
            emr_collect.patient_id = emr_apply_approve.patient_id
		AND
		    emr_apply_approve.approve_state = 1
        AND
            emr_apply_approve.applyer = #{applyer}
        AND
            emr_apply_approve.effe_time > GETDATE()-1
        LEFT JOIN
            emr_lock
        ON
            emr_collect.patient_id = emr_lock.patient_id
        AND
            emr_lock.lock_state = 1
        LEFT JOIN
            commomtable
        ON
            emr_collect.patient_id = commomtable.patient_id
		WHERE
		    emr_collect.patient_id=(
		        SELECT
		            patient_id
                FROM
                    emr_collect
                WHERE
                    id=${id}
                )) a where a.id = ${id}
  </select>

  <!--删除父集时，父id置空-->
  <update id="updateByDelParentId" parameterType="java.lang.Integer" >
    update emr_collect
    set parent_id = NULL
    where parent_id = #{parentId,jdbcType=INTEGER}
  </update>
  <!--判断类别标志或名称不能重复-->
  <select id="checkTypeFlagOrTypeName" resultMap="BaseResultMap"  parameterType="java.lang.String" >
    SELECT id
    FROM emr_collect
    WHERE
        collect_type_flag is not null
    AND user_id = ${userId}
    <if test="collectTypeFlag != null and collectTypeFlag != ''">
        AND collect_type_flag = #{collectTypeFlag}
    </if>
    <if test="collectTypeName != null and collectTypeName != ''">
        AND collect_type_name = #{collectTypeName}
    </if>
  </select>

  <!--根据id集合查询-->
  <select id="selectAllByIds" resultMap="BaseResultMap"  parameterType="java.lang.String" >
    SELECT
          a.id,
          a.title,
          a.content,
          a.collect_time,
          a.inpatient_no,
          a.admiss_times,
          b.collect_type_name,
          a.creater,
          a.parent_id
      FROM
          emr_collect a
      LEFT JOIN
          emr_collect b
      ON
          a.parent_id = b.id
    WHERE
        a.id IN (${checks})
  </select>
</mapper>