package com.hdkj.lt.io.feign;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.io.FileUtil;
import com.hdkj.attachments.client.entity.Attachment;
import com.hdkj.attachments.client.response.ResponseData;
import com.hdkj.hussar.ApiResponse;
import com.hdkj.lt.feign.IFileApiClient;
import com.hdkj.lt.io.service.IFileService;
import com.hdkj.lt.io.service.IObjectStorageService;
import com.hdkj.lt.modle.FileInfoDTO;
import com.hdkj.lt.modle.FileRequest;
import com.hdkj.lt.modle.FileUploadDTO;
import com.hdkj.lt.utils.file.FileExtUtils;
import com.hdkj.lt.utils.file.FileUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @Description
 * @Author wcx
 * @Date 2025/6/3 17:58
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class FileApiClient implements IFileApiClient {

    private final IFileService fileService;
    private final IObjectStorageService objectStorageService;

    @PostMapping(IFileApiClient.API_IO_SERVICE_FILE_UPLOAD)
    public ApiResponse<FileInfoDTO> upload(@RequestBody FileRequest request) {
        Assert.isTrue(!StringUtils.isAnyEmpty(request.getFileContent(), request.getFileName()), "附件内容和附件名称不允许为空!");
        // 上传临时文件信息, 成图服务文件名防止不加后缀时处理方式，配电云主站不存在
        String fileName = request.getFileName();
        if (!StringUtils.endsWithAny(fileName.toLowerCase(), ".svg", ".xml")) {
            fileName = String.format("%s.svg", request.getFileName());
        }
        File file = FileUtils.customFileByPath(fileName);
        try {
            // 生成本地SVG或者XML临时文件
            FileUtil.writeBytes(request.getFileContent().getBytes(StandardCharsets.UTF_8), file);
            // 上传文件
            MultipartFile multipartFile = FileUtils.file2MultipartFile(file);
            ResponseData<Attachment> upload = fileService.upload(multipartFile); // 文件上传
            if (Integer.compare(upload.getCode(), ResponseData.DEFAULT_SUCCESS_CODE) == 0) { // 上传成功转换输出
                FileInfoDTO fileInfoDTO = BeanUtil.copyProperties(upload.getData(), FileInfoDTO.class);
                return ApiResponse.success(fileInfoDTO);
            } else { // 上传失败提示错误
                return ApiResponse.fail(upload.getMessage());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            return ApiResponse.fail(ex.getMessage());
        } finally {
            FileExtUtils.deleteTempFile(file);
        }
    }

    @PostMapping(IFileApiClient.API_IO_SERVICE_FILE_GET)
    public ApiResponse<byte[]> getFileById(@RequestBody FileRequest request) {
        Assert.isTrue(ObjectUtils.isNotEmpty(request.getFileId()), "附件ID不允许为空!");
        try {
            ResponseData<byte[]> fileDate = fileService.getFileById(request.getFileId());
            return ApiResponse.success(fileDate.getData());
        } catch (Exception ex) {
            ex.printStackTrace();
            return ApiResponse.fail(ex.getMessage());
        }
    }

    @GetMapping(IFileApiClient.API_IO_SERVICE_LIST_OBJECT)
    public ApiResponse<List<FileUploadDTO>> listObject(@RequestParam("fileDir") String fileDir) {
        Assert.isTrue(StringUtils.isNotEmpty(fileDir), "文件夹名称不允许为空!");
        return ApiResponse.success(objectStorageService.listObject(fileDir));
    }

    @GetMapping(IFileApiClient.API_IO_SERVICE_GET_OBJECT)
    public ApiResponse<byte[]> getObject(@RequestParam("objectName") String objectName) {
        Assert.isTrue(StringUtils.isNotEmpty(objectName), "附件全路径名称不允许为空!");
        return ApiResponse.success(objectStorageService.getObject(objectName));
    }

    @GetMapping(IFileApiClient.API_IO_SERVICE_REMOVE_OBJECT)
    public ApiResponse<String> removeObject(@RequestParam("objectName") String objectName) {
        Assert.isTrue(StringUtils.isNotEmpty(objectName), "附件全路径名称不允许为空!");
        objectStorageService.removeObject(objectName);
        return ApiResponse.success("配电云主站异动附件删除成功");
    }

}
