<?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.jsowell.pile.mapper.MemberWalletInfoMapper">
  <resultMap id="BaseResultMap" type="com.jsowell.pile.domain.MemberWalletInfo">
    <!--@mbg.generated-->
    <!--@Table member_wallet_info-->
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="member_id" jdbcType="VARCHAR" property="memberId" />
    <result column="merchant_id" jdbcType="VARCHAR" property="merchantId" />
    <result column="wallet_code" jdbcType="VARCHAR" property="walletCode" />
    <result column="principal_balance" jdbcType="DECIMAL" property="principalBalance" />
    <result column="gift_balance" jdbcType="DECIMAL" property="giftBalance" />
    <result column="version" jdbcType="INTEGER" property="version" />
    <result column="create_by" jdbcType="VARCHAR" property="createBy" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="del_flag" jdbcType="CHAR" property="delFlag" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    id, member_id, merchant_id, wallet_code, principal_balance, gift_balance, version, 
    create_by, create_time, update_by, update_time, del_flag
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    <!--@mbg.generated-->
    select 
    <include refid="Base_Column_List" />
    from member_wallet_info
    where id = #{id,jdbcType=INTEGER}
  </select>
  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    <!--@mbg.generated-->
    delete from member_wallet_info
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.jsowell.pile.domain.MemberWalletInfo" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into member_wallet_info (member_id, merchant_id, wallet_code, 
      principal_balance, gift_balance, version, 
      create_by, create_time, update_by, 
      update_time, del_flag)
    values (#{memberId,jdbcType=VARCHAR}, #{merchantId,jdbcType=VARCHAR}, #{walletCode,jdbcType=VARCHAR}, 
      #{principalBalance,jdbcType=DECIMAL}, #{giftBalance,jdbcType=DECIMAL}, #{version,jdbcType=INTEGER}, 
      #{createBy,jdbcType=VARCHAR}, #{createTime,jdbcType=TIMESTAMP}, #{updateBy,jdbcType=VARCHAR}, 
      #{updateTime,jdbcType=TIMESTAMP}, #{delFlag,jdbcType=CHAR})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.jsowell.pile.domain.MemberWalletInfo" useGeneratedKeys="true">
    <!--@mbg.generated-->
    insert into member_wallet_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="memberId != null">
        member_id,
      </if>
      <if test="merchantId != null">
        merchant_id,
      </if>
      <if test="walletCode != null">
        wallet_code,
      </if>
      <if test="principalBalance != null">
        principal_balance,
      </if>
      <if test="giftBalance != null">
        gift_balance,
      </if>
      <if test="version != null">
        version,
      </if>
      <if test="createBy != null">
        create_by,
      </if>
      <if test="createTime != null">
        create_time,
      </if>
      <if test="updateBy != null">
        update_by,
      </if>
      <if test="updateTime != null">
        update_time,
      </if>
      <if test="delFlag != null">
        del_flag,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="memberId != null">
        #{memberId,jdbcType=VARCHAR},
      </if>
      <if test="merchantId != null">
        #{merchantId,jdbcType=VARCHAR},
      </if>
      <if test="walletCode != null">
        #{walletCode,jdbcType=VARCHAR},
      </if>
      <if test="principalBalance != null">
        #{principalBalance,jdbcType=DECIMAL},
      </if>
      <if test="giftBalance != null">
        #{giftBalance,jdbcType=DECIMAL},
      </if>
      <if test="version != null">
        #{version,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        #{delFlag,jdbcType=CHAR},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.jsowell.pile.domain.MemberWalletInfo">
    <!--@mbg.generated-->
    update member_wallet_info
    <set>
      <if test="memberId != null">
        member_id = #{memberId,jdbcType=VARCHAR},
      </if>
      <if test="merchantId != null">
        merchant_id = #{merchantId,jdbcType=VARCHAR},
      </if>
      <if test="walletCode != null">
        wallet_code = #{walletCode,jdbcType=VARCHAR},
      </if>
      <if test="principalBalance != null">
        principal_balance = #{principalBalance,jdbcType=DECIMAL},
      </if>
      <if test="giftBalance != null">
        gift_balance = #{giftBalance,jdbcType=DECIMAL},
      </if>
      <if test="version != null">
        version = #{version,jdbcType=INTEGER},
      </if>
      <if test="createBy != null">
        create_by = #{createBy,jdbcType=VARCHAR},
      </if>
      <if test="createTime != null">
        create_time = #{createTime,jdbcType=TIMESTAMP},
      </if>
      <if test="updateBy != null">
        update_by = #{updateBy,jdbcType=VARCHAR},
      </if>
      <if test="updateTime != null">
        update_time = #{updateTime,jdbcType=TIMESTAMP},
      </if>
      <if test="delFlag != null">
        del_flag = #{delFlag,jdbcType=CHAR},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.jsowell.pile.domain.MemberWalletInfo">
    <!--@mbg.generated-->
    update member_wallet_info
    set member_id = #{memberId,jdbcType=VARCHAR},
      merchant_id = #{merchantId,jdbcType=VARCHAR},
      wallet_code = #{walletCode,jdbcType=VARCHAR},
      principal_balance = #{principalBalance,jdbcType=DECIMAL},
      gift_balance = #{giftBalance,jdbcType=DECIMAL},
      version = #{version,jdbcType=INTEGER},
      create_by = #{createBy,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_by = #{updateBy,jdbcType=VARCHAR},
      update_time = #{updateTime,jdbcType=TIMESTAMP},
      del_flag = #{delFlag,jdbcType=CHAR}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectByMemberId" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from member_wallet_info
    where del_flag = '0'
    and member_id = #{memberId,jdbcType=INTEGER}
    and merchant_id = #{merchantId,jdbcType=VARCHAR}
  </select>

  <select id="selectByMemberWalletList" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from member_wallet_info
    where del_flag = '0'
    and member_id = #{memberId,jdbcType=INTEGER}
    and merchant_id is not null
  </select>

  <select id="selectByWalletCode" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from member_wallet_info
    where del_flag = '0'
    and wallet_code = #{walletCode,jdbcType=VARCHAR}
  </select>

  <select id="selectMemberWalletInfo" resultType="com.jsowell.pile.vo.base.MemberWalletVO">
    SELECT t1.member_id                               as memberId,
           t1.wallet_code                             as walletCode,
           t1.merchant_id                             as merchantId,
           ifnull(t1.principal_balance, 0)            as principalBalance,
           ifnull(t1.gift_balance, 0)                 as giftBalance,
           ifnull(t2.accumulatedRechargePrincipal, 0) as accumulatedRechargePrincipal,
           ifnull(t3.accumulatedRechargeGift, 0)      as accumulatedRechargeGift
    from member_wallet_info t1
           left join (select wallet_code, sum(amount) as accumulatedRechargePrincipal
                      from `member_wallet_log`
                      WHERE `wallet_code` = #{walletCode,jdbcType=VARCHAR}
                        and type = '1'
                        and sub_type = '10') t2 on t2.wallet_code = t1.wallet_code
           left join (select wallet_code, sum(amount) as accumulatedRechargeGift
                      from `member_wallet_log`
                      WHERE `wallet_code` = #{walletCode,jdbcType=VARCHAR}
                        and type = '1'
                        and sub_type = '11') t3 on t3.wallet_code = t1.wallet_code
    where t1.wallet_code = #{walletCode,jdbcType=VARCHAR}
  </select>
</mapper>