<?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="cn.reghao.tnb.account.app.db.mapper.UserAccountMapper">
    <insert id="save">
        insert into account_user_account
        (`user_id`,`username`,`mobile`,`email`,`encoded_password`,`salt`,`create_at`,`enabled`,`locked`,`screen_name`,`avatar_url`)
        values
        (#{userId},#{username},#{mobile},#{email},#{encodedPassword},#{salt},#{createAt},#{enabled},#{locked},#{screenName},#{avatarUrl})
    </insert>
    <insert id="saveAll" useGeneratedKeys="true" keyProperty="id">
        insert into account_user_account
        (`user_id`,`username`,`mobile`,`email`,`encoded_password`,`salt`,`create_at`,`enabled`,`locked`,`screen_name`,`avatar_url`)
        values
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.userId},#{item.username},#{item.mobile},#{item.email},#{item.encodedPassword},#{item.salt},#{item.createAt},#{item.enabled},#{item.locked},#{item.screenName},#{item.avatarUrl})
        </foreach>
    </insert>

    <update id="updateUserMobile">
        update account_user_account
        set update_time=now(),mobile=#{mobile}
        where user_id=#{userId}
    </update>
    <update id="updateUserEmail">
        update account_user_account
        set update_time=now(),email=#{email}
        where user_id=#{userId}
    </update>
    <update id="updateSetPassword">
        update account_user_account set encoded_password=#{encodedPassword},salt=#{salt}
        where user_id=#{userId}
    </update>
    <update id="updateUserProfile">
        update account_user_account
        set update_time=now(),username=#{username},screen_name=#{screenName}
        where user_id=#{userId}
    </update>
    <update id="updateUserScreenName">
        update account_user_account
        set update_time=now(),screen_name=#{screenName}
        where user_id=#{userId}
    </update>
    <update id="updateUserAvatar">
        update account_user_account
        set update_time=now(),avatar_url=#{avatarUrl}
        where user_id=#{userId}
    </update>

    <select id="findAll1" resultType="java.lang.Long">
        select user_id,count(user_id) as total
        from account_user_account
        group by screen_name
        order by total desc
        limit 236
    </select>
    <select id="findMaxUserId" resultType="java.lang.Long">
        select max(user_id)
        from account_user_account
    </select>
    <select id="findMaxMobile" resultType="java.lang.Long">
        select max(mobile) from account_user_account
        where mobile like '129%'
    </select>
    <select id="findByUsername" resultType="cn.reghao.tnb.account.app.model.po.UserAccount">
        select *
        from account_user_account
        where username=#{username}
    </select>
    <select id="findByEmail" resultType="cn.reghao.tnb.account.app.model.po.UserAccount">
        select *
        from account_user_account
        where email=#{email}
    </select>
    <select id="findByMobile" resultType="cn.reghao.tnb.account.app.model.po.UserAccount">
        select *
        from account_user_account
        where mobile=#{mobile}
    </select>
    <select id="findUserIdByUsername" resultType="java.lang.Long">
        select user_id
        from account_user_account
        where username=#{username}
    </select>
    <select id="findAccountInfo" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
        select user_id,screen_name,avatar_url,username,mobile,email
        from account_user_account
        where user_id=#{userId}
    </select>
    <select id="findByUserId" resultType="cn.reghao.tnb.account.app.model.po.UserAccount">
        select *
        from account_user_account
        where user_id=#{userId}
    </select>
    <select id="findUserInfo" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
        select user_id,screen_name,avatar_url,username,mobile,email
        from account_user_account
        where user_id=#{userId}
    </select>
    <select id="findUserAvatar" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
        select user_id,screen_name,avatar_url,username,mobile,email
        from account_user_account
        where user_id=#{userId}
    </select>
    <select id="findUserAvatars" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
        select user_id,screen_name,avatar_url,username,mobile,email
        from account_user_account
        where user_id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="findAccountInfos" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
        select user_id,screen_name,avatar_url,username,mobile,email
        from account_user_account
        where user_id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </select>

    <select id="findAccountInfoByMobile" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
        select user_id,screen_name,avatar_url,username,mobile,email
        from account_user_account
        where mobile=#{mobile}
    </select>
    <select id="findAccountInfoByEmail" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
        select user_id,screen_name,avatar_url,username,mobile,email
        from account_user_account
        where email=#{email}
    </select>
    <select id="findAccountInfoByUsername" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
        select user_id,screen_name,avatar_url,username,mobile,email
        from account_user_account
        where username=#{username}
    </select>

    <select id="countByScreenName" resultType="java.lang.Integer">
        select count(*)
        from account_user_account
        where screen_name like concat('%',#{screenName},'%')
    </select>
    <select id="findScreenNameByPage" resultType="cn.reghao.tnb.account.api.dto.AccountInfo">
        select user_id,screen_name,avatar_url,username,mobile,email
        from account_user_account
        where screen_name like concat('%',#{screenName},'%')
    </select>
</mapper>