package com.chushang.oss.remote;

import com.chushang.common.core.constant.SecurityConstants;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.core.web.Result;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.oss.entity.dto.UploadBytesDTO;
import com.chushang.oss.entity.dto.UploadFileDTO;
import com.chushang.oss.entity.dto.UploadStreamDTO;
import com.chushang.oss.entity.vo.FileSourceVo;
import com.chushang.oss.feign.RemoteOssService;
import com.chushang.oss.service.FileSourceService;
import com.chushang.security.annotation.InnerAuth;
import com.chushang.security.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping(value = "/remote/file")
public class RemoteFileController implements RemoteOssService {
    @Resource
    FileSourceService fileSourceService;
    @Override
    @SysLog(value = "feign文件上传", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload")
    @InnerAuth
    public Result<List<FileSourceVo>> uploadFile(@RequestPart(name = "files") MultipartFile[] files,
                                                   @RequestParam(value = "ocrType", required = false) String ocrType,
                                                   @RequestParam(value = "sealFlag", required = false, defaultValue= "false") Boolean sealFlag,
                                                   @RequestParam(value = "formats", required = false) String formats,
                                                   @RequestHeader(SecurityConstants.FROM_SOURCE)String source,
                                                   @RequestParam(value = "fileType", required = false) String fileType) {
        return Result.ok(fileSourceService.addFile(files, ocrType, sealFlag, formats, fileType));
    }



    @Override
    @SysLog(value = "feign文件上传File类型", businessType = BusinessType.INSERT)
    @PostMapping(value = "/uploadqccode")
    @InnerAuth
    public Result<String> uploadQcCode(@RequestHeader(SecurityConstants.FROM_SOURCE) String source) throws Exception{
        return Result.ok(fileSourceService.uploadQcCode());
    }

    /**
     * 文件导出, 有可能是 excel, word, zip, 单图片等
     * @param uploadBytes 字节
     */
    @Override
    @SysLog(value = "feign文件", businessType = BusinessType.EXPORT)
    @PostMapping(value = "/upload/bytes")
    @InnerAuth
    public Result<List<FileSourceVo>> uploadBytes(@RequestBody UploadBytesDTO uploadBytes,
                                                   String fileName,
                                                   Boolean sealFlag,
                                                   String formats,
                                                   String fileType,
                                                   @RequestHeader(SecurityConstants.FROM_SOURCE) String source)
    {
        byte[] bytes = uploadBytes.getBytes();
        return Result.ok(fileSourceService.uploadFile(bytes, fileName, sealFlag, formats, fileType));
    }

    @Override
    @SysLog(value = "feign文件", businessType = BusinessType.DOWNLOAD)
    @GetMapping("/getFile/{fid}")
    @InnerAuth
    public Result<byte[]> getFile(@PathVariable String fid,@RequestHeader(SecurityConstants.FROM_SOURCE) String source)
    {
        return Result.ok(fileSourceService.getFile(fid));
    }

    /**
     * 删除文件
     * @param fid 文件id
     */
    @SysLog(value = "文件", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/del/{fid}")
    @InnerAuth
    public Result<String> delFile(@PathVariable String fid, @RequestHeader(SecurityConstants.FROM_SOURCE) String source) {
        fileSourceService.delFile(fid);
        return Result.ok("success");
    }

    /**
     * 删除文件
     * @param fids 文件fid
     */
    @SysLog(value = "批量文件", businessType = BusinessType.DELETE)
    @PostMapping(value = "/del/batch")
    @InnerAuth
    public Result<String> delFile(@RequestBody Set<String> fids,
                              @RequestHeader(SecurityConstants.FROM_SOURCE) String source) {
        return Result.ok( fileSourceService.delFileBatch(fids));
    }

    @Override
    @PostMapping(value = "/upload/stream")
    public Result<List<FileSourceVo>> uploadStream(@RequestBody UploadStreamDTO uploadStream, String fileName, Boolean sealFlag, String formats, String fileType, String source)
    {
        InputStream is = uploadStream.getIs();
        return Result.ok(fileSourceService.uploadFile(is, fileName, sealFlag, formats, fileType));
    }


}
