package com.transaction_.service.impl;

import com.transaction_.dao.AccountDao;
import com.transaction_.entity.AccountEntity;
import com.transaction_.service.AccountService;
import org.springframework.transaction.annotation.Transactional;

public class AccountServiceImpl implements AccountService {
    AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void initAccount() {
        AccountEntity accountEntity = new AccountEntity();
        accountEntity.setUserName("李四");
        accountEntity.setBalance(300.0);
        int i = accountDao.saveAccount(accountEntity);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean transfer(Integer fromId, Integer toId, Double balance) {
        // 转账操作
        AccountEntity fromEntity = new AccountEntity();
        AccountEntity toEntity = new AccountEntity();
        fromEntity.setId(fromId).setBalance(balance);
        toEntity.setId(toId).setBalance(balance);
        // 开启事务
        accountDao.incomeAccount(toEntity,"to");
//        int i = 1/0;
        accountDao.incomeAccount(fromEntity,"in");
        // 提交事务
        return false;
    }
}
