package com.haha.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haha.entity.Account;
import com.haha.mapper.AccountMapper;
import com.haha.service.AccountService;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;

@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements AccountService {

    @Override
    public Account getByUserId(Long userId) {
        return lambdaQuery().eq(Account::getUserId, userId).one();
    }

    @Override
    public boolean updateBalance(Long userId, BigDecimal amount) {
        Account account = getByUserId(userId);
        if (account != null) {
            BigDecimal newBalance = account.getBalance().add(amount);
            return lambdaUpdate().eq(Account::getUserId, userId).set(Account::getBalance, newBalance).update();
        }
        return false;
    }
}
