<?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.boco.nbd.wios.manage.mapper.def.DepartmentMapper">

<resultMap id="BaseResultMap" type="com.boco.nbd.wios.manage.entity.bo.Department">
  <id column="id" jdbcType="INTEGER" property="id" />
  <result column="name" jdbcType="VARCHAR" property="name" />
  <result column="region_id" jdbcType="INTEGER" property="regionId" />
  <result column="address" jdbcType="VARCHAR" property="address" />
  <result column="parent_id" jdbcType="INTEGER" property="parentId" />
  <result column="id_tree" jdbcType="VARCHAR" property="idTree" />
  <result column="remark" jdbcType="VARCHAR" property="remark" />
  <result column="status" jdbcType="INTEGER" property="status" />
  <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
  <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
  <result column="create_account" jdbcType="INTEGER" property="createAccount" />
  <result column="update_account" jdbcType="INTEGER" property="updateAccount" />
</resultMap>


<sql id="Base_Column_List">
  id, name, region_id, address, parent_id, id_tree, remark, status, create_time, update_time,
  create_account, update_account
</sql>


<delete id="delete" parameterType="java.lang.Integer">
  delete from t_department
  where id = #{id,jdbcType=INTEGER}
</delete>


<insert id="insert" parameterType="com.boco.nbd.wios.manage.entity.bo.Department"
useGeneratedKeys="true" keyProperty="id">
  insert into t_department (id, name, region_id,
    address, parent_id, id_tree,
    remark, status, create_time,
    update_time, create_account, update_account
    )
  values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{regionId,jdbcType=INTEGER},
    #{address,jdbcType=VARCHAR}, #{parentId,jdbcType=INTEGER}, #{idTree,jdbcType=VARCHAR},
    #{remark,jdbcType=VARCHAR}, #{status,jdbcType=INTEGER}, #{createTime,jdbcType=TIMESTAMP},
    #{updateTime,jdbcType=TIMESTAMP}, #{createAccount,jdbcType=INTEGER}, #{updateAccount,jdbcType=INTEGER}
    )
</insert>


<insert id="insertDynamic" parameterType="com.boco.nbd.wios.manage.entity.bo.Department">
  insert into t_department
  <trim prefix="(" suffix=")" suffixOverrides=",">
    <if test="id != null">
      id,
    </if>
    <if test="name != null">
      name,
    </if>
    <if test="regionId != null">
      region_id,
    </if>
    <if test="address != null">
      address,
    </if>
    <if test="parentId != null">
      parent_id,
    </if>
    <if test="idTree != null">
      id_tree,
    </if>
    <if test="remark != null">
      remark,
    </if>
    <if test="status != null">
      status,
    </if>
    <if test="createTime != null">
      create_time,
    </if>
    <if test="updateTime != null">
      update_time,
    </if>
    <if test="createAccount != null">
      create_account,
    </if>
    <if test="updateAccount != null">
      update_account,
    </if>
  </trim>
  <trim prefix="values (" suffix=")" suffixOverrides=",">
    <if test="id != null">
      #{id,jdbcType=INTEGER},
    </if>
    <if test="name != null">
      #{name,jdbcType=VARCHAR},
    </if>
    <if test="regionId != null">
      #{regionId,jdbcType=INTEGER},
    </if>
    <if test="address != null">
      #{address,jdbcType=VARCHAR},
    </if>
    <if test="parentId != null">
      #{parentId,jdbcType=INTEGER},
    </if>
    <if test="idTree != null">
      #{idTree,jdbcType=VARCHAR},
    </if>
    <if test="remark != null">
      #{remark,jdbcType=VARCHAR},
    </if>
    <if test="status != null">
      #{status,jdbcType=INTEGER},
    </if>
    <if test="createTime != null">
      #{createTime,jdbcType=TIMESTAMP},
    </if>
    <if test="updateTime != null">
      #{updateTime,jdbcType=TIMESTAMP},
    </if>
    <if test="createAccount != null">
      #{createAccount,jdbcType=INTEGER},
    </if>
    <if test="updateAccount != null">
      #{updateAccount,jdbcType=INTEGER},
    </if>
  </trim>
</insert>


<update id="updateDynamic" parameterType="com.boco.nbd.wios.manage.entity.bo.Department">
  update t_department
  <set>
    <if test="name != null">
      name = #{name,jdbcType=VARCHAR},
    </if>
    <if test="regionId != null">
      region_id = #{regionId,jdbcType=INTEGER},
    </if>
    <if test="address != null">
      address = #{address,jdbcType=VARCHAR},
    </if>
    <if test="parentId != null">
      parent_id = #{parentId,jdbcType=INTEGER},
    </if>
    <if test="idTree != null">
      id_tree = #{idTree,jdbcType=VARCHAR},
    </if>
    <if test="remark != null">
      remark = #{remark,jdbcType=VARCHAR},
    </if>
    <if test="status != null">
      status = #{status,jdbcType=INTEGER},
    </if>
    <if test="createTime != null">
      create_time = #{createTime,jdbcType=TIMESTAMP},
    </if>
    <if test="updateTime != null">
      update_time = #{updateTime,jdbcType=TIMESTAMP},
    </if>
    <if test="createAccount != null">
      create_account = #{createAccount,jdbcType=INTEGER},
    </if>
    <if test="updateAccount != null">
      update_account = #{updateAccount,jdbcType=INTEGER},
    </if>
  </set>
  where id = #{id,jdbcType=INTEGER}
</update>

<update id="update" parameterType="com.boco.nbd.wios.manage.entity.bo.Department">
  update t_department
  set name = #{name,jdbcType=VARCHAR},
    region_id = #{regionId,jdbcType=INTEGER},
    address = #{address,jdbcType=VARCHAR},
    parent_id = #{parentId,jdbcType=INTEGER},
    id_tree = #{idTree,jdbcType=VARCHAR},
    remark = #{remark,jdbcType=VARCHAR},
    status = #{status,jdbcType=INTEGER},
    create_time = #{createTime,jdbcType=TIMESTAMP},
    update_time = #{updateTime,jdbcType=TIMESTAMP},
    create_account = #{createAccount,jdbcType=INTEGER},
    update_account = #{updateAccount,jdbcType=INTEGER}
  where id = #{id,jdbcType=INTEGER}
</update>


<select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  select
	<include refid="Base_Column_List" />
  from  t_department
  where id = #{id,jdbcType=INTEGER}
</select>

<select id="selectAll" parameterType="java.lang.Integer"
resultType="com.boco.nbd.wios.manage.entity.bo.DepartmentVo">
  select t.*,r.name as regionName from  t_department t
  left join t_region r on r.id=t.region_id
   <where>
	    <if test="status != null">
	        and t.status = #{status}
	    </if>
	</where>
</select>

<update id="updateIdTree">
  update t_department
  set id_tree = concat(#{idTreeNewPrefix},substring(id_tree,length(#{idTreePrefix})+1,length(id_tree)))
  where id_tree like concat(#{idTreePrefix},'%')
</update>

<select id="selectByParentId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
  select
	<include refid="Base_Column_List" />
  from  t_department
  where parent_id = #{parentId,jdbcType=INTEGER} and status=1
</select>

</mapper>