<?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.Emr_DictionaryMapper" >
  <resultMap id="BaseResultMap" type="com.emr.entity.Emr_Dictionary" >
    <result column="id" property="id" jdbcType="INTEGER" />
    <result column="typeCode" property="typecode" jdbcType="NVARCHAR" />
    <result column="typeName" property="typename" jdbcType="NVARCHAR" />
    <result column="code" property="code" jdbcType="NVARCHAR" />
    <result column="Name" property="name" jdbcType="NVARCHAR" />
    <result column="py_code" property="pyCode" jdbcType="NVARCHAR" />
    <result column="wb_code" property="wbCode" jdbcType="NVARCHAR" />
    <result column="zip_code" property="zipCode" jdbcType="NCHAR" />
    <result column="gb_code" property="gbCode" jdbcType="NCHAR" />
    <result column="flag" property="flag" jdbcType="SMALLINT" />
    <result column="c_comment" property="cComment" jdbcType="NVARCHAR" />
    <result column="parent_id" property="parentId" jdbcType="NVARCHAR" />
    <result column="effective" property="effective" jdbcType="INTEGER" />
    <result column="updater" property="updater" jdbcType="NVARCHAR" />
    <result column="update_time" property="updateTime" jdbcType="NCHAR" />
    <result column="creater" property="creater" jdbcType="NVARCHAR" />
    <result column="create_time" property="createTime" jdbcType="NCHAR" />
    <result column="remark" property="remark" jdbcType="NVARCHAR" />
  </resultMap>
    <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
        delete from emr_dictionary
        where id = #{id,jdbcType=INTEGER}
  </delete>
    <insert id="insert" parameterType="com.emr.entity.Emr_Dictionary" useGeneratedKeys="true" keyProperty="id">
    insert into emr_dictionary (
        typeCode,
        typeName,
        code,
        Name,
        py_code,
        wb_code,
        zip_code,
        gb_code,
        flag,
        c_comment,
        parent_id,
        effective,
        updater,
        update_time,
        creater,
        create_time,
        remark)
    values (
      #{typecode,jdbcType=NVARCHAR},
      #{typename,jdbcType=NVARCHAR},
      #{code,jdbcType=NVARCHAR},
      #{name,jdbcType=NVARCHAR},
      #{pyCode,jdbcType=NVARCHAR},
      #{wbCode,jdbcType=NVARCHAR},
      #{zipCode,jdbcType=NCHAR},
      #{gbCode,jdbcType=NCHAR},
      #{flag,jdbcType=SMALLINT},
      #{cComment,jdbcType=NVARCHAR},
      #{parentId,jdbcType=NVARCHAR},
      #{effective,jdbcType=INTEGER},
      #{updater,jdbcType=NVARCHAR},
      #{updateTime,jdbcType=NCHAR},
      #{creater,jdbcType=NVARCHAR},
      #{createTime,jdbcType=NCHAR},
      #{remark,jdbcType=NVARCHAR}
      )
  </insert>
    <update id="updateByPrimaryKeySelective" parameterType="com.emr.entity.Emr_Dictionary" >
        update emr_dictionary
        <set >
            <if test="typecode != null" >
                typeCode = #{typecode,jdbcType=NVARCHAR},
            </if>
            <if test="typename != null" >
                typeName = #{typename,jdbcType=NVARCHAR},
            </if>
            <if test="code != null" >
                code = #{code,jdbcType=NVARCHAR},
            </if>
            <if test="name != null" >
                Name = #{name,jdbcType=NVARCHAR},
            </if>
            <if test="pyCode != null" >
                py_code = #{pyCode,jdbcType=NVARCHAR},
            </if>
            <if test="wbCode != null" >
                wb_code = #{wbCode,jdbcType=NVARCHAR},
            </if>
            <if test="zipCode != null" >
                zip_code = #{zipCode,jdbcType=NCHAR},
            </if>
            <if test="gbCode != null" >
                gb_code = #{gbCode,jdbcType=NCHAR},
            </if>
            <if test="flag != null" >
                flag = #{flag,jdbcType=SMALLINT},
            </if>
            <if test="cComment != null" >
                c_comment = #{cComment,jdbcType=NVARCHAR},
            </if>
            <if test="parentId != null and parentId != ''" >
                parent_id = #{parentId,jdbcType=NVARCHAR},
            </if>
            <if test="effective != null" >
                effective = #{effective,jdbcType=INTEGER},
            </if>
            <if test="updater != null" >
                updater = #{updater,jdbcType=NVARCHAR},
            </if>
            <if test="updateTime != null" >
                update_time = #{updateTime,jdbcType=NCHAR},
            </if>
            <if test="creater != null" >
                creater = #{creater,jdbcType=NVARCHAR},
            </if>
            <if test="createTime != null" >
                create_time = #{createTime,jdbcType=NCHAR},
            </if>
            <if test="remark != null" >
                remark = #{remark,jdbcType=NVARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>
  <!--根据分类标识查询字典-->
  <select id="selectDictionaryByTypeCode" resultMap="BaseResultMap" parameterType="string">
    SELECT
        code,name
    FROM
        emr_dictionary
    WHERE
        effective = 1 AND parent_id =  #{typeCode}
  </select>
