package com.hdkj.lt.io.controller;

import com.hdkj.attachments.client.entity.Attachment;
import com.hdkj.attachments.client.entity.AttachmentsParam;
import com.hdkj.attachments.client.response.ResponseData;
import com.hdkj.lt.io.service.IFileService;
import com.hdkj.lt.io.service.IObjectStorageService;
import com.hdkj.lt.modle.FileIdRequestDTO;
import com.hdkj.lt.modle.FileUploadDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.nio.charset.StandardCharsets;
import java.util.List;

@Api(tags = "附件相关")
@RestController
@RequiredArgsConstructor
@Validated
public class FileController {

    private final IFileService fileService;
    private final IObjectStorageService objectStorageService;

    /**
     * 附件下载
     */
    @GetMapping("/download")
    @ApiOperation(value = "附件下载")
    public void download(@Valid @NotNull(message = "附件ID不能为空") Long id, HttpServletResponse response) throws Exception {
        fileService.download(id, response);
    }

    /**
     * 附件批量下载
     */
    @PostMapping("/downloadZip")
    @ApiOperation(value = "附件批量下载")
    public void downloadZip(@Valid @RequestBody FileIdRequestDTO fileIdRequest, HttpServletResponse response) throws Exception {
        fileService.downloadZip(fileIdRequest.getIds(), response);
    }

    /**
     * 查询附件列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "查询附件列表")
    public ResponseData<List<Attachment>> list(@RequestBody AttachmentsParam attachmentsParam) throws Exception {
        return fileService.list(attachmentsParam);
    }

    /**
     * 附件信息详情
     */
    @GetMapping("/detail")
    @ApiOperation(value = "附件信息详情")
    public ResponseData<Attachment> detail(@Valid @NotNull(message = "附件ID不能为空") Long id) throws Exception {
        return fileService.detail(id);
    }

    /**
     * 多文件上传附件上传
     */
    @PostMapping("/uploads")
    @ApiOperation(value = "多文件上传附件上传")
    public ResponseData<List<Attachment>> uploads(@RequestParam("files") MultipartFile[] multipartFiles) throws Exception {
        return fileService.uploads(multipartFiles);
    }

    /**
     * 单附件上传
     */
    @PostMapping("/upload")
    @ApiOperation(value = "附件上传")
    public ResponseData<Attachment> upload(@RequestParam("file") MultipartFile multipartFile) throws Exception {
        return fileService.upload(multipartFile);
    }

    /**
     * 附件删除
     */
    @GetMapping("/delete")
    @ApiOperation(value = "附件删除")
    public ResponseData<String> delete(@Valid @NotNull(message = "附件ID不能为空") Long id) throws Exception {
        fileService.delete(id);
        return ResponseData.success("附件删除成功");
    }

    /**
     * 配电云主站附件查询
     */
    @GetMapping("/listObject")
    @ApiOperation(value = "附件查询")
    public ResponseData<List<FileUploadDTO>> listObject(@Valid @NotNull(message = "附件文件夹名不能为空") String fileDir) {
        return ResponseData.success(objectStorageService.listObject(fileDir));
    }

    /**
     * 配电云主站附件下载
     */
    @GetMapping("/getObject")
    @ApiOperation(value = "附件下载")
    public ResponseData<String> getObject(@Valid @NotNull(message = "附件全路径名不能为空") String objectName) {
        return ResponseData.success(new String(objectStorageService.getObject(objectName), StandardCharsets.UTF_8));
    }

    /**
     * 配电云主站附件删除
     */
    @GetMapping("/removeObject")
    @ApiOperation(value = "附件删除")
    public ResponseData<String> removeObject(@Valid @NotNull(message = "附件全路径名不能为空") String objectName) {
        objectStorageService.removeObject(objectName);
        return ResponseData.success("云主站附件删除成功");
    }

    @GetMapping("/feign/topo/getById")
    @ApiOperation(value = "附件信息详情")
    public byte[] getById(@Valid @NotNull(message = "附件ID不能为空") Long id) throws Exception {
        return fileService.getFileById(id).getData();
    }

}
