<?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.bonus.canteen.core.account.mapper.AccWalletInfoMapper">
    <resultMap type="com.bonus.canteen.core.account.domain.AccWalletInfo" id="AccWalletInfoResult">
        <result property="userId"    column="user_id"    />
        <result property="accId"    column="acc_id"    />
        <result property="walletId"    column="wallet_id"    />
        <result property="walletBal"    column="wallet_bal"    />
        <result property="limitBalance"    column="limit_balance"    />
        <result property="frozenBalance"    column="frozen_balance"    />
        <result property="expiredTime"    column="expired_time"    />
        <result property="lastSubsidyAmount"    column="last_subsidy_amount"    />
        <result property="lastSubsidyTime"    column="last_subsidy_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectAccWalletInfoVo">
        select user_id, acc_id, wallet_id, wallet_bal, limit_balance, frozen_balance, expired_time, last_subsidy_amount, last_subsidy_time, create_by, create_time, update_by, update_time from acc_wallet_info
    </sql>

    <select id="selectAccWalletInfoList" parameterType="com.bonus.canteen.core.account.domain.AccWalletInfo" resultMap="AccWalletInfoResult">
        <include refid="selectAccWalletInfoVo"/>
        <where>  
            <if test="accId != null "> and acc_id = #{accId}</if>
            <if test="walletBal != null "> and wallet_bal = #{walletBal}</if>
            <if test="limitBalance != null "> and limit_balance = #{limitBalance}</if>
            <if test="frozenBalance != null "> and frozen_balance = #{frozenBalance}</if>
            <if test="expiredTime != null "> and expired_time = #{expiredTime}</if>
            <if test="lastSubsidyAmount != null "> and last_subsidy_amount = #{lastSubsidyAmount}</if>
            <if test="lastSubsidyTime != null "> and last_subsidy_time = #{lastSubsidyTime}</if>
        </where>
    </select>
    
    <select id="selectAccWalletInfoByUserId" parameterType="Long" resultMap="AccWalletInfoResult">
        <include refid="selectAccWalletInfoVo"/>
        where user_id = #{userId}
    </select>

    <select id="selectAccWalletInfoByUserIdAndWalletId" resultMap="AccWalletInfoResult">
        <include refid="selectAccWalletInfoVo"/>
        where user_id = #{userId} and wallet_id = #{walletId}
    </select>

    <select id="selectAccWalletInfoByUserIds" resultType="com.bonus.canteen.core.account.domain.vo.AccWalletInfoVO">
        SELECT
        user_id,
        acc_id,
        wallet_id,
        wallet_bal,
        limit_balance,
        frozen_balance,
        last_subsidy_amount,
        last_subsidy_time
        FROM acc_wallet_info
        WHERE user_id IN
        <foreach collection="userIds" item="userId" separator="," open="(" close=")">
            #{userId}
        </foreach>
    </select>
        
    <insert id="insertAccWalletInfo" parameterType="com.bonus.canteen.core.account.domain.AccWalletInfo">
        insert into acc_wallet_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="accId != null">acc_id,</if>
            <if test="walletId != null">wallet_id,</if>
            <if test="walletBal != null">wallet_bal,</if>
            <if test="limitBalance != null">limit_balance,</if>
            <if test="frozenBalance != null">frozen_balance,</if>
            <if test="expiredTime != null">expired_time,</if>
            <if test="lastSubsidyAmount != null">last_subsidy_amount,</if>
            <if test="lastSubsidyTime != null">last_subsidy_time,</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>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="accId != null">#{accId},</if>
            <if test="walletId != null">#{walletId},</if>
            <if test="walletBal != null">#{walletBal},</if>
            <if test="limitBalance != null">#{limitBalance},</if>
            <if test="frozenBalance != null">#{frozenBalance},</if>
            <if test="expiredTime != null">#{expiredTime},</if>
            <if test="lastSubsidyAmount != null">#{lastSubsidyAmount},</if>
            <if test="lastSubsidyTime != null">#{lastSubsidyTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <insert id="batchInsertAccWalletInfo" parameterType="com.bonus.canteen.core.account.domain.AccWalletInfo">
        insert into acc_wallet_info(user_id,acc_id,wallet_id,create_by,create_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.userId},#{item.accId},#{item.walletId},#{item.createBy},NOW())
        </foreach>
    </insert>

    <delete id="deleteAccWalletInfoByUserId" parameterType="Long">
        delete from acc_wallet_info where user_id = #{userId}
    </delete>

    <delete id="deleteAccWalletInfoByUserIds" parameterType="String">
        delete from acc_wallet_info where user_id in 
        <foreach item="userId" collection="array" open="(" separator="," close=")">
            #{userId}
        </foreach>
    </delete>

    <update id="updateMinBalanceByUserId">
        UPDATE
        acc_wallet_info
        SET limit_balance = #{minBalance},
        update_by = #{updateBy},
        update_time = #{updateTime}
        <where>
            <if test="userIds != null and userIds.size() != 0">
                AND user_id IN
                <foreach collection="userIds" item="userId" separator="," open="(" close=")">
                    #{userId}
                </foreach>
            </if>
            AND wallet_id = #{walletId}
        </where>
    </update>

    <update id="addAccWalletInfo">
        UPDATE acc_wallet_info
        SET wallet_bal = wallet_bal + #{amount},
            update_by = #{updateBy},
            update_time = #{updateTime}
        WHERE user_id = #{userId}
        AND wallet_id = #{walletId}
    </update>

    <update id="reduceAccWalletInfo">
        UPDATE acc_wallet_info
        SET wallet_bal = wallet_bal - #{amount},
            update_by = #{updateBy},
            update_time = #{updateTime}
        WHERE user_id = #{userId}
          AND wallet_id = #{walletId}
    </update>

    <update id="clearAccWalletInfo">
        UPDATE acc_wallet_info
        SET wallet_bal = 0,
            update_by = #{updateBy},
            update_time = #{updateTime}
        WHERE user_id = #{userId}
          AND wallet_id = #{walletId}
    </update>
</mapper>