package com.ruoyi.web.modules.industryservice.research.controller;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
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.research.domain.dto.ResearchTokenRequest;
import com.ruoyi.web.modules.industryservice.research.domain.dto.ResearchTokenVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchPastureVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchYakAssetVo;
import com.ruoyi.web.modules.industryservice.research.service.IResearchOpenApiService;
import com.ruoyi.web.modules.industryservice.research.support.ResearchOpenApiSupport;

/**
 * 科研开放数据接口（高校/研究机构）。
 * <p>
 * 除 Token 申请外，须在请求头携带 {@code Authorization: Bearer &lt;accessToken&gt;}。
 * 返回数据已脱敏，不含第三方编号、明文牦牛编号、详细地址、耳标等敏感字段。
 */
@Anonymous
@RestController
@RequestMapping("/open-api/v1/research")
public class ResearchOpenApiController
{
    @Autowired
    private IResearchOpenApiService researchOpenApiService;

    /**
     * 申请访问令牌（appKey + appSecret）。
     */
    @PostMapping("/auth/token")
    public OpenApiResponse<ResearchTokenVo> token(@Valid @RequestBody ResearchTokenRequest request)
    {
        try
        {
            ResearchTokenVo data = researchOpenApiService.authenticate(request.getAppKey(), request.getAppSecret());
            return ResearchOpenApiSupport.success(data);
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 牧场列表（脱敏）。
     */
    @GetMapping("/pastures")
    public OpenApiResponse<OpenApiPageResult<ResearchPastureVo>> listPastures(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "keyword", required = false) String keyword,
            @RequestParam(value = "farmType", required = false) String farmType)
    {
        try
        {
            return ResearchOpenApiSupport.success(
                    researchOpenApiService.listPastures(pageNum, pageSize, keyword, farmType));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 牦牛资产档案列表（脱敏，供种群健康等科研用途）。
     */
    @GetMapping("/yak-assets")
    public OpenApiResponse<OpenApiPageResult<ResearchYakAssetVo>> listYakAssets(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "assetStatus", required = false) Integer assetStatus,
            @RequestParam(value = "gender", required = false) String gender,
            @RequestParam(value = "pastureId", required = false) Long pastureId)
    {
        try
        {
            return ResearchOpenApiSupport.success(
                    researchOpenApiService.listYakAssets(pageNum, pageSize, assetStatus, gender, pastureId));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }
}