<!--
  <select id="selectDictionaryByTypeCode" resultMap="BaseResultMap" parameterType="string">
    SELECT
        code,name
    FROM
        emr_dictionary
    WHERE
        effective = 1 AND parent_id = (select id from emr_dictionary where typeCode = #{typeCode})
  </select>
-->
  <!--查询字典-->
  <select id="selectAll" resultMap="BaseResultMap" parameterType="string">
      SELECT
      emr_dictionary.id,
      emr_dictionary.typeCode,
      emr_dictionary.typeName,
      emr_dictionary. CODE,
      emr_dictionary. NAME,
      emr_dictionary.py_code,
      emr_dictionary.wb_code,
      emr_dictionary.zip_code,
      emr_dictionary.gb_code,
      emr_dictionary.flag,
      emr_dictionary.c_comment,
      case
      when  emr_dictionary.parent_id = '0' then emr_dictionary.parent_id
      else emr_dictionary1.id
      end parent_id,
      emr_dictionary.effective,
      emr_dictionary.remark,
      CASE
      WHEN emr_dictionary.typeName = '' THEN
      emr_dictionary. NAME
      WHEN emr_dictionary.typeName IS NULL THEN
      emr_dictionary. NAME
      ELSE
      emr_dictionary.typeName
      END newName,
      case
      when  emr_dictionary.parent_id = '0' then emr_dictionary.parent_id
      else emr_dictionary1.id
      end newParentId
      FROM
      emr_dictionary
      LEFT JOIN emr_dictionary emr_dictionary1 ON emr_dictionary.parent_id = emr_dictionary1.typeCode
      AND emr_dictionary.parent_id != '0'
      AND emr_dictionary.parent_id IS NOT NULL
      AND emr_dictionary.parent_id != ''
      WHERE emr_dictionary.effective = 1
      <if test="searchContext != null and searchContext != ''">
          AND (emr_dictionary.typeName LIKE '%${searchContext}%' OR emr_dictionary.Name LIKE '%${searchContext}%')
          UNION ALL
          SELECT
          emr_dictionary.id,
          emr_dictionary.typeCode,
          emr_dictionary.typeName,
          emr_dictionary. CODE,
          emr_dictionary. NAME,
          emr_dictionary.py_code,
          emr_dictionary.wb_code,
          emr_dictionary.zip_code,
          emr_dictionary.gb_code,
          emr_dictionary.flag,
          emr_dictionary.c_comment,
          case
          when  emr_dictionary.parent_id = '0' then emr_dictionary.parent_id
          else emr_dictionary1.id
          end parent_id,
          emr_dictionary.effective,
          emr_dictionary.remark,
          CASE
          WHEN emr_dictionary.typeName = '' THEN
          emr_dictionary. NAME
          WHEN emr_dictionary.typeName IS NULL THEN
          emr_dictionary. NAME
          ELSE
          emr_dictionary.typeName
          END newName,
          case
          when  emr_dictionary.parent_id = '0' then emr_dictionary.parent_id
          else emr_dictionary1.id
          end newParentId
          FROM
          emr_dictionary
          LEFT JOIN emr_dictionary emr_dictionary1 ON emr_dictionary.parent_id = emr_dictionary1.typeCode
          AND emr_dictionary.parent_id != '0'
          AND emr_dictionary.parent_id IS NOT NULL
          AND emr_dictionary.parent_id != ''
          WHERE emr_dictionary.effective = 1
          AND
            emr_dictionary.parent_id IN (select typeCode from emr_dictionary where effective = 1 AND (emr_dictionary.typeName LIKE '%${searchContext}%' OR emr_dictionary.Name LIKE '%${searchContext}%'))
      </if>
<!--
  <select id="selectAll" resultMap="BaseResultMap" parameterType="string">
      SELECT
      *,case
          when typeName = ''  then Name
          when typeName IS NULL  then Name
          else typeName end newName,parent_id newParentId
      FROM
      emr_dictionary
      WHERE effective = 1
      <if test="searchContext != null and searchContext != ''">
          AND (emr_dictionary.typeName LIKE '%${searchContext}%' OR emr_dictionary.Name LIKE '%${searchContext}%')
          UNION ALL
          SELECT
              *,case
              when typeName = ''  then Name
              when typeName IS NULL  then Name
              else typeName end newName,parent_id newParentId
          FROM
            emr_dictionary
          WHERE
              effective = 1
          AND
              parent_id IN (select id from emr_dictionary where effective = 1 AND (emr_dictionary.typeName LIKE '%${searchContext}%' OR emr_dictionary.Name LIKE '%${searchContext}%'))
      </if>
-->
    <!--SELECT
        *,typeName newName
    FROM
        emr_dictionary emr_dictionary_1
    WHERE
        parent_id = 0
        <if test="searchContext != null and searchContext != ''">
            AND emr_dictionary_1.typeName LIKE '%${searchContext}%'
            UNION ALL
            SELECT
                *,Name newName
            FROM
            emr_dictionary emr_dictionary_1
            WHERE
                parent_id != 0
            <if test="searchContext != null and searchContext != ''">
                 AND parent_id IN (select id from emr_dictionary where typeName LIKE '%${searchContext}%')
            </if>
        </if>
    UNION ALL
        SELECT
          *,Name newName
        FROM
            emr_dictionary emr_dictionary_1
        WHERE
            parent_id != 0
      <if test="searchContext != null and searchContext != ''">
          AND emr_dictionary_1.Name LIKE '%${searchContext}%'
      </if>-->
  </select>
    <select id="getDictionaryByParentId" resultType="java.lang.String">
        SELECT typeName FROM emr_dictionary WHERE id = #{parentId}
    </select>
    <select id="getDictionaryByTypeCodes" resultMap="BaseResultMap" parameterType="java.lang.String">
        SELECT * FROM t_dictionary WHERE typeCode IN (${typeCodes})
    </select>
</mapper>