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.OpenFarmEnclosureDto;

/**
 * 第三方养殖圈舍客户端实现。
 * <p>
 * {@code mode=http} 时调用 {@link ThirdPartyFarmingPageFetcher} 分页拉取；
 * {@code mode=stub} 时读取 {@value #STUB_RESOURCE}。
 * </p>
 */
@Component
public class ThirdPartyFarmEnclosureClientImpl implements ThirdPartyFarmEnclosureClient
{
    /** Stub 样例：classpath:thirdparty/stub-farm-enclosures.json */
    private static final String STUB_RESOURCE = "thirdparty/stub-farm-enclosures.json";

    /** 查询养殖圈舍列表（Apifox 开放接口） */
    private static final String LIST_PATH = "/open-api/v1/farming/enclosures";

    private final ObjectMapper objectMapper = ThirdPartyFarmingHttpSupport.createObjectMapper();

    @Autowired
    private ThirdPartyFarmingProperties properties;

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