package net.northking.cctp.dw.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.dw.api.service.*;
import net.northking.cctp.dw.db.entity.*;
import net.northking.cctp.dw.db.service.MidCaseDesignService;
import net.northking.cctp.dw.db.service.MidManualExecJobService;
import net.northking.cctp.dw.db.service.MidProjectInfoService;
import net.northking.cctp.dw.db.service.MtTestChartService;
import net.northking.cctp.dw.dto.*;
import net.northking.cctp.dw.feign.ProjectsFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: puchao.ma
 * @date: 2021/12/17 11:12
 */
@RestController
@RequestMapping("/pub/ScreenData")
@Api(tags = "大屏展示图表")
public class ScreenDataCtrl {
    @Autowired
    private BugChartService apiService;

    @Autowired
    private BugStatusStatisticsApiService bugStatusStatisticsApiService;

    @Autowired
    private BugOverallTendencyChartApiService bugOverallTendencyChartApiService;

    @Autowired
    private MidProjectInfoService midProjectInfoService;

    @Autowired
    private MtTestChartService mtTestChartService;

    @Autowired
    private MidManualExecJobService midManualExecJobService;

    @Autowired
    private MidCaseDesignService midCaseDesignService;

    @Autowired
    private ChartApiService chartApiService;

    @Autowired
    private ProjectsFeignClient projectsFeignClient;


    @ApiOperation("缺陷密度排行")//用例系统和缺陷系统
    @GetMapping("/queryBugDensityBySystemTenantId")
    public ResultWrapper<List<ProjectDataVo>> queryBugDensityByTenantId(@RequestParam(value = "tenantId") String tenantId)  {
        ResultWrapper<List<ProjectDataVo>> wrapper = new ResultWrapper<>();
        List<String> projectIds = projectsFeignClient.queryProjectIds(tenantId);
        if (null==projectIds||projectIds.size()==0)return wrapper.success();
        List<ProjectDataVo> bugDensityCharts = apiService.queryBugDensityByTenantId(tenantId,projectIds);
        return wrapper.success(bugDensityCharts);
    }

    @ApiOperation("未解决缺陷模块分布图")
    @GetMapping("/queryUnresolvedBugModuleData")
    public ResultWrapper<List<BugStatusStatistics>> queryUnresolvedBugModuleData(@RequestParam("projectId") String projectId,@RequestParam("tenantId") String tenantId) {
        ResultWrapper<List<BugStatusStatistics>> wrapper = new ResultWrapper<>();
        List<BugStatusStatistics> bugStatusStatistics = bugStatusStatisticsApiService.queryUnresolvedBugModuleData(projectId,tenantId);
        return wrapper.success(bugStatusStatistics);
    }

    @ApiOperation("缺陷燃尽图")
    @GetMapping(value = "/queryUnsolvedBugDataByProjectId")
    public ResultWrapper<List<BugOverallTendencyChartDetailDto>> queryUnsolvedBugDataByProjectId(@RequestParam("projectId") String projectId) {
        ResultWrapper<List<BugOverallTendencyChartDetailDto>> wrapper = new ResultWrapper<>();
        List<BugOverallTendencyChartDetailDto> bugOverallTendencyChartDetailDtos =bugOverallTendencyChartApiService.queryUnsolvedBugDataByProjectId(projectId);
        bugOverallTendencyChartDetailDtos.forEach(bugOverallTendencyChartDetailDto->{
            bugOverallTendencyChartDetailDto.setViewTime(bugOverallTendencyChartDetailDto.getCreatedTime());
            bugOverallTendencyChartDetailDto.setCreatedTime(null);
        });
        return wrapper.success(bugOverallTendencyChartDetailDtos);
    }

    @ApiOperation("系统未解决缺陷排序统计图")//按数量desc 取前五
    @GetMapping(value = "/queryAllUnsolvedBugDataSortedByTenantId")
    public ResultWrapper<List<BugStatusStatistics>> queryAllUnsolvedBugDataSortedByTenantId(@RequestParam(value = "tenantId") String tenantId) {
        ResultWrapper<List<BugStatusStatistics>> wrapper = new ResultWrapper<>();
        List<String> projectIds = projectsFeignClient.queryProjectIds(tenantId);
        if (null==projectIds||projectIds.size()==0)return wrapper.success();
        List<BugStatusStatistics> bugOverallTendencyChartDetailDtos =bugOverallTendencyChartApiService.queryAllUnsolvedBugDataSortedByTenantId(tenantId,projectIds);
        return wrapper.success(bugOverallTendencyChartDetailDtos);
    }

    @ApiOperation("缺陷分类统计图")//
    @GetMapping(value = "/queryTop5TypeBugDataByTenantId")
    public ResultWrapper<List<BugStatusStatistics>> queryTop5TypeBugDataByTenantId(@RequestParam(value = "tenantId") String tenantId) {
        ResultWrapper<List<BugStatusStatistics>> wrapper = new ResultWrapper<>();
        List<String> projectIds = projectsFeignClient.queryProjectIds(tenantId);
        if (null==projectIds||projectIds.size()==0)return wrapper.success();
        List<BugStatusStatistics> bugOverallTendencyChartDetailDtos =bugStatusStatisticsApiService.queryTop5TypeBugDataByTenantId(tenantId,projectIds);
        return wrapper.success(bugOverallTendencyChartDetailDtos);
    }

