<?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.fs.company.mapper.CompanyUserMapper">

    <resultMap type="CompanyUser" id="CompanyUserResult">
        <result property="userId"    column="user_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userName"    column="user_name"    />
        <result property="nickName"    column="nick_name"    />
        <result property="userType"    column="user_type"    />
        <result property="email"    column="email"    />
        <result property="phonenumber"    column="phonenumber"    />
        <result property="sex"    column="sex"    />
        <result property="avatar"    column="avatar"    />
        <result property="idCard"    column="id_card"    />
        <result property="password"    column="password"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="loginIp"    column="login_ip"    />
        <result property="loginDate"    column="login_date"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="token"    column="token"    />
        <result property="remark"    column="remark"    />
        <result property="openId"    column="open_id"    />
        <result property="qrCodeWeixin"    column="qr_code_weixin"    />
        <result property="qrCodeWecom"    column="qr_code_wecom"    />
        <result property="jpushId"    column="jpush_id"    />
        <result property="firstchar"    column="firstchar"    />
        <result property="postName"    column="post_name"    />
        <result property="deptName"    column="dept_name"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="qwStatus"    column="qw_status"    />
        <result property="domain"    column="domain"    />
        <result property="isAudit"    column="is_audit"    />
        <result property="addressId"    column="address_id"    />
        <association property="dept"    column="dept_id" javaType="CompanyDept" resultMap="deptResult" />
        <collection  property="roles"   javaType="java.util.List"        resultMap="RoleResult" />

    </resultMap>

    <resultMap id="deptResult" type="CompanyDept">
        <id     property="deptId"   column="dept_id"     />
        <result property="parentId" column="parent_id"   />
        <result property="deptName" column="dept_name"   />
        <result property="orderNum" column="order_num"   />
        <result property="leader"   column="leader"      />
        <result property="status"   column="dept_status" />
    </resultMap>

    <resultMap id="RoleResult" type="CompanyRole">
        <id     property="roleId"       column="role_id"        />
        <result property="roleName"     column="role_name"      />
        <result property="roleKey"      column="role_key"       />
        <result property="roleSort"     column="role_sort"      />
        <result property="dataScope"     column="data_scope"    />
        <result property="status"       column="role_status"    />
    </resultMap>



    <select id="selectCompanyUserList" parameterType="CompanyUser" resultMap="CompanyUserResult">
        select u.*, d.dept_name, d.leader from company_user u
        left join company_dept d on u.dept_id = d.dept_id
        where u.del_flag = '0'
        <if test="userName != null and userName != ''">
            AND u.user_name like concat('%', #{userName}, '%')
        </if>

        <if test="nickName != null and nickName != ''">
            AND u.nick_name like concat('%', #{nickName}, '%')
        </if>
        <if test="companyId != null and companyId != ''">
            AND u.company_id = #{companyId}
        </if>
        <if test="status != null and status != ''">
            AND u.status = #{status}
        </if>
        <if test="phonenumber != null and phonenumber != ''">
            AND u.phonenumber like concat('%', #{phonenumber}, '%')
        </if>
        <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
            AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        <if test="deptId != null and deptId != 0">
            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{deptId}, ancestors) ))
        </if>
        <!-- 数据范围过滤 -->
        ${params.dataScope}
    </select>



    <select id="selectCompanyUserById" parameterType="Long" resultMap="CompanyUserResult">
        <include refid="selectUserVo"/>
        where u.user_id = #{userId}
    </select>

    <insert id="insertCompanyUser" parameterType="CompanyUser" useGeneratedKeys="true" keyProperty="userId">
        insert into company_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="userName != null and userName != ''">user_name,</if>
            <if test="nickName != null and nickName != ''">nick_name,</if>
            <if test="userType != null">user_type,</if>
            <if test="email != null">email,</if>
            <if test="phonenumber != null">phonenumber,</if>
            <if test="sex != null">sex,</if>
            <if test="avatar != null">avatar,</if>
            <if test="idCard != null">id_card,</if>
            <if test="password != null">password,</if>
            <if test="status != null">status,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="loginIp != null">login_ip,</if>
            <if test="loginDate != null">login_date,</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="token != null">token,</if>
            <if test="remark != null">remark,</if>
            <if test="openId != null">open_id,</if>
            <if test="qrCodeWeixin != null">qr_code_weixin,</if>
            <if test="qrCodeWecom != null">qr_code_wecom,</if>
            <if test="jpushId != null">jpush_id,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="qwStatus != null">qw_status,</if>
            <if test="domain != null">`domain`,</if>
            <if test="isAudit != null">`is_audit`,</if>
            <if test="addressId != null">`address_id`,</if>
            <if test="maOpenId != null">`ma_open_id`,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="userName != null and userName != ''">#{userName},</if>
            <if test="nickName != null and nickName != ''">#{nickName},</if>
            <if test="userType != null">#{userType},</if>
            <if test="email != null">#{email},</if>
            <if test="phonenumber != null">#{phonenumber},</if>
            <if test="sex != null">#{sex},</if>
            <if test="avatar != null">#{avatar},</if>
            <if test="idCard != null">#{idCard},</if>
            <if test="password != null">#{password},</if>
            <if test="status != null">#{status},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="loginIp != null">#{loginIp},</if>
            <if test="loginDate != null">#{loginDate},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="token != null">#{token},</if>
            <if test="remark != null">#{remark},</if>
            <if test="openId != null">#{openId},</if>
            <if test="qrCodeWeixin != null">#{qrCodeWeixin},</if>
            <if test="qrCodeWecom != null">#{qrCodeWecom},</if>
            <if test="jpushId != null">#{jpushId},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="qwStatus != null">#{qwStatus},</if>
            <if test="domain != null">#{domain},</if>
            <if test="isAudit != null">#{isAudit},</if>
            <if test="addressId != null">#{addressId},</if>
            <if test="maOpenId != null">#{maOpenId},</if>
        </trim>
    </insert>

    <update id="updateCompanyUser" parameterType="CompanyUser">
        update company_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="email != null">email = #{email},</if>
            <if test="phonenumber != null">phonenumber = #{phonenumber},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="password != null and password != '' ">password = #{password},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="loginIp != null">login_ip = #{loginIp},</if>
            <if test="loginDate != null">login_date = #{loginDate},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="token != null">token = #{token},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="qrCodeWeixin != null">qr_code_weixin = #{qrCodeWeixin},</if>
            <if test="qrCodeWecom != null">qr_code_wecom = #{qrCodeWecom},</if>
            <if test="jpushId != null">jpush_id = #{jpushId},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="qwStatus != null">qw_status = #{qwStatus},</if>
            <if test="domain != null">`domain` = #{domain},</if>
            <if test="isAudit != null">`is_audit` = #{isAudit},</if>
            <if test="addressId != null">`address_id` = #{addressId},</if>
            <if test="maOpenId != null">`ma_open_id` = #{maOpenId},</if>
        </trim>
        where user_id = #{userId}
    </update>

    <update id="updateAllCompanyUser" parameterType="CompanyUser">
        update company_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
            <if test="email != null">email = #{email},</if>
            <if test="phonenumber != null">phonenumber = #{phonenumber},</if>
            <if test="sex != null">sex = #{sex},</if>
            <if test="avatar != null">avatar = #{avatar},</if>
            <if test="idCard != null">id_card = #{idCard},</if>
            <if test="password != null and password != '' ">password = #{password},</if>
            <if test="status != null">status = #{status},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="loginIp != null">login_ip = #{loginIp},</if>
            <if test="loginDate != null">login_date = #{loginDate},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="token != null">token = #{token},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="qrCodeWeixin != null">qr_code_weixin = #{qrCodeWeixin},</if>
            <if test="qrCodeWecom != null">qr_code_wecom = #{qrCodeWecom},</if>
            <if test="jpushId != null">jpush_id = #{jpushId},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="qwStatus != null">qw_status = #{qwStatus},</if>
            <if test="domain != null">`domain` = #{domain},</if>
            <if test="isAudit != null">`is_audit` = #{isAudit},</if>
            <if test="addressId != null">`address_id` = #{addressId},</if>
            <if test="maOpenId != null">`ma_open_id` = #{maOpenId},</if>
        </trim>
        where company_id = #{companyId}
    </update>


    <delete id="deleteCompanyUserById" parameterType="Long">
        delete from company_user where user_id = #{userId}
    </delete>

    <delete id="deleteCompanyUserByIds" parameterType="String">
        delete from company_user where user_id in
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <update id="updateUserAvatar" parameterType="SysUser">
        update company_user set avatar = #{avatar} where user_name = #{userName}
    </update>

    <update id="resetUserPwd" parameterType="SysUser">
        update company_user set password = #{password} where user_name = #{userName}
    </update>


    <select id="selectUserByUserName" parameterType="String" resultMap="CompanyUserResult">
        <include refid="selectUserVo"/>
        where u.user_name = #{userName}
    </select>
    <sql id="selectUserVo">
        select u.user_id,u.company_id,u.qw_user_id, u.dept_id, u.user_name, u.nick_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time,u.id_card, u.remark,u.user_type,u.open_id,u.qr_code_weixin,u.qr_code_wecom,u.jpush_id,u.domain,u.is_audit,u.address_id,
               d.dept_id, d.parent_id, d.dept_name, d.order_num, d.leader, d.status as dept_status,
               r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope, r.status as role_status
        from company_user u
                 left join company_dept d on u.dept_id = d.dept_id
                 left join company_user_role ur on u.user_id = ur.user_id
                 left join company_role r on r.role_id = ur.role_id
    </sql>


    <select id="selectMyUserList" parameterType="com.fs.company.vo.CompanyUserVO" resultMap="CompanyUserVOResult">
        select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,u.open_id,u.id_card, d.dept_name, d.leader, u.is_audit
        from company_user u
        left join company_dept d on u.dept_id = d.dept_id
        where u.del_flag = '0' and u.status=0
        <if test="companyId != null  ">
            AND u.company_id = #{companyId}
        </if>
        <if test="nickName != null and nickName != ''">
            AND u.nick_name like concat('%', #{nickName}, '%')
        </if>
        <if test="phonenumber != null and phonenumber != ''">
            AND u.phonenumber like concat('%', #{phonenumber}, '%')
        </if>
        <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
            AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        <if test="deptId != null and deptId != 0">
            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{deptId}, ancestors) ))
        </if>
        <if test="isAudit != null">
            AND u.is_audit = #{isAudit}
        </if>

    </select>

    <select id="selecCompanyAllUsersList" parameterType="com.fs.company.vo.CompanyUserVO" resultMap="CompanyUserVOResult">
        select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,u.open_id,u.id_card, d.dept_name, d.leader ,
        (select count(1) from crm_customer cc where u.user_id= cc.receive_user_id and date_format(cc.receive_time,'%y%m%d') = date_format(now(),'%y%m%d')  ) as now_day_customer_count
        from company_user u
        left join company_dept d on u.dept_id = d.dept_id

        where u.del_flag = '0' and u.status=0
        <if test="companyId != null  ">
            AND u.company_id = #{companyId}
        </if>
        <if test="nickName != null and nickName != ''">
            AND u.nick_name like concat('%', #{nickName}, '%')
        </if>
        <if test="phonenumber != null and phonenumber != ''">
            AND u.phonenumber like concat('%', #{phonenumber}, '%')
        </if>
        <if test="beginTime != null and beginTime != ''"><!-- 开始时间检索 -->
            AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')
        </if>
        <if test="endTime != null and endTime != ''"><!-- 结束时间检索 -->
            AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')
        </if>
        <if test="deptId != null and deptId != 0">
            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{deptId}, ancestors) ))
        </if>

    </select>

    <select id="selectCompanyQwUserByIds" resultType="com.fs.company.vo.CompanyQwUserByIdsVo">
        SELECT b.user_id,b.company_id,a.qw_user_id,a.qw_user_name,b.nick_name
        FROM qw_user a inner join company_user b on a.company_user_id = b.user_id
        where a.id in <foreach collection="ids" open="(" close=")" separator="," item="item">#{item}</foreach>
        and b.voice_print_url is not null
    </select>

    <select id="selectCompanyUserByIds" resultType="com.fs.company.domain.CompanyUser">
        select cu.*
        from company_user cu
        where cu.user_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>

    </select>


    <resultMap type="com.fs.company.vo.CompanyUserVO" id="CompanyUserVOResult">
        <result property="userId"    column="user_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="userName"    column="user_name"    />
        <result property="nickName"    column="nick_name"    />
        <result property="userType"    column="user_type"    />
        <result property="email"    column="email"    />
        <result property="phonenumber"    column="phonenumber"    />
        <result property="sex"    column="sex"    />
        <result property="avatar"    column="avatar"    />
        <result property="idCard"    column="id_card"    />
        <result property="password"    column="password"    />
        <result property="status"    column="status"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="loginIp"    column="login_ip"    />
        <result property="loginDate"    column="login_date"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="token"    column="token"    />
        <result property="remark"    column="remark"    />
        <result property="openId"    column="open_id"    />
        <result property="nowDayCustomerCount"    column="now_day_customer_count"    />
        <result property="domain"    column="domain"    />
        <result property="isAudit"    column="is_audit"    />
        <result property="addressId"    column="address_id"    />
        <association property="dept"    column="dept_id" javaType="CompanyDept" resultMap="deptResult" />
        <collection  property="roles"   javaType="java.util.List"        resultMap="RoleResult" />
        <collection property="posts" javaType="java.util.ArrayList" ofType="com.fs.company.domain.CompanyPost"
                    select="com.fs.company.mapper.CompanyPostMapper.selectPostsByUserId" column="{user_id=user_id}" />

    </resultMap>



    <select id="selectAllCompanyUserByParentId" resultType="CompanyUser">
        SELECT
            *
        FROM
            company_user
        WHERE
            del_flag = 0
          AND parent_id = #{parentId}
    </select>
    <select id="getAllUserListLimit" resultType="com.fs.company.domain.CompanyUser">
        SELECT * FROM company_user
        <where>
            <if test="companyId != null">
                AND company_id != #{companyId}
            </if>
            <if test="keywords != null and keywords != ''">
                AND (
                phonenumber LIKE CONCAT(#{keywords}, '%')
                OR user_name LIKE CONCAT(#{keywords}, '%')
                OR nick_name LIKE CONCAT(#{keywords}, '%')
                )
            </if>
        </where>
        limit 10
    </select>

    <update id="auditUsers">
        update company_user
        set is_audit = 1
        where user_id in
        <foreach collection="userIds" open="(" close=")" separator="," item="id">
            #{id}
        </foreach>
    </update>

    <select id="selectAllCompanyUserAndSelf" resultType="CompanyUser">
        SELECT
            *
        FROM
            company_user
        WHERE
            del_flag = 0
          AND (company_user.user_id = #{userId} or company_user.parent_id = #{userId})
    </select>
    <select id="selectQwWatchLogStatisticsListVONewCount" resultType="java.lang.Long">

    </select>

    <select id="selectCompanyUserListByMap" resultType="com.fs.his.vo.OptionsVO">
        select
        cu.user_id dictValue,
        cu.nick_name dictLabel
        from company_user cu
        <where>
            <if test="params.companyUserName != null and params.companyUserName != ''">
                and cu.nick_name like concat('%', #{params.companyUserName}, '%')
            </if>
            <if test="params.companyId != null">
                and cu.company_id = #{params.companyId}
            </if>
        </where>
    </select>
    <select id="selectUserAllCompanyUserId" resultType="java.lang.Long">
        select distinct user_id from company_user where parent_id=${companyUserId}
    </select>
    <select id="selectCompanyUserNameUserById" resultType="java.lang.String">
        select concat(nick_name,'_',user_name) from company_user where user_id=${userId} limit 1
    </select>

</mapper>
