package com.ruoyi.system.omgpay.service;

import com.ruoyi.system.domain.IpnLog;
import com.ruoyi.system.service.IIpnLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/** Appends every OMG callback to the trusted existing IPN log in its own transaction. */
@Service
public class OmgIpnAuditService {
    private final IIpnLogService ipnLogService;

    public OmgIpnAuditService(IIpnLogService ipnLogService) {
        this.ipnLogService = ipnLogService;
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Exception.class)
    public void append(String ip, String rawForm) {
        IpnLog row = new IpnLog();
        row.setIp(ip == null ? "unknown" : ip);
        row.setCretim(new Date());
        row.setIpnLog(rawForm == null ? "" : rawForm);
        row.setType("omg");
        if (ipnLogService.insertIpnLog(row) != 1) {
            throw new IllegalStateException("OMG IPN log insert returned no row");
        }
    }
}
