<?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.multishop.mapper.ShopUserMapper">
  <resultMap id="BaseResultMap" type="com.tmerclub.cloud.multishop.model.ShopUser">
    <!--@mbg.generated-->
    <!--@Table shop_user-->
    <id column="shop_user_id" property="shopUserId" />
    <result column="create_time" property="createTime" />
    <result column="update_time" property="updateTime" />
    <result column="shop_id" property="shopId" />
    <result column="nick_name" property="nickName" />
    <result column="code" property="code" />
    <result column="phone_num" property="phoneNum" />
    <result column="has_account" property="hasAccount"/>
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    shop_user_id, create_time, update_time, shop_id, nick_name,`code`,phone_num,has_account
  </sql>

  <select id="getByUserId" resultType="com.tmerclub.cloud.multishop.vo.ShopUserVO">
    select shop_user_id,nick_name,`code`,phone_num,has_account,shop_id  from shop_user where shop_user_id = #{userId}
  </select>
  <select id="listByShopId" resultType="com.tmerclub.cloud.multishop.vo.ShopUserVO">
    select shop_user_id,nick_name,`code`,phone_num,has_account from shop_user
    <where>
      shop_id = #{shopUser.shopId}
      <if test="shopUser.nickName != null and shopUser.nickName != ''">
        and nick_name like concat('%', #{shopUser.nickName}, '%')
      </if>
      <if test="shopUser.phoneNum != null and shopUser.phoneNum != ''">
        and phone_num like concat('%', #{shopUser.phoneNum}, '%')
      </if>
    </where>
    order by shop_user_id
  </select>
  <insert id="save">
    insert into `shop_user` ( `shop_user_id`, `shop_id`, `nick_name`, `code`, `phone_num`,has_account)
    values (#{shopUser.shopUserId},#{shopUser.shopId},#{shopUser.nickName},#{shopUser.code},#{shopUser.phoneNum}, #{shopUser.hasAccount});
  </insert>
  <update id="update">
    update shop_user
    <set>
      <if test="shopUser.nickName != null">
        nick_name = #{shopUser.nickName},
      </if>
      <if test="shopUser.code != null">
        code = #{shopUser.code},
      </if>
      <if test="shopUser.phoneNum != null">
        phone_num = #{shopUser.phoneNum},
      </if>
      <if test="shopUser.hasAccount != null">
        has_account = #{shopUser.hasAccount},
      </if>
    </set>
    where shop_user_id = #{shopUser.shopUserId} and shop_id = #{shopUser.shopId}
  </update>

  <update id="updateShopIdByShopUserId">
    UPDATE shop_user SET shop_id = #{shopId} WHERE shop_user_id = #{shopUserId} LIMIT 1
  </update>

    <delete id="deleteById">
    delete  from shop_user where shop_user_id = #{shopUserId}
  </delete>

  <select id="getUserIdByShopId" resultType="java.lang.Long">
    SELECT shop_user_id FROM shop_user WHERE shop_id = #{shopId}  ORDER BY create_time ASC limit 1
  </select>

  <select id="listShopUserByUserIds" resultType="com.tmerclub.cloud.multishop.model.ShopUser">
    SELECT * FROM shop_user WHERE shop_user_id IN
    <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
      #{userId}
    </foreach>
  </select>

  <select id="countAccoutByMobile" resultType="java.lang.Integer">
    select ifnull(count(*),0) from `shop_user` where phone_num = #{mobile} and has_account = 1
  </select>
</mapper>
