package com.bizzan.bitrade.dao;

import java.math.BigDecimal;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import com.bizzan.bitrade.dao.base.BaseDao;
import com.bizzan.bitrade.entity.GameWallet;

public interface GameWalletDao extends BaseDao<GameWallet> {

	@Transactional
    @Modifying
    @Query("update GameWallet wallet set wallet.balance = wallet.balance + :amount where wallet.id = :id")
    int increaseBalance(@Param("id") long id, @Param("amount") BigDecimal amount);
	
	@Transactional
    @Modifying
    @Query("update GameWallet wallet set wallet.balance = wallet.balance - :amount where wallet.id = :id")
    int decreaseBalance(@Param("id") long id, @Param("amount") BigDecimal amount);
	
	@Transactional
    @Modifying
    @Query("update GameWallet wallet set wallet.balance = wallet.balance - :amount, wallet.frozenBalance = wallet.frozenBalance + :amount where wallet.id = :id and wallet.balance >= :amount")
    int freezeBalance(@Param("id") long id, @Param("amount") BigDecimal amount);
	
	@Transactional
    @Modifying
    @Query("update GameWallet wallet set wallet.balance = wallet.balance + :amount, wallet.frozenBalance = wallet.frozenBalance - :amount where wallet.id = :id")
    int thawBalance(@Param("id") long id, @Param("amount") BigDecimal amount);
	
	@Transactional
    @Modifying
    @Query("update GameWallet wallet set wallet.balance = wallet.balance + :profit, wallet.frozenBalance = wallet.frozenBalance - :thawedBalance where wallet.id = :id")
    int clear(@Param("id") long id, @Param("thawedBalance") BigDecimal thawedBalance, @Param("profit") BigDecimal profit);
}
