<?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.Zd_AssortMapper" >
  <resultMap id="BaseResultMap" type="com.emr.entity.Zd_Assort" >
    <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" />
  </resultMap>
  <insert id="insert" parameterType="com.emr.entity.Zd_Assort" >
    insert into zd_assort (assort_id, assort_name, assort_sort,
      print_flag)
    values (#{assortId,jdbcType=VARCHAR}, #{assortName,jdbcType=VARCHAR}, #{assortSort,jdbcType=SMALLINT}, 
      #{printFlag,jdbcType=VARCHAR})
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.emr.entity.Zd_Assort" >
      update zd_assort
      <set>
      <if test="assortName != null" >
        assort_name = #{assortName},
      </if>
      <if test="assortSort != null" >
        assort_sort = ${assortSort},
      </if>
      <if test="printFlag != null" >
        print_flag = #{printFlag},
      </if>
      </set>
      where assort_id = #{assortId}
  </update>
  <!--查询全部-->
  <select id="selectAll" resultMap="BaseResultMap">
    SELECT
        *
    FROM
        zd_assort
    <where>
        <if test="record.assortName != '' and record.assortName != null">
          assort_name = #{record.assortName}
        </if>
        <if test="record.printFlag != '' and record.printFlag != null">
            AND print_flag = ${record.printFlag}
        </if>
    </where>
    ORDER BY
        assort_sort
  </select>
    <!--验证名称是否重复-->
    <select id="checkAssortName" resultType="com.emr.entity.Zd_Assort">
        SELECT
            assort_id
        FROM
            dbo.zd_assort
        WHERE
            assort_name = #{assortName}
    </select>
    <!--根据assortId查询assort对象-->
    <select id="getAssortByAssortId" resultType="com.emr.entity.Zd_Assort">
        SELECT
          *
        FROM
            dbo.zd_assort
        WHERE
            assort_id = #{assortId}
    </select>
    <!--根据assortId删除assort对象-->
    <delete id="deleteAssortByAssortId">
        DELETE  FROM
            dbo.zd_assort
        WHERE
            assort_id = #{assortId}
    </delete>
  <!--查询全部关联根据类别Id选中集合-->
  <select id="selectAllAndRelatedByTypeId" resultMap="BaseResultMap">
    SELECT
        dbo.zd_assort.assort_id,
        dbo.zd_assort.assort_name,
        dbo.emr_type_related.id printFlag
    FROM
        dbo.zd_assort
    LEFT OUTER JOIN
        dbo.emr_type_related
    ON
        dbo.zd_assort.assort_id = dbo.emr_type_related.assort_id
        <if test="typeId != null">
          AND dbo.emr_type_related.type_id = ${typeId}
        </if>
    WHERE print_flag = 0
    ORDER BY
        assort_sort
  </select>

  <!--查询全部根据角色id关联角色分配表-->
  <select id="selectAllAndRoleReaderByRoleId" resultMap="BaseResultMap">
    SELECT
        zd_assort.assort_id,
        zd_assort.assort_name,
        emr_role_reader.record_id printFlag
    FROM
        zd_assort
    LEFT JOIN
        emr_role_reader
    ON
        zd_assort.assort_id = emr_role_reader.record_id
    AND
        emr_role_reader.role_id = ${roleId}
    WHERE
        print_flag = 0
    ORDER BY
        assort_sort
  </select>

  <!--查询全部根据用户id关联用户分配表-->
  <select id="selectAllAndUserReaderByUserIdAndRoleId" resultMap="BaseResultMap">
    SELECT
        DISTINCT
        assort_id,
        assort_name,
        emr_user_reader.record_id printFlag,
        assort_sort
    FROM
        zd_assort
    LEFT JOIN (
        SELECT
            record_id
        FROM
            emr_role_reader
        WHERE
            role_id = ${roleId}
        AND
            record_id NOT IN (
                SELECT
                    record_id
                FROM
                    emr_user_reader
                WHERE
                    user_id = ${userId}
                AND
                    flag = 0)
        UNION ALL
            SELECT
                record_id
            FROM
                emr_user_reader
            WHERE
                user_id = ${userId}
            AND
                flag = 1) emr_user_reader
    ON
        assort_id = emr_user_reader.record_id
    WHERE
        zd_assort.print_flag = 0
    ORDER BY
        zd_assort.assort_sort
  </select>
    <select id="selectEmrType" resultType="com.emr.entity.recordType.Emr_Type">
        select * from emr_type
    </select>
    <select id="getZdAssortByName" resultType="com.emr.entity.Zd_Assort">
        select  * from  zd_assort t where t.assort_name like '%${assortName}%'
    </select>
</mapper>