<?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.manage.dao.Power_DeptMapper">
  <resultMap id="BaseResultMap" type="com.manage.vo.Power_DeptVo">
    <id column="dept_id" jdbcType="INTEGER" property="deptId" />
    <result column="dept_name" jdbcType="VARCHAR" property="deptName" />
    <result column="dict_id" jdbcType="INTEGER" property="dictId" />
    <result column="effective" jdbcType="INTEGER" property="effective" />
    <result column="create_date" jdbcType="CHAR" property="createDate" />
    <result column="creater" jdbcType="VARCHAR" property="creater" />
    <result column="update_date" jdbcType="CHAR" property="updateDate" />
    <result column="updater" jdbcType="VARCHAR" property="updater" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="dept_code" jdbcType="VARCHAR" property="deptCode" />
  </resultMap>
  <sql id="Base_Column_List">
    dept_id, dept_name, dict_id, effective, create_date, creater, update_date, updater, 
    remark,dept_code
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
        power_dept.dept_id,
        power_dept.dept_name,
        power_dept.dict_id,
        power_dept.effective,
        power_dept.create_date,
        power_dept.creater,
        power_dept.update_date,
        power_dept.updater,
        power_dept.remark,
        power_sys_dict.hospital_name,
        power_dept.dept_code
    from power_dept
    LEFT JOIN power_sys_dict ON power_sys_dict.dict_id = power_dept.dict_id
    where power_dept.dept_id = #{deptId,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from power_dept
    where dept_id = #{deptId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="com.manage.entity.Power_Dept">
    insert into power_dept (dept_id, dept_name, dict_id, 
      effective, create_date, creater, 
      update_date, updater, remark,dept_code
      )
    values (#{deptId,jdbcType=INTEGER}, #{deptName,jdbcType=VARCHAR}, #{dictId,jdbcType=INTEGER},
      #{effective,jdbcType=INTEGER}, #{createDate,jdbcType=CHAR}, #{creater,jdbcType=VARCHAR},
      #{updateDate,jdbcType=CHAR}, #{updater,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR},#{deptCode,jdbcType=VARCHAR}
      )
  </insert>
  <insert id="insertSelective" parameterType="com.manage.entity.Power_Dept">
    insert into power_dept
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="deptId != null">
        dept_id,
      </if>
      <if test="deptName != null">
        dept_name,
      </if>
      <if test="dictId != null">
        dict_id,
      </if>
      <if test="effective != null">
        effective,
      </if>
      <if test="createDate != null">
        create_date,
      </if>
      <if test="creater != null">
        creater,
      </if>
      <if test="updateDate != null">
        update_date,
      </if>
      <if test="updater != null">
        updater,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="deptCode != null">
        dept_code,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="deptId != null">
        #{deptId,jdbcType=INTEGER},
      </if>
      <if test="deptName != null">
        #{deptName,jdbcType=VARCHAR},
      </if>
      <if test="dictId != null">
        #{dictId,jdbcType=INTEGER},
      </if>
      <if test="effective != null">
        #{effective,jdbcType=INTEGER},
      </if>
      <if test="createDate != null">
        #{createDate,jdbcType=CHAR},
      </if>
      <if test="creater != null">
        #{creater,jdbcType=VARCHAR},
      </if>
      <if test="updateDate != null">
        #{updateDate,jdbcType=CHAR},
      </if>
      <if test="updater != null">
        #{updater,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="deptCode != null">
        #{deptCode,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.manage.entity.Power_Dept">
    update power_dept
    <set>
      <if test="deptName != null">
        dept_name = #{deptName,jdbcType=VARCHAR},
      </if>
      <if test="dictId != null">
        dict_id = #{dictId,jdbcType=INTEGER},
      </if>
      <if test="effective != null">
        effective = #{effective,jdbcType=INTEGER},
      </if>
      <if test="createDate != null">
        create_date = #{createDate,jdbcType=CHAR},
      </if>
      <if test="creater != null">
        creater = #{creater,jdbcType=VARCHAR},
      </if>
      <if test="updateDate != null">
        update_date = #{updateDate,jdbcType=CHAR},
      </if>
      <if test="updater != null">
        updater = #{updater,jdbcType=VARCHAR},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="deptCode != null">
        dept_code = #{deptCode,jdbcType=VARCHAR},
      </if>
    </set>
    where dept_id = #{deptId,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.manage.entity.Power_Dept">
    update power_dept
    set dept_name = #{deptName,jdbcType=VARCHAR},
      dict_id = #{dictId,jdbcType=INTEGER},
      effective = #{effective,jdbcType=INTEGER},
      create_date = #{createDate,jdbcType=CHAR},
      creater = #{creater,jdbcType=VARCHAR},
      update_date = #{updateDate,jdbcType=CHAR},
      updater = #{updater,jdbcType=VARCHAR},
      remark = #{remark,jdbcType=VARCHAR},
      dept_code = #{deptCode,jdbcType=VARCHAR}
    where dept_id = #{deptId,jdbcType=INTEGER}
  </update>


  <!--2019-4-22 ly -->
  <select id="findSomeByMore" resultMap="BaseResultMap">
    select
        power_dept.dept_id,
        power_dept.dept_name,
        power_dept.dict_id,
        power_dept.effective,
        power_dept.create_date,
        power_dept.creater,
        power_dept.update_date,
        power_dept.updater,
        power_dept.remark,
        power_sys_dict.hospital_name
    from power_dept
    left join power_sys_dict on power_sys_dict.dict_id = power_dept.dict_id
    where 1 = 1
    <if test="deptName!=null and deptName!=''">
      and dept_name LIKE '%${deptName}%'
    </if>
    <if test="dictId!=null">
      and power_dept.dict_id=${dictId}
    </if>
    <if test="effective!=null">
      and effective=${effective}
    </if>
    <if test="creater!=null and creater!=''">
      and power_dept.creater LIKE '%${creater}%'
    </if>
    <if test="checks!=null and checks!=''">
      and power_dept.dept_id  IN (${checks})
    </if>
    ORDER BY create_date DESC,power_dept.dept_id DESC
    <if test="offset != null and limit != null">
        LIMIT #{offset},#{limit}
    </if>
  </select>
  <!--验证同一医院科室名不能重复-->
  <select id="checkDeptName" resultMap="BaseResultMap" parameterType="java.lang.String">
      SELECT
          dept_id
      FROM
          power_dept
      WHERE
          dept_name = #{deptName}
      <if test="dictId != null">
          AND dict_id = #{dictId}
      </if>
  </select>
  <select id="getTotal" resultType="int">
    SELECT count(1) FROM power_dept WHERE 1 = 1
    <if test="deptName!=null and deptName!=''">
      and dept_name LIKE '%${deptName}%'
    </if>
    <if test="dictId!=null">
      and dict_id=${dictId}
    </if>
    <if test="effective!=null">
      and effective=${effective}
    </if>
    <if test="creater!=null and creater!=''">
      and creater LIKE '%${creater}%'
    </if>
  </select>
  <select id="getDeptList" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from power_dept
  </select>
  <select id="selectByPrimaryKeys" parameterType="HashMap" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from power_dept
    where 1=1
    <if test="list !=null and list!=''">
    and dept_id in
    <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
      #{item}
    </foreach>
    </if>
  </select>
  <select id="selectDeptByDeptName" parameterType="HashMap" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from power_dept
    where 1=1
    <if test="list !=null and list!=''">
      and dept_name in
      <foreach collection="list" item="item" index="index" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
  </select>
  <!--根据权限查询医院科室树-->
  <select id="selectDeptByUserId" resultMap="BaseResultMap" parameterType="java.lang.Integer">
    SELECT
        power_sys_dict.dict_id,
        power_sys_dict.hospital_name,
        power_dept.dept_id,
        power_dept.dept_name
    FROM
        power_dept
    INNER JOIN power_sys_dict ON power_sys_dict.dict_id = power_dept.dict_id
    WHERE
        power_sys_dict.dict_status = 1
    AND power_dept.effective = 1
      <if test="userId != null">
        and power_sys_dict.dict_id = (select power_user_dict.dict_id from power_user_dict WHERE power_user_dict.user_id = ${userId})
      </if>
  </select>
  <!--根据医院id查科室集合-->
  <select id="selectDeptByDictId" resultMap="BaseResultMap">
    SELECT
        power_dept.dept_id,
        power_dept.dept_name
        <if test="sysFlag != null and sysFlag != ''">
          ,power_sys_dict.dept_id checks
        </if>
    FROM
        power_dept
        <if test="sysFlag != null and sysFlag != ''">
          LEFT JOIN
          power_sys_dict on power_dept.dept_id = power_sys_dict.dept_id and sys_flag = #{sysFlag}
        </if>
    WHERE
        effective = 1
      <if test="dictId != null">
        AND
          power_dept.dict_id = #{dictId}
      </if>
  </select>

  <!--批量插入-->
  <insert id="SimpleInsert" parameterType="java.util.List">
    INSERT INTO power_dept(
      dept_id,
      dept_name,
      dict_id,
      effective,
      create_date,
      creater,
      update_date,
      updater,
      remark,
      dept_code
      )
    VALUES
    <foreach collection ="list" item="item" index= "index" separator =",">
      (
      #{item.deptId,jdbcType=INTEGER},
      #{item.deptName,jdbcType=VARCHAR},
      #{item.dictId,jdbcType=INTEGER},
      #{item.effective,jdbcType=INTEGER},
      #{item.createDate,jdbcType=CHAR},
      #{item.creater,jdbcType=VARCHAR},
      #{item.updateDate,jdbcType=CHAR},
      #{item.updater,jdbcType=VARCHAR},
      #{item.remark,jdbcType=VARCHAR},
      #{item.deptCode,jdbcType=VARCHAR}
      )
    </foreach >
  </insert>
</mapper>