package com.org.hktshop.controller.Admin;

import com.aliyun.oss.OSSClient;
import com.org.hktshop.base.BaseResult;
import com.org.hktshop.properties.AliyunOSSProperties;
import com.org.hktshop.service.aliyun.AliyunOssService;
import com.org.hktshop.utils.*;
import com.org.hktshop.controller.BaseController;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;


@RestController
@RequestMapping("/oss")
@Api(tags = "阿里云存储接口")
public class AliyunOSSController extends BaseController {

    @Autowired
    private AliyunOssService aliyunOssService;

    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    /**
     * 上传文件到阿里云OSS存储
     *
     * @return
     */
    @PostMapping("/fileUpload")
    @ApiImplicitParam(name = "file", value = "上传的文件", required = true, paramType = "form", dataType = "__file")
    @ApiOperation(value = "上传文件到阿里云OSS存储", notes = "上传文件到阿里云OSS存储")
    public BaseResult fileUpload(@RequestParam("file") MultipartFile file) {
        try {
            String folderName = aliyunOssService.getFolderName(file.getOriginalFilename());
            OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
            String url = aliyunOssService.uploadByFile(ossClient, file, folderName);
            return BaseResult.successWithData(url);
        } catch (Exception ex) {
            ex.printStackTrace();
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @PostMapping("/fileUploadWithPath")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "上传的文件", paramType = "form", dataType = "__file", required = true),
            @ApiImplicitParam(name = "path", value = "文件路径", paramType = "form", required = true),
    })
    @ApiOperation(value = "上传文件到阿里云OSS存储", notes = "上传文件到阿里云OSS存储")
    public BaseResult fileUpload(@RequestParam("file") MultipartFile file, @RequestParam("path") String path) {
        try {
            String folderName = aliyunOssService.getFolderName(file.getOriginalFilename());
            folderName = path +"/" + folderName  ;

            OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
            String url = aliyunOssService.uploadByFile(ossClient, file, folderName);
            return BaseResult.successWithData(url);
        } catch (Exception ex) {
            ex.printStackTrace();
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }


    /**
     * 删除阿里云OSS存储文件
     *
     * @return
     */
    @GetMapping("/removeFile")
    @ApiOperation(value = "删除阿里云OSS存储文件", notes = "删除阿里云OSS存储文件")
    @ApiImplicitParam(name = "fileName", value = "文件名", required = true, paramType = "query")
    public BaseResult removeFile(@RequestParam("fileName") String url) {
        try {
            OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
            String fileName = url.substring(url.indexOf("hkt/"), url.length());
            boolean flag = aliyunOssService.deleteFile(ossClient, fileName);
            if (!flag) {
                return BaseResult.fail(HttpCode.BAD_REQUEST.getCode());
            }
            return BaseResult.success();
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error("删除文件失败");
            return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
        }
    }

    @GetMapping("/strUpload")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "str", value = "上传的字符内容", required = true, paramType = "query"),
            @ApiImplicitParam(name = "folder", value = "上传到阿里云的文件名", required = true, paramType = "query")
    })
    @ApiOperation(value = "上传字符到阿里云OSS存储", notes = "上传字符到阿里云OSS存储")
    public BaseResult<String> strUpload(@RequestParam("str") String str, @RequestParam("folder") String folder) {
        File file = FileUtil.saveTxtFile(str);
        try {
            OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
            String url = aliyunOssService.uploadByFile(ossClient, FileUtil.fileToMultipartFile(file), folder);
            return BaseResult.successWithData(url);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
        } finally {
            //删除临时文件
            file.delete();
        }
        return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
    }


    @ApiOperation(value = "通过url获取txt文件内容", notes = "通过url获取txt文件内容")
    @ApiImplicitParam(name = "url", value = "文件路径", required = true, paramType = "query")
    @GetMapping("/getTxtMsg")
    public BaseResult<String> getTxtMsg(@RequestParam("url") String url) {
        String fileName = url.substring(url.indexOf("hkt/"), url.length());
        try {
            OSSClient ossClient = new OSSClient(aliyunOSSProperties.getEndpoint(), aliyunOSSProperties.getAccessKeyId(), aliyunOSSProperties.getAccessKeySecret());
            String msg = aliyunOssService.getTxtMsg(ossClient, fileName);
            return BaseResult.successWithData(msg);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info(ex.toString());
        }
        return BaseResult.fail(HttpCode.INTERNAL_SERVER_ERROR.getCode());
    }
}