package com.seer.teach.pay.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seer.teach.pay.entity.PayCoinAccountEntity;
import com.seer.teach.pay.entity.PayCoinAccountTradeLogEntity;
import com.seer.teach.pay.mapper.PayCoinAccountTradeLogMapper;
import com.seer.teach.pay.service.IPayCoinAccountService;
import com.seer.teach.pay.service.IPayCoinAccountTradeLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户账户交易明细表 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2025-06-25 16:25:06
 */
@RequiredArgsConstructor
@Slf4j
@Service
public class PayCoinAccountTradeLogServiceImpl extends ServiceImpl<PayCoinAccountTradeLogMapper, PayCoinAccountTradeLogEntity> implements IPayCoinAccountTradeLogService {

    private final IPayCoinAccountService payCoinAccountService;

    @Override
    public boolean saveLog(PayCoinAccountTradeLogEntity tradeLog) {
        PayCoinAccountEntity account = payCoinAccountService.getByUserId(tradeLog.getUserId());
        tradeLog.setAfterBalance(account.getAvailableBalance());
        tradeLog.setBeforeBalance(account.getAvailableBalance().subtract(tradeLog.getTradeAmount()));
        tradeLog.setTradeTime(LocalDateTime.now());
        boolean saved = super.save(tradeLog);
        log.info("保存用户账户交易明细: {}", saved);

        return saved;
    }
}