package com.ruoyi.web.modules.industryservice.support;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenApiPageResult;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenApiResponse;
import com.ruoyi.web.modules.industryservice.domain.dto.OpenFarmingBatchDto;

/**
 * 第三方养殖批次客户端实现。
 */
@Component
public class ThirdPartyFarmingBatchClientImpl implements ThirdPartyFarmingBatchClient
{
    private static final String STUB_RESOURCE = "thirdparty/stub-farming-batches.json";

    private static final String LIST_PATH = "/open-api/v1/farming/batches";

    private final ObjectMapper objectMapper = ThirdPartyFarmingHttpSupport.createObjectMapper();

    @Autowired
    private ThirdPartyFarmingProperties properties;

    @Override
    public List<OpenFarmingBatchDto> fetchAll()
    {
        if (!properties.isEnabled())
        {
            throw new ServiceException("第三方养殖批次同步未启用");
        }
        if ("http".equalsIgnoreCase(properties.getMode()))
        {
            return ThirdPartyFarmingPageFetcher.fetchAllPages(properties, objectMapper, LIST_PATH,
                    new TypeReference<OpenApiResponse<OpenApiPageResult<OpenFarmingBatchDto>>>() { }, "养殖批次");
        }
        return ThirdPartyFarmingPageFetcher.fetchFromStub(objectMapper, STUB_RESOURCE,
                new TypeReference<List<OpenFarmingBatchDto>>() { }, "养殖批次");
    }
}
