package com.jeeplus.test.oss.controller;

import com.jeeplus.test.oss.domain.WorkAttachment;
import com.jeeplus.test.oss.service.OssService;
import com.jeeplus.test.oss.service.dto.FileDetailDTO;
import com.jeeplus.test.user.service.dto.FileUrlDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Slf4j
@Api(tags ="oss")
@RestController
@RequestMapping(value = "/oss/file")
public class OssFileController {

    @Value("${config.accessory.aliyun.aliyunUrl}")
    private String aliyunUrl;

    @Value("${config.accessory.aliyun.aliyunDownloadUrl}")
    private String aliyunDownloadUrl;

    @Resource
    private OssService ossService;

    @ApiOperation(value = "保存数据")
    @PostMapping("/saveMsg")
    public void saveMsg(@RequestBody List<WorkAttachment> workAttachments) {
        ossService.saveMsg(workAttachments);
    }

    @GetMapping("/deleteMsgByFileName")
    @ApiOperation(value = "删除数据")
    public void deleteMsgByFileName(@RequestParam String url) {
        ossService.deleteMsgByFileName(url);
    }

    @GetMapping("/deleteMsgById")
    @ApiOperation(value = "根据id删除数据")
    public void deleteMsgById(@RequestParam String id) {
        ossService.deleteMsgById(id);
    }

    @GetMapping("/getTemporaryUrl")
    @ApiOperation(value = "根据url获取临时文件地址")
    public ResponseEntity<String> getTemporaryUrl(@RequestParam String url) {
        String temporaryLookUrl = ossService.getFileTemporaryLookUrl(aliyunUrl + url);
        return ResponseEntity.ok(temporaryLookUrl);
    }

    @GetMapping("/findFileList")
    @ApiOperation(value = "查询数据")
    public ResponseEntity<List<WorkAttachment>> findFileList(@RequestParam("attachmentId") String attachmentId) {
        List<WorkAttachment> list = ossService.findFileList(attachmentId);
        return ResponseEntity.ok(list);
    }

    /**
     * 下载附件
     */
    @GetMapping("/downLoadAttach")
    @ApiOperation(value = "下载附件")
    public String downLoadAttach(@RequestParam String file, HttpServletResponse response, HttpServletRequest request) throws IOException {
        file = "http://oss.gangwaninfo.com" + file;
        file = file.replace("amp;", "");
        String fileName = file.substring(file.lastIndexOf("/") + 1, file.length());
        String cons = "";
        if (file.contains(aliyunUrl)) {
            cons = aliyunUrl;
        } else if (file.contains("http://gangwan-app.oss-cn-hangzhou.aliyuncs.com")) {
            cons = "http://gangwan-app.oss-cn-hangzhou.aliyuncs.com";
        } else {
            cons = aliyunDownloadUrl;
        }
        String key = file.split(cons + "/")[1];
        log.info("-----------------------------------------");
        log.info("fileName=" + fileName);
        log.info("key=" + key);
        log.info("-----------------------------------------");
        ossService.downByStream(key, fileName, response, request.getHeader("USER-AGENT"));
        return null;
    }

    @GetMapping("/getFileSizeByUrl")
    @ApiOperation(value = "根据文件路径获取文件信息")
    public ResponseEntity<FileDetailDTO> getFileSizeByUrl(@RequestParam("url") String url) {
        FileDetailDTO fileDetailDTO = ossService.getFileSizeByUrl(url);
        return ResponseEntity.ok(fileDetailDTO);
    }

    /**
     * 上传文件
     * @return
     * @throws IOException
     */
    @ApiOperation("上传文件")
    @PostMapping("webUpload/upload")
    public ResponseEntity<FileUrlDto> webUpload(MultipartFile file) throws IOException {
        String dir = "note";
        FileUrlDto dto = ossService.webUpload(file,dir);
        return ResponseEntity.ok(dto);
    }
}
