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

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.testbug.api.service.BugManagementApiService;
import net.northking.cctp.testbug.dto.BugManagementAddDto;
import net.northking.cctp.testbug.dto.BugManagementDetailDto;
import net.northking.cctp.testbug.dto.BugManagementUpdateDto;
import net.northking.cctp.testbug.dto.IsCommitBugDetailDto;
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.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 缺陷管理 => Restful 类型 HTTP/HTTPS 接口
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */
@Api(tags = "缺陷管理")
@RequestMapping("/v1/BugManagements")
@RestController
public class BugManagementRestfulCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(BugManagementRestfulCtrl.class);

  @Autowired
  private BugManagementApiService apiService;

  /**
   * Restful接口-post： 新增缺陷管理
   *
   * @param inputDto         接口数据传输对象
   * @return 主键
   */
  @ApiOperation(value = "新增：缺陷管理")
  @PostMapping(
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> add(
      @RequestBody @Validated BugManagementAddDto inputDto)
  {
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String uuid = apiService.add(inputDto);
    return wrapper.success(uuid);
  }

  /**
   * Restful接口-put： 更新缺陷管理
   *
   * @param inputDto         接口数据传输对象
   * @return 修改的记录数量
   */
  @ApiOperation(value = "更新：缺陷管理")
  @PutMapping(
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> update(
      @RequestBody @Validated BugManagementUpdateDto inputDto) throws IllegalAccessException
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = apiService.updateByPK(inputDto);
    return wrapper.success(count);
  }

  /**
   * Restful接口-get： 获取缺陷管理
   *
   * @param uuid       主键
   * @return 接口数据传输对象
   */
  @ApiOperation(value = "获取：缺陷管理")
  @GetMapping(
      value = "/{uuid}",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<BugManagementDetailDto> findByPK(
      @PathVariable("uuid") String uuid)
  {
    ResultWrapper<BugManagementDetailDto> wrapper = new ResultWrapper<>();
    BugManagementDetailDto dto = apiService.findByPK(uuid);
    return wrapper.success(dto);
  }

  /**
   * Restful接口-delete： 删除缺陷管理
   *
   * @param uuid       主键
   * @return 删除的记录数量
   */
  @ApiOperation(value = "删除：缺陷管理")
  @DeleteMapping(
      value = "/{uuid}",
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> deleteByPK(
      @PathVariable("uuid") String uuid)
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = apiService.deleteByPK(uuid);
    return wrapper.success(count);
  }

  @ApiOperation("根据执行id或步骤id查询缺陷详情")
  @GetMapping("/findByLinkId")
  public ResultWrapper<List<BugManagementDetailDto>> findByLinkId(@RequestParam("linkId") String linkId) {

    ResultWrapper<List<BugManagementDetailDto>> wrapper = new ResultWrapper<>();

    List<BugManagementDetailDto> detailDtos = apiService.findByLinkId(linkId);

    return wrapper.success(detailDtos);
  }

  @ApiOperation("根据执行id或步骤id集合查询是否关联了缺陷")
  @PostMapping(value = "/findByLinkIdList",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Map<String,Integer>> findByLinkIdList(@RequestBody List<String> linkIdList) {
    ResultWrapper<Map<String,Integer>> wrapper = new ResultWrapper<>();
    List<IsCommitBugDetailDto> list= apiService.findByLinkIdList(linkIdList);
    Map<String,Integer> map=new HashMap<>();
    list.forEach(a->{
      map.put(a.getLinkId(),a.getIsSubmit());
    });
    return wrapper.success(map);
  }
}
