<?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.KwsUserRoleDao">
  <resultMap id="BaseResultMap" type="com.sckw.system.model.KwsUserRole">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="user_id" jdbcType="BIGINT" property="userId" />
    <result column="role_id" jdbcType="BIGINT" property="roleId" />
    <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, user_id, role_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_user_role
    where id = #{id,jdbcType=BIGINT}
  </select>

  <select id="findAllByUserId" resultType="com.sckw.system.model.KwsUserRole">
    select * from kws_user_role where user_id = #{userId} and del_flag = 0 and status = 0
  </select>

    <select id="findRoleAllByUserIds" resultType="com.sckw.system.model.KwsUserRole">
        select * from kws_user_role
        <where>
            del_flag = 0 and status = 0
            <if test="userIds != null and userIds.size()>0">
                and user_id in
                <foreach collection="userIds" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>

            </if>
        </where>
    </select>

  <select id="findAllByRoleId" resultType="com.sckw.system.model.KwsUserRole">
    select * from kws_user_role where role_id = #{roleId} and del_flag = 0 and status = 0
  </select>

  <select id="findAllByRoleIds" resultType="com.sckw.system.model.KwsUserRole">
    select *
      from kws_user_role
     where del_flag = 0
       and status = 0
       and role_id in
        <foreach collection="list" separator="," open="(" close=")" item="item">
          #{item}
        </foreach>
  </select>

  <insert id="insert" parameterType="com.sckw.system.model.KwsUserRole">
    insert into kws_user_role
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="userId != null">
        user_id,
      </if>
      <if test="roleId != null">
        role_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="userId != null">
        #{userId,jdbcType=BIGINT},
      </if>
      <if test="roleId != null">
        #{roleId,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>

  <insert id="saveBatch" parameterType="com.sckw.system.model.KwsUserRole">
    insert into kws_user_role
    <trim prefix="(" suffix=")" suffixOverrides=",">
      id,
      user_id,
      role_id,
      remark,
      status,
      create_by,
      create_time,
      update_by,
      update_time,
      del_flag
    </trim>
    values
    <foreach collection="list" item="item" separator=",">
      <trim prefix="(" suffix=")" suffixOverrides=",">
        #{item.id,jdbcType=BIGINT},
        #{item.userId,jdbcType=BIGINT},
        #{item.roleId,jdbcType=BIGINT},
        #{item.remark,jdbcType=VARCHAR},
        #{item.status,jdbcType=INTEGER},
        #{item.createBy,jdbcType=BIGINT},
        #{item.createTime,jdbcType=TIMESTAMP},
        #{item.updateBy,jdbcType=BIGINT},
        #{item.updateTime,jdbcType=TIMESTAMP},
        #{item.delFlag,jdbcType=INTEGER}
      </trim>
    </foreach>
  </insert>

  <update id="update" parameterType="com.sckw.system.model.KwsUserRole">
    update kws_user_role
    <set>
      <if test="userId != null">
        user_id = #{userId,jdbcType=BIGINT},
      </if>
      <if test="roleId != null">
        role_id = #{roleId,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="findAllByUserIds" resultMap="BaseResultMap">
      select * from kws_user_role where del_flag = 0 and status = 0
      <if test="userIds != null and userIds.size() != 0">
          and user_id in
          <foreach collection="userIds" separator="," close=")" open="(" item="item">
      #{item}
          </foreach>
      </if>
  </select>

  <select id="queryRoleList" resultType="com.sckw.system.api.model.dto.res.RoleInfoDto">
      select kur.role_id,
             kr.name      roleName,
             kr.ent_id,
             ke.firm_name entName
      from kws_user_role kur
               left join kws_role kr on kur.role_id = kr.id and kr.del_flag = 0
               left join kws_enterprise ke on ke.id = kr.ent_id and ke.del_flag = 0
      <where>
          kur.del_flag = 0
            and kur.user_id = #{userId}
      </where>
      order by kr.create_time
  </select>
</mapper>
