package com.microservices.dms.achievementLibrary.controller;

import com.microservices.common.core.utils.poi.ExcelUtil;
import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.page.TableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.dms.achievementLibrary.domain.AchQueryVo;
import com.microservices.dms.achievementLibrary.domain.Achievements;
import com.microservices.dms.achievementLibrary.service.IAchievementsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 成果Controller
 *
 * @author microservices
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/achievements")
@Api(tags = "数据管理体系-成果库接口")
public class AchievementsController extends BaseController {
    @Autowired
    private IAchievementsService achievementsService;

    /**
     * 查询成果列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Achievements achievements) {
        startPage();
        List<Achievements> list = achievementsService.selectAchievementsList(achievements);
        return getDataTable(list);
    }

    /**
     * 导出成果列表
     */
    @Log(title = "成果", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Achievements achievements) {
        List<Achievements> list = achievementsService.selectAchievementsList(achievements);
        ExcelUtil<Achievements> util = new ExcelUtil<Achievements>(Achievements.class);
        util.exportExcel(response, list, "成果数据");
    }

    /**
     * 获取成果详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(achievementsService.selectAchievementsById(id));
    }

    /**
     * 新增成果
     */
    @Log(title = "成果", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Achievements achievements) {
        return toAjax(achievementsService.insertAchievements(achievements));
    }

    /**
     * 修改成果
     */
    @Log(title = "成果", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Achievements achievements) {
        return toAjax(achievementsService.updateAchievements(achievements));
    }

    /**
     * 删除成果
     */
    @Log(title = "成果", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(achievementsService.deleteAchievementsByIds(ids));
    }

    /**
     * 获取精选成果
     *
     * @return
     */
    @ApiOperation("获取精选成果")
    @GetMapping("/getChoiceImport")
    public AjaxResult getChoiceImport() {
        return success(achievementsService.getChoiceImport());
    }

    /**
     * 根据成果来源对成果数据进行分类统计
     *
     * @return
     */
    @ApiOperation("根据成果来源对成果数据进行分类统计")
    @GetMapping("/getTjBySources")
    public AjaxResult getTjBySources() {
        return success(achievementsService.getTjBySources());
    }

    /**
     * 根据成果领域对成功数据进行汇总
     *
     * @return
     */
    @ApiOperation("根据成果领域对成果数据进行汇总")
    @GetMapping("/getTjByAreas")
    public AjaxResult getTjByAreas() {
        return success(achievementsService.getTjByAreas());
    }

    /**
     * 根据领域分类名称，获取领域相关数据(创客任务、开放竞赛、校企成果）
     */
    @ApiOperation("根据领域分类名称，获取领域相关数据")
    @GetMapping("/getAreasByName")
    public AjaxResult getAreasByName(String areaName) {
        return success(achievementsService.getAreasByName(areaName));
    }


    @ApiOperation("根据领域分类名称，获取项目领域")
    @GetMapping("/getProjectAreasByName")
    public AjaxResult getProjectAreasByName(String areaName) {
        return success(achievementsService.getProjectAreasByName(areaName));
    }

    @ApiOperation("根据领域分类名称，获取专家领域")
    @GetMapping("/getExpertAreasByName")
    public AjaxResult getExpertAreasByName(String areaName) {
        return success(achievementsService.getExpertAreasByName(areaName));
    }


    /* 获取全部成果,根据成果名称、成果领域、成果来源进行查询
     * @return
     */
    @ApiOperation("获取全部成果,根据成果名称、成果领域、成果来源进行查询")
    @GetMapping("/getAllResult")
    public TableDataInfo getAllResult(AchQueryVo achQueryVo) {
        startPage();
        return getDataTable(achievementsService.getAllResult(achQueryVo));
    }

    @ApiOperation("近七日用户行为数据")
    @GetMapping("/getUerActionData")
    public AjaxResult getUerActionData() {
        return success(achievementsService.getUerActionData());
    }

    @ApiOperation("热门成果")
    @GetMapping("/getHotAchievement")
    public AjaxResult getHotAchievement() {
        return success(achievementsService.getHotAchievement());
    }

    @ApiOperation("七日新增")
    @GetMapping("/get7DayAdd")
    public AjaxResult get7DayAdd(AchQueryVo achQueryVo) {
        return success(achievementsService.get7DayAdd(achQueryVo));
    }


