<?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.hzs.system.sys.mapper.SysUserMapper">

    <resultMap type="com.hzs.common.domain.system.sys.SysUser" id="SysUserResult">
        <id property="userId" column="user_id"/>
        <result property="userName" column="user_name"/>
        <result property="nickName" column="nick_name"/>
        <result property="phonenumber" column="phonenumber"/>
        <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="pkCreator" column="PK_CREATOR"/>
        <result property="creationTime" column="CREATION_TIME"/>
        <result property="pkModified" column="PK_MODIFIED"/>
        <result property="modifiedTime" column="MODIFIED_TIME"/>
        <result property="pkCountry" column="PK_COUNTRY"/>
        <result property="authority" column="AUTHORITY"/>
        <result property="sms" column="SMS"/>
        <result property="roleIds" column="ROLE_IDS"/>
        <result property="countryIds" column="COUNTRY_IDS"/>
        <result property="vertexIds" column="VERTEX_IDS"/>
        <result property="teamIds" column="TEAM_IDS"/>
        <result property="pkDept" column="PK_DEPT"/>
        <result property="deptName" column="DEPT_NAME"/>
        <result property="headPath" column="HEAD_PATH"/>
        <collection property="roleList" javaType="java.util.List" resultMap="RoleResult"/>
    </resultMap>

    <resultMap id="RoleResult" type="com.hzs.common.domain.system.sys.SysRole">
        <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>


    <resultMap id="RoleNoticeResult" type="com.hzs.common.domain.system.sys.SysRole">
        <id property="roleId" column="role_id"/>
        <result property="roleName" column="role_name"/>
        <collection property="sysUserList" javaType="java.util.List" ofType="com.hzs.common.domain.system.sys.SysUser">
            <id property="userId" column="user_id"/>
            <result property="userName" column="user_name"/>
        </collection>
    </resultMap>

    <select id="selectSysUserRole" resultMap="RoleNoticeResult">
        select u.user_id, u.nick_name, r.role_id, r.role_name
        from sys_role r
        left join sys_user_role ur
        on r.role_id = ur.role_id
        left join sys_user u
        on u.user_id = ur.user_id
        where r.del_flag = 0
        and u.del_flag = 0
        <if test="pkCountry != null">
            and u.pk_country = #{pkCountry}
        </if>
    </select>


    <sql id="selectUserVo">
        select u.user_id,
               u.user_name,
               u.nick_name,
               u.phonenumber,
               u.password,
               u.status,
               u.DEL_FLAG,
               u.login_ip,
               u.login_date,
               u.PK_CREATOR,
               u.CREATION_TIME,
               u.PK_MODIFIED,
               u.MODIFIED_TIME,
               u.PK_COUNTRY,
               u.AUTHORITY,
               u.SMS,
               u.ROLE_IDS,
               u.COUNTRY_IDS,
               u.VERTEX_IDS,
               u.TEAM_IDS,
               u.PK_DEPT,
               u.HEAD_PATH,
               r.role_id,
               r.role_name,
               r.role_key,
               r.role_sort,
               r.data_scope,
               r.status as role_status,
               d.dept_name
        from sys_user u
                 left join sys_user_role ur on u.user_id = ur.user_id
                 left join sys_role r on r.role_id = ur.role_id
                 left join sys_dept d on d.dept_id = u.pk_dept
    </sql>

    <!-- 根据条件分页查询用户列表 -->
    <select id="selectUserList" resultType="com.hzs.system.sys.vo.SysUserQueryVO">
        select su.*, sd.dept_name
        from sys_user su
        left join sys_dept sd
        on sd.dept_id = su.pk_dept
        and sd.del_flag = 0
        <if test="param.pkCountry != null">
            left join sys_user_country suc
            on suc.user_id = su.user_id
        </if>
        <if test="param.pkRole != null">
            left join sys_user_role sur
            on sur.user_id = su.user_id
        </if>
        where su.del_flag = 0
        <if test="param.pkCountry != null">
            and suc.pk_country = #{param.pkCountry}
        </if>
        <if test="param.pkRole != null">
            and sur.role_id = #{param.pkRole}
        </if>
        <if test="param.nickName != null and param.nickName != ''">
            AND su.NICK_NAME like #{param.nickName} || '%'
        </if>
        <if test="param.userName != null and param.userName != ''">
            AND su.user_name like #{param.userName} || '%'
        </if>
        <if test="param.phonenumber != null and param.phonenumber != ''">
            AND su.phonenumber like #{param.phonenumber} || '%'
        </if>
        <if test="param.sms != null ">
            AND su.sms = #{param.sms}
        </if>
        <if test="param.authority != null ">
            AND su.authority = #{param.authority}
        </if>
        <if test="param.status != null ">
            AND su.status = #{param.status}
        </if>
        <if test="param.creationTime != null and param.creationTime.length > 1">
            and su.creation_time >= #{param.creationTime[0]}
            and #{param.creationTime[1]} >= sd.creation_time
        </if>
        <if test="param.pkDept != null ">
            AND su.PK_DEPT = #{param.pkDept}
        </if>
        order by su.creation_time desc
    </select>

    <select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.user_name = #{userName}
    </select>

    <select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
        <include refid="selectUserVo"/>
        where u.user_id = #{userId}
        and u.del_flag = 0
    </select>

    <insert id="insertUser" parameterType="SysUser" useGeneratedKeys="true" keyProperty="userId">
        <selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="userId">
            SELECT SYS_USER_USER_ID_SEQ.nextval AS userId FROM dual
        </selectKey>
        insert into sys_user(
        <if test="userId != null and userId != 0">user_id,</if>
        <if test="userName != null and userName != ''">user_name,</if>
        <if test="nickName != null and nickName != ''">nick_name,</if>
        <if test="phonenumber != null and phonenumber != ''">phonenumber,</if>
        <if test="password != null and password != ''">password,</if>
        <if test="status != null and status != ''">status,</if>
        <if test="authority != null">authority,</if>
        <if test="sms != null">sms,</if>
        <if test="roleIds != null and roleIds != ''">role_ids,</if>
        <if test="countryIds != null and countryIds != ''">country_ids,</if>
        <if test="vertexIds != null and vertexIds != ''">vertex_ids,</if>
        <if test="teamIds != null and teamIds != ''">team_ids,</if>
        <if test="pkDept != null and pkDept != ''">pk_dept,</if>
        <if test="pkCountry != null">PK_COUNTRY,</if>
        <if test="pkCreator != null">PK_CREATOR,</if>
        CREATION_TIME
        )values(
        <if test="userId != null and userId != ''">#{userId},</if>
        <if test="userName != null and userName != ''">#{userName},</if>
        <if test="nickName != null and nickName != ''">#{nickName},</if>
        <if test="phonenumber != null and phonenumber != ''">#{phonenumber},</if>
        <if test="password != null and password != ''">#{password},</if>
        <if test="status != null and status != ''">#{status},</if>
        <if test="authority != null">#{authority},</if>
        <if test="sms != null">#{sms},</if>
        <if test="roleIds != null and roleIds != ''">#{roleIds},</if>
        <if test="countryIds != null and countryIds != ''">#{countryIds},</if>
        <if test="vertexIds != null and vertexIds != ''">#{vertexIds},</if>
        <if test="teamIds != null and teamIds != ''">#{teamIds},</if>
        <if test="pkDept != null and pkDept != ''">#{pkDept},</if>
        <if test="pkCountry != null">#{pkCountry},</if>
        <if test="pkCreator != null">#{pkCreator},</if>
        sysdate
        )
    </insert>

    <!-- 修改用户信息 -->
    <update id="updateUser" parameterType="SysUser">
        update sys_user
        <set>
            <if test="userName != null and userName != ''">user_name = #{userName},</if>
            <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
            <if test="phonenumber != null ">phonenumber = #{phonenumber},</if>
            <if test="password != null and password != ''">password = #{password},</if>
            <if test="status != null">status = #{status},</if>
            <if test="authority != null">authority = #{authority},</if>
            <if test="sms != null">sms = #{sms},</if>
            <if test="roleIds != null ">role_ids = #{roleIds},</if>
            <if test="countryIds != null ">country_ids = #{countryIds},</if>
            <if test="vertexIds != null ">vertex_ids = #{vertexIds},</if>
            <if test="teamIds != null ">team_ids = #{teamIds},</if>
            <if test="pkDept != null and pkDept != ''">pk_dept = #{pkDept},</if>
            <if test="loginIp != null and loginIp != ''">login_ip = #{loginIp},</if>
            <if test="loginDate != null">login_date = #{loginDate},</if>
            <if test="headPath != null and headPath != ''">head_path = #{headPath},</if>
            <if test="pkModified != null">PK_MODIFIED = #{pkModified},</if>
            <if test="pkCountry != null">PK_COUNTRY = #{pkCountry},</if>
            MODIFIED_TIME = sysdate
        </set>
        where user_id = #{userId}
    </update>

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

    <!-- 批量删除用户信息 -->
    <update id="deleteUserByIds" parameterType="Long">
        update sys_user
        set del_flag = 1,
        pk_modified = #{userId},
        modified_time = sysdate
        where user_id in
        <foreach collection="userIds" item="userId" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </update>

    <!-- 校验用户名称是否唯一 -->
    <select id="checkUserNameUnique" parameterType="String" resultType="int">
        select count(1)
        from sys_user
        where user_name = #{userName}
          and del_flag = 0
          and rownum = 1
    </select>

    <!-- 校验手机号码是否唯一 -->
    <select id="checkPhoneUnique" parameterType="String" resultMap="SysUserResult">
        select user_id, phonenumber
        from sys_user
        where phonenumber = #{phonenumber}
          and del_flag = 0
          and rownum = 1
    </select>

    <!-- 根据角色ID查询用户 -->
    <select id="queryUserByRoleId" resultType="com.hzs.system.sys.vo.SysRoleUserVO">
        select su.user_id, su.user_name, su.nick_name
        from sys_user_role sur
                 inner join sys_user su
                            on su.user_id = sur.user_id
        where sur.role_id = #{roleId}
    </select>

    <!-- 查询会员以及部门名称 -->
    <select id="queryUserAndDeptName" resultMap="SysUserResult">
        select su.*, sd.dept_name
        from sys_user su
                 left join sys_dept sd
                           on sd.dept_id = su.pk_dept
        where su.user_id = #{userId}
    </select>

    <!-- 查询所有管理员 -->
    <select id="selectAll" resultMap="SysUserResult">
        select su.*, sd.dept_name, sur.role_id
        from sys_user su
                 left join sys_dept sd
                           on sd.dept_id = su.pk_dept
                               and sd.del_flag = 0
                 left join sys_user_role sur
                           on sur.user_id = su.user_id
        where su.del_flag = 0
        order by su.nick_name
    </select>

</mapper>
