package cn.wisenergy.service.Manager;

import cn.wisenergy.mapper.AccountMapper;
import cn.wisenergy.mapper.BankInfoMapper;
import cn.wisenergy.mapper.TradeRecordMapper;
import cn.wisenergy.model.app.AccountInfo;
import cn.wisenergy.model.app.TradeRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 86187
 * @ Description: 银行卡管理类
 * @ Author     : 86187
 * @ Date       : 2021/3/26 19:38
 */
@Component
@Slf4j
public class BankManger {

    @Autowired
    private TradeRecordMapper recordMapper;

    @Autowired
    private AccountMapper accountMapper;

    /**
     * 更新账户信息和保存交易流水记录
     *
     * @param accountInfo 账户信息
     * @param record  交易流水信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountAddRecord(AccountInfo accountInfo, TradeRecord record) {

        //更新账户信息
        if (null != accountInfo) {
            int count = accountMapper.edit(accountInfo);
            if (count == 0) {
                return false;
            }
        }

        //新增交易流水记录
        if (null != record) {
            int count = recordMapper.add(record);
            return count != 0;
        }
        return true;
    }

    /**
     * 更新账户信息和提现记录状态
     *
     * @param accountInfo 账户信息
     * @param record  交易流水信息
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAccountAndRecord(AccountInfo accountInfo, TradeRecord record) {

        //更新账户信息
        if (null != accountInfo) {
            int count = accountMapper.edit(accountInfo);
            if (count == 0) {
                return false;
            }
        }

        //更改提现状态
        if (null != record) {
            int count = recordMapper.edit(record);
            return count != 0;
        }
        return true;
    }
}
