<?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.zhiqiyun.open.mapper.UserBaseInfoMapper">
    <resultMap id="BaseResultMap" type="com.zhiqiyun.open.models.UserBaseInfo">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="nick_name" property="nickName" jdbcType="VARCHAR"/>
        <result column="avatar" property="avatar" jdbcType="VARCHAR"/>
        <result column="gender" property="gender" typeHandler="com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler"/>
        <result column="register_time" property="registerTime" jdbcType="TIMESTAMP"/>
        <result column="register_ip" property="registerIp" jdbcType="VARCHAR"/>
        <result column="state" property="state" typeHandler="com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler"/>
    </resultMap>

    <resultMap id="UserLoginLogResultMap" type="com.zhiqiyun.open.models.UserLoginLog">
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="login_account" property="loginAccount" jdbcType="VARCHAR"/>
        <result column="account_type" property="accountType" typeHandler="com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler"/>
        <result column="login_ip" property="loginIp" jdbcType="VARCHAR"/>
        <result column="login_local" property="loginLocal" jdbcType="VARCHAR"/>
        <result column="from_source" property="fromSource" jdbcType="VARCHAR"/>
        <result column="device_id" property="deviceId" jdbcType="VARCHAR"/>
        <result column="login_time" property="loginTime" jdbcType="TIMESTAMP"/>
        <result column="state" property="state" typeHandler="com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler"/>
    </resultMap>
    <sql id="Base_Column">
        `id`
        , `nick_name`, `avatar`, `gender`, `state`, `register_time`, `register_ip`
    </sql>

    <select id="findPage" resultMap="BaseResultMap">
        select
        <include refid="Base_Column"/>
        from `user_base_info`
        <where>
            <if test="nickName != null and nickName !=''">
                AND `nick_name` = #{nickName,jdbcType=VARCHAR}
            </if>
            <if test="userName != null and userName !=''">
                AND `id` in (select `uid` from `user_login_account` where `login_account` like CONCAT('%',#{userName,jdbcType=VARCHAR},'%'))
            </if>
            <if test="state != null">
                AND `state` = #{state, typeHandler=com.dliyun.commons.typeHandler.IEnumTypeHandler}
            </if>
            <if test="gender != null">
                AND `gender` = #{gender, typeHandler=com.dliyun.commons.typeHandler.IEnumTypeHandler}
            </if>
            <if test="registerTime != null">
                AND `register_time` between #{registerTime.statDate, jdbcType=TIMESTAMP} and #{registerTime.endDate, jdbcType=TIMESTAMP}
            </if>
        </where>
        order by id desc
    </select>
    <select id="findById" resultMap="BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="Base_Column"/>
        from user_base_info
        <where>
            id = #{id,jdbcType=BIGINT}
        </where>
    </select>

    <delete id="deleteRoles">
        delete
        FROM user_role
        WHERE uid = #{uid,jdbcType=BIGINT}
    </delete>
    <insert id="insertRoles">
        insert into user_role (uid, role_id)
        values
        <foreach collection="roles" item="item" index="index" separator=",">
            (#{uid,jdbcType=BIGINT},#{item,jdbcType=BIGINT})
        </foreach>
    </insert>
    <insert id="insert" parameterType="com.zhiqiyun.open.models.UserBaseInfo">
        insert into `user_base_info` (`id`, `nick_name`, `avatar`, `gender`, `state`, `register_time`, `register_ip`)
        values (#{id, jdbcType=BIGINT},
        #{nickName, jdbcType=VARCHAR},
        #{avatar, jdbcType=VARCHAR},
        #{gender, typeHandler=com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler},
        #{state, typeHandler=com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler},
        #{registerTime, jdbcType=TIMESTAMP},
        #{registerIp, jdbcType=VARCHAR})
    </insert>
    <insert id="insertOperateLog" parameterType="com.zhiqiyun.open.models.UserOperateLog">
        insert into `user_operate_logs` (`id`, `uid`, `tags`, `from_source`, `request_url`, `request_ip`, `request_params`, `request_data`, `request_time`)
        values (#{id, jdbcType=VARCHAR},
        #{uid, jdbcType=BIGINT},
        #{tags, jdbcType=VARCHAR},
        #{fromSource, jdbcType=VARCHAR},
        #{requestUrl, jdbcType=VARCHAR},
        #{requestIp, jdbcType=VARCHAR},
        #{requestParams, jdbcType=VARCHAR},
        #{requestData, jdbcType=VARCHAR},
        #{requestTime, jdbcType=TIMESTAMP})
    </insert>
    <insert id="insertLoginLog">
        insert into `user_login_logs` (`login_account`, `account_type`, `login_ip`, `login_local`, `from_source`, `device_id`, `login_time`, `state`)
        values (#{loginAccount},
        #{accountType, typeHandler=com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler},
        #{loginIp},
        #{loginLocal},
        #{fromSource},
        #{deviceId},
        #{loginTime, jdbcType=TIMESTAMP},
        #{state, typeHandler=com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler})
    </insert>
    <update id="updateById" parameterType="com.zhiqiyun.open.models.UserBaseInfo">
        update `user_base_info`
        <set>
            <if test="nickName != null and nickName !=''">
                `nick_name` = #{nickName,jdbcType=VARCHAR},
            </if>
            <if test="avatar != null">
                `avatar` = #{avatar,jdbcType=VARCHAR},
            </if>
            <if test="gender != null">
                `gender` = #{gender, typeHandler=com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler},
            </if>
            <if test="state != null">
                `state` = #{state, typeHandler=com.zhiqiyun.open.mybatis.typeHandler.IEnumTypeHandler},
            </if>
        </set>
        <where>id=#{id}</where>
    </update>
    <update id="updateOperateLogById">
        update `user_operate_logs`
        <set>
            <if test="responseTime != null">
                `response_time` = #{responseTime,jdbcType=TIMESTAMP},
            </if>
        </set>
        <where>id=#{id}</where>
    </update>
    <select id="findUserAuthorities" resultType="java.lang.String">
        SELECT name
        FROM authority_info
        WHERE id IN (
        SELECT authority_id
        FROM role_authority
        WHERE role_id IN (
        SELECT role_id
        FROM user_role
        WHERE uid = #{uid,jdbcType=BIGINT}
        )
        )
    </select>
    <select id="findRoles" resultMap="com.zhiqiyun.open.mapper.RoleInfoMapper.BaseResultMap" parameterType="java.lang.Long">
        select
        <include refid="com.zhiqiyun.open.mapper.RoleInfoMapper.Base_Column"/>
        from `role_info`
        <where>
            `id` in (select `role_id` from `user_role` where `uid` = #{uid,jdbcType=BIGINT})
        </where>
    </select>
    <select id="findLoginLogPage" resultMap="UserLoginLogResultMap">
        select
        a.`id`, a.`login_account`, a.`account_type`, a.`login_ip`, a.`login_local`, a.`from_source`, a.`device_id`, a.`login_time`, a.`state`
        from user_login_logs a
        left join user_login_account b on a.`login_account`=b.`login_account`
        <where>
            b.`uid`=#{uid}
        </where>
    </select>
    <select id="findSelectAccounts" resultType="java.util.Map">
        select id,`nick_name` as nickName from `user_base_info`
        <where>
            <if test="searchKey != null and searchKey !=''">
                or `nick_name` like CONCAT("%",#{searchKey},"%")
                or `id` like CONCAT("%",#{searchKey},"%")
                or `id` in (select `uid` from `user_login_account` where `login_account` like CONCAT("%",#{searchKey},"%"))
            </if>
        </where>
        limit 10
    </select>
</mapper>