<?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.ssm.manage.mapper.RechargeAccountMapper">
  <resultMap id="BaseResultMap" type="com.ssm.manage.entity.RechargeAccount">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="member_id" jdbcType="BIGINT" property="memberId" />
    <result column="member_name" jdbcType="VARCHAR" property="memberName" />
    <result column="card" jdbcType="VARCHAR" property="card" />
    <result column="recharge_num" jdbcType="VARCHAR" property="rechargeNum" />
    <result column="card_recharge_money" jdbcType="DECIMAL" property="cardRechargeMoney" />
    <result column="card_rest_money" jdbcType="DECIMAL" property="cardRestMoney" />
    <result column="last_consume_time" jdbcType="TIMESTAMP" property="lastConsumeTime" />
    <result column="recharge_type_id" jdbcType="BIGINT" property="rechargeTypeId" />
    <result column="recharge_type_name" jdbcType="VARCHAR" property="rechargeTypeName" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="discount" jdbcType="DECIMAL" property="discount" />
    <result column="card_gift_money" jdbcType="DECIMAL" property="cardGiftMoney" />
    <result column="store_id" jdbcType="BIGINT" property="storeId" />
    <result column="store_name" jdbcType="VARCHAR" property="storeName" />
    <result column="card_consume_money" jdbcType="DECIMAL" property="cardConsumeMoney" />
    <result column="gift_consume_money" jdbcType="DECIMAL" property="giftConsumeMoney" />
    <result column="state" jdbcType="INTEGER" property="state" />
    <result column="end_time" jdbcType="DATE" property="endTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_id" jdbcType="BIGINT" property="updateId" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="discount_free_amount" jdbcType="DECIMAL" property="discountFreeAmount" />
    <result column="recharge_bl" jdbcType="DECIMAL" property="rechargeBl" />
    <result column="free_of" jdbcType="DECIMAL" property="freeOf" />
    <result column="card_gift_discount" jdbcType="DECIMAL" property="cardGiftDiscount" />
    <result column="first_charge" jdbcType="BIGINT" property="firstCharge" />
    <result column="card_num" jdbcType="INTEGER" property="cardNum" />
    <result column="recharge_type_num" jdbcType="VARCHAR" property="rechargeTypeNum"/>
  </resultMap>

  <resultMap id="MemberRecharge" type="com.ssm.manage.response.RechargeAccountResponse">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="member_id" jdbcType="BIGINT" property="memberId" />
    <result column="member_name" jdbcType="VARCHAR" property="memberName" />
    <result column="card" jdbcType="VARCHAR" property="card" />
    <result column="recharge_num" jdbcType="VARCHAR" property="rechargeNum" />
    <result column="card_recharge_money" jdbcType="DECIMAL" property="cardRechargeMoney" />
    <result column="card_rest_money" jdbcType="DECIMAL" property="cardRestMoney" />
    <result column="last_consume_time" jdbcType="TIMESTAMP" property="lastConsumeTime" />
    <result column="recharge_type_id" jdbcType="BIGINT" property="rechargeTypeId" />
    <result column="recharge_type_name" jdbcType="VARCHAR" property="rechargeTypeName" />
    <result column="type" jdbcType="INTEGER" property="type" />
    <result column="discount" jdbcType="DECIMAL" property="discount" />
    <result column="card_gift_money" jdbcType="DECIMAL" property="cardGiftMoney" />
    <result column="store_id" jdbcType="BIGINT" property="storeId" />
    <result column="store_name" jdbcType="VARCHAR" property="storeName" />
    <result column="card_consume_money" jdbcType="DECIMAL" property="cardConsumeMoney" />
    <result column="gift_consume_money" jdbcType="DECIMAL" property="giftConsumeMoney" />
    <result column="state" jdbcType="INTEGER" property="state" />
    <result column="end_time" jdbcType="DATE" property="endTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_id" jdbcType="BIGINT" property="updateId" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="member_recharge_money" jdbcType="DECIMAL" property="memberRechargeMoney" />
    <result column="discount_free_amount" jdbcType="DECIMAL" property="discountFreeAmount" />
    <result column="recharge_bl" jdbcType="DECIMAL" property="rechargeBl" />
    <result column="free_of" jdbcType="DECIMAL" property="freeOf" />
    <result column="card_gift_discount" jdbcType="DECIMAL" property="cardGiftDiscount" />
    <result column="first_charge" jdbcType="BIGINT" property="firstCharge" />
    <result column="card_num" jdbcType="INTEGER" property="cardNum" />
    <result column="recharge_type_num" jdbcType="VARCHAR" property="rechargeTypeNum"/>
  </resultMap>
  <sql id="baseColumnList">
    id,
    member_id,
    member_name,
    card,
    recharge_num,
    card_recharge_money,
    card_rest_money,
    last_consume_time,
    recharge_type_id,
    recharge_type_name,
    type,
    discount,
    card_gift_money,
    store_id,
    store_name,
    card_consume_money,
    gift_consume_money,
    state,
    end_time,
    update_by,
    update_id,
    update_time,
    discount_free_amount,
    recharge_bl,
    free_of,
    card_gift_discount,
    first_charge,
    card_num
  </sql>

  <update id="updateMoney" parameterType="java.util.List">

    <foreach collection="list" item="item" separator=";">
      update recharge_account
      <set>
        <if test="lastConsumeTime != null">
          last_consume_time = #{lastConsumeTime,jdbcType = TIMESTAMP}
        </if>
        <if test="cardRestMoney != null">
          card_rest_money = #{cardRestMoney,jdbcType = DECIMAL}
        </if>
        <if test="cardConsumeMoney != null">
          card_consume_money = #{cardConsumeMoney,jdbcType = DECIMAL}
        </if>
        <if test="cardGiftMoney != null">
          card_gift_money = #{cardGiftMoney,jdbcType = DECIMAL}
        </if>
        <if test="giftConsumeMoney != null">
          gift_consume_money = #{giftConsumeMoney,jdbcType = DECIMAL}
        </if>
        <if test="firstCharge != null">
          first_charge = #{firstCharge,jdbcType = BIGINT}
        </if>
        <if test="cardNum != null">
          card_num = #{cardNum,jdbcType = INTEGER}
        </if>
        <where>
          id = #{id, jdbcType = BIGINT}
        </where>
      </set>
    </foreach>
  </update>

  <insert id="insertBatch" parameterType="java.util.ArrayList">
    insert into recharge_account(<include refid="baseColumnList"/>) values
    <foreach collection="list" item="item" separator="," index="index">
      (
      #{item.id},
      #{item.memberId},
      #{item.memberName},
      #{item.card},
      #{item.rechargeNum},
      #{item.cardRechargeMoney},
      #{item.cardRestMoney},
      #{item.lastConsumeTime},
      #{item.rechargeTypeId},
      #{item.rechargeTypeName},
      #{item.type},
      #{item.discount},
      #{item.cardGiftMoney},
      #{item.storeId},
      #{item.storeName},
      #{item.cardConsumeMoney},
      #{item.giftConsumeMoney},
      #{item.state},
      #{item.endTime},
      #{item.updateBy},
      #{item.updateId},
      #{item.updateTime},
      #{item.discountFreeAmount},
      #{item.rechargeBl},
      #{item.freeOf},
      #{item.cardGiftDiscount},
      #{item.firstCharge},
      #{item.cardNum}
      )
    </foreach>
  </insert>

  <select id="memberRecharge" parameterType="com.ssm.manage.query.RechargeAccountRequest" resultMap="MemberRecharge">
    select member_id, member_name, sum(card_rest_money) as member_recharge_money from recharge_account where store_id = #{storeId, jdbcType = BIGINT}
    <if test="state != null">
      and state = #{state, jdbcType = INTEGER}
    </if>
      GROUP BY member_id
  </select>
    <select id="selectByPrimaryKeyList" resultMap="BaseResultMap" >
      SELECT * FROM `recharge_account` 
               WHERE Id in 
               <foreach collection="rechargeAccountIdList" open="(" close=")" separator="," item="item" index="index">
                 #{item,jdbcType = BIGINT}
               </foreach>
    </select>
    <select id="selectByMemberIdAndRechargeTypeIdAndMoney" resultMap="BaseResultMap" parameterType="com.ssm.manage.entity.RechargeAccount" >
      select * from recharge_account where member_id = #{memberId} and recharge_type_id = #{rechargeTypeId} and card_gift_money > 0 limit 1
    </select>
    <select id="exportachi" resultMap="BaseResultMap">
      select * from recharge_account <where> 1=1
      <if test="meberId != null">and member_id = #{memberId}</if>
      <if test="storeIds != null  and storeIds.size()>0 ">and store_id in <foreach collection="storeIds" open="(" close=")" separator="," item="item">#{item}</foreach>
      </if>
      <if test="cardRestMoney != null">and card_rest_money > 0</if>
    </where>
    </select>
</mapper>