<?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.tScanAssort.T_Scan_AssortMapper" >
  <resultMap id="BaseResultMap" type="com.emr.entity.tScanAssort.T_Scan_Assort" >
    <result column="patient_id" property="patientId" jdbcType="VARCHAR" />
    <result column="assort_id" property="assortId" jdbcType="VARCHAR" />
    <result column="scan_page" property="scanPage" jdbcType="VARCHAR" />
  </resultMap>
  <resultMap id="BaseResultMap1" type="com.emr.vo.FontVo.ScanAssortVo" >
    <result column="patient_id" property="patientId" jdbcType="VARCHAR" />
    <result column="inpatient_no" property="inpatientNo" jdbcType="VARCHAR" />
    <result column="admiss_id" property="admissId" jdbcType="VARCHAR" />
    <result column="admiss_times" property="admissTimes" jdbcType="SMALLINT" />
    <result column="dis_date" property="disDate" jdbcType="VARCHAR" />
    <result column="id_card" property="idCard" jdbcType="VARCHAR" />
    <result column="assort_id" property="assortId" jdbcType="VARCHAR" />
    <result column="assort_name" property="assortName" jdbcType="VARCHAR" />
    <result column="assort_sort" property="assortSort" jdbcType="SMALLINT" />
    <result column="print_flag" property="printFlag" jdbcType="VARCHAR" />
    <result column="parent_id" property="parentId" jdbcType="SMALLINT" />
    <result column="page_count" property="pageCount" jdbcType="SMALLINT" />
    <result column="file_path" property="filePath" jdbcType="VARCHAR" />
    <result column="scan_page" property="scanPage" jdbcType="VARCHAR" />
  </resultMap>



    <resultMap id="ZhFyImagetMap" type="com.emr.vo.ZhFyImageVo" >
        <result column="inpatient_No" property="inpatientNo" jdbcType="VARCHAR" />
        <result column="admiss_times" property="admissTimes" jdbcType="VARCHAR" />
        <result column="patient_id" property="patientId" jdbcType="VARCHAR" />
        <result column="dis_date" property="disDate" jdbcType="VARCHAR" />
        <collection property="picList" resultMap="PicListMap"/>
    </resultMap>

    <resultMap id="PicListMap" type="com.emr.vo.PicListVo">
        <result column="picUrl" property="picUrl"/>
        <result column="assort_id" property="assortId"/>
        <result column="assort_name" property="assortName"/>
        <result column="scan_page" property="scanPage"/>
    </resultMap>

    <resultMap id="PatientAssortMap" type="com.emr.vo.PatientAssortVo" >
        <result column="patientId" property="patientId" />
        <result column="visitId" property="visitId" />
        <result column="admId" property="admId" />
        <result column="scanServer" property="scanServer" />
        <result column="inDate" property="inDate" />
        <result column="outDate" property="outDate" />
        <collection property="indexFds" resultMap="IndexFdsMap"/>
    </resultMap>

    <resultMap id="IndexFdsMap" type="com.emr.vo.IndexFds">
        <result column="id" property="id"/>
        <result column="indexId" property="indexId"/>
        <result column="patientId" property="patientId"/>
        <result column="visitId" property="visitId"/>
        <result column="fdCode" property="fdCode"/>
        <result column="scanFile" property="scanFile"/>
        <result column="admId" property="admId"/>
    </resultMap>
  <insert id="insert" parameterType="com.emr.entity.tScanAssort.T_Scan_Assort" >
    insert into t_scan_assort (patient_id, assort_id, scan_page
      )
    values (#{patientId,jdbcType=VARCHAR}, #{assortId,jdbcType=VARCHAR}, #{scanPage,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.emr.entity.tScanAssort.T_Scan_Assort" >
    insert into t_scan_assort
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="patientId != null" >
        patient_id,
      </if>
      <if test="assortId != null" >
        assort_id,
      </if>
      <if test="scanPage != null" >
        scan_page,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="patientId != null" >
        #{patientId,jdbcType=VARCHAR},
      </if>
      <if test="assortId != null" >
        #{assortId,jdbcType=VARCHAR},
      </if>
      <if test="scanPage != null" >
        #{scanPage,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective">
    UPDATE t_scan_assort
    <set>
      <if test="newAssort.patientId != null" >
        patient_id = #{newAssort.patientId,jdbcType=VARCHAR},
      </if>
      <if test="newAssort.assortId != null" >
        assort_id = #{newAssort.assortId,jdbcType=VARCHAR},
      </if>
      <if test="newAssort.scanPage != null" >
        scan_page = #{newAssort.scanPage,jdbcType=VARCHAR},
      </if>
    </set>
    WHERE
        patient_id = #{oldAssort.patientId,jdbcType=VARCHAR}
    AND
        assort_id = #{oldAssort.assortId,jdbcType=VARCHAR}
    AND
        scan_page = #{oldAssort.scanPage,jdbcType=VARCHAR}
  </update>

  <select id="seleteScanAssortPath" resultMap="BaseResultMap1">
    SELECT
        dbo.commomtable.patient_id,
        dbo.commomtable.inpatient_no,
        dbo.commomtable.admiss_id,
        dbo.commomtable.admiss_times,
      (CASE WHEN dbo.commomtable.dis_date is not null and dbo.commomtable.dis_date != '' THEN CONVERT(varchar(100),dbo.commomtable.dis_date, 23)  ELSE null end) dis_date,
        dbo.commomtable1.id_card,
        t_scan_assort.assort_id,
        dbo.zd_assort.assort_name,
        dbo.zd_assort.assort_sort,
        dbo.zd_assort.print_flag,
        0 as parent_id,
          null as page_count,
          dbo.t_gdh_index.file_path file_path,
          t_scan_assort.scan_page
          FROM
          dbo.commomtable
          INNER JOIN
          dbo.commomtable1 ON dbo.commomtable.patient_id = dbo.commomtable1.patient_id
          INNER JOIN
          (select patient_id,assort_id,stuff((select ','+scan_page from  t_scan_assort where t_scan_assort1.patient_id=patient_id and t_scan_assort1.assort_id = assort_id for xml path('')),1,1,'') scan_page from t_scan_assort t_scan_assort1  group by patient_id,t_scan_assort1.assort_id) as t_scan_assort ON dbo.commomtable.patient_id = t_scan_assort.patient_id
          INNER JOIN
          dbo.zd_assort ON t_scan_assort.assort_id = dbo.zd_assort.assort_id
    INNER JOIN
        dbo.t_gdh_index ON dbo.commomtable.patient_id = dbo.t_gdh_index.patient_id
    <where>
        <if test="patientId != null and patientId != ''">
            AND dbo.commomtable.patient_id = #{patientId}
        </if>
        <if test="inpatientNo != null and inpatientNo != ''">
            AND dbo.commomtable.inpatient_no = #{inpatientNo}
        </if>
        <if test="admissId != null and admissId != ''">
            AND dbo.commomtable.admiss_id = #{admissId}
        </if>
        <if test="admissTimes != null and admissTimes != ''">
            AND dbo.commomtable.admiss_times = ${admissTimes}
        </if>
        <if test="disDate != null and disDate != ''">
            AND dbo.commomtable.dis_date BETWEEN #{disDate} AND #{disDate}
        </if>
        <if test="idCard != null and idCard != ''">
            AND dbo.commomtable1.id_card = #{idCard}
        </if>
    </where>
    ORDER BY
        assort_sort
  </select>
    <update id="SimpleUpdate">
        UPDATE t_scan_assort
        SET
          assort_id = #{assortId}
        WHERE
          patient_id = #{patientId}
        AND
          scan_page IN
          <foreach collection="list" item="item" open="(" separator="," close=")">
              #{item}
          </foreach>
    </update>
    <insert id="SimpleInsert" parameterType="java.util.List">
        insert into t_scan_assort(
          patient_id, assort_id, scan_page,source,is_del,up_date
        )
        <foreach collection="list" item="items" separator="union">
            select
            #{items.patientId,jdbcType=VARCHAR}, #{items.assortId,jdbcType=VARCHAR}, #{items.scanPage,jdbcType=VARCHAR}, #{items.source,jdbcType=SMALLINT},#{items.isDel,jdbcType=SMALLINT},#{items.upDate,jdbcType=VARCHAR}
        </foreach>
    </insert>
    <!--根据patiengId和图片集合批量更新-->
    <update id="SimpleUpdateByPatientIdAndScanPages">
        update t_scan_assort set assort_id = #{newAssortId}
        where patient_id = #{patientId} and scan_page in (${scanPages})
    </update>
    <!--根据patiengId和图片集合批量删除-->
    <update id="SimpleDelByPatientIdAndScanPages">
        update t_scan_assort set is_del = 1 where patient_id = #{patientId} and scan_page in
        (
        <foreach collection="scanPages.split(',')" item="item" separator=",">
            #{item}
        </foreach>
        )
    </update>
    <!--查询图片地址集合-->
    <select id="selectPicPaths" resultMap="BaseResultMap1">
        SELECT
            CASE
                WHEN
                    source = 1 THEN
                        #{flag} + '/' + scan_page ELSE new_path + '/' + scan_page
                    END filePath
            FROM
                t_scan_assort
                INNER JOIN commomtable ON t_scan_assort.patient_id = commomtable.patient_id
                INNER JOIN zd_assort ON t_scan_assort.assort_id = zd_assort.assort_id
            WHERE
                t_scan_assort.patient_id = #{patientId}
                AND scan_page IN (${scanPages})
                AND t_scan_assort.is_del = 0
            ORDER BY
                zd_assort.assort_sort
    </select>
    <!--根据patientId查询已删除图片集合-->
    <select id="selectDeledPicList" resultMap="BaseResultMap1">
        SELECT
            t_scan_assort.scan_page,
            zd_assort.assort_name,
            case
                when  t_scan_assort.source = 1
                then commomtable.${flag}
                when  t_scan_assort.source = 2
                then commomtable.new_path
            end rootPath
        FROM
            commomtable
            INNER JOIN t_scan_assort ON commomtable.patient_id = t_scan_assort.patient_id
            AND t_scan_assort.is_del = 1
            INNER JOIN zd_assort ON t_scan_assort.assort_id = zd_assort.assort_id
            AND zd_assort.print_flag = 0
        WHERE
            commomtable.patient_id = #{patientId}
        ORDER BY
            zd_assort.assort_sort
    </select>
    <select id="getPathListByInpatientNo" resultMap="ZhFyImagetMap">
        SELECT
            rtrim(c.inpatient_no) AS inpatient_no,c.admiss_times,c.dis_date,c.file_path+'\'+t.scan_page as picUrl,t.assort_id,z.assort_name,t.scan_page,c.patient_id
        FROM
            commomtable c
                LEFT JOIN  t_scan_assort t on c.patient_id=t.patient_id
                LEFT JOIN   zd_assort z on z.assort_id=t.assort_id
        WHERE
            inpatient_no = #{inpatientNo}
          AND admiss_times =#{admissTimes}
        ORDER BY z.assort_sort
    </select>
    <select id="getPathListByDisDate" resultMap="ZhFyImagetMap">
        SELECT
            rtrim(c.inpatient_no) AS inpatient_no,c.admiss_times,c.dis_date,c.file_path+'\'+t.scan_page as picUrl,t.assort_id,z.assort_name,t.scan_page,c.patient_id
        FROM
            commomtable c
                LEFT JOIN  t_scan_assort t on c.patient_id=t.patient_id
                LEFT JOIN   zd_assort z on z.assort_id=t.assort_id
        <where> 1=1
            <if test="startTime != null and startTime != ''">
                AND dis_date > #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                AND dis_date &lt; #{endTime}
            </if>
        </where>
        ORDER BY z.assort_sort
    </select>
    <select id="getPatAssortListByPatientId" resultMap="PatientAssortMap">
        SELECT
            rtrim(c.inpatient_no) AS patientId,
            c.admiss_times as visitId,
            '' as admId,
            '' as scanServer,
            CONVERT(varchar, c.admiss_date, 127)+'.000+0000' as inDate,
            CONVERT(varchar, c.dis_date, 127)+'.000+0000' as outDate,
            c.file_path+'\'+t.scan_page as scanFile,
            t.assort_id as fdCode,
            c.patient_id as indexId
        FROM
            commomtable c
                LEFT JOIN  t_scan_assort t on c.patient_id=t.patient_id
        WHERE
            c.inpatient_no = #{patientId}
          and  t.is_del = 0
        ORDER BY c.admiss_times,t.assort_id
    </select>


    <!--修改图片集合为未删除-->
    <update id="recoverPic">
        update t_scan_assort set is_del = 0 where patient_id = #{patientId}
        and scan_page in (${scanPages})
    </update>
    <!--根据patientId删除图片集合-->
    <delete id="deletePicByPatientId">
        delete from t_scan_assort where patient_id = #{patientId}
    </delete>
</mapper>