<?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.xhpc.activity.mapper.XhpcWorkDeptMapper">
  <resultMap id="BaseResultMap" type="com.xhpc.activity.domain.XhpcWorkDeptDomain">
    <id column="work_dept_id" jdbcType="BIGINT" property="workDeptId" />
    <result column="dept_name" jdbcType="VARCHAR" property="deptName" />
    <result column="code" jdbcType="VARCHAR" property="code" />
    <result column="sort" jdbcType="SMALLINT" property="sort" />
    <result column="parent_dept_id" jdbcType="BIGINT" property="parentDeptId" />
    <result column="status" jdbcType="SMALLINT" property="status" />
    <result column="del_flag" jdbcType="SMALLINT" property="delFlag" />
    <result column="tenant_id" jdbcType="VARCHAR" property="tenantId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
  </resultMap>
  <sql id="Base_Column_List">
    work_dept_id, dept_name, code, sort, parent_dept_id, `status`, del_flag, tenant_id,
    create_time, create_by, update_time, update_by
  </sql>


  <select id="selectListByParams" resultType="com.xhpc.activity.domain.XhpcWorkDeptDomain">
    select
    <include refid="Base_Column_List" />
    from xhpc_work_dept
    where del_flag = 0 and status = 1
    <if test="params.tenantId !=null and params.tenantId!=''">
      and tenant_id=#{params.tenantId}
    </if>
    <if test="params.deptId !=null and params.deptId!=''">
      and work_dept_id=#{params.deptId}
    </if>
    <if test="params.parentDeptId !=null and params.parentDeptId!=''">
      and parent_dept_id=#{params.parentDeptId}
    </if>
   <choose>
     <when test="params.parentDeptIds !=null and params.parentDeptIds!=''">
       and find_in_set(parent_dept_id, #{params.parentDeptIds})
     </when>
    <otherwise>
      and parent_dept_id is null
    </otherwise>
   </choose>
    order by sort desc
  </select>


  <select id="selectMapListByParams" resultType="map">
    select
     concat('d_',work_dept_id) as 'id',
    dept_name as 'name'
    from xhpc_work_dept
    where del_flag = 0 and status = 1
    <if test="params.tenantId !=null and params.tenantId!=''">
      and tenant_id=#{params.tenantId}
    </if>
      <choose>
        <when test="params.parentDeptId !=null and params.parentDeptId!=''">
          and parent_dept_id=#{params.parentDeptId}
        </when>
      <otherwise>
        and parent_dept_id is null
      </otherwise>
      </choose>
    order by sort desc
  </select>


  <select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from xhpc_work_dept
    where work_dept_id = #{workDeptId,jdbcType=BIGINT}
  </select>


    <select id="selectDomainByNameAndTenant" resultType="com.xhpc.activity.domain.XhpcWorkDeptDomain">
      select
      <include refid="Base_Column_List" />
      from xhpc_work_dept
      where del_flag = 0 and dept_name = #{deptName} and tenant_id=#{tenantId}
        <choose>
          <when test="parentDeptId != null">
            and parent_dept_id=#{parentDeptId}
          </when>
        <otherwise>
          and parent_dept_id is null
        </otherwise>
        </choose>
    </select>


    <update id="deleteLogicByPrimaryKey" parameterType="java.lang.Long">
    update xhpc_work_dept set del_flag=2
    where work_dept_id = #{workDeptId,jdbcType=BIGINT}
  </update>


  <insert id="insert" keyColumn="work_dept_id" keyProperty="workDeptId" parameterType="com.xhpc.activity.domain.XhpcWorkDeptDomain" useGeneratedKeys="true">
    insert into xhpc_work_dept (dept_name, code, sort,
      parent_dept_id, `status`, del_flag,
      tenant_id, create_time, create_by,
      update_time, update_by)
    values (#{deptName,jdbcType=VARCHAR}, #{code,jdbcType=VARCHAR}, #{sort,jdbcType=SMALLINT},
      #{parentDeptId,jdbcType=BIGINT}, #{status,jdbcType=SMALLINT}, 0,
      #{tenantId,jdbcType=VARCHAR}, SYSDATE(), #{createBy,jdbcType=VARCHAR},
      SYSDATE(), #{updateBy,jdbcType=VARCHAR})
  </insert>


  <update id="updateByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcWorkDeptDomain">
    update xhpc_work_dept
    set dept_name = #{deptName,jdbcType=VARCHAR},
      code = #{code,jdbcType=VARCHAR},
      sort = #{sort,jdbcType=SMALLINT},
      parent_dept_id = #{parentDeptId,jdbcType=BIGINT},
      `status` = #{status,jdbcType=SMALLINT},
      update_time = SYSDATE(),
      update_by = #{updateBy,jdbcType=VARCHAR}
    where work_dept_id = #{workDeptId,jdbcType=BIGINT}
  </update>


  <update id="updateStatusByPrimaryKey" parameterType="com.xhpc.activity.domain.XhpcWorkDeptDomain">
    update xhpc_work_dept
    set `status` = #{status,jdbcType=SMALLINT},
        update_time = SYSDATE(),
        update_by = #{updateBy,jdbcType=VARCHAR}
    where work_dept_id = #{workDeptId,jdbcType=BIGINT}
  </update>

</mapper>