package com.sf.file.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.sf.common.annotation.Log;
import com.sf.common.core.controller.BaseController;
import com.sf.common.core.domain.AjaxResult;
import com.sf.common.core.domain.R;
import com.sf.common.core.page.TableDataInfo;
import com.sf.common.enums.BusinessType;
import com.sf.common.exception.ServiceException;
import com.sf.common.exception.file.FileSizeLimitExceededException;
import com.sf.common.utils.poi.ExcelUtil;
import com.sf.file.domain.SysOss;
import com.sf.file.service.ISysOssService;

import cn.hutool.core.util.ObjectUtil;

/**
 * OSS对象存储Controller
 * 
 * @author ztzh
 * @date 2024-03-14
 */
@RestController
@RequestMapping("/system/oss")
public class SysOssController extends BaseController
{
	@Value("${file.ceph.endpoint}")
    private String FILE_CEPH_POINT;
	@Value("${file.ceph.defaultMaxSize}")
    private Long defaultMaxSize;
	
    @Autowired
    private ISysOssService sysOssService;

    /**
     * 查询OSS对象存储列表
     */
    @PreAuthorize("@ss.hasPermi('system:oss:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysOss sysOss)
    {
        startPage();
        List<SysOss> list = sysOssService.selectSysOssList(sysOss);
        return getDataTable(list);
    }

    /**
     * 导出OSS对象存储列表
     */
    @PreAuthorize("@ss.hasPermi('system:oss:export')")
    @Log(title = "OSS对象存储", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOss sysOss)
    {
        List<SysOss> list = sysOssService.selectSysOssList(sysOss);
        ExcelUtil<SysOss> util = new ExcelUtil<SysOss>(SysOss.class);
        util.exportExcel(response, list, "OSS对象存储数据");
    }

    /**
     * 获取OSS对象存储详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:oss:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(sysOssService.selectSysOssById(id));
    }

    /**
     * 新增OSS对象存储
     */
    @PreAuthorize("@ss.hasPermi('system:oss:add')")
    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOss sysOss)
    {
        return toAjax(sysOssService.insertSysOss(sysOss));
    }

    /**
     * 修改OSS对象存储
     */
    @PreAuthorize("@ss.hasPermi('system:oss:edit')")
    @Log(title = "OSS对象存储", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOss sysOss)
    {
        return toAjax(sysOssService.updateSysOss(sysOss));
    }

    /**
     * 删除OSS对象存储
     */
    @PreAuthorize("@ss.hasPermi('system:oss:remove')")
    @Log(title = "OSS对象存储", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(sysOssService.deleteSysOssByIds(ids));
    }
    
    /**
     * 上传OSS对象存储
     *
     * @param file 文件
     */
    @PreAuthorize("@ss.hasPermi('system:oss:upload')")
    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<SysOss> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        long size = file.getSize();
		if (size > defaultMaxSize) {
			throw new FileSizeLimitExceededException(defaultMaxSize / 1024 / 1024);
		}
        SysOss oss = sysOssService.upload(file);
        oss.setSize(file.getSize());
        return R.ok(oss);
    }


    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @PostMapping(value = "/zip/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<SysOss> zipUpload(@RequestPart("file") MultipartFile file) throws Exception {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        long size = file.getSize();
        if (size > defaultMaxSize) {
            throw new FileSizeLimitExceededException(defaultMaxSize / 1024 / 1024);
        }
        SysOss oss = sysOssService.uploadSignZip(file);
        oss.setSize(file.getSize());
        return R.ok(oss);
    }

    /**
     * 下载OSS对象
     *
     * @param ossId OSS对象ID
     */
    //@PreAuthorize("@ss.hasPermi('system:oss:download')")
    @GetMapping("/download/{ossId}")
    public void download(@PathVariable String ossId, HttpServletResponse response) throws Exception {
    	sysOssService.download(ossId, response);
    }
    
}