    @ApiOperation("已完成系统数")//在测//延期//进度异常//已完成
    @GetMapping("/getFinishedProjectAmountByTenantId")
    public ResultWrapper<ProjectDataVo> getFinishedProjectAmountByTenantId(@RequestParam(value = "tenantId" ) String tenantId) {
        ResultWrapper<ProjectDataVo> wrapper = new ResultWrapper<>();
        List<String> projectIds = projectsFeignClient.queryProjectIds(tenantId);
        if (null==projectIds||projectIds.size()==0)return wrapper.success();
        ProjectDataVo amount = midProjectInfoService.getFinishedProjectAmountByTenantId(tenantId,projectIds);
        return wrapper.success(amount);
    }

    @ApiOperation("用例执行燃尽图")
    @GetMapping("/getNoExecCaseAmountByProjectId")
    public ResultWrapper<List<MtCaseExecChart>> getNoExecCaseAmountByProjectId(@RequestParam("projectId") String projectId) {
        ResultWrapper<List<MtCaseExecChart>> wrapper = new ResultWrapper<>();
        List<MtCaseExecChart> amount = midManualExecJobService.getNoExecCaseAmountByProjectId(projectId);
        return wrapper.success(amount);
    }
    @ApiOperation("需求覆盖分析图")
    @GetMapping("/getTestPointAmountAndCaseDesignAmountByProjectId")
    public ResultWrapper<List<ProjectDataVo>> getTestPointAmountAndCaseDesignAmountByProjectId(@RequestParam("tenantId") String tenantId,@RequestParam("projectId") String projectId) {
        ResultWrapper<List<ProjectDataVo>> wrapper = new ResultWrapper<>();
        List<ProjectDataVo> amount = midCaseDesignService.getTestPointAmountAndCaseDesignAmountByProjectId(tenantId,projectId);
        return wrapper.success(amount);
    }

    @ApiOperation("用例缺陷发现率,人均测试点设计率,人均用例设计率")
    @GetMapping("/getCaseBugFindRateByProjectId")
    public ResultWrapper<ProjectDataVo> getCaseBugFindRateByProjectId(@RequestParam("tenantId") String tenantId,@RequestParam("projectId") String projectId) {
        ResultWrapper<ProjectDataVo> wrapper = new ResultWrapper<>();
        ProjectDataVo projectDataVo=midCaseDesignService.getProjectEffData(projectId,tenantId);
        return wrapper.success(projectDataVo);
    }

    @ApiOperation("总体效能-用例缺陷发现率,人均测试点设计率,人均用例设计率,缺陷逃逸率")
    @GetMapping("/getCaseBugFindRateByTenantId")
    public ResultWrapper<ProjectDataVo> getCaseBugFindRateByTenantId(@RequestParam(value = "tenantId") String tenantId) {
        ResultWrapper<ProjectDataVo> wrapper = new ResultWrapper<>();
        List<String> projectIds = projectsFeignClient.queryProjectIds(tenantId);
        if (null==projectIds||projectIds.size()==0)return wrapper.success();
        ProjectDataVo projectDataVo=midCaseDesignService.getData(tenantId,projectIds);
        return wrapper.success(projectDataVo);
    }

    @ApiOperation("缺陷密度")
    @GetMapping("/queryBugDensity")//加了筛选
    public ResultWrapper<List<BugDensityChart>> queryBugDensity(@RequestParam("projectId") String projectId) {
        ResultWrapper<List<BugDensityChart>> wrapper = new ResultWrapper<>();
        List<BugDensityChart> bugDensityCharts = apiService.queryBugDensity(projectId);
        return wrapper.success(bugDensityCharts);
    }

    @GetMapping("/chartProgress")
    @ApiOperation("进度图查询")//加了筛选
    public ResultWrapper<List<MtTestChartDto>> chartProgress(@RequestParam("projectId") String projectId) {
        ResultWrapper<List<MtTestChartDto>> wrapper = new ResultWrapper<>();
        ChartProgressQueryDto queryDto=new ChartProgressQueryDto();
        queryDto.setProjectId(projectId);
        queryDto.setType(4);
        List<MtTestChartDto> chartList = chartApiService.queryChartProgress(queryDto);
        return wrapper.success(chartList);
    }

    @GetMapping("/getProjectIds")
    @ApiOperation("获取当前租户下的系统")//界面轮询的系统id
    public ResultWrapper<List<ProjectDataVo>> getProjectIds(@RequestParam("tenantId") String tenantId) {
        ResultWrapper<List<ProjectDataVo>> wrapper = new ResultWrapper<>();
        List<ProjectDataVo> chartList = chartApiService.getProjectIds(tenantId);
        return wrapper.success(chartList);
    }


    @ApiOperation("查询系统状态")//在测//延期//进度异常//已完成
    @PostMapping("/getProjectStatuss")
    public ResultWrapper<List<ProjectDataVo>> getProjectStatuss(@RequestBody List<String> projectIds) {
        ResultWrapper<List<ProjectDataVo>> wrapper = new ResultWrapper<>();
        if (null==projectIds||projectIds.size()==0)return wrapper.success();
        List<ProjectDataVo> amount = midProjectInfoService.getProjectStatuss(projectIds);
        return wrapper.success(amount);
    }

}
