package com.sckw.report.service;

import com.sckw.core.exception.SystemException;
import com.sckw.core.web.constant.HttpStatus;
import com.sckw.core.web.response.BaseResult;
import com.sckw.transport.api.feign.TransportApi;
import com.sckw.transport.api.model.vo.RealtimeSalesVolumeVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 实时销量（聚合运输服务）
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class KwRealtimeSalesService {

    private final TransportApi transportApi;

    public RealtimeSalesVolumeVo realtimeSalesVolume() {
        BaseResult<RealtimeSalesVolumeVo> remote;
        try {
            remote = transportApi.realtimeSalesVolume();
        } catch (Exception e) {
            log.error("调用运输服务「实时销量」失败", e);
            throw new SystemException("获取实时销量失败：" + e.getMessage());
        }
        if (remote == null) {
            throw new SystemException("获取实时销量失败：远程无响应");
        }
        if (remote.getCode() != HttpStatus.SUCCESS_CODE) {
            throw new SystemException(remote.getCode(), remote.getMessage() != null ? remote.getMessage() : "远程调用失败");
        }
        return remote.getData();
    }
}
