<?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.yami.shop.dao.UserExtensionMapper">

  <resultMap id="userExtensionMap" type="com.yami.shop.bean.model.UserExtension">
    <id column="user_extension_id" property="userExtensionId" />
    <result column="user_id" property="userId"/>
    <result column="level" property="level"/>
    <result column="growth" property="growth"/>
    <result column="score" property="score"/>
    <result column="balance" property="balance"/>
    <result column="version" property="version"/>
    <result column="update_time" property="updateTime"/>
  </resultMap>
  <update id="updateByVersion">
    update tz_user_extension set level = #{userExtension.level},version = version + 1,update_time = now() where user_id = #{userExtension.userId}
  </update>
    <select id="getPage" resultType="com.yami.shop.bean.model.UserExtension">
      SELECT ue.*,u.nick_name FROM tz_user_extension ue
      JOIN tz_user u ON u.user_id = ue.user_id
      <where>
        <if test="userExtension.nickName != null">
          u.nick_name LIKE CONCAT("%",#{userExtension.nickName},"%")
        </if>
      </where>
    </select>
    <update id="setUserLevelBylevelId">
      UPDATE
        tz_user_extension
      SET
        `level` = #{level}-1
      WHERE `level` = #{level}

    </update>

    <update id="serMaxUserLavelByLevelId">
      UPDATE
      tz_user_extension
      SET
      `level` = #{level}
      WHERE growth &gt; #{minNeedGrowth}
      <if test="maxNeedGrowth != null">
        AND growth &lt; #{maxNeedGrowth}
      </if>
    </update>

  <update id="updateBatchByUserId">
    <foreach collection="userExtensions" item="userExtension" separator=";">
      UPDATE
      tz_user_extension
      SET
      `score` = `score` - #{userExtension.score}
      WHERE user_id = #{userExtension.userId}
    </foreach>
  </update>

</mapper>
