package com.usky.vpp.service.impl;

import com.usky.vpp.config.VppUnProperties;
import com.usky.vpp.config.VppUnPropertiesRegistry;
import com.usky.vpp.config.VppUnTenantContext;
import com.usky.vpp.service.VppUnIntervalDataReportTaskService;
import com.usky.vpp.service.VppUnReportService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VppUnIntervalDataReportTaskServiceImpl implements VppUnIntervalDataReportTaskService {

    private static final Logger log = LoggerFactory.getLogger(VppUnIntervalDataReportTaskServiceImpl.class);

    @Autowired
    private VppUnPropertiesRegistry propertiesRegistry;

    @Autowired
    private VppUnReportService reportService;

    @Override
    public void intervalDataReportUn(Integer tenantId) {
        log.info("intervalDataReportUn start, tenantId={}", tenantId);
        VppUnProperties properties = propertiesRegistry.get(tenantId);
        if (properties == null) {
            log.warn("intervalDataReportUn skipped: tenantId={} 未配置运管平台 UN 对接信息", tenantId);
            return;
        }
        if (!properties.isOutboundActive()) {
            log.info("intervalDataReportUn skipped: outbound not active, tenantId={}", tenantId);
            return;
        }
        VppUnTenantContext.runWithTenant(tenantId, () -> {
            try {
                reportService.submitIntervalDataReport(null);
            } catch (Exception ex) {
                log.warn("IntervalDataReport 提交失败, tenantId={}: {}", tenantId, ex.getMessage());
            }
        });
    }
}
