package com.zhgd.xmgl.modules.basicdata.controller.admin;

import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import com.alibaba.fastjson.JSONObject;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.modules.basicdata.api.camera.MonitorApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Map;

/**
 * @Title: Controller
 * @Description: 文件操作
 * @author： pds
 * @date：   2023-02-16
 * @version： V1.0
 */
@RestController
@RequestMapping("/xmgl/file")
@Slf4j
@Api(tags = "文件操作")
public class FileController {

    @Autowired
    private FileStorageService fileStorageService;

    /**
     * 上传文件到指定存储平台，成功返回文件信息
     */
    @OperLog(operModul = "文件管理", operType = "文件上传", operDesc = "文件上传")
    @ApiOperation(value = "文件上传", notes = "文件上传",  httpMethod="POST")
    @PostMapping("/upload")
    public Result<FileInfo> uploadPlatform(MultipartFile file) {
        return Result.success(fileStorageService.of(file)
                .setPlatform("minio-1")    //使用指定的存储平台
                .upload());
    }


    /**
     * 下载文件
     */
    @OperLog(operModul = "文件管理", operType = "文件下载", operDesc = "文件下载")
    @ApiOperation(value = "文件下载", notes = "文件下载",  httpMethod="POST")
    @ApiImplicitParam(name = "fileUrl", value = "文件路径", paramType = "query", dataType = "String")
    @PostMapping("/download")
    public void download(@RequestBody Map<String, Object> map, HttpServletResponse response) throws Exception {
        // 获取文件信息
        String fileUrl = MapUtils.getString(map, "fileUrl");
        FileInfo fileInfo = fileStorageService.getFileInfoByUrl(fileUrl);
        if (null == fileInfo) {
            throw new Exception("缩略图文件下载失败，文件不存在！fileInfo：" + fileInfo);
        }
        response.setHeader("Content-disposition", "attachment; filename=" + fileUrl.substring(fileUrl.lastIndexOf("/") + 1));
        response.setContentType("application/octet-stream");// 定义输出类型
        fileStorageService.download(fileInfo).outputStream(response.getOutputStream());
    }

    /**
     * 获取预览文件流
     */
    @OperLog(operModul = "文件管理", operType = "获取预览文件流", operDesc = "获取预览文件流")
    @ApiOperation(value = "获取预览文件流", notes = "获取预览文件流",  httpMethod="POST")
    @GetMapping("/preview")
    public void download(@RequestParam(value = "fileUrl", required = true) String fileUrl, HttpServletResponse response) throws Exception {
        // 获取文件信息
        FileInfo fileInfo = fileStorageService.getFileInfoByUrl(fileUrl);
        if (null == fileInfo) {
            throw new Exception("缩略图文件预览失败，文件不存在！fileInfo：" + fileInfo);
        }
        response.setHeader("Content-disposition", "attachment; filename=" + fileUrl.substring(fileUrl.lastIndexOf("/") + 1));
        response.setContentType("application/octet-stream");// 定义输出类型
        fileStorageService.download(fileInfo).outputStream(response.getOutputStream());
    }

    /**
     * 海康监控图片抓拍
     */
    @OperLog(operModul = "文件管理", operType = "文件上传", operDesc = "海康监控图片抓拍")
    @ApiOperation(value = "海康监控图片抓拍", notes = "海康监控图片抓拍",  httpMethod="POST")
    @ApiImplicitParam(name = "monitorCode", value = "监控点编码", paramType = "query", dataType = "String")
    @PostMapping("/manualCapture")
    public Result<FileInfo> manualCapture(@RequestBody Map<String, Object> map) {
        JSONObject jsonObject = MonitorApi.manualCapture(MapUtils.getString(map, "monitorCode"));
        String filePath = jsonObject.getJSONObject("data").getString("picUrl");
        File file = new File(filePath);
        return Result.success(fileStorageService.of(file)
                .setPlatform("minio-1")    //使用指定的存储平台
                .upload());
    }
}
