<?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.emrReasonAnnoModel.Emr_Reason_Anno_ModelMapper">
  <resultMap id="BaseResultMap" type="com.emr.entity.emrReasonAnnoModel.Emr_Reason_Anno_Model">
    <result column="id" jdbcType="INTEGER" property="id" />
    <result column="type_flag" jdbcType="NVARCHAR" property="typeFlag" />
    <result column="type_name" jdbcType="NVARCHAR" property="typeName" />
    <result column="content" jdbcType="NVARCHAR" property="content" />
    <result column="effective" jdbcType="INTEGER" property="effective" />
    <result column="parent_id" jdbcType="INTEGER" property="parentId" />
    <result column="creater" jdbcType="NVARCHAR" property="creater" />
    <result column="create_time" jdbcType="NCHAR" property="createTime" />
    <result column="updater" jdbcType="NVARCHAR" property="updater" />
    <result column="update_time" jdbcType="NCHAR" property="updateTime" />
  </resultMap>
  <sql id="Base_Column_List">
    id, type_flag, type_name, content, effective, parent_id, creater, create_time, updater,
    update_time
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from emr_reason_anno_model
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from emr_reason_anno_model
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.emr.entity.emrReasonAnnoModel.Emr_Reason_Anno_Model" useGeneratedKeys="true" keyProperty="id">
    insert into emr_reason_anno_model (type_flag, type_name,
      content, effective, parent_id, 
      creater, create_time, updater, 
      update_time)
    values (#{typeFlag,jdbcType=NVARCHAR}, #{typeName,jdbcType=NVARCHAR},
      #{content,jdbcType=NVARCHAR}, #{effective,jdbcType=INTEGER}, #{parentId,jdbcType=INTEGER}, 
      #{creater,jdbcType=NVARCHAR}, #{createTime,jdbcType=NCHAR}, #{updater,jdbcType=NVARCHAR}, 
      #{updateTime,jdbcType=NCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.emr.entity.emrReasonAnnoModel.Emr_Reason_Anno_Model">
    insert into emr_reason_anno_model
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="typeFlag != null">
        type_flag,
      </if>
      <if test="typeName != null">
        type_name,
      </if>
      <if test="content != null">
        content,
      </if>
      <if test="effective != null">
        effective,
      </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="typeFlag != null">
        #{typeFlag,jdbcType=NVARCHAR},
      </if>
      <if test="typeName != null">
        #{typeName,jdbcType=NVARCHAR},
      </if>
      <if test="content != null">
        #{content,jdbcType=NVARCHAR},
      </if>
      <if test="effective != null">
        #{effective,jdbcType=INTEGER},
      </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.emrReasonAnnoModel.Emr_Reason_Anno_Model">
    update emr_reason_anno_model
    <set>
      <if test="typeFlag != null">
        type_flag = #{typeFlag,jdbcType=NVARCHAR},
      </if>
      <if test="typeName != null">
        type_name = #{typeName,jdbcType=NVARCHAR},
      </if>
      <if test="content != null">
        content = #{content,jdbcType=NVARCHAR},
      </if>
      <if test="effective != null">
        effective = #{effective,jdbcType=INTEGER},
      </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.emrReasonAnnoModel.Emr_Reason_Anno_Model">
    update emr_reason_anno_model
    set type_flag = #{typeFlag,jdbcType=NVARCHAR},
      type_name = #{typeName,jdbcType=NVARCHAR},
      content = #{content,jdbcType=NVARCHAR},
      effective = #{effective,jdbcType=INTEGER},
      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>
  <!--根据用户名称查询类别-->
  <select id="selectAllAnnoTypeByCreater" resultMap="BaseResultMap">
      SELECT a.*,emr_reason_anno_model.id isOper from (SELECT
      *,
      type_name newName
      FROM
      emr_reason_anno_model
      <where>content IS NULL
          AND
          parent_id != 0
          <if test="creaters != null and creaters != ''">
            AND creater IN (${creaters})
        </if>
        <if test="isEffective != null">
            AND effective = 1
        </if>
    </where>
    UNION ALL
      SELECT
      *,
      type_name newName
      FROM
      emr_reason_anno_model
      <where>
          content IS NULL
          AND parent_id = 0
          <if test="isEffective != null">
              AND effective = 1
          </if>
      </where>) a left join
      emr_reason_anno_model  on a.id = emr_reason_anno_model.id
      <if test="creaters != null and creaters != ''">
          AND emr_reason_anno_model.creater IN (${creaters})
      </if>
  </select>

  <!--根据用户名称查询未添加内容的分类-->
  <select id="selectNotContentAllAnnoTypeByCreater" resultMap="BaseResultMap">
    SELECT
        id,
        type_name
    FROM
        emr_reason_anno_model
    WHERE
        effective = 1
    AND content IS NULL
    <if test="creaters != '' and creaters != null">
        AND creater IN (${creaters})
    </if>
    AND parent_id != 0
    AND id NOT IN (
        SELECT
            parent_id
        FROM
            emr_reason_anno_model
        WHERE
            content IS NOT NULL
        AND parent_id is not null
    )
  </select>

  <!--根据用户名查询模板集合-->
  <select id="selectAnnoTemplateListByCreater" resultMap="BaseResultMap">
    SELECT
        a.id,
        a.content,
        b.type_flag,
        b.type_name,
        a.type_name newName,
        a.creater,
        a.parent_id,
        a.create_time,
        a.effective,
        a.updater,
        a.update_time
    FROM
        emr_reason_anno_model a
    LEFT JOIN
        emr_reason_anno_model b
    ON
        a.parent_id = b.id
    WHERE
        a.type_flag ${typeFlag}
    <if test="creaters != null">
      AND b.creater IN (${creaters})
    </if>
    <if test="record.creater != '' and record.creater != null">
      AND a.creater LIKE '%${record.creater}%'
    </if>
    <if test="record.typeName != '' and record.typeName != null">
      AND b.type_name LIKE '%${record.typeName}%'
    </if>
    <if test="record.typeFlag != '' and record.typeFlag != null">
      AND b.type_flag LIKE '%${record.typeFlag}%'
    </if>
    <if test="record.content != '' and record.content != null">
      AND a.content LIKE '%${record.content}%'
    </if>
    <if test="record.effective != null">
        AND a.effective = ${record.effective}
    </if>
    ORDER BY a.create_time DESC
  </select>

  <!--根据用户名称和类别名称查验类别名称是否存在-->
  <select id="checkAnoTypeNameByCreaterAndTypeName" resultType="com.emr.entity.emrReasonAnnoModel.Emr_Reason_Anno_Model">
      SELECT
          id
      FROM
          emr_reason_anno_model
      <where>
          <if test="creater != null and creater != ''">
              creater = #{creater}
          </if>
          <if test="typeFlag != null and typeFlag != ''">
              AND type_flag = #{typeFlag}
          </if>
          <if test="typeName != null and typeName != ''">
              AND type_name = #{typeName} AND parent_id = #{parentId}
          </if>
      </where>
  </select>

    <!--根据标志分类原因模板集合和标注模板集合-->
  <select id="selectReasonOrAnnoTemplateList" resultMap="BaseResultMap">
      SELECT
            id,
            type_name
        FROM
            emr_reason_anno_model
        WHERE
            effective = 1
        AND parent_id = (
            SELECT
                id
            FROM
                emr_reason_anno_model
            WHERE
                type_flag = #{typeFlag}
        )
        <if test="creaters != null and creaters != ''">
            AND creater IN (${creaters})
        </if>
  </select>
  <!--根据模板id查询内容-->
  <select id="selectContentByParentId" resultType="String">
      SELECT
          content
      FROM
          emr_reason_anno_model
      WHERE
          parent_id = ${parentId}
      AND effective = 1
  </select>
  <!--根据模板id查询内容是否存在-->
  <select id="checkContentByParentId" resultType="com.emr.entity.emrReasonAnnoModel.Emr_Reason_Anno_Model">
      SELECT
          a.id,
          a.content
      FROM
          emr_reason_anno_model a
      LEFT JOIN
          emr_reason_anno_model b
      ON
          a.parent_id = b.id
      WHERE
          a.type_flag is null
          <if test="creater != '' and creater != null">
              AND
              a.creater = #{creater}
          </if>
      AND
          b.type_flag = (select type_flag from emr_reason_anno_model where id = ${record.parentId})
          <if test="record.content != '' and record.content != null">
              AND
                  a.content = #{record.content}
          </if>
  </select>
  <!--删除父集时，父id置空-->
  <update id="updateByDelParentId" parameterType="java.lang.Integer">
    update emr_reason_anno_model
    set parent_id = NULL
    where parent_id = #{parentId,jdbcType=INTEGER}
  </update>
  <!--根据id查询带类别名称的对象-->
  <select id="getAnnoTemplateById" resultType="com.emr.entity.emrReasonAnnoModel.Emr_Reason_Anno_Model" parameterType="java.lang.Integer">
     SELECT
        a.*,
        b.type_name newName
      FROM
          (SELECT * FROM emr_reason_anno_model WHERE id = ${id}) a
      LEFT JOIN
          emr_reason_anno_model b
      ON a.parent_id = b.id
  </select>
    <!--根据用户名查询模板集合-->
    <select id="selectAllByIds" resultMap="BaseResultMap">
        SELECT
        a.id,
        a.content,
        b.type_flag,
        b.type_name,
        a.type_name newName,
        a.creater,
        a.parent_id,
        a.create_time,
        a.effective,
        a.updater,
        a.update_time
        FROM
        emr_reason_anno_model a
        LEFT JOIN
        emr_reason_anno_model b
        ON
        a.parent_id = b.id
        WHERE
        a.id IN (${checks})
    </select>
</mapper>