package com.casic.missiles.controller.business;

import com.casic.missiles.core.base.controller.ExportController;
import com.casic.missiles.dto.ReturnDTO;
import com.casic.missiles.dto.ReturnUtil;
import com.casic.missiles.dto.business.board.*;
import com.casic.missiles.enums.ExportEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.model.exception.enums.CoreExceptionEnum;
import com.casic.missiles.service.business.IBusinessBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @Description: 业务看板
 *          注：所有证书统计相关sql都是统计部门/部门出具了非草稿状态的证书
 * @Author: wangpeng
 * @Date: 2023/4/7 16:44
 */
@Api(tags = "业务看板接口")
@Slf4j
@RestController
@RequestMapping("/business/board")
public class BusinessBoardController extends ExportController {
    @Autowired
    private IBusinessBoardService boardService;

    @ApiOperation("实时工作统计")
    @GetMapping("/workStatistics")
    public ReturnDTO<List<BoardWorkStatisticsResponse>> workStatistics() throws ExecutionException, InterruptedException {
        return ReturnUtil.success(boardService.workStatistics());
    }

    /**
     * 个人财务产值计算公式：合同实际收款金额/合同总样品数 *（个人对该样品出具证书的总权重/该样品出具所有证书的总权重）
     * 合同实际收款金额使用人员检测样品的委托书关联找到合同获取
     * 个人对该样品出具证书的总权重：部门人员对本人出具证书完成的样品的证书之和
     * 该样品出具所有证书的总权重：部门人员涉及的出具证书完成的样品的证书之和
     * 默认报告的权重都是1
     */
    @ApiOperation("部门产值分析")
    @PostMapping("/deptOutput")
    public ReturnDTO<BoardDeptOutputListResponse> deptOutput(@RequestBody BoardBaseRequest request) {
        return ReturnUtil.success(boardService.deptOutputList(request));
    }

    /**
     * 接口返回条件过滤后数据，部门有人员无查询年分月份的数据不返回
     */
    @ApiOperation("业务工作量分析")
    @PostMapping("/workload")
    public ReturnDTO<List<BoardWorkloadResponse>> workload(@RequestBody @Validated(BoardWorkloadValid.class) BoardBaseRequest request, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        return ReturnUtil.success(boardService.workload(request));
    }

    @ApiOperation("部门综合分析")
    @PostMapping("/deptComprehensive")
    public ReturnDTO<List<BoardDeptComprehensiveResponse>> deptComprehensive(@RequestBody BoardDeptComprehensiveRequest request) {
        return ReturnUtil.success(boardService.deptComprehensive(request));
    }

    @ApiOperation("实时工作统计导出")
    @GetMapping("/workStatistics/export")
    public void workStatisticsExport() throws IOException, ExecutionException, InterruptedException {
        List<BoardWorkStatisticsResponse> list = boardService.workStatistics();
        super.exportExcel(BoardWorkStatisticsResponse.class, list, ExportEnum.WORK_STATISTICS_EXPORT.getSheetName());
    }

    @ApiOperation("部门产值分析导出")
    @PostMapping("/deptOutput/export")
    public void deptOutputExport(@RequestBody BoardBaseRequest request) throws IOException {
        List<BoardDeptOutputResponse> list = boardService.deptOutput(request);
        super.exportExcel(BoardDeptOutputResponse.class, list, ExportEnum.DEPT_OUTPUT_EXPORT.getSheetName());
    }

    @ApiOperation("业务工作量分析导出")
    @PostMapping("/workload/export")
    public void workloadExport(@RequestBody @Validated(BoardWorkloadValid.class) BoardBaseRequest request, BindingResult bindingResult) throws IOException {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(CoreExceptionEnum.REQUEST_NULL.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        List<BoardWorkloadResponse> list = boardService.workload(request);
        super.exportExcel(BoardWorkloadResponse.class, list, ExportEnum.WORKLOAD_EXPORT.getSheetName());
    }

    @ApiOperation("部门综合分析导出")
    @PostMapping("/deptComprehensive/export")
    public void deptComprehensiveExport(@RequestBody BoardDeptComprehensiveRequest request) throws IOException {
        List<BoardDeptComprehensiveResponse> list = boardService.deptComprehensive(request);
        super.exportExcel(BoardDeptComprehensiveResponse.class, list, ExportEnum.DEPT_COMPREHENSIVE_EXPORT.getSheetName());
    }

}
