/*
* Copyright (c) 京北方信息技术股份有限公司 Corporation 2021 . All rights reserved.
*
*/
package net.northking.cctp.dw.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.db.Pagination;
import net.northking.cctp.common.http.QueryByPage;
import net.northking.cctp.common.http.ResultWrapper;

import net.northking.cctp.common.security.authentication.NKSecurityContext;
import net.northking.cctp.dw.api.service.BugStatusStatisticsApiService;
import net.northking.cctp.dw.dto.BugStatusStatisticsDetailDto;
import net.northking.cctp.dw.dto.BugStatusStatisticsQueryDto;
import net.northking.cctp.dw.dto.ChartDataGeneQueryDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "缺陷状态统计")
@RequestMapping("/v1")
@RestController
public class BugStatusStatisticsQueryCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugStatusStatisticsQueryCtrl.class);

  @Autowired
  private BugStatusStatisticsApiService apiService;

  /**
   * 查询用户列表
   *
   * @param queryByPage  查询条件
   * @return 数据列表
   */
  @ApiOperation(value = "分页查询列表：缺陷状态统计")
  @PostMapping(value = "/paging/query/BugStatusStatisticss",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Pagination<BugStatusStatisticsDetailDto>> pagingQuery(
      @RequestBody @Validated QueryByPage<BugStatusStatisticsQueryDto> queryByPage)
  {
    ResultWrapper<Pagination<BugStatusStatisticsDetailDto>> wrapper = new ResultWrapper<>();
    Pagination<BugStatusStatisticsDetailDto> page = apiService.pagingQuery(queryByPage);

    return wrapper.success(page);
  }

  @ApiOperation(value = "导出Excel：缺陷状态统计")
  @PostMapping(value = "/excel/export/BugStatusStatisticss",
      produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void exportExcel(
      @RequestBody @Validated QueryByPage<BugStatusStatisticsQueryDto> queryByPage,
      HttpServletResponse response)
  {
    // 定义导出的Excel在客户端浏览器上提示下载的文件名，不写后缀
    String fileName = "CCTP-缺陷状态统计";
    apiService.exportExcel(response, queryByPage, fileName);
  }

  @ApiOperation(value = "下载Excel模板：缺陷状态统计")
  @PostMapping(value = "/excel/template/BugStatusStatisticss",
      produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public void downloadTemplate(HttpServletResponse response)
  {
    String fileName = "CCTP模板-缺陷状态统计";
    apiService.downloadTemplate(response, fileName);
  }

  @ApiOperation(value = "导入Excel：缺陷状态统计")
  @PostMapping(value = "/excel/import/BugStatusStatisticss",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<Integer> importExcel(
      @RequestParam("file") MultipartFile file)
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    int count = apiService.importExcel(file);

    return wrapper.success(count);
  }

  /**
   * 总体缺陷状态数据统计(按模块、人员)
   * 本周缺陷状态数据统计(按模块、人员)
   * 当日缺陷状态数据统计(按人员)
   *
   * @param
   * @return 数据列表
   */
  @ApiOperation(value = "查询列表：缺陷状态统计")
  @PostMapping(value = "/list/query/BugStatusStatisticss",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<List<BugStatusStatisticsDetailDto>> listQuery(@RequestBody ChartDataGeneQueryDto chartDataGeneQueryDto)
  {
    ResultWrapper<List<BugStatusStatisticsDetailDto>> wrapper = new ResultWrapper<>();
    chartDataGeneQueryDto.setTenantId(NKSecurityContext.getTenantId());
    if(null==chartDataGeneQueryDto.getRecordType()){
      chartDataGeneQueryDto.setRecordType("0");
    }
    List<BugStatusStatisticsDetailDto> page = apiService.listQuery(chartDataGeneQueryDto);

    return wrapper.success(page);
  }
}


