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.common.security.authentication.NKSecurityContext;
import net.northking.cctp.dw.api.service.BugChartService;
import net.northking.cctp.dw.api.service.BugOverallTendencyChartApiService;
import net.northking.cctp.dw.api.service.BugStatusStatisticsApiService;
import net.northking.cctp.dw.api.service.ChartApiService;
import net.northking.cctp.dw.db.entity.BugStatusStatistics;
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.ProjectDataVo;
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("/v1/ScreenData")
@Api(tags = "大屏展示图表")
public class MobileScreenDataCtrl {
    @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()  {
        ResultWrapper<List<ProjectDataVo>> wrapper = new ResultWrapper<>();
        List<String> projectIds = projectsFeignClient.queryProjectIds(NKSecurityContext.getTenantId());
        if (null==projectIds||projectIds.size()==0)return wrapper.success();
        List<ProjectDataVo> bugDensityCharts = apiService.queryBugDensityByTenantId(NKSecurityContext.getTenantId(),projectIds);
        return wrapper.success(bugDensityCharts);
    }

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

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

    @ApiOperation("已完成系统数")//在测//延期//进度异常//已完成
    @GetMapping("/getFinishedProjectAmountByTenantId")
    public ResultWrapper<ProjectDataVo> getFinishedProjectAmountByTenantId() {
        ResultWrapper<ProjectDataVo> wrapper = new ResultWrapper<>();
        List<String> projectIds = projectsFeignClient.queryProjectIds(NKSecurityContext.getTenantId());
        if (null==projectIds||projectIds.size()==0)return wrapper.success();
        ProjectDataVo amount = midProjectInfoService.getFinishedProjectAmountByTenantId(NKSecurityContext.getTenantId(),projectIds);
        return wrapper.success(amount);
    }
    @ApiOperation("总体效能-用例缺陷发现率,人均测试点设计率,人均用例设计率,缺陷逃逸率")
    @GetMapping("/getCaseBugFindRateByTenantId")
    public ResultWrapper<ProjectDataVo> getCaseBugFindRateByTenantId() {
        ResultWrapper<ProjectDataVo> wrapper = new ResultWrapper<>();
        String tenantId1 = NKSecurityContext.getTenantId();
        List<String> projectIds = projectsFeignClient.queryProjectIds(NKSecurityContext.getTenantId());
        if (null==projectIds||projectIds.size()==0)return wrapper.success();
        ProjectDataVo projectDataVo=midCaseDesignService.getData(NKSecurityContext.getTenantId(),projectIds);
        return wrapper.success(projectDataVo);
    }
}
