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

import com.alibaba.cloud.commons.lang.StringUtils;
import com.hzbank.testteam.autotest.dependencies.baseDefineDependency.vo.ResponseVO;
import io.swagger.annotations.ApiParam;
import net.northking.cctp.attachment.api.service.AttachFileService;
import net.northking.cctp.attachment.dto.ChunkFiles;
import net.northking.cctp.attachment.dto.FileMessage;
import net.northking.cctp.common.dto.AssociatedFilesDto;
import net.northking.cctp.common.enums.FileBusinessTypeEnum;
import net.northking.cctp.common.http.ResultWrapper;
import net.northking.cctp.common.security.authentication.NKSecurityContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 附件下载
 *
 * @author : xiusong.xie
 * @version : 1.0
 */
@RestController
public class AttachFileCtrl
{
  @Autowired
  private AttachFileService attachFileService;

  /**
   * 下载文件
   *
   * @param fileId   文件ID
   * @param response response
   */
  @GetMapping(value = {"/v1/download/{fileId}"})
  public void downloadByRestful(@PathVariable("fileId") String fileId,
                                HttpServletResponse response, HttpServletRequest request)
  {
    attachFileService.download(fileId, response, request);
  }

  /**
   * 下载文件
   *
   * @param fileId   文件ID
   * @param response response
   */
  @GetMapping(value = {"/pub/download/{fileId}"})
  public void downloadByURL(@PathVariable("fileId") String fileId,
                            HttpServletResponse response, HttpServletRequest request)
  {
    attachFileService.downloadPub(fileId, response, request);
  }


  @PostMapping(value = {"/v1/upload"},
      produces = MediaType.APPLICATION_JSON_VALUE,
      consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<String> upload(HttpServletRequest request, HttpServletResponse response,
                                      @RequestParam("file") MultipartFile multipartFile,
                                      @RequestParam(value = "objId", required = false) String objId,
                                      @RequestParam("businessCode") String businessCode)
  {
    ResultWrapper<String> wrapper = new ResultWrapper<>();
    FileBusinessTypeEnum fileBusinessTypeByCode = FileBusinessTypeEnum.getFileBusinessTypeByCode(businessCode);
    if (null == fileBusinessTypeByCode) {
      wrapper.setMessage("缺少文件类型信息");
      return wrapper;
    }
    return attachFileService.upload(response, multipartFile, objId, fileBusinessTypeByCode);
  }

  @PostMapping(value = {"/v1/update"},
          produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
          consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
  public ResultWrapper<String> update(@RequestBody Map<String,String> file)
  {
    return attachFileService.update(file);
  }

  /**
   * 文件关联文件
   * @param associatedFilesDto 关联文件请求对象
   * @return
   */
  @PostMapping(value = {"/pub/associatedFiles"})
  public ResultWrapper<String> associatedFiles(@ApiParam(value = "关联文件请求对象") @RequestBody AssociatedFilesDto associatedFilesDto)
  {
    return attachFileService.associatedFiles(associatedFilesDto.getObjId(), associatedFilesDto.getFileIds());
  }

  @GetMapping(value = {"/v1/delete/{fileId}"})
  public ResultWrapper<String> delete(@PathVariable("fileId") String fileId)
  {
    return attachFileService.delete(fileId);
  }

  /**
   * 根据objId和businessCode 删除文件
   * @param objectId 对象Id
   * @param businessCode 文件类型，可不填
   * @return
   */
  @GetMapping(value = {"/pub/deleteFileByObjIdAndBusinessCode"})
  public ResultWrapper<String> deleteFileByObjIdAndBusinessCode(@RequestParam("objectId") String objectId, @RequestParam(value = "businessCode", required = false) String businessCode)
  {
    return attachFileService.deleteFileByObjIdAndBusinessCode(objectId, businessCode);
  }
  /**
   * 根据objIds 删除文件
   * @param objectIds 对象Ids
   * @return
   */
  @PostMapping(value = "/pub/deleteFileByObjIds")
  ResultWrapper<String> deleteFileByObjIds(@RequestBody List<String> objectIds){
    return attachFileService.deleteFileByObjIds(objectIds);
  }

  /**
   *
   * @param multipartFile 文件
   * @param chunkIndex    分片文件下标
   * @param uploadId      分片上传id
   * @return
   */
  @PostMapping(value = {"/v1/uploadChunk"},
          produces = MediaType.APPLICATION_JSON_VALUE,
          consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<Boolean> uploadChunkFile(@RequestParam("file") MultipartFile multipartFile,
                                               @RequestParam("chunkIndex") String chunkIndex,
                                               @RequestParam("uploadId") String uploadId )
  {
    return attachFileService.uploadChunkFile(multipartFile, uploadId, chunkIndex);
  }

  /**
   * 合并文件
   * @param chunkFiles   分片上传id
   * @return
   */
  @PostMapping(value = "/v1/mergeChunkFiles",
          produces = MediaType.APPLICATION_JSON_VALUE,
          consumes = MediaType.APPLICATION_JSON_VALUE)
  public ResultWrapper<String> mergeChunkFiles(@RequestBody ChunkFiles chunkFiles)
  {
    return attachFileService.mergeChunkFiles(chunkFiles);
  }


  @PostMapping(value = {"/pub/upload"},
          produces = MediaType.APPLICATION_JSON_VALUE,
          consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<Map<String, Object>> pubUpload(HttpServletResponse response,
                                                      @RequestParam("file") MultipartFile multipartFile,
                                                      @RequestParam("tenantId") String tenantId,
                                                      @RequestParam(value = "objId", required = false) String objId,
                                                      @RequestParam("businessCode") String businessCode,
                                                      @RequestParam(value = "attachId", required = false) String attachId) {
    ResultWrapper<Map<String, Object>> wrapper = new ResultWrapper<>();
    FileBusinessTypeEnum fileBusinessTypeByCode = FileBusinessTypeEnum.getFileBusinessTypeByCode(businessCode);
    if (null == fileBusinessTypeByCode) {
      wrapper.setMessage("缺少文件类型信息");
      return wrapper;
    }
    if (StringUtils.isBlank(tenantId)) {
      wrapper.setMessage("缺少租户id信息");
      return wrapper;
    }
    NKSecurityContext.setTenantId(tenantId);
    return attachFileService.uploadResource(response, multipartFile, objId, fileBusinessTypeByCode, attachId);
  }

  @PostMapping(value = {"/v1/uploadForApp"},
          produces = MediaType.APPLICATION_JSON_VALUE,
          consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
  public ResultWrapper<FileMessage> uploadForApp(HttpServletRequest request, HttpServletResponse response,
                                                 @RequestParam("file") MultipartFile multipartFile)
  {
    return attachFileService.uploadForApp(response, multipartFile);
  }

  /**
   * 文件清理 monster使用接口
   * @return
   */
  @PostMapping(value = {"/pub/clearAttachments"})
  public ResponseVO<String> clearAttachments()
  {
    return attachFileService.clearAttachments();
  }

  /**
   * 删除场景任务刷新文件附件
   * @return
   */
  @PostMapping(value = {"/pub/clearSceneTaskAttachments"})
  public ResponseVO<String> clearSceneTaskAttachments(@RequestParam(value = "objId") String objId,
                                                      @RequestParam("businessCode") String businessCode,
                                                      @RequestParam("noDeleteFileId") String noDeleteFileId)
  {
    return attachFileService.clearSceneTaskAttachments(objId, businessCode, noDeleteFileId);
  }

}
