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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.exception.PlatformRuntimeException;
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.testbug.api.service.BugSearchApiService;
import net.northking.cctp.testbug.constants.TestBugError;
import net.northking.cctp.testbug.dto.BugSearchDetailDto;
import net.northking.cctp.testbug.dto.BugSearchQueryDto;
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.ArrayList;
import java.util.List;

@Api(tags = "缺陷条件搜索表")
@RequestMapping("/v1")
@RestController
public class BugSearchQueryCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugSearchQueryCtrl.class);

  @Autowired
  private BugSearchApiService apiService;

  /**
   * 分页查询 缺陷条件搜索表 列表
   *
   * @param queryByPage  查询条件
   * @return 数据列表
   */
  @ApiOperation(value = "分页查询列表：缺陷条件搜索表")
  @RequiredPermissions("ui-mtDefectManage")
  @PostMapping(value = "/paging/query/BugSearchs",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<List<BugSearchDetailDto>> pagingQuery(
          @RequestBody @Validated BugSearchQueryDto queryByPage)
  {
    ResultWrapper<List<BugSearchDetailDto>>wrapper =null;
      List<BugSearchDetailDto> bugSearchDetailDtos=null;
    try {
      wrapper = new ResultWrapper<>();
      queryByPage.setTenantId(NKSecurityContext.getTenantId());
      List<BugSearchDetailDto> page = apiService.pagingQuery2(queryByPage);
      //将json字符串转化为java对象
        bugSearchDetailDtos=new ArrayList<>();
      if(null!=page&&page.size()!=0){
          for (BugSearchDetailDto bugSearchDetailDto:page){
            BugSearchDetailDto bugSearchDetailDto1 = JSON.parseObject(bugSearchDetailDto.getExpands(), BugSearchDetailDto.class);
            bugSearchDetailDto1.setExpands(null);
            bugSearchDetailDtos.add(bugSearchDetailDto1);
          }
      }
    }catch(Exception e){
      logger.error("QueryByPage",e);
      throw new PlatformRuntimeException(TestBugError.QUERY_BUG_SEARCH_FAILED);
    }
    return wrapper.success(bugSearchDetailDtos);
  }

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

  @ApiOperation(value = "下载Excel模板：缺陷条件搜索表")
  @PostMapping(value = "/excel/template/BugSearchs",
      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/BugSearchs",
      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);
  }


}


