<?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.KwsUserDao">
  <resultMap id="BaseResultMap" type="com.sckw.system.model.KwsUser">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="system_type" jdbcType="INTEGER" property="systemType" />
    <result column="account" jdbcType="VARCHAR" property="account" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="salt" jdbcType="VARCHAR" property="salt" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="photo" jdbcType="VARCHAR" property="photo" />
    <result column="email" jdbcType="VARCHAR" property="email" />
    <result column="client_id" jdbcType="VARCHAR" property="clientId" />
    <result column="is_main" jdbcType="INTEGER" property="isMain" />
    <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, account, password, name, phone, photo, email, client_id, is_main,
    remark, status, create_by, create_time, update_by, update_time, del_flag, salt
  </sql>

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

  <select id="selectByKeys"  resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from kws_user
    where del_flag = 0
      and id in
    <foreach collection="list" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </select>


  <insert id="insert" parameterType="com.sckw.system.model.KwsUser">
    insert into kws_user
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="systemType != null">
        system_type,
      </if>
      <if test="account != null">
        account,
      </if>
      <if test="password != null">
        password,
      </if>
      <if test="name != null">
        name,
      </if>
      <if test="phone != null">
        phone,
      </if>
      <if test="photo != null">
        photo,
      </if>
      <if test="email != null">
        email,
      </if>
      <if test="clientId != null">
        client_id,
      </if>
      <if test="isMain != null">
        is_main,
      </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>
      <if test="salt != null">
        salt,
      </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="account != null">
        #{account,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        #{password,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        #{phone,jdbcType=VARCHAR},
      </if>
      <if test="photo != null">
        #{photo,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        #{email,jdbcType=VARCHAR},
      </if>
      <if test="clientId != null">
        #{clientId,jdbcType=VARCHAR},
      </if>
      <if test="isMain != null">
        #{isMain,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>
      <if test="salt != null">
        #{salt,jdbcType=VARCHAR},
      </if>
    </trim>
  </insert>

  <update id="update" parameterType="com.sckw.system.model.KwsUser">
    update kws_user
    <set>
      <if test="systemType != null">
        system_type = #{systemType,jdbcType=INTEGER},
      </if>
      <if test="account != null">
        account = #{account,jdbcType=VARCHAR},
      </if>
      <if test="password != null">
        password = #{password,jdbcType=VARCHAR},
      </if>
      <if test="name != null">
        name = #{name,jdbcType=VARCHAR},
      </if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},
      </if>
      <if test="photo != null">
        photo = #{photo,jdbcType=VARCHAR},
      </if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},
      </if>
      <if test="clientId != null">
        client_id = #{clientId,jdbcType=VARCHAR},
      </if>
      <if test="isMain != null">
        is_main = #{isMain,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>
      <if test="salt != null">
        salt = #{salt,jdbcType=VARCHAR},
      </if>
    </set>
    where id = #{id,jdbcType=BIGINT}
  </update>

  <select id="findPage" resultType="com.sckw.system.model.vo.res.KwsUserResVo" parameterType="java.util.Map" >
    select distinct
    su.id, su.system_type systemType, su.account, su.password, su.name,
    su.phone, su.photo, su.email, su.is_main isMain, su.remark, su.status, su.salt,
    c.id deptId,c.name deptName,e.id roleId, e.name roleName,
    su.create_time,
    su2.`name` createByName,
    su.update_time
    from kws_user su
    left join kws_user_dept b on su.id = b.user_id
    left join kws_dept c on b.dept_id = c.id
    left join kws_user_role d on su.id = d.user_id
    left join kws_role e on e.id = d.role_id
    left join kws_user su2 on su.create_by = su2.id
    where su.del_flag = 0
      and b.del_flag = 0
      and d.del_flag = 0
    <if test="systemType != null and systemType != ''">
      and su.system_type = #{system_type, jdbcType=VARCHAR}
    </if>
    <if test="account != null and account != ''">
      and su.account = #{account, jdbcType=VARCHAR}
    </if>
    <if test="name != null and name != ''">
      and su.name = #{name, jdbcType=VARCHAR}
    </if>
    <if test="phone != null and phone != ''">
      and su.phone = #{phone, jdbcType=VARCHAR}
    </if>
    <if test="email != null and email != ''">
      and su.email = #{email, jdbcType=VARCHAR}
    </if>
    <if test="deptId != null and deptId != ''">
      and c.id = #{deptId, jdbcType=VARCHAR}
    </if>
      <if test="roleId != null and roleId != ''">
        and e.id = #{roleId}
      </if>
      <if test="keywords != null and keywords != ''">
        and (su.name like concat('%', #{keywords}, '%') or su.account like concat('%', #{keywords}, '%') or su2.name like concat('%', #{keywords}, '%'))
      </if>
    <if test="entId != null and entId != ''">
      and c.ent_id = #{entId}
    </if>
    <if test="startTime != null and startTime != ''">
      and su.create_time >= #{startTime}
    </if>
    <if test="endTime != null and endTime != ''">
      and su.create_time &lt; date_add(#{endTime}, INTERVAL 1 DAY)
    </if>
    ORDER BY su.create_time desc
  </select>

  <select id="findList" resultType="com.sckw.system.model.vo.res.KwsUserResVo" parameterType="com.sckw.system.model.KwsUser" >
    select
      su.id, su.system_type systemType, su.account, su.password, su.name,
      su.phone, su.photo, su.email, su.is_main isMain, su.remark, su.status, su.salt,
      c.id deptId,c.name deptName,e.id roleId, e.name roleName,
      u1.name createByName, su.create_time createTime, su.update_time updateTime
    from kws_user su
    left join kws_user_dept b on su.id = b.user_id
    left join kws_dept c on b.dept_id = c.id
    left join kws_user_role d on su.id = d.user_id
    left join kws_role e on e.id = d.role_id
    left join kws_user u1 on su.create_by = u1.id
    where su.del_flag = 0
    <if test="systemType != null and systemType != ''">
      and su.system_type = #{systemType, jdbcType=INTEGER}
    </if>
    <if test="account != null and account != ''">
      and su.account = #{account, jdbcType=VARCHAR}
    </if>
    <if test="name != null and name != ''">
      and su.name = #{name, jdbcType=VARCHAR}
    </if>
    <if test="phone != null and phone != ''">
      and su.phone = #{phone, jdbcType=VARCHAR}
    </if>
    ORDER BY su.create_time desc
  </select>

  <select id="queryUserDeptByUserId" resultType="com.sckw.system.model.KwsUserDept">
    select a.* from kws_user_dept a where a.user_id = #{userId} and a.del_flag = 0
  </select>

  <select id="queryDeptByIds" resultType="com.sckw.system.model.KwsDept">
    select a.*
      from kws_dept a
     where a.id in
     <foreach collection="list" item="item" open="(" close=")" separator=",">
        #{item}
     </foreach>
  </select>
  <select id="selectByEntId" resultType="com.sckw.system.model.KwsUser">
    select distinct c.*
      from kws_dept a
      left join kws_user_dept b on a.id = b.dept_id
      left join kws_user c on b.user_id = c.id
     where a.ent_id = #{entId}
       and a.del_flag = 0
       and b.del_flag = 0
       and c.del_flag = 0
  </select>

  <select id="selectByRoleIds" resultType="com.sckw.system.model.KwsUser">
    select distinct a.*
      from kws_user a
      left join kws_user_role b on a.id = b.user_id
     where a.del_flag = 0
       and b.del_flag = 0
      and b.role_id in
     <foreach collection="roleIdList" item="item" open="(" close=")" separator=",">
       #{item}
     </foreach>
  </select>

  <select id="getUserByName" resultType="com.sckw.system.model.KwsUser">
    select a.*
      from kws_user a
     where a.del_flag = 0
       and a.name like concat('%', #{name}, '%')
  </select>

</mapper>