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

import cn.gjing.tools.auth.annotation.RequiredPermissions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.northking.cctp.common.http.ResultWrapper;

import net.northking.cctp.platform.dto.comlibrary.*;
import net.northking.cctp.platform.api.comlibrary.service.CpComponentLibraryApiService;
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 org.springframework.web.multipart.MultipartFile;

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


/**
 * 平台组件库 => Restful 类型 HTTP/HTTPS 接口
 *
 * @author maven-cctp-plugin
 * @since 1.0
 */
@Api(tags = "组件库:平台组件库")
@RequestMapping("/v1/CpComLibrary")
@RestController
public class CpComponentLibraryRestfulCtrl
{
  /**
   * 日志
   */
  private final static Logger logger = LoggerFactory.getLogger(CpComponentLibraryRestfulCtrl.class);

  @Autowired
  private CpComponentLibraryApiService apiService;


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

  /**
   * Restful接口-get： 获取组件库信息
   *
   * @param target       主键
   * @return 接口数据传输对象
   */
  @ApiOperation(value = "获取组件库信息")
  @PostMapping( value = "/getLibInfoByName",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<CpComponentLibraryDetailDto> getLibInfoByName(
          @RequestBody @Validated CpComponentLibraryAddDto target)
  {
    ResultWrapper<CpComponentLibraryDetailDto> wrapper = new ResultWrapper<>();
    CpComponentLibraryDetailDto dto = apiService.getLibInfoByName(target);
    return wrapper.success(dto);
  }


  /**
   * Restful接口-put： 更新平台组件库
   *
   * @param inputDto         接口数据传输对象
   * @return 修改的记录数量
   */
  @ApiOperation(value = "更新：平台组件库-启用禁用")
  @RequiredPermissions("ui-updateLibFile")
  @PutMapping(
      produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
      consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<Integer> update(
      @RequestBody @Validated CpComponentLibraryUpdateDto inputDto) throws IllegalAccessException
  {
    ResultWrapper<Integer> wrapper = new ResultWrapper<>();
    Integer count = apiService.updateByPK(inputDto);
    return wrapper.success(count);
  }


  /**
   * 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(value = "导入平台组件库:第一次")
  @RequiredPermissions("ui-uploadLibrary")
  @PostMapping(value = "/import/uploadLibrary",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<CpComponentLibraryAddDto> uploadLibrary(
          @RequestParam("file") MultipartFile file)
  {
    ResultWrapper<CpComponentLibraryAddDto> wrapper = new ResultWrapper<>();
    CpComponentLibraryAddDto fileId = apiService.importComponentLibrary(file);

    return wrapper.success(fileId);
  }


  @ApiOperation(value = "导入平台组件库：版本更新确认")
  @PostMapping(value = "/import/updateLibrary",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> updateLibrary(
          @RequestBody @Validated CpComponentLibraryAddDto fileId)
  {
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    String library = apiService.updateLibrary(fileId);

    return wrapper.success(library);
  }


  @ApiOperation(value = "组件库列表:模糊搜索")
  @RequiredPermissions("basicComponent")
  @PostMapping(value = "/query/selectByLike",
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<List<CpComLibraryTree>> selectByLike(
          @RequestBody @Validated CpComponentLibraryQueryDto query)
  {
    ResultWrapper<List<CpComLibraryTree>> wrapper = new ResultWrapper<>();
    List<CpComLibraryTree> list = apiService.selectByLike(query);

    return wrapper.success(list);
  }

  @ApiOperation(value = "下载：组件库原文件下载")
  @RequiredPermissions("ui-downloadLibFile")
  @GetMapping(value = "/downloadLibFile/{libId}")
  public void downloadLibFile( @PathVariable("libId") String libId, HttpServletResponse response)
  {
    apiService.downloadLibFile(response,libId);
  }



}
