package cn.iocoder.yudao.module.energy.listener;

import cn.iocoder.yudao.module.energy.event.RecordImportedEvent;
import cn.iocoder.yudao.module.energy.event.RecordMatchedEvent;
import cn.iocoder.yudao.module.energy.service.detail.HydrogenDetailService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
@Slf4j
public class DetailEventListener {

    @Resource
    private HydrogenDetailService hydrogenDetailService;

    /**
     * 记录导入完成 → 触发自动匹配（占位，后续实现批量自动匹配）
     * AFTER_COMMIT: 非资金操作
     */
    @TransactionalEventListener
    public void onRecordImported(RecordImportedEvent event) {
        log.info("[onRecordImported] stationId={}, recordCount={}, batchNo={}",
                event.getStationId(), event.getRecordIds().size(), event.getBatchNo());
        // TODO: 后续接入 asset 模块实现批量自动匹配
        // hydrogenRecordService.batchMatch(event.getRecordIds());
    }

    /**
     * 记录匹配完成 → 创建明细
     * AFTER_COMMIT: 明细创建在独立事务中
     */
    @TransactionalEventListener
    public void onRecordMatched(RecordMatchedEvent event) {
        log.info("[onRecordMatched] recordId={}, vehicleId={}, customerId={}",
                event.getRecordId(), event.getVehicleId(), event.getCustomerId());
        hydrogenDetailService.createFromRecord(event);
    }
}
