package org.jebot.service.impl;

import org.jebot.constant.Constant;
import org.jebot.models.jebot.BotAccountBookHistory;
import org.jebot.repository.jebot.BotAccountBookHistoryRepository;
import org.jebot.repository.jebot.BotAccountBookRepository;
import org.jebot.service.IAccountBookService;
import org.jebot.service.dto.UpdateBalance;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;


@Service
public class AccountBookServiceImpl implements IAccountBookService {

    @Resource
    BotAccountBookRepository accountBookRepository;

    @Resource
    BotAccountBookHistoryRepository accountBookHistoryRepository;

    @Override
    @Transactional
    public void updatePaymentBalanceByIdAndPaymentBalance(UpdateBalance updateBalance) {
        accountBookRepository.updatePaymentBalanceByIdAndPaymentBalance(updateBalance.getNewBalance(), updateBalance.getId(), updateBalance.getOldBalance());
        BotAccountBookHistory accountBookHistory = new BotAccountBookHistory();
        accountBookHistory.setAmount(updateBalance.getAmount());
        accountBookHistory.setBeforeBalance(updateBalance.getOldBalance());
        accountBookHistory.setAfterBalance(updateBalance.getNewBalance());
        accountBookHistory.setType(Constant.PAYMENT);
        if (updateBalance.getMessage().isGroupMch()) {
            accountBookHistory.setBelongType(Constant.DATA_TYPE_MERCHANT);
        }
        if (updateBalance.getMessage().isGroupChannel()) {
            accountBookHistory.setBelongType(Constant.DATA_TYPE_CHANNEL);
        }
        accountBookHistory.setBelongId(updateBalance.getMessage().getBotGroup().getDataId());
        accountBookHistory.setBelongName(updateBalance.getMessage().getBotGroup().getDataName());
        accountBookHistory.setUserId(updateBalance.getMessage().getBotUser().getUserId());
        accountBookHistory.setUserName(updateBalance.getMessage().getBotUser().getUserName());
        accountBookHistoryRepository.save(accountBookHistory);
    }

    @Override
    @Transactional
    public void updateAgentBalanceByIdAndAgentBalance(UpdateBalance updateBalance) {
        accountBookRepository.updateAgentBalanceByIdAndAgentBalance(updateBalance.getNewBalance(), updateBalance.getId(), updateBalance.getOldBalance());
        BotAccountBookHistory accountBookHistory = new BotAccountBookHistory();
        accountBookHistory.setAmount(updateBalance.getAmount());
        accountBookHistory.setBeforeBalance(updateBalance.getOldBalance());
        accountBookHistory.setAfterBalance(updateBalance.getNewBalance());
        if (updateBalance.getMessage().isGroupMch()) {
            accountBookHistory.setBelongType(Constant.DATA_TYPE_MERCHANT);
        }
        if (updateBalance.getMessage().isGroupChannel()) {
            accountBookHistory.setBelongType(Constant.DATA_TYPE_CHANNEL);
        }
        accountBookHistory.setType(Constant.AGENT);
        accountBookHistory.setBelongId(updateBalance.getMessage().getBotGroup().getDataId());
        accountBookHistory.setBelongName(updateBalance.getMessage().getBotGroup().getDataName());
        accountBookHistory.setUserId(updateBalance.getMessage().getBotUser().getUserId());
        accountBookHistory.setUserName(updateBalance.getMessage().getBotUser().getUserName());
        accountBookHistoryRepository.save(accountBookHistory);
    }

    @Override
    @Transactional
    public void updateMutiPaymentBalanceByIdAndPaymentBalance(UpdateBalance... updateBalance) {
        for (UpdateBalance balance : updateBalance) {
            this.updatePaymentBalanceByIdAndPaymentBalance(balance);
        }
    }


}
