package com.hz.pm.api.dashboard.controller;

import com.ningdatech.log.annotation.WebLog;
import com.hz.pm.api.dashboard.manage.CockpitStatsManage;
import com.hz.pm.api.dashboard.manage.DashboardProjectManage;
import com.hz.pm.api.dashboard.model.dto.CockpitStatsSaveDTO;
import com.hz.pm.api.dashboard.model.po.QueryYearPO;
import com.hz.pm.api.dashboard.model.vo.*;
import com.hz.pm.api.scheduler.task.CockpitStatsStatisticsTask;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.net.UnknownHostException;
import java.util.List;

/**
 * @author liuxinxin
 * @date 2023/8/2 上午10:36
 */

@RestController
@RequestMapping("/api/v1/dashboard")
@Validated
@RequiredArgsConstructor
@Api(value = "DashboardController", tags = "驾驶舱相关接口")
public class DashboardController {

    private final DashboardProjectManage dashboardProjectManage;

    private final CockpitStatsManage cockpitStatsManage;

    private final CockpitStatsStatisticsTask cockpitStatsStatisticsTask;

    @PostMapping("project-summary")
    @ApiOperation("驾驶舱项目总览")
    @WebLog("驾驶舱项目总览")
    public DashboardProjectSummaryVO getProjectSummary(@Valid @RequestBody QueryYearPO queryYearPO) {
        return dashboardProjectManage.getProjectSummary(queryYearPO);
    }

    @PostMapping("project-cnt-summary")
    @ApiOperation("驾驶舱项目数量")
    @WebLog("驾驶舱项目数量")
    public DashboardProjectCntSummaryVO getProjectCntSummary(@Valid @RequestBody QueryYearPO queryYearPO) {
        return dashboardProjectManage.getProjectCntSummary(queryYearPO);
    }

    @PostMapping("investment-summary")
    @ApiOperation("驾驶舱投资金额")
    @WebLog("驾驶舱投资金额")
    public DashboardInvestmentSummaryVO getInvestmentSummary(@Valid @RequestBody QueryYearPO queryYearPO) {
        return dashboardProjectManage.getInvestmentSummary(queryYearPO);
    }

    @GetMapping("/project-portrait/{projectCode}")
    @ApiOperation("驾驶舱-项目画像接口")
    public DashboardProjectProtraitVO getProjectPortrait(@PathVariable String projectCode) {
        return dashboardProjectManage.getProjectPortrait(projectCode);
    }

    @GetMapping("cockpit-stats")
    @ApiOperation("驾驶舱-统计数据接口")
    public CockpitStatsVO getData(@RequestParam(required = false) String regionCode,
                                  @RequestParam(required = false) Integer year) {
        return cockpitStatsManage.getData(regionCode,year);
    }

    @GetMapping("cockpit-stats-start")
    @ApiOperation("驾驶舱-统计数据接口 强制运行任务")
    public String statsStart() throws UnknownHostException {
        cockpitStatsStatisticsTask.doTask();
        return "运行成功";
    }

    @PostMapping("cockpit-stats-save")
    @ApiOperation("驾驶舱-统计数据接口 假数据保存")
    public String statsSave(@RequestBody List<CockpitStatsSaveDTO> saves) {
        cockpitStatsManage.save(saves);
        return "保存成功";
    }
}
