<?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="ent_id" jdbcType="BIGINT" property="entId"/>
        <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>
    <resultMap id="EnterpriseVOMap" type="com.sckw.system.model.vo.res.KwsUserResVo">
        <result column="id" property="id"/>
        <result column="remark" property="remark"/>
        <result column="system_type" property="systemType"/>
        <result column="account" property="account"/>
        <result column="name" property="name"/>
        <result column="phone" property="phone"/>
        <result column="photo" property="photo"/>
        <result column="email" property="email"/>
        <result column="ent_id" property="entId"/>
        <result column="entName" property="entName"/>
        <result column="status" property="status"/>
        <result column="createByName" property="createByName"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="roleIdList" property="roleIdList" typeHandler="com.sckw.core.handler.LongListTypeHandler"/>
        <result column="roleNameList" property="roleNameList"
                typeHandler="com.sckw.core.handler.StringListTypeHandler"/>
    </resultMap>
    <sql id="Base_Column_List">
        id,
        ent_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="selectByPhone" parameterType="java.lang.String" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from kws_user
        where phone = #{phone, jdbcType=VARCHAR}
    </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>


    <select id="findPage" resultMap="EnterpriseVOMap" parameterType="java.util.Map">
        select su.id,
               su.ent_id,
               ke.firm_name         entName,
               su.system_type,
               su.account,
               su.password,
               su.name,
               su.phone,
               su.photo,
               su.email,
               su.is_main           isMain,
               su.remark,
               su.status,
               group_concat(e.id)   roleIdList,
               group_concat(e.name) roleNameList,
               su.create_time,
               su2.`name`           createByName,
               su.update_time
        from kws_user su
                 left join kws_user_role d on su.id = d.user_id and d.del_flag = 0
                 left join kws_role e on e.id = d.role_id and e.del_flag = 0
                 left join kws_user su2 on su.create_by = su2.id and su2.del_flag = 0
                 left join kws_enterprise ke on ke.id = su.ent_id and ke.del_flag = 0
        where su.del_flag = 0
        <if test="systemType != null and systemType != ''">
            and su.system_type = #{systemType, jdbcType=VARCHAR}
        </if>
        <if test="idList != null and idList.size() > 0">
            and su.id in
            <foreach collection="idList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="entList != null and entList.size() > 0">
            and su.ent_id in
            <foreach collection="entList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </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="isMain != null">
            and su.is_main = #{isMain}
        </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 su.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>
        group by su.id,
                 su.ent_id,
                 ke.firm_name,
                 su.system_type,
                 su.account,
                 su.password,
                 su.name,
                 su.phone,
                 su.photo,
                 su.email,
                 su.is_main,
                 su.remark,
                 su.status, su.create_time,
                 su2.`name`,
                 su.update_time
        ORDER BY su.create_time desc
    </select>

    <select id="findList"
            parameterType="com.sckw.system.model.KwsUser" resultType="com.sckw.system.model.vo.res.KwsUserResVo">
        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,
               su.ent_id      entId,
               u1.name        createByName,
               su.create_time createTime,
               su.update_time updateTime
        from kws_user su
                 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="isMain != null ">
            and su.is_main = #{isMain}
        </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="userName != null and userName != ''">
            and su.name like concat('%', #{userName}, '%')
        </if>
        <if test="phone != null and phone != ''">
            and su.phone = #{phone, jdbcType=VARCHAR}
        </if>
        <choose>
            <when test="entIds != null and entIds != '' and entIds.size() > 0">
                and su.ent_id in
                <foreach collection="entIds" item="id" open="(" close=")" separator=",">
                    #{id,jdbcType=BIGINT}
                </foreach>
            </when>
        </choose>
        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>

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

    <select id="selectUserBySystemType" resultType="com.sckw.system.model.vo.res.KwsUserSystemTypeVo">
        select u.id          AS id,
               u.system_type AS systemType,
               u.account     AS account,
               u.name        AS name,
               u.phone       AS phone,
               u.is_main     as isMain
        from kws_user AS u
        where u.del_flag = 0
          and u.status = 0
          and u.system_type = #{systemType}
    </select>

    <select id="selectUserBySystemTypeAndMain" resultType="com.sckw.system.model.vo.res.KwsUserSystemTypeVo">
        select u.id          AS id,
               u.system_type AS systemType,
               u.account     AS account,
               u.name        AS name,
               u.phone       AS phone,
               u.is_main     as isMain
        from kws_user AS u
        where u.del_flag = 0
          and u.status = 0
          and u.system_type = #{systemType}
          and u.is_main = #{isMain}
    </select>

    <select id="selectManager" resultMap="BaseResultMap">
        select a.*
        from kws_user a
        where a.del_flag = 0
          and a.status = 0
          and a.is_main = 0
    </select>

    <select id="queryUser" resultType="com.sckw.system.model.vo.res.KwsUserResVo">
        select su.id,
               su.system_type,
               su.account,
               su.password,
               su.name,
               su.phone,
               su.photo,
               su.email,
               su.is_main,
               su.remark,
               su.status,
               su.salt,
               su.ent_id,
               su.driver_id,
               u1.name createByName,
               su.create_time,
               su.update_time
        from kws_user su
                 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>
    </select>
</mapper>
