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

import java.util.Date;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
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.ResearchCooperativeDevelopmentVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchCommonProsperityProjectVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchDisposableIncomeVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchFamilyRanchVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchFarmerHouseholdVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchLargeLivestockFarmerVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchLivestockOwnershipHouseholdVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchLivestockProductOutputVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchPastureVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchRuralThreeAssetsVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchTechServicePersonnelVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchThreeAssetsProblemReportVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchVaccinationDataVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchYakAssetVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchYakHerdInventoryVo;
import com.ruoyi.web.modules.industryservice.research.domain.vo.ResearchYakOutboundReportVo;
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());
        }
    }

    /**
     * 牦牛存栏数据列表（村级汇总）。
     */
    @GetMapping("/yak-herd-inventories")
    public OpenApiResponse<OpenApiPageResult<ResearchYakHerdInventoryVo>> listYakHerdInventories(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear,
            @RequestParam(value = "statMonth", required = false) Integer statMonth,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listYakHerdInventories(
                    pageNum, pageSize, statYear, statMonth, townName, villageName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 牦牛出栏数据列表（村级汇总）。
     */
    @GetMapping("/yak-outbound-reports")
    public OpenApiResponse<OpenApiPageResult<ResearchYakOutboundReportVo>> listYakOutboundReports(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear,
            @RequestParam(value = "statMonth", required = false) Integer statMonth,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listYakOutboundReports(
                    pageNum, pageSize, statYear, statMonth, townName, villageName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 合作社发展数据列表。
     */
    @GetMapping("/cooperative-developments")
    public OpenApiResponse<OpenApiPageResult<ResearchCooperativeDevelopmentVo>> listCooperativeDevelopments(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear,
            @RequestParam(value = "statMonth", required = false) Integer statMonth,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName,
            @RequestParam(value = "cooperativeName", required = false) String cooperativeName)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listCooperativeDevelopments(
                    pageNum, pageSize, statYear, statMonth, townName, villageName, cooperativeName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 畜牧产品产量登记列表（村级汇总）。
     */
    @GetMapping("/livestock-product-outputs")
    public OpenApiResponse<OpenApiPageResult<ResearchLivestockProductOutputVo>> listLivestockProductOutputs(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear,
            @RequestParam(value = "statMonth", required = false) Integer statMonth,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listLivestockProductOutputs(
                    pageNum, pageSize, statYear, statMonth, townName, villageName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 家庭牧场数据列表。
     */
    @GetMapping("/family-ranches")
    public OpenApiResponse<OpenApiPageResult<ResearchFamilyRanchVo>> listFamilyRanches(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear,
            @RequestParam(value = "statMonth", required = false) Integer statMonth,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName,
            @RequestParam(value = "enterpriseName", required = false) String enterpriseName)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listFamilyRanches(
                    pageNum, pageSize, statYear, statMonth, townName, villageName, enterpriseName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 可支配收入数据列表（县级年度汇总）。
     */
    @GetMapping("/disposable-incomes")
    public OpenApiResponse<OpenApiPageResult<ResearchDisposableIncomeVo>> listDisposableIncomes(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear)
    {
        try
        {
            return ResearchOpenApiSupport.success(
                    researchOpenApiService.listDisposableIncomes(pageNum, pageSize, statYear));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 农牧户数据列表（村级汇总）。
     */
    @GetMapping("/farmer-households")
    public OpenApiResponse<OpenApiPageResult<ResearchFarmerHouseholdVo>> listFarmerHouseholds(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear,
            @RequestParam(value = "statMonth", required = false) Integer statMonth,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listFarmerHouseholds(
                    pageNum, pageSize, statYear, statMonth, townName, villageName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 共同富裕项目列表。
     */
    @GetMapping("/common-prosperity-projects")
    public OpenApiResponse<OpenApiPageResult<ResearchCommonProsperityProjectVo>> listCommonProsperityProjects(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "projectName", required = false) String projectName,
            @RequestParam(value = "projectType", required = false) Integer projectType,
            @RequestParam(value = "implYear", required = false) Integer implYear)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listCommonProsperityProjects(
                    pageNum, pageSize, projectName, projectType, implYear));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 养殖大户数据列表。
     */
    @GetMapping("/large-livestock-farmers")
    public OpenApiResponse<OpenApiPageResult<ResearchLargeLivestockFarmerVo>> listLargeLivestockFarmers(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear,
            @RequestParam(value = "statMonth", required = false) Integer statMonth,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName,
            @RequestParam(value = "enterpriseName", required = false) String enterpriseName)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listLargeLivestockFarmers(
                    pageNum, pageSize, statYear, statMonth, townName, villageName, enterpriseName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 疫苗接种数据列表（村级汇总）。
     */
    @GetMapping("/vaccination-data")
    public OpenApiResponse<OpenApiPageResult<ResearchVaccinationDataVo>> listVaccinationData(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear,
            @RequestParam(value = "statMonth", required = false) Integer statMonth,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listVaccinationData(
                    pageNum, pageSize, statYear, statMonth, townName, villageName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 有无畜户数据列表（村级汇总）。
     */
    @GetMapping("/livestock-ownership-households")
    public OpenApiResponse<OpenApiPageResult<ResearchLivestockOwnershipHouseholdVo>> listLivestockOwnershipHouseholds(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "statYear", required = false) Integer statYear,
            @RequestParam(value = "statMonth", required = false) Integer statMonth,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listLivestockOwnershipHouseholds(
                    pageNum, pageSize, statYear, statMonth, townName, villageName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 农村三资数据列表（村级）。
     */
    @GetMapping("/rural-three-assets")
    public OpenApiResponse<OpenApiPageResult<ResearchRuralThreeAssetsVo>> listRuralThreeAssets(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName)
    {
        try
        {
            return ResearchOpenApiSupport.success(
                    researchOpenApiService.listRuralThreeAssets(pageNum, pageSize, townName, villageName));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 三资具体问题上报列表。
     */
    @GetMapping("/three-assets-problem-reports")
    public OpenApiResponse<OpenApiPageResult<ResearchThreeAssetsProblemReportVo>> listThreeAssetsProblemReports(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "20") int pageSize,
            @RequestParam(value = "townName", required = false) String townName,
            @RequestParam(value = "villageName", required = false) String villageName,
            @RequestParam(value = "beginReportTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date beginReportTime,
            @RequestParam(value = "endReportTime", required = false)
            @DateTimeFormat(pattern = "yyyy-MM-dd") Date endReportTime)
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.listThreeAssetsProblemReports(
                    pageNum, pageSize, townName, villageName, beginReportTime, endReportTime));
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }

    /**
     * 科技服务人员管理详情（单例配置，无查询参数）。
     */
    @GetMapping("/tech-service-personnel")
    public OpenApiResponse<ResearchTechServicePersonnelVo> getTechServicePersonnel()
    {
        try
        {
            return ResearchOpenApiSupport.success(researchOpenApiService.getTechServicePersonnel());
        }
        catch (ServiceException ex)
        {
            int code = ex.getCode() != null ? ex.getCode() : 500;
            return ResearchOpenApiSupport.error(code, ex.getMessage());
        }
    }
}
