package com.microservices.dss.projects.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.dss.annotation.GitlinkAdminAccessRequired;
import com.microservices.dss.projects.service.IProjectGlobalResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;

@RestController
@RequestMapping("/open/projects")
@Api(tags = "数据统计-开源项目相关接口")
public class ProjectDataController extends BaseController {

    @Autowired
    private IProjectGlobalResourcesService projectGlobalResourcesService;

    @ApiOperation("开源项目全局资源获取")
    @GetMapping(value = "/global-resources")
    @GitlinkAdminAccessRequired
    public AjaxResult getGlobalResourcesData() throws ParseException, JsonProcessingException {
        return success(projectGlobalResourcesService.getProjectGlobalResourcesData());
    }

    @ApiOperation("开源项目最新动态获取")
    @GetMapping(value = "/latestActivities")
    @GitlinkAdminAccessRequired
    public AjaxResult getLatestActivitiesData(@ApiParam("分页数") @RequestParam(value = "page", defaultValue = "1") int page,
                                              @ApiParam("分页大小") @RequestParam(value = "limit", defaultValue = "4") int limit){
        return success(projectGlobalResourcesService.getLatestActivitiesData(page, limit));
    }

    @ApiOperation("开源项目-项目排行获取")
    @GetMapping(value = "/projectRank")
    @GitlinkAdminAccessRequired
    public AjaxResult getProjectRankData(@ApiParam("统计时间范围（天）") @RequestParam(value = "time", defaultValue = "365") int time,
                                              @ApiParam("top数") @RequestParam(value = "limit", defaultValue = "5") int limit){
        return success(projectGlobalResourcesService.getProjectRankData(time, limit));
    }

    @ApiOperation("开源项目-用户排行获取")
    @GetMapping(value = "/userRank")
    @GitlinkAdminAccessRequired
    public AjaxResult getUserRankData(@ApiParam("统计时间范围（天）") @RequestParam(value = "time", defaultValue = "365") int time,
                          @ApiParam("top数") @RequestParam(value = "limit", defaultValue = "5") int limit){
        return success(projectGlobalResourcesService.getUserRankData(time, limit));
    }

}