    @ApiOperation("首页项目统计")
    @GetMapping("/indexProjectStatistic")
    public AjaxResult indexProjectStatistic() {
        return success(achievementsService.indexProjectStatistic());
    }

    @ApiOperation("首页task统计")
    @GetMapping("/indexTaskStatistic")
    public AjaxResult indexTaskStatistic() {
        return success(achievementsService.indexTaskStatistic());
    }

    @ApiOperation("首页Competition统计")
    @GetMapping("/indexCompetitionStatistic")
    public AjaxResult indexCompetitionStatistic() {
        return success(achievementsService.indexCompetitionStatistic());
    }

    @ApiOperation("首页SchoolEnterprise统计")
    @GetMapping("/indexSchoolEnterpriseStatistic")
    public AjaxResult indexSchoolEnterpriseStatistic() {
        return success(achievementsService.indexSchoolEnterpriseStatistic());
    }


    @ApiOperation("首页专家统计")
    @GetMapping("/indexExpertStatistic")
    public AjaxResult indexExpertStatistic() {
        return success(achievementsService.indexExpertStatistic());
    }


    @ApiOperation("根据当前用户以及成果ID,获取当前成果的 是否收藏与关注状态")
    @GetMapping("/getWatchFavoriteStatusById")
    public AjaxResult getWatchFavoriteStatusById(Long id, Long userId) {
        return success(achievementsService.getWatchFavoriteStatusById(id, userId));
    }
    @ApiOperation("根据成果ID，获取近7日成果点击数量（折线图）")
    @GetMapping("/get7AddClickById")
    public AjaxResult get7AddClickById(String id,@RequestParam(value = "clickType",required = false, defaultValue = "Achievements")
    String clickType) {
        return success(achievementsService.get7AddClickById(id,clickType));
    }

    @ApiOperation("根据成果ID,获取成果的相关行为数据统计（点击量、搜索量、附件下载量、收藏、关注）")
    @GetMapping("/getActDataStatisticById")
    public AjaxResult getActDataStatisticById(Long id) {
        return success(achievementsService.getActDataStatisticById(id));
    }

    @ApiOperation("根据领域，将成果数据进行分类")
    @GetMapping("/getAreaStatistic")
    public AjaxResult getAreaStatistic(String areaKey)
    {
        return success(achievementsService.getAreaStatistic(areaKey));
    }

    /**
     * 获取开源项目的相关成果
     */
    @ApiOperation(value = "获取开源项目的相关成果")
    @GetMapping("/getRelatedAch")
    public AjaxResult getRelatedAch(Long id,Long sourceId)
    {
        return success(achievementsService.getRelatedAch(id,sourceId));
    }


    @ApiOperation(value = "获取开源竞赛的相关成果")
    @GetMapping("/getRelatedAchCompetition")
    public AjaxResult getRelatedAchCompetition(Long id,Long sourceId)
    {
        return success(achievementsService.getRelatedAchCompetition(id,sourceId));
    }


    @ApiOperation(value = "获取创客任务的相关成果")
    @GetMapping("/getRelatedAchTask")
    public AjaxResult getRelatedAchTask(Long id,Long sourceId)
    {
        return success(achievementsService.getRelatedAchTask(id,sourceId));
    }

    @ApiOperation(value = "获取专家-创客任务的相关成果")
    @GetMapping("/getRelatedAchTaskOfExpert")
    public AjaxResult getRelatedAchTaskOfExpert(Long expertId)
    {
        return success(achievementsService.getRelatedAchTaskOfExpert(expertId));
    }

    @ApiOperation(value = "获取专家-开源竞赛的相关成果")
    @GetMapping("/getRelatedAchCompetitionOfExpert")
    public AjaxResult getRelatedAchCompetitionOfExpert(Long expertId)
    {
        return success(achievementsService.getRelatedAchCompetitionOfExpert(expertId));
    }

    @ApiOperation(value = "搜索行为数据记录")
    @GetMapping("/getSearchResult")
    public AjaxResult getSearchResult(String achName,Long userId)
    {
        return success(achievementsService.getSearchResult(achName,userId));
    }
}
