<?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.emrPrintAndDownLoad.EmrPrintDownloadInfoMapper" >
  <resultMap id="BaseResultMap" type="com.emr.entity.emrPrintAndDownLoad.EmrPrintDownloadInfo" >
    <result column="id" property="id" jdbcType="INTEGER" />
    <result column="patient_id" property="patientId" jdbcType="NVARCHAR" />
    <result column="scan_page" property="scanPage" jdbcType="VARCHAR" />
    <result column="type_id" property="typeId" jdbcType="INTEGER" />
    <result column="oper_type" property="operType" jdbcType="SMALLINT" />
    <result column="create_time" property="createTime" jdbcType="NVARCHAR" />
    <result column="creater" property="creater" jdbcType="NVARCHAR" />
    <result column="ip" property="ip" jdbcType="NVARCHAR" />
  </resultMap>
  <resultMap id="BaseResultMapVo" type="com.emr.vo.EmrPrintOrDownLoadInfo.EmrPrintOrDownLoadInfoVo" >
    <result column="id" property="id" jdbcType="INTEGER" />
    <result column="patient_id" property="patientId" jdbcType="NVARCHAR" />
    <result column="scan_page" property="scanPage" jdbcType="VARCHAR" />
    <result column="type_id" property="typeId" jdbcType="INTEGER" />
    <result column="oper_type" property="operType" jdbcType="SMALLINT" />
    <result column="create_time" property="createTime" jdbcType="VARCHAR" />
    <result column="creater" property="creater" jdbcType="NVARCHAR" />
    <result column="inpatient_no" property="inpatientNo" jdbcType="VARCHAR" />
    <result column="admiss_times" property="admissTimes" jdbcType="SMALLINT" />
    <result column="type_name" property="typeName" jdbcType="VARCHAR" />
    <result column="assort_id" property="assortId" jdbcType="VARCHAR" />
    <result column="assort_name" property="assortName" jdbcType="VARCHAR" />
    <result column="ip" property="ip" jdbcType="VARCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, patient_id, scan_page, type_id, oper_type, create_time, creater, ip
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from emr_print_download_info
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from emr_print_download_info
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.emr.entity.emrPrintAndDownLoad.EmrPrintDownloadInfo" >
    insert into emr_print_download_info (id, patient_id, scan_page,
      type_id, oper_type, create_time,
      creater, ip)
    values (#{id,jdbcType=INTEGER}, #{patientId,jdbcType=NVARCHAR}, #{scanPage,jdbcType=VARCHAR},
      #{typeId,jdbcType=INTEGER}, #{operType,jdbcType=SMALLINT}, #{createTime,jdbcType=NVARCHAR},
      #{creater,jdbcType=NVARCHAR}, #{ip,jdbcType=NVARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.emr.entity.emrPrintAndDownLoad.EmrPrintDownloadInfo" >
    insert into emr_print_download_info
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="patientId != null" >
        patient_id,
      </if>
      <if test="scanPage != null" >
        scan_page,
      </if>
      <if test="typeId != null" >
        type_id,
      </if>
      <if test="operType != null" >
        oper_type,
      </if>
      <if test="createTime != null" >
        create_time,
      </if>
      <if test="creater != null" >
        creater,
      </if>
      <if test="ip != null" >
        ip,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="patientId != null" >
        #{patientId,jdbcType=NVARCHAR},
      </if>
      <if test="scanPage != null" >
        #{scanPage,jdbcType=VARCHAR},
      </if>
      <if test="typeId != null" >
        #{typeId,jdbcType=INTEGER},
      </if>
      <if test="operType != null" >
        #{operType,jdbcType=SMALLINT},
      </if>
      <if test="createTime != null" >
        #{createTime,jdbcType=NVARCHAR},
      </if>
      <if test="creater != null" >
        #{creater,jdbcType=NVARCHAR},
      </if>
      <if test="ip != null" >
        #{ip,jdbcType=NVARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.emr.entity.emrPrintAndDownLoad.EmrPrintDownloadInfo" >
    update emr_print_download_info
    <set >
      <if test="patientId != null" >
        patient_id = #{patientId,jdbcType=NVARCHAR},
      </if>
      <if test="scanPage != null" >
        scan_page = #{scanPage,jdbcType=VARCHAR},
      </if>
      <if test="typeId != null" >
        type_id = #{typeId,jdbcType=INTEGER},
      </if>
      <if test="operType != null" >
        oper_type = #{operType,jdbcType=SMALLINT},
      </if>
      <if test="createTime != null" >
        create_time = #{createTime,jdbcType=NVARCHAR},
      </if>
      <if test="creater != null" >
        creater = #{creater,jdbcType=NVARCHAR},
      </if>
      <if test="ip != null" >
        ip = #{ip,jdbcType=NVARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.emr.entity.emrPrintAndDownLoad.EmrPrintDownloadInfo" >
    update emr_print_download_info
    set patient_id = #{patientId,jdbcType=NVARCHAR},
      scan_page = #{scanPage,jdbcType=VARCHAR},
      type_id = #{typeId,jdbcType=INTEGER},
      oper_type = #{operType,jdbcType=SMALLINT},
      create_time = #{createTime,jdbcType=NVARCHAR},
      creater = #{creater,jdbcType=NVARCHAR},
      ip = #{ip,jdbcType=NVARCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <!--查询列表-->
  <select id="selectAll" resultMap="BaseResultMapVo" parameterType="com.emr.vo.EmrPrintOrDownLoadInfo.EmrPrintOrDownLoadInfoVo">
    SELECT
    emr_print_download_info.id,
    emr_print_download_info.create_time,
    emr_print_download_info.patient_id,
    emr_print_download_info.scan_page,
    emr_print_download_info.creater,
    emr_print_download_info.ip,
    commomtable.name,
    commomtable.admiss_times,
    commomtable.inpatient_no,
    emr_type.type_name
    FROM
    emr_print_download_info
    INNER JOIN commomtable ON emr_print_download_info.patient_id = commomtable.patient_id
    <if test="record.name != null and record.name != ''">
      AND commomtable.name LIKE '%${record.name}%'
    </if>
    <if test="record.inpatientNo != null and record.inpatientNo != ''">
      AND commomtable.inpatient_no LIKE '%${record.inpatientNo}%'
    </if>
    LEFT JOIN emr_type ON emr_print_download_info.type_id = emr_type.id
    WHERE
    emr_print_download_info.oper_type = #{record.operType}
    <if test="record.assortId != null and record.assortId != ''">
      AND emr_print_download_info.scan_page like '%${record.assortId}%'
    </if>
    <if test="record.typeName != null and record.typeName != ''">
      AND emr_type.type_name like '%${record.typeName}%'
    </if>
    <if test="record.creater != null and record.creater != ''">
      AND emr_print_download_info.creater LIKE '%${record.creater}%'
    </if>
    <if test="record.startTime != '' and record.startTime != null">
      AND emr_print_download_info.create_time &gt;=  '${record.startTime} 00:00:00'
      <if test="record.endTime != '' and record.endTime != null">
        AND emr_print_download_info.create_time  &lt;= '${record.endTime} 23:59:59'
      </if>
    </if>
    order by emr_print_download_info.create_time desc
  </select>
  <!--按id集合查-->
  <select id="selectAllByIds" resultMap="BaseResultMapVo">
    SELECT
    emr_print_download_info.id,
    emr_print_download_info.create_time,
    emr_print_download_info.patient_id,
    emr_print_download_info.scan_page,
    emr_print_download_info.creater,
    emr_print_download_info.ip,
    commomtable.name,
    commomtable.inpatient_no,
    commomtable.admiss_times,
    emr_type.type_name
    FROM
    emr_print_download_info
    INNER JOIN commomtable ON emr_print_download_info.patient_id = commomtable.patient_id
    LEFT JOIN emr_type ON emr_print_download_info.type_id = emr_type.id
    where emr_print_download_info.id in (${ids})
  </select>

  <!--查询最近打印次数-->
  <select id="getPrintCount" resultMap="BaseResultMapVo">
    SELECT
        top (
            SELECT
                times
            FROM
                emr_comom_set
            WHERE
                id = 1
        ) COUNT(emr_print_download_info.id) printCount,
        ISNULL(emr_type.type_name,'无') typeName,
        CONVERT(varchar(100),emr_print_download_info.create_time,23) create_time,
        emr_print_download_info.creater
    FROM
        emr_print_download_info
    LEFT JOIN emr_type ON emr_print_download_info.type_id = emr_type.id
    WHERE
        oper_type = 1
    AND patient_id = #{patientId}
    GROUP BY
        emr_print_download_info.creater,
        emr_print_download_info.create_time,
        emr_type.type_name
    ORDER BY
        emr_print_download_info.create_time DESC
  </select>

  <!--批量插入-->
  <insert id="SimpleInsert" parameterType="java.util.List">
    insert into emr_print_download_info
    (patient_id, scan_page,
    type_id, oper_type, create_time,
    creater, ip)
    values
    <foreach collection="list" item="record" separator=",">
      (#{record.patientId,jdbcType=NVARCHAR}, #{record.scanPage,jdbcType=VARCHAR},
      #{record.typeId,jdbcType=INTEGER}, #{record.operType,jdbcType=SMALLINT}, #{record.createTime,jdbcType=NVARCHAR},
      #{record.creater,jdbcType=NVARCHAR}, #{record.ip,jdbcType=NVARCHAR})
    </foreach>
  </insert>
    <insert id="insertBrowseRecords" parameterType="com.emr.vo.browseRecords">
      insert into browseRecords
      <trim prefix="(" suffix=")" suffixOverrides="," >
        <if test="patientId != null" >
          patient_id,
        </if>
        <if test="patientName != null" >
          patient_name,
        </if>
        <if test="admissTimes != null" >
          admiss_times,
        </if>
        <if test="disDate != null" >
          dis_date,
        </if>
        <if test="disDept != null" >
          dis_dept,
        </if>
        <if test="attending != null" >
          attending,
        </if>
        <if test="browseName != null" >
          browse_name,
        </if>
        <if test="browseTime != null" >
        browse_time,
      </if>
        <if test="admissId != null" >
          admiss_id,
        </if>
        <if test="ip != null" >
          ip,
        </if>
      </trim>
      <trim prefix="values (" suffix=")" suffixOverrides="," >
        <if test="patientId != null" >
          #{patientId},
        </if>
        <if test="patientName != null" >
          #{patientName},
        </if>
        <if test="admissTimes != null" >
          #{admissTimes},
        </if>
        <if test="disDate != null" >
          #{disDate},
        </if>
        <if test="disDept != null" >
          #{disDept},
        </if>
        <if test="attending != null" >
          #{attending},
        </if>
        <if test="browseName != null" >
          #{browseName},
        </if>
        <if test="browseTime != null" >
          #{browseTime},
        </if>
        <if test="admissId != null" >
          #{admissId},
        </if>
        <if test="ip != null" >
          #{ip},
        </if>
      </trim>
    </insert>
</mapper>