<?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_Fun_Group_DistributionMapper" >
  <resultMap id="BaseResultMap" type="com.emr.vo.Emr_Fun_Group_DistributionVo" >
    <id column="id" property="id" jdbcType="INTEGER" />
    <result column="type_flag" property="typeFlag" jdbcType="NCHAR" />
    <result column="user_id" property="userId" jdbcType="INTEGER" />
    <result column="user_name" property="userName" jdbcType="NCHAR" />
    <result column="effective_time" property="effectiveTime" jdbcType="NCHAR" />
    <result column="effective" property="effective" jdbcType="SMALLINT" />
    <result column="remark" property="remark" jdbcType="NCHAR" />
    <result column="creater" property="creater" jdbcType="NCHAR" />
    <result column="create_date" property="createDate" jdbcType="NCHAR" />
    <result column="updater" property="updater" jdbcType="NCHAR" />
    <result column="update_date" property="updateDate" jdbcType="NCHAR" />
  </resultMap>
  <sql id="Base_Column_List" >
    id, type_flag, user_id, user_name, effective_time, effective, remark, creater, create_date,
    updater, update_date
  </sql>
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select
    <include refid="Base_Column_List" />
    from emr_fun_group_distribution
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from emr_fun_group_distribution
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.emr.entity.Emr_Fun_Group_Distribution" >
    insert into emr_fun_group_distribution (id, type_flag, user_id,
      user_name, effective_time, effective,
      remark, creater, create_date,
      updater, update_date)
    values (#{id,jdbcType=INTEGER}, #{typeFlag,jdbcType=NCHAR}, #{userId,jdbcType=INTEGER},
      #{userName,jdbcType=NCHAR}, #{effectiveTime,jdbcType=NCHAR}, #{effective,jdbcType=SMALLINT},
      #{remark,jdbcType=NCHAR}, #{creater,jdbcType=NCHAR}, #{createDate,jdbcType=NCHAR},
      #{updater,jdbcType=NCHAR}, #{updateDate,jdbcType=NCHAR})
  </insert>
  <insert id="insertSelective" parameterType="com.emr.entity.Emr_Fun_Group_Distribution" >
    insert into emr_fun_group_distribution
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        id,
      </if>
      <if test="typeFlag != null" >
        type_flag,
      </if>
      <if test="userId != null" >
        user_id,
      </if>
      <if test="userName != null" >
        user_name,
      </if>
      <if test="effectiveTime != null" >
        effective_time,
      </if>
      <if test="effective != null" >
        effective,
      </if>
      <if test="remark != null" >
        remark,
      </if>
      <if test="creater != null" >
        creater,
      </if>
      <if test="createDate != null" >
        create_date,
      </if>
      <if test="updater != null" >
        updater,
      </if>
      <if test="updateDate != null" >
        update_date,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="id != null" >
        #{id,jdbcType=INTEGER},
      </if>
      <if test="typeFlag != null" >
        #{typeFlag,jdbcType=NCHAR},
      </if>
      <if test="userId != null" >
        #{userId,jdbcType=INTEGER},
      </if>
      <if test="userName != null" >
        #{userName,jdbcType=NCHAR},
      </if>
      <if test="effectiveTime != null" >
        #{effectiveTime,jdbcType=NCHAR},
      </if>
      <if test="effective != null" >
        #{effective,jdbcType=SMALLINT},
      </if>
      <if test="remark != null" >
        #{remark,jdbcType=NCHAR},
      </if>
      <if test="creater != null" >
        #{creater,jdbcType=NCHAR},
      </if>
      <if test="createDate != null" >
        #{createDate,jdbcType=NCHAR},
      </if>
      <if test="updater != null" >
        #{updater,jdbcType=NCHAR},
      </if>
      <if test="updateDate != null" >
        #{updateDate,jdbcType=NCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.emr.entity.Emr_Fun_Group_Distribution" >
    update emr_fun_group_distribution
    <set >
      <if test="typeFlag != null" >
        type_flag = #{typeFlag,jdbcType=NCHAR},
      </if>
      <if test="userId != null" >
        user_id = #{userId,jdbcType=INTEGER},
      </if>
      <if test="userName != null" >
        user_name = #{userName,jdbcType=NCHAR},
      </if>
      <if test="effectiveTime != null" >
        effective_time = #{effectiveTime,jdbcType=NCHAR},
      </if>
      <if test="effective != null" >
        effective = #{effective,jdbcType=SMALLINT},
      </if>
      <if test="remark != null" >
        remark = #{remark,jdbcType=NCHAR},
      </if>
      <if test="creater != null" >
        creater = #{creater,jdbcType=NCHAR},
      </if>
      <if test="createDate != null" >
        create_date = #{createDate,jdbcType=NCHAR},
      </if>
      <if test="updater != null" >
        updater = #{updater,jdbcType=NCHAR},
      </if>
      <if test="updateDate != null" >
        update_date = #{updateDate,jdbcType=NCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.emr.entity.Emr_Fun_Group_Distribution" >
    update emr_fun_group_distribution
    set type_flag = #{typeFlag,jdbcType=NCHAR},
      user_id = #{userId,jdbcType=INTEGER},
      user_name = #{userName,jdbcType=NCHAR},
      effective_time = #{effectiveTime,jdbcType=NCHAR},
      effective = #{effective,jdbcType=SMALLINT},
      remark = #{remark,jdbcType=NCHAR},
      creater = #{creater,jdbcType=NCHAR},
      create_date = #{createDate,jdbcType=NCHAR},
      updater = #{updater,jdbcType=NCHAR},
      update_date = #{updateDate,jdbcType=NCHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <!--批量插入-->
  <insert id="SimpleInsert" parameterType="java.util.List">
      INSERT INTO
          emr_fun_group_distribution(
            type_flag, user_id,user_name, effective_time, effective,remark, creater, create_date,updater, update_date
          )
          VALUES
          <foreach collection="list" item="item" separator=",">
            (
              #{item.typeFlag,jdbcType=INTEGER},
              #{item.userId,jdbcType=INTEGER},
              #{item.userName,jdbcType=NCHAR},
              #{item.effectiveTime,jdbcType=NCHAR},
              #{item.effective,jdbcType=SMALLINT},
              #{item.remark,jdbcType=NCHAR},
              #{item.creater,jdbcType=NCHAR},
              #{item.createDate,jdbcType=NCHAR},
              #{item.updater,jdbcType=NCHAR},
              #{item.updateDate,jdbcType=NCHAR}
            )
          </foreach>
  </insert>
  <!--查询该typeFlag有效的用户id-->
  <select id="selectUserIdsByTypeFlag" resultMap="BaseResultMap">
      SELECT
          user_id
      FROM
          emr_fun_group_distribution
      WHERE
          effective = 1
      AND effective_time > GETDATE () - 1
      AND type_flag = #{typeFlag}
      <if test="userId != null">
          AND user_id = #{userId}
      </if>
  </select>

  <!--查询所有-->
  <select id="selectAll" resultMap="BaseResultMap">
     SELECT
        f.id,
        dictionary. NAME type_flag,
        f.user_id,
        f.user_name,
        f.effective_time,
        f.effective,
        f.remark,
        f.creater,
        f.create_date,
        f.updater,
        f.update_date
    FROM
        emr_fun_group_distribution f
    INNER JOIN (
        SELECT
            CODE,
            NAME
        FROM
            emr_dictionary
        WHERE
            parent_id = 'fun_group'
    ) dictionary ON f.type_flag = dictionary. CODE
    <where>
        <if test="record.typeFlag != null and record.typeFlag != ''">
            AND f.type_flag = #{record.typeFlag}
        </if>
        <if test="record.userName != null and record.userName != ''">
            AND f.user_name LIKE '%${record.userName}%'
        </if>
        <if test="record.effective != null">
            AND f.effective = #{record.effective}
        </if>
        <if test="startTime != '' and startTime != null">
            AND f.effective_time &gt;=  #{startTime}
            <if test="endTime != '' and endTime != null">
                AND f.effective_time  &lt;= #{endTime}
            </if>
        </if>
    </where>
    ORDER BY f.create_date DESC
  </select>
    <select id="checkTypeFlagAndUserName" resultType="com.emr.entity.Emr_Fun_Group_Distribution">
        SELECT id FROM emr_fun_group_distribution WHERE type_flag = #{typeFlag} AND user_name = #{userName}
    </select>
    <delete id="deleteByIds" parameterType="java.lang.String">
        DELETE FROM emr_fun_group_distribution WHERE id IN (${ids})
    </delete>
    <!--查询所有-->
    <select id="selectAllByIds" resultMap="BaseResultMap">
    SELECT
        f.id,
        dictionary. NAME type_flag,
        f.user_id,
        f.user_name,
        f.effective_time,
        f.effective,
        f.remark,
        f.creater,
        f.create_date,
        f.updater,
        f.update_date
    FROM
      emr_fun_group_distribution f
    INNER JOIN (
        SELECT
          CODE,
          NAME
        FROM
          emr_dictionary
        WHERE
            parent_id =  'fun_group'
        ) dictionary ON f.type_flag = dictionary. CODE
        <where>
            f.id IN (${ids})
        </where>
    ORDER BY f.create_date DESC
    </select>
</mapper>