package com.qmth.sop.server.api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.JsonObject;
import com.qmth.boot.api.annotation.Aac;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.result.TBQualityProblemApplyResult;
import com.qmth.sop.business.entity.TBService;
import com.qmth.sop.business.service.QualityAnalyseService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.InfluenceDegreeEnum;
import com.qmth.sop.common.enums.QualityAnalyseGroupEnum;
import com.qmth.sop.common.enums.QualityProblemReasonEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Map;

/**
 * 质量分析监控 控制器
 *
 * @author: shudonghui
 * @date: 2023-08-14 15:55:18
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Api(tags = "质量分析监控 Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/quality/analyse")
public class QualityAnalyseController {


    @Resource
    QualityAnalyseService qualityAnalyseService;


    @ApiOperation(value = "服务单元列表接口")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "服务单元列表", response = TBService.class)})
    public Result list(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                       @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime) {
        List<TBService> list = qualityAnalyseService.list(startTime,endTime);
        return ResultUtil.ok(list);
    }


    /**
     * 4.质量问题总体盘点
     * ①饼图：显示已完成审核，且确定为质量问题（审核结果非“不是问题”）的质量问题的数量统计，以及各影响度的质量问题事件的占比；
     * ②【数据下钻】点击影响度行数据，可显示影响度下质量问题查询明细，筛选条件为：年度+服务单元+非“不是问题”+影响度；
     * ③归因雷达图：按找质量问题归因维度展示雷达图，以及全部及各供应商的雷达图。不提供【数据下钻】；
     */
    //质量问题总体盘点饼图
    @ApiOperation(value = "质量问题总体盘点饼图")
    @RequestMapping(value = "/pie", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "质量问题总体盘点饼图", response = Map.class)})
    public Result pie(@ApiParam(value = "服务单元", required = true) @RequestParam Long serviceUnitId) {
        Map<String, Object> map = qualityAnalyseService.pie(serviceUnitId);
        return ResultUtil.ok(map);
    }

    //质量问题总体盘点归因雷达图
    @ApiOperation(value = "质量问题总体盘点归因雷达图")
    @RequestMapping(value = "/radar", method = RequestMethod.POST)
    public Result radar(@ApiParam(value = "服务单元", required = true) @RequestParam Long serviceUnitId,
                        @ApiParam(value = "影响度分组", required = true) @RequestParam QualityAnalyseGroupEnum group) {
        Map<String, Map<String, Object>> map = qualityAnalyseService.radar(serviceUnitId, group);
        return ResultUtil.ok(map);
    }


    /**
     * 5.质量问题审核进度=（已完成审核的质量问题总数÷总数）×100%；不提供【数据下钻】；
     */

    @ApiOperation(value = "质量问题审核进度")
    @RequestMapping(value = "/progress", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "质量问题审核进度", response = Double.class)})
    public Result progress(@ApiParam(value = "服务单元", required = true) @RequestParam Long serviceUnitId) {
        Double progress = qualityAnalyseService.progress(serviceUnitId);
        return ResultUtil.ok(progress);
    }

    /**
     * * 6.影响度/归因    供应商/大区分布及对比：
     * * 可视化展示如图，供应商/大区的归属，仅针对审核完成且定责为供应商的人员，或大区管辖的项目；
     * * 【数据下钻】点击行数据，可查看质量问题数据明细。
     */
    @ApiOperation(value = "影响度/归因")
    @RequestMapping(value = "/influence", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "影响度/归因", response = JSONObject.class)})
    public Result influence(@ApiParam(value = "服务单元", required = true) @RequestParam Long serviceUnitId,
                            @ApiParam(value = "影响度分组", required = true) @RequestParam QualityAnalyseGroupEnum group) {
        return ResultUtil.ok(qualityAnalyseService.influence(serviceUnitId, group));
    }

    //* ②【数据下钻】点击影响度行数据，可显示影响度下质量问题查询明细，筛选条件为：年度+服务单元+非“不是问题”+影响度；
    //质量问题查询明细

    @ApiOperation(value = "质量问题饼图明细")
    @RequestMapping(value = "/supplier/detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "质量问题饼图明细", response = TBQualityProblemApplyResult.class)})
    public Result detail(@ApiParam(value = "服务单元", required = true) @RequestParam Long serviceUnitId,
                         @ApiParam(value = "供应商") @RequestParam(required = false) InfluenceDegreeEnum degree,
                         @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                         @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<TBQualityProblemApplyResult> resultIPage = qualityAnalyseService.detail(new Page<>(pageNumber, pageSize), serviceUnitId, degree);

        return ResultUtil.ok(resultIPage);
    }

    //* ②【数据下钻】点击影响度行数据，可显示影响度下质量问题查询明细，筛选条件为：年度+服务单元+非“不是问题”+影响度；
    //质量问题查询明细

    @ApiOperation(value = "影响度/归因明细")
    @RequestMapping(value = "/influence/detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "影响度/归因明细", response = TBQualityProblemApplyResult.class)})
    public Result influence(@ApiParam(value = "服务单元", required = true) @RequestParam Long serviceUnitId,
                            @ApiParam(value = "供应商") @RequestParam(required = false) Long supplierId,
                            @ApiParam(value = "大区") @RequestParam(required = false) Long region_id,
                            @ApiParam(value = "影响度") @RequestParam(required = false) InfluenceDegreeEnum Influence_degree,
                            @ApiParam(value = "问题原因类型") @RequestParam(required = false) QualityProblemReasonEnum reason,
                            @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                            @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        IPage<TBQualityProblemApplyResult> resultIPage = qualityAnalyseService.influence(new Page<>(pageNumber, pageSize), serviceUnitId, supplierId,region_id,Influence_degree,reason);

        return ResultUtil.ok(resultIPage);
    }

}