<?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_Master_FollowingMapper">
  <resultMap id="BaseResultMap" type="com.emr.entity.Archive_Master_Following">
    <id column="id" jdbcType="VARCHAR" property="id" />
    <result column="master_id" jdbcType="VARCHAR" property="masterId" />
    <result column="following_type" jdbcType="VARCHAR" property="followingType" />
    <result column="following_content" jdbcType="NVARCHAR" property="followingContent" />
    <result column="handle_time" jdbcType="TIMESTAMP" property="handleTime" />
    <result column="handle_id" jdbcType="VARCHAR" property="handleId" />
    <result column="handle_name" jdbcType="NVARCHAR" property="handleName" />
    <result column="remark" jdbcType="NVARCHAR" property="remark" />
  </resultMap>
  <sql id="Base_Column_List">
    id, master_id, following_type, following_content, handle_time, handle_id, handle_name, 
    remark
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from Archive_Master_Following
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
    delete from Archive_Master_Following
    where id = #{id,jdbcType=VARCHAR}
  </delete>
  <insert id="insert" parameterType="com.emr.entity.Archive_Master_Following">
    insert into Archive_Master_Following (id, master_id, following_type, 
      following_content, handle_time, handle_id, 
      handle_name, remark)
    values ((select replace(newid(),'-','')), #{masterId,jdbcType=VARCHAR}, #{followingType,jdbcType=VARCHAR},
      #{followingContent,jdbcType=NVARCHAR}, #{handleTime,jdbcType=TIMESTAMP}, #{handleId,jdbcType=VARCHAR}, 
      #{handleName,jdbcType=NVARCHAR}, #{remark,jdbcType=NVARCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.emr.entity.Archive_Master_Following">
    insert into Archive_Master_Following
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <!--<if test="id != null">-->
        id,
      <!--</if>-->
      <if test="masterId != null">
        master_id,
      </if>
      <if test="followingType != null">
        following_type,
      </if>
      <if test="followingContent != null">
        following_content,
      </if>
      <if test="handleTime != null">
        handle_time,
      </if>
      <if test="handleId != null">
        handle_id,
      </if>
      <if test="handleName != null">
        handle_name,
      </if>
      <if test="remark != null">
        remark,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <!--<if test="id != null">-->
        (select replace(newid(),'-','')),
      <!--</if>-->
      <if test="masterId != null">
        #{masterId,jdbcType=VARCHAR},
      </if>
      <if test="followingType != null">
        #{followingType,jdbcType=VARCHAR},
      </if>
      <if test="followingContent != null">
        #{followingContent,jdbcType=NVARCHAR},
      </if>
      <if test="handleTime != null">
        #{handleTime,jdbcType=TIMESTAMP},
      </if>
      <if test="handleId != null">
        #{handleId,jdbcType=VARCHAR},
      </if>
      <if test="handleName != null">
        #{handleName,jdbcType=NVARCHAR},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=NVARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.emr.entity.Archive_Master_Following">
    update Archive_Master_Following
    <set>
      <if test="masterId != null">
        master_id = #{masterId,jdbcType=VARCHAR},
      </if>
      <if test="followingType != null">
        following_type = #{followingType,jdbcType=VARCHAR},
      </if>
      <if test="followingContent != null">
        following_content = #{followingContent,jdbcType=NVARCHAR},
      </if>
      <if test="handleTime != null">
        handle_time = #{handleTime,jdbcType=TIMESTAMP},
      </if>
      <if test="handleId != null">
        handle_id = #{handleId,jdbcType=VARCHAR},
      </if>
      <if test="handleName != null">
        handle_name = #{handleName,jdbcType=NVARCHAR},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=NVARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.emr.entity.Archive_Master_Following">
    update Archive_Master_Following
    set master_id = #{masterId,jdbcType=VARCHAR},
      following_type = #{followingType,jdbcType=VARCHAR},
      following_content = #{followingContent,jdbcType=NVARCHAR},
      handle_time = #{handleTime,jdbcType=TIMESTAMP},
      handle_id = #{handleId,jdbcType=VARCHAR},
      handle_name = #{handleName,jdbcType=NVARCHAR},
      remark = #{remark,jdbcType=NVARCHAR}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  <select id="selectByMasterId" parameterType="hashmap" resultMap="BaseResultMap">
    select handle_name,handle_time,following_content from Archive_Master_Following
    where 1=1
    <if test="masterId != null and masterId != ''">
      and master_id = #{masterId,jdbcType=NVARCHAR}
    </if>
    <if test="type==1">
      and following_type in (1,2,3,4,5,6,7)
    </if>
  </select>
  <select id="selByCol" parameterType="com.emr.entity.Archive_Master_Following" resultMap="BaseResultMap">
      SELECT
          id,
          master_id,
          CONVERT ( VARCHAR, handle_time, 120 ) handle_time,
          handle_id,
          handle_name,
          following_type,
          COUNT(id) remark
      FROM
          Archive_Master_Following
      <where> 1=1
      <if test="followingType!= null and followingType!= ''">
        and following_type in
        <foreach item="item" collection="followingType.split(',')" open="(" separator="," close=")">
          #{item}
        </foreach>
      </if>
      <if test="masterId != null and masterId != ''">
        and master_id = #{masterId,jdbcType=NVARCHAR}
      </if>
      <if test="handleTime != null and handleTime != ''">
        and CONVERT(VARCHAR(20),handle_time,23) = #{handleTime,jdbcType=NVARCHAR}
      </if>
      <if test="handleId != null and handleId != ''">
        and CONVERT(VARCHAR(20),handle_id,23) = #{handleId,jdbcType=NVARCHAR}
      </if>
      <if test="handleName != null and handleName != ''">
        and CONVERT(VARCHAR(20),handle_name,23) = #{handleName,jdbcType=NVARCHAR}
      </if>
      GROUP BY
          id,
          master_id,
          handle_time,
          handle_id,
          handle_name,
          following_type
      order by handle_time
    </where>
  </select>
  <select id="selLastByType" parameterType="com.emr.entity.Archive_Master_Following" resultMap="BaseResultMap">
     select *  from (
        select ROW_NUMBER() over(partition by master_id order by handle_time desc) RowNum
        ,Archive_Master_Following.*
        from Archive_Master_Following where 1=1

    ) as t1
    <where> 1=1 and RowNum = 1
      <if test="masterId != null and masterId != ''">
        and master_id = #{masterId,jdbcType=NVARCHAR}
      </if>
      <if test="followingType!= null and followingType!= ''">
        and following_type in
        <foreach item="item" collection="followingType.split(',')" open="(" separator="," close=")">
          #{item}
        </foreach>
      </if>
      <if test="handleTime != null and handleTime != ''">
        and CONVERT(VARCHAR(20),handle_time,23) = #{handleTime,jdbcType=NVARCHAR}
      </if>
      <if test="handleId != null and handleId != ''">
        and CONVERT(VARCHAR(20),handle_id,23) = #{handleId,jdbcType=NVARCHAR}
      </if>
      <if test="handleName != null and handleName != ''">
        and CONVERT(VARCHAR(20),handle_name,23) = #{handleName,jdbcType=NVARCHAR}
      </if>
    </where>
  </select>
</mapper>