<?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.sckw.system.dao.KwsDeptDao">
  <resultMap id="BaseResultMap" type="com.sckw.system.model.KwsDept">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="system_type" jdbcType="INTEGER" property="systemType" />
    <result column="ent_id" jdbcType="BIGINT" property="entId" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="company" jdbcType="INTEGER" property="company" />
    <result column="parent_id" jdbcType="BIGINT" property="parentId" />
    <result column="parent_ids" jdbcType="VARCHAR" property="parentIds" />
    <result column="level" jdbcType="INTEGER" property="level" />
    <result column="sort" jdbcType="INTEGER" property="sort" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_by" jdbcType="BIGINT" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="BIGINT" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="del_flag" jdbcType="INTEGER" property="delFlag" />
  </resultMap>

  <sql id="Base_Column_List">
    id, system_type, ent_id, name, company, parent_id, parent_ids, level, sort, remark, 
    status, create_by, create_time, update_by, update_time, del_flag
  </sql>

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

  <insert id="insert" parameterType="com.sckw.system.model.KwsDept">
    insert into kws_dept
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="systemType != null">
        system_type,
      </if>
      <if test="entId != null">
        ent_id,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="company != null">
        company,
      </if>
      <if test="parentId != null">
        parent_id,
      </if>
      <if test="parentIds != null">
        parent_ids,
      </if>
      <if test="level != null">
        level,
      </if>
      <if test="sort != null">
        sort,
      </if>
      <if test="remark != null">
        remark,
      </if>
      <if test="status != null">
        status,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="delFlag != null">
        del_flag,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=BIGINT},
      </if>
      <if test="systemType != null">
        #{systemType,jdbcType=INTEGER},
      </if>
      <if test="entId != null">
        #{entId,jdbcType=BIGINT},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="company != null">
        #{company,jdbcType=INTEGER},
      </if>
      <if test="parentId != null">
        #{parentId,jdbcType=BIGINT},
      </if>
      <if test="parentIds != null">
        #{parentIds,jdbcType=VARCHAR},
      </if>
      <if test="level != null">
        #{level,jdbcType=INTEGER},
      </if>
      <if test="sort != null">
        #{sort,jdbcType=INTEGER},
      </if>
      <if test="remark != null">
        #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        #{status,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        #{createBy,jdbcType=BIGINT},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        #{updateBy,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        #{delFlag,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>

  <update id="update" parameterType="com.sckw.system.model.KwsDept">
    update kws_dept
    <set>
      <if test="systemType != null">
        system_type = #{systemType,jdbcType=INTEGER},
      </if>
      <if test="entId != null">
        ent_id = #{entId,jdbcType=BIGINT},
      </if>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="company != null">
        company = #{company,jdbcType=INTEGER},
      </if>
      <if test="parentId != null">
        parent_id = #{parentId,jdbcType=BIGINT},
      </if>
      <if test="parentIds != null">
        parent_ids = #{parentIds,jdbcType=VARCHAR},
      </if>
      <if test="level != null">
        level = #{level,jdbcType=INTEGER},
      </if>
      <if test="sort != null">
        sort = #{sort,jdbcType=INTEGER},
      </if>
      <if test="remark != null">
        remark = #{remark,jdbcType=VARCHAR},
      </if>
      <if test="status != null">
        status = #{status,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        create_by = #{createBy,jdbcType=BIGINT},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=BIGINT},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        del_flag = #{delFlag,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="findList" resultType="com.sckw.system.model.vo.res.KwsDeptResVo" parameterType="com.sckw.system.model.KwsDept" >
    select
    sd.id, sd.system_type systemType, sd.ent_id entId, sd.name, sd.company, sd.parent_id parentId, sd.level, sd.sort, sd.remark,
    sd.status, u1.name createBy, sd.create_time createTime, u2.name updateBy, sd.update_time updateTime,
    sd2.name parentName
    from kws_dept sd
    left join kws_dept sd2 on sd.parent_id = sd2.id and sd2.del_flag=0
    left join kws_user u1 on sd.create_by = u1.id
    left join kws_user u2 on sd.update_by = u2.id
    where sd.del_flag = 0
    <if test="systemType != null and systemType != ''">
      and sd.system_type = #{systemType, jdbcType=VARCHAR}
    </if>
    <if test="name != null and name != ''">
      and sd.name like concat('%', #{name, jdbcType=VARCHAR}, '%')
    </if>
    <if test="entId != null and entId != ''">
      and sd.ent_id = #{entId, jdbcType=VARCHAR}
    </if>
    <if test="parentIds != null and parentIds != ''">
      and FIND_IN_SET(#{parentIds, jdbcType=VARCHAR}, parent_ids)
    </if>
    <if test="company != null">
      and sd.company = #{company}
    </if>
    <if test="parentId != null">
      and sd.parent_id = #{parentId}
    </if>
    ORDER BY level, sort
  </select>

  <select id="selectByKeys" resultType="com.sckw.system.model.KwsDept">
    select
    <include refid="Base_Column_List" />
    from kws_dept
    where del_flag = 0
      and id in
    <foreach collection="list" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </select>

  <select id="selectByEntIdList" resultType="com.sckw.system.model.KwsDept">
    select
    <include refid="Base_Column_List" />
    from kws_dept
    where del_flag = 0
    and ent_id in
    <foreach collection="list" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </select>

    <select id="findDeptUserByDeptIds" resultType="com.sckw.system.model.pojo.FindDeptUserPojo">
      select distinct b.id deptId,
             b.name deptName,
             d.id userId,
             d.name,
             d.account,
             d.phone
        from kws_dept b
        left join kws_user_dept c on b.id = c.dept_id
        left join kws_user d on c.user_id = d.id
       where b.del_flag = 0
         and c.del_flag = 0
         and d.del_flag = 0
         and b.id in
         <foreach collection="list" separator="," open="(" close=")" item="item">
           #{item}
         </foreach>
    </select>
</mapper>