<?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.KwsRoleDao">
  <resultMap id="BaseResultMap" type="com.sckw.system.model.KwsRole">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="dept_id" jdbcType="BIGINT" property="deptId" />
    <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, name, dept_id, 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_role
    where id = #{id,jdbcType=BIGINT}
  </select>

  <insert id="insert" parameterType="com.sckw.system.model.KwsRole">
    insert into kws_role
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="deptId != null">
        dept_id,
      </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="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="deptId != null">
        #{deptId,jdbcType=BIGINT},
      </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.KwsRole">
    update kws_role
    <set>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="deptId != null">
        dept_id = #{deptId,jdbcType=BIGINT},
      </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="findPage" resultType="com.sckw.system.model.vo.res.RoleResVo" parameterType="java.util.Map" >
    select
    sr.id,
    sr.name,
    sr.dept_id,
    sr.remark,
    sr.status,
    su.name createBy,
    sr.create_time,
    sr.update_by,
    sr.update_time,
    sr.del_flag
    from kws_role sr
    left join kws_user su on sr.create_by = su.id
    left join kws_dept d on sr.dept_id = d.id
    where sr.del_flag = 0
      and su.del_flag = 0
      and sr.id != #{manageRoleId}
    <if test="name != null and name != ''">
      and sr.name like concat('%', #{name, jdbcType=VARCHAR}, '%')
    </if>
    <if test="deptId != null and deptId != ''">
      and sr.deptId = #{deptId, jdbcType=VARCHAR}
    </if>
    <if test="entId != null and entId != ''">
      and d.ent_id = #{entId, jdbcType=VARCHAR}
    </if>
    <if test="startTime != null and startTime != ''">
      and sr.create_time >= #{startTime,jdbcType=DATE}
    </if>
    <if test="endTime != null and endTime != ''" >
      and sr.create_time &lt; date_add(#{endTime}, INTERVAL 1 DAY)
    </if>
    <if test="idList != null and idList.size() > 0">
        and sr.id in
      <foreach collection="idList" item="item" open="(" close=")" separator=",">
        #{item}
      </foreach>
    </if>
    ORDER BY sr.create_time desc
  </select>

  <select id="queryRoleByUserId" resultType="com.sckw.system.model.KwsRole">
    select b.*
      from kws_user_role a
      left join kws_role b on a.role_id = b.id
      where a.del_flag = 0
        and b.del_flag = 0
        and a.user_id = #{userId}
  </select>

  <select id="selectBatchByKeys" resultType="com.sckw.system.model.KwsRole">
    select a.*
      from kws_role a
     where a.id in
        <foreach collection="list" item="item" open="(" close=")" separator=",">
          #{item}
        </foreach>
    and a.del_flag = 0
  </select>

    <select id="selectBatchByUnitIds" resultType="com.sckw.system.model.KwsRole">
      select a.*
        from kws_role a
       where a.dept_id in
      <foreach collection="list" item="item" open="(" close=")" separator=",">
        #{item}
      </foreach>
      and a.del_flag = 0
    </select>

</mapper>