<?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.Archive_DetailMapper" >
  <resultMap id="BaseResultMap" type="com.emr.entity.Archive_Detail" >
    <result column="id" property="id" jdbcType="NVARCHAR" />
    <result column="PDF_PATH" property="pdfPath" jdbcType="NVARCHAR" />
    <result column="MasterID" property="masterid" jdbcType="NVARCHAR" />
    <result column="UpLoadDateTime" property="uploaddatetime" jdbcType="NVARCHAR" />
    <result column="AssortID" property="assortid" jdbcType="NVARCHAR" />
    <result column="Source" property="source" jdbcType="NVARCHAR" />
    <result column="SubAssort" property="subassort" jdbcType="NVARCHAR" />
    <result column="Title" property="title" jdbcType="NVARCHAR" />
    <result column="flag" property="flag" jdbcType="NVARCHAR" />
    <result column="Sys" property="sys" jdbcType="NVARCHAR" />
    <result column="assort_name" property="assortName" jdbcType="NVARCHAR" />

  </resultMap>
  <insert id="insert" parameterType="com.emr.entity.Archive_Detail" >
    insert into archive_detail (id, PDF_PATH, MasterID, 
      UpLoadDateTime, AssortID, Source, 
      SubAssort, Title, flag, 
      Sys)
    values (#{id,jdbcType=NVARCHAR}, #{pdfPath,jdbcType=NVARCHAR}, #{masterid,jdbcType=NVARCHAR}, 
      #{uploaddatetime,jdbcType=NVARCHAR}, #{assortid,jdbcType=NVARCHAR}, #{source,jdbcType=NVARCHAR}, 
      #{subassort,jdbcType=NVARCHAR}, #{title,jdbcType=NVARCHAR}, #{flag,jdbcType=NVARCHAR}, 
      #{sys,jdbcType=NVARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.emr.entity.Archive_Detail" >
    insert into archive_detail
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="pdfPath != null" >
        PDF_PATH,
      </if>
      <if test="masterid != null" >
        MasterID,
      </if>
      <if test="uploaddatetime != null" >
        UpLoadDateTime,
      </if>
      <if test="assortid != null" >
        AssortID,
      </if>
      <if test="source != null" >
        Source,
      </if>
      <if test="subassort != null" >
        SubAssort,
      </if>
      <if test="title != null" >
        Title,
      </if>
      <if test="flag != null" >
        flag,
      </if>
      <if test="sys != null" >
        Sys,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=NVARCHAR},
      </if>
      <if test="pdfPath != null" >
        #{pdfPath,jdbcType=NVARCHAR},
      </if>
      <if test="masterid != null" >
        #{masterid,jdbcType=NVARCHAR},
      </if>
      <if test="uploaddatetime != null" >
        #{uploaddatetime,jdbcType=NVARCHAR},
      </if>
      <if test="assortid != null" >
        #{assortid,jdbcType=NVARCHAR},
      </if>
      <if test="source != null" >
        #{source,jdbcType=NVARCHAR},
      </if>
      <if test="subassort != null" >
        #{subassort,jdbcType=NVARCHAR},
      </if>
      <if test="title != null" >
        #{title,jdbcType=NVARCHAR},
      </if>
      <if test="flag != null" >
        #{flag,jdbcType=NVARCHAR},
      </if>
      <if test="sys != null" >
        #{sys,jdbcType=NVARCHAR},
      </if>
    </trim>
  </insert>
  <select id="selectPdfPathByPatient" resultMap="BaseResultMap">
    SELECT
        PDF_PATH
    FROM
        archive_detail
    LEFT JOIN zd_assort
    ON archive_detail.AssortID = zd_assort.assort_id
    WHERE
        MasterID = ${patientId} and flag != 1
    AND
        AssortID in (${assortIds})
    ORDER BY zd_assort.assort_sort,UpLoadDateTime
  </select>

  <!--查询pdf路径逗号拼接-->
  <select id="selectPdfCountByMasterId" resultMap="BaseResultMap">
     select AssortID,
        stuff((select ','+PDF_PATH from  archive_detail
            where c.AssortID=AssortID and archive_detail.MasterID = #{patientId} and flag = 0
            for xml path('')),1,1,'') as pdfPath
     from archive_detail c where c.MasterID = #{patientId}
     group by c.AssortID
  </select>
  <select id="selectPdfPathByPatient2" resultType="com.emr.vo.commomSearch.CommomTree"
          parameterType="java.lang.String">
    SELECT
    archive_detail.PDF_PATH,
    archive_detail.MasterID as patient_id,
    archive_detail.AssortID as assort_id,
    zd_assort.assort_name,
    archive_detail.PDF_PATH as scan_page,
    archive_detail.Source as source,
    archive_detail.PageCount,
    archive_detail.Title as title
    FROM
        archive_detail
    LEFT JOIN zd_assort
    ON archive_detail.AssortID = zd_assort.assort_id
    WHERE
        MasterID = #{patientId} and flag != 1
    ORDER BY zd_assort.assort_sort,UpLoadDateTime
  </select>
  <select id="selectPdfPathByPatient3" resultType="com.emr.vo.commomSearch.CommomTree">
     SELECT
    archive_detail.PDF_PATH
    FROM
        archive_detail
    LEFT JOIN zd_assort
    ON archive_detail.AssortID = zd_assort.assort_id
    WHERE
        MasterID = #{patientIds}
    ORDER BY zd_assort.assort_sort,UpLoadDateTime
  </select>
  <select id="selectPDFRATH" resultType="java.lang.String">
    SELECT
      archive_detail.PDF_PATH
    FROM
      archive_detail
        LEFT JOIN zd_assort
                  ON archive_detail.AssortID = zd_assort.assort_id
    WHERE
      MasterID = #{patientId}
    ORDER BY zd_assort.assort_sort,UpLoadDateTime
  </select>




  <select id="selectPDFRATH2" resultType="java.lang.String">
    SELECT
      archive_detail.PDF_PATH
    FROM
      archive_detail
        LEFT JOIN zd_assort
                  ON archive_detail.AssortID = zd_assort.assort_id
    WHERE
      MasterID = #{masterId} and  zd_assort.assort_id=#{assortId}
    ORDER BY zd_assort.assort_sort,UpLoadDateTime
  </select>




  <select id="getPDFRATH" resultType="java.lang.String">
    SELECT
      archive_detail.PDF_PATH
    FROM
      archive_detail
        LEFT JOIN zd_assort
                  ON archive_detail.AssortID = zd_assort.assort_id
    WHERE
      MasterID = #{patientId}
      and PDF_PATH in
        <foreach item="item" collection="scanPages.split(',')" open="(" separator="," close=")">
          #{item}
        </foreach>
    ORDER BY zd_assort.assort_sort,UpLoadDateTime
  </select>

</mapper>