<?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.github.wxiaoqi.security.admin.mapper.AppUserLoginMapper">

    <select id="selectbyPhones" resultType="com.github.wxiaoqi.security.admin.entity.AppUserLogin">
        select `id`,`username` from `app_user_login` where `isdel`=0  and `username` in
        <foreach collection="phones" item="phone" open="(" close=")" separator=",">
            #{phone}
        </foreach>
    </select>

    <update id="bindOpenId" parameterType="com.github.wxiaoqi.security.admin.entity.AppUserLogin">
        UPDATE  app_user_login SET wx_openid=#{wxOpenid},unionid=#{unionid},openid=#{openid} where  id=#{id}
    </update>

    <select id="selectAllExistUserIds" resultType="integer">
        SELECT  `id` FROM `app_user_login`  WHERE `isdel`=0;
    </select>
    <select id="getUserByUsernameAndRealName" resultType="com.github.wxiaoqi.security.admin.entity.AppUserLogin">
        select l.* from app_user_login l left  join app_user_detail d on l.id=d.userid where 1=1

        <if test="username!=null ">
         and   l.username like concat('%',#{username},'%')
        </if>

        <if test="realName !=null">
            and   d.realname like concat('%',#{realName},'%')
        </if>
    </select>

    <select id="findAppUser" resultType="integer">
        select aul.`id` from (select `id` from `app_user_login` where `isdel`=0
        <if test="phone!=null and phone!=''">
            and `username`=#{phone}
        </if>
        <if test="memberIds!=null and memberIds.size()>0">
            and  `id` IN
            <foreach collection="memberIds" item="userId" separator="," open="(" close=")">
                #{userId}
            </foreach>
        </if>
        ) AS `aul`
        INNER JOIN
        `app_user_detail` AS `aud` ON aud.userid=aul.id
       <if test="name!=null and name!=''">
         and ( aud.realname like CONCAT('%',#{name},'%') OR aud.nickname like CONCAT('%',#{name},'%'))
       </if>
    </select>

    <select id="selectByUserIds" resultType="com.github.wxiaoqi.security.admin.bo.UserBo">
         select  aul.`id` AS `userId`,aul.username AS `phone`,IFNULL(aud.realname,aud.nickname) AS `name` from ( select `id`,`username` from `app_user_login` where `isdel`=0 and `id` IN
        <foreach collection="userIds" item="uid" separator="," open="(" close=")">
            #{uid}
        </foreach>) AS `aul`
         INNER JOIN
         `app_user_detail` AS `aud` ON aud.userid=aul.id
    </select>

    <select id="selectByParentId" resultType="com.github.wxiaoqi.security.admin.vo.AppUserManageVo">
       select IFNULL(d.realname, d.nickname) AS nameOfSuperior,l.username AS superiorMobileNumber
       from app_user_login l left  join  app_user_detail d on  l.id = d.userid where l.isdel=0 and l.id =#{parentId}
    </select>
</mapper>