package com.ema.admin.modules.system.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.ema.admin.modules.system.converter.SysFileConverter;
import com.ema.admin.modules.system.entity.SysFile;
import com.ema.admin.modules.system.service.SysAuthService;
import com.ema.admin.modules.system.service.SysFileService;
import com.ema.common.exceptions.MyAssert;
import com.ema.common.utils.CodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.dromara.x.file.storage.core.FileInfo;
import org.dromara.x.file.storage.core.FileStorageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

/**
 * 文件上传表 前端控制器
 */
@Api(tags = "系统文件管理")
@Slf4j
@Validated
@RestController
public class SysFileController {

    @Resource
    private FileStorageService fileStorageService;
    @Resource
    private SysFileService sysFileService;
    @Resource
    private SysAuthService sysAuthService;
    @Resource
    private SysFileConverter sysFileConverter;

    @SneakyThrows
    @ApiOperation(value = "文件上传")
    @PostMapping("/upload/file")
    public SysFile upload(@NotNull MultipartFile file, Integer fileType) {
        return uploadFile(file);
    }

    @SneakyThrows
    @ApiOperation(value = "批量文件上传")
    @PostMapping("/upload/batch")
    public List<SysFile> batchUpload(@NotNull MultipartFile[] files) {
        List<SysFile> sysFiles = CollUtil.newArrayList();
        for (MultipartFile file : files) {
            sysFiles.add(uploadFile(file));
        }
        return sysFiles;
    }

    /**
     * 文件上传到服务器
     *
     * @param file     文件
     * @return 上传后的文件信息
     */
    private SysFile uploadFile(MultipartFile file) {
        FileInfo fileInfo = fileStorageService.of(file)
                .setPath(DateUtil.format(new Date(), "yyyyMMdd") + "/") //保存到相对路径下，为了方便管理，不需要可以不写
                .setSaveFilename(CodeUtil.getUUID() + "." + FileUtil.getSuffix(file.getOriginalFilename())) //设置保存的文件名，不需要可以不写，会随机生成
                .upload();  //将文件上传到对应地方
        return sysFileConverter.toSysFile(fileInfo);
    }

    @SneakyThrows
    @ApiOperation(value = "文件下载")
    @ApiImplicitParam(name = "url", value = "文件url", paramType = "query", required = true, dataTypeClass = String.class)
    @GetMapping("/download")
    public void download(@NotBlank String url, HttpServletResponse response) {
        // 获取文件信息
        FileInfo fileInfo = fileStorageService.getFileInfoByUrl(url);
        MyAssert.notNull(fileInfo, "文件不存在");
        fileStorageService.download(fileInfo).outputStream(response.getOutputStream());
    }

    /**
     * 文件删除
     */
    @ApiOperation(value = "文件删除")
    @ApiImplicitParam(name = "url", value = "文件url", paramType = "query", required = true, dataTypeClass = String.class)
    @GetMapping("/delete")
    public void delete(@NotBlank String url) {
        fileStorageService.delete(url);
    }


}
