<?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.tmerclub.cloud.auth.mapper.AuthAccountMapper">
  <resultMap id="BaseResultMap" type="com.tmerclub.cloud.auth.model.AuthAccount">
    <!--@mbg.generated-->
    <!--@Table auth_account-->
    <id column="uid" property="uid" />
    <result column="create_time" property="createTime" />
    <result column="update_time" property="updateTime" />
    <result column="email" property="email" />
    <result column="phone" property="phone" />
    <result column="username" property="username" />
    <result column="password" property="password" />
    <result column="create_ip" property="createIp" />
    <result column="status" property="status" />
    <result column="sys_type" property="sysType" />
    <result column="user_id" property="userId"/>
    <result column="tenant_id" property="tenantId"/>
    <result column="is_admin" property="isAdmin"/>
    <result column="is_pass_shop" property="isPassShop"/>
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    `uid`, create_time, update_time, email, phone, username, `password`, create_ip,
     `status`, sys_type, user_id, tenant_id, is_admin, is_pass_shop
  </sql>

  <delete id="deleteByTenantIdAndSysType">
    UPDATE
      auth_account
    SET
      status = -1
    WHERE
      tenant_id = #{tenantId}
      AND sys_type = #{sysType}
  </delete>

  <select id="getAuthAccountInVerifyByInputUserName" resultType="com.tmerclub.cloud.common.security.bo.AuthAccountInVerifyBO">
    select user_id, username, sys_type,uid as account_uid,password,is_admin,is_pass_shop,tenant_id, status from auth_account
    where sys_type = #{sysType} and status != -1
    <if test="inputUserNameType == 1">
        and username = #{inputUserName}
    </if>
    <if test="inputUserNameType == 2">
        and phone = #{inputUserName}
    </if>
    <if test="inputUserNameType == 3">
        and email = #{inputUserName}
    </if>

  </select>
  <select id="getByUserIdAndType" resultMap="BaseResultMap">
    select uid, email, phone, username, `password`, create_ip,
     `status`, sys_type, user_id, tenant_id, is_admin, is_pass_shop from auth_account where sys_type = #{sysType} and user_id = #{userId}
  </select>
  <select id="getBySysTypeAndIsAdminAndTenantId" resultMap="BaseResultMap">
    select uid, email, phone, username, `password`, create_ip,
           `status`, sys_type, user_id, tenant_id, is_admin, is_pass_shop from auth_account where sys_type = #{sysType} and is_admin = #{isAdmin} and tenant_id = #{tenantId}
  </select>
  <select id="getByUid" resultMap="BaseResultMap">
    select uid, email, phone, username, `password`, create_ip,
           `status`, sys_type, user_id, tenant_id, is_admin, is_pass_shop from auth_account where uid = #{uid}
  </select>

  <select id="countByMobile" resultType="int">
    select ifnull(count(*),0) from auth_account where sys_type = #{sysType} and phone = #{mobile} and status != -1
  </select>

  <update id="updatePassword">
    update auth_account set password = #{newPassWord} where sys_type = #{sysType} and user_id = #{userId}
  </update>
  <insert id="save">
    insert into auth_account (`uid`,`email`,`phone`,`username`,`password`,`create_ip`,`status`,`sys_type`,`user_id`,`tenant_id`,`is_admin`,`is_pass_shop`)
    values (#{authAccount.uid},#{authAccount.email},#{authAccount.phone},#{authAccount.username},#{authAccount.password},#{authAccount.createIp},#{authAccount.status},#{authAccount.sysType},#{authAccount.userId},#{authAccount.tenantId},#{authAccount.isAdmin},#{authAccount.isPassShop});
  </insert>

  <insert id="batchSaveAccounts" parameterType="java.util.List">
    insert into auth_account (`uid`,`email`,`phone`,`username`,`password`,`create_ip`,`status`,`sys_type`,`user_id`,`tenant_id`,`is_admin`)
    select * from (
      <foreach collection="authAccounts" item="authAccount" separator="UNION ALL">
        SELECT #{authAccount.uid} `uid`,#{authAccount.email} `email`, #{authAccount.phone} `phone`,#{authAccount.username} `username`,
            #{authAccount.password} `password`,#{authAccount.createIp} `create_ip`,#{authAccount.status} `status`,
            #{authAccount.sysType} `sys_type`,#{authAccount.userId} `user_id`,#{authAccount.tenantId} `tenant_id`,#{authAccount.isAdmin} `is_admin`
        FROM DUAL
      </foreach>
    ) a WHERE NOT EXISTS (SELECT `phone`,`email`,`username` FROM `auth_account` b
    WHERE b.status != -1 AND b.sys_type = 0 AND (a.`phone` = b.`phone` OR a.`email` = b.`email` OR a.`username` = b.`username`))
  </insert>
  <update id="updateAccountInfo">
    update auth_account
    <set>
      <if test="authAccount.email != null">
        email = #{authAccount.email},
      </if>
      <if test="authAccount.phone != null">
        phone = #{authAccount.phone},
      </if>
      <if test="authAccount.username != null">
        username = #{authAccount.username},
      </if>
      <if test="authAccount.password != null">
        password = #{authAccount.password},
      </if>
      <if test="authAccount.status != null">
        status = #{authAccount.status},
      </if>
      <if test="authAccount.isPassShop != null">
        is_pass_shop = #{authAccount.isPassShop}
      </if>
    </set>
    where user_id = #{authAccount.userId} and sys_type = #{authAccount.sysType}
  </update>
  <update id="deleteByUserIdAndSysType">
    update auth_account set status = -1 where user_id = #{userId} and sys_type = #{sysType}
  </update>

  <update id="updateUserMobile">
    update auth_account set phone = #{authAccountDTO.phone} where user_id = #{authAccountDTO.userId} and sys_type = #{authAccountDTO.sysType}
  </update>
  <update id="updateUserInfoByUserId">
    update auth_account
    <set>
        <if test="authAccount.tenantId != null">
          tenant_id = #{authAccount.tenantId},
        </if>
        <if test="authAccount.isPassShop != null">
          is_pass_shop = #{authAccount.isPassShop},
        </if>
    </set>
    where user_id = #{userId} and sys_type = #{sysType} and status != -1 limit 1
  </update>

    <select id="getAccountByInputAccount" resultType="com.tmerclub.cloud.auth.model.AuthAccount">
    select <include refid="Base_Column_List"/> from auth_account where phone = #{validAccount} and sys_type = #{systemType} and `status` &gt;= 0
  </select>
  <select id="listByUserAccount" resultType="com.tmerclub.cloud.api.auth.vo.AuthAccountVO">
    select uid,email, phone, username,status  from auth_account
    where status != -1
    <if test="sysType != null">
      and sys_type = #{sysType}
    </if>
    and (false
    <if test="userName != null and userName != ''">
      or username = #{userName}
    </if>
    <if test="phone != null and phone != ''">
      or phone = #{phone}
    </if>
    <if test="email != null and email != ''">
      or email = #{email}
    </if>
    )
  </select>

  <select id="listUserByUserIdsAndType" resultType="com.tmerclub.cloud.api.auth.vo.AuthAccountVO">
    select username,`status`,user_id,is_admin,phone from auth_account where sys_type = #{systemType} and user_id in
    <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
      #{userId}
    </foreach>
  </select>
  <select id="getBizUserIdListByUserId" resultType="string">
    SELECT ass.biz_user_id FROM auth_account  aa
<!--    这里不能用左连接，当auth_social中没有关联数据时，列表中包含null-->
    JOIN auth_social ass ON ass.`uid` = aa.`uid` AND social_type = 1
    WHERE user_id = #{userId}
  </select>

  <select id="listByUserIdsAndPhoneAndType" resultType="com.tmerclub.cloud.api.auth.vo.AuthAccountVO">
    select email,phone,username,`status`,user_id,uid from auth_account
    where sys_type = #{sysType}
    <if test="phone != null and phone != ''">
      AND phone LIKE CONCAT("%",#{phone},"%")
    </if>
    <if test="userIds != null and userIds.size() > 0">
      AND user_id IN
      <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
        #{userId}
      </foreach>
    </if>
  </select>
  <select id="getAuthAccountByInputUserName" resultType="com.tmerclub.cloud.api.auth.vo.AuthAccountVO">
    select user_id, sys_type,uid,password,is_admin,tenant_id, status, email,phone,username from auth_account
    where sys_type = #{sysType} and status != -1
    <if test="inputUserNameType == 1">
      and username IN
    </if>
    <if test="inputUserNameType == 2">
      and phone IN
    </if>
    <if test="inputUserNameType == 3">
      and email IN
    </if>
    <foreach collection="params" item="item" open="(" close=")" separator=",">
      #{item}
    </foreach>
  </select>
  <select id="countByUserIds" resultType="java.lang.Integer">
    select count(*) from auth_account where sys_type = #{sysType} and user_id in
    <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
      #{userId}
    </foreach>
  </select>
  <select id="getAccountInfoByTenantId" resultType="com.tmerclub.cloud.api.auth.vo.AuthAccountVO">
    select user_id, sys_type,uid,phone,password,is_admin,tenant_id, status,username, is_pass_shop from auth_account
    where sys_type = #{sysType} and is_admin = 1 and tenant_id = #{tenantId} limit 1
  </select>
  <select id="countByUserNameAndSysType" resultType="java.lang.Integer">
    select ifnull(count(*),0) from auth_account where sys_type = #{sysType} and username = #{userName} and status != -1
  </select>

  <select id="listUserNameByNames" resultType="java.lang.String">
    SELECT
      username
    FROM
      auth_account
    WHERE
      sys_type = 0
      AND username IN
        <foreach collection="names" item="name" open="(" close=")" separator=",">
          #{name}
        </foreach>
      AND status != -1
  </select>
</mapper>
