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

/**
 * 第三方养殖生产计划客户端实现。
 */
@Component
public class ThirdPartyFarmingPlanClientImpl implements ThirdPartyFarmingPlanClient
{
    private static final String STUB_RESOURCE = "thirdparty/stub-farming-plans.json";

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

    private final ObjectMapper objectMapper = ThirdPartyFarmingHttpSupport.createObjectMapper();

    @Autowired
    private ThirdPartyFarmingProperties properties;

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