package com.org.oss.controller;

import cn.hutool.core.util.IdUtil;
import com.org.oss.entity.SysOss;
import com.org.oss.service.SysOssService;
import com.org.system.annotation.Log;
import com.org.system.controller.BaseController;
import com.org.system.enums.BusinessType;
import com.org.utils.AjaxResult;
import com.org.utils.StringUtils;
import com.org.utils.exception.CustomException;
import com.org.utils.page.TableDataInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("sys/oss")
public class SysOssController extends BaseController {

    @Autowired
    private SysOssService sysOssService;

    /**
     * 分页查询
     *
     * @return RestResponse
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('sys:oss:list')")
    @Log(title = "系统文件分页查询", businessType = BusinessType.SELECT)
    public TableDataInfo getList(SysOss sysOss) {
        log.info("SysOssController - getList sysOss:{}", sysOss);

        startPage();
        List<SysOss> list = sysOssService.queryList(sysOss);

        return getDataTable(list);
    }

    /**
     * 上传文件
     *
     * @param file file
     * @return RestResponse
     */
    @RequestMapping("/upload")
    @PreAuthorize("@ss.hasPermi('sys:oss:edit')")
    @Log(title = "系统文件上传", businessType = BusinessType.INSERT)
    public Object upload(@RequestParam(value = "file", required = false) MultipartFile file,
                         @RequestParam(value = "sourceId", required = false) String sourceId,
                         @RequestParam(value = "fileType", required = false) String fileType) throws IOException {
        if (null == file || file.isEmpty()) throw new CustomException("上传文件不能为空");

        return sysOssService.upload(file, sourceId, fileType);
    }


    /**
     * 上传图片-指定图片大小
     *
     * @param file file
     * @return RestResponse
     */
    @RequestMapping("/uploadImg")
    @PreAuthorize("@ss.hasPermi('sys:oss:edit')")
    @Log(title = "系统文件上传", businessType = BusinessType.INSERT)
    public Object uploadImg(@RequestParam(value = "file", required = false) MultipartFile file,
                         @RequestParam(value = "sourceId", required = false) String sourceId,
                         @RequestParam(value = "fileType", required = false) String fileType,
                         @RequestParam(value = "fileSize", required = false) Long fileSize) throws IOException {
        if (null == file || file.isEmpty()) throw new CustomException("上传文件不能为空");

        if (null == fileSize) return sysOssService.upload(file, sourceId, fileType);
        return sysOssService.uploadImg(file, sourceId, fileType, fileSize);
    }

    /**
     * 文件上传-图片Base64
     *
     * @param base64
     * @return
     * @throws IOException
     */
    @PostMapping("/upload/base64")
    @PreAuthorize("@ss.hasPermi('sys:oss:edit')")
    @Log(title = "系统文件上传-图片Base64", businessType = BusinessType.INSERT)
    public Object uploadBase64(@RequestBody String base64) throws IOException {
        if(StringUtils.isEmpty(base64)) throw new CustomException("上传文件Base64不能为空");

        base64 = base64.substring(base64.indexOf(",")+1);
        byte[] data = Base64.decodeBase64(base64);

        String fileName = String.format("%s.%s", IdUtil.simpleUUID(), "png") ;
        MultipartFile file = new MockMultipartFile(fileName, fileName, "application/x-png", data);

        return sysOssService.upload(file, "", "");
    }

    /**
     * 删除文件上传记录
     *
     * @param ids ids
     * @return RestResponse
     */
    @PostMapping("/delete")
    @PreAuthorize("@ss.hasPermi('sys:oss:edit')")
    @Log(title = "系统文件删除", businessType = BusinessType.DELETE)
    public AjaxResult delete(@RequestBody Integer[] ids) {
        log.info("SysOssController - delete ids:{}", ids);

        sysOssService.delete(ids);

        return AjaxResult.success();
    }

    /**
     * 根据文件全路径数组删除上传文件
     *
     * @param urls
     * @return
     */
    @PostMapping("/deleteByUrl")
    @PreAuthorize("@ss.hasPermi('sys:oss:edit')")
    @Log(title = "系统文件删除", businessType = BusinessType.DELETE)
    public AjaxResult deleteByUrl(@RequestBody String[] urls) {
        log.info("SysOssController - deleteByUrl urls:{}", urls);

        sysOssService.deleteByUrl(urls);

        return AjaxResult.success();
    }

    /**
     * 获取文件
     *
     * @param dateDir
     * @param fileName
     */
    @GetMapping(value = "/show/{dateDir}/{fileName}")
    public void show(@PathVariable("dateDir") String dateDir, @PathVariable("fileName") String fileName) {
        log.info("SysOssController - show dateDir:{}", dateDir);
        log.info("SysOssController - show fileName:{}", fileName);

        sysOssService.show(dateDir, fileName);
    }

    /**
     * 根据文件ID获取文件
     * @param id
     */
    @GetMapping(value = "/show")
    public void show(@RequestParam String id) {
        log.info("SysOssController - show id:{}", id);

        sysOssService.show(id);
    }

    /**
     * COS工作流回调
     * @param callbackStr
     * @return

    @PostMapping("/cos/callback")
    public String cosCallback(@RequestBody String callbackStr){
        log.info("SysOssController - cosCallback callbackStr:{}", callbackStr);

        sysOssService.cosCallback(callbackStr);

        return "ok";
    }
     */
}
