package org.jeecg.modules.oss.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.oss.entity.OssFile;
import org.jeecg.modules.oss.service.IOssFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * 云存储示例 DEMO
 *
 * @author: jeecg-boot
 */
@Slf4j
@Controller
@RequestMapping("/sys/oss/file")
public class OssFileController {

    @Autowired
    private IOssFileService ossFileService;

    @ResponseBody
    @GetMapping("/list")
    public Result<IPage<OssFile>> queryPageList(OssFile file,
                                                @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<OssFile>> result = new Result<>();
        QueryWrapper<OssFile> queryWrapper = QueryGenerator.initQueryWrapper(file, req.getParameterMap());
        Page<OssFile> page = new Page<>(pageNo, pageSize);
        IPage<OssFile> pageList = ossFileService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    @ResponseBody
    @PostMapping("/upload")
    //@RequiresRoles("admin")
    @RequiresPermissions("system:ossFile:upload")
    public Result upload(@RequestParam("file") MultipartFile multipartFile) {
        Result result = new Result();
        try {
            ossFileService.upload(multipartFile);
            result.success("上传成功！");
        } catch (Exception ex) {
            log.info(ex.getMessage(), ex);
            result.error500("上传失败");
        }
        return result;
    }

    @ResponseBody
    @DeleteMapping("/delete")
    public Result delete(@RequestParam(name = "id") String id) {
        Result result = new Result();
        OssFile file = ossFileService.getById(id);
        if (file == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = ossFileService.delete(file);
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询.
     */
    @ResponseBody
    @GetMapping("/queryById")
    @Operation(summary = "文件管理-通过id查询")
    public Result<OssFile> queryById(@RequestParam(name = "id") String id) {
        Result<OssFile> result = new Result<>();
        OssFile file = ossFileService.getById(id);
        if (file == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(file);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 通过id查询.
     */
    @ResponseBody
    @GetMapping("/queryByIds")
    @Operation(summary = "文件管理-通过多个id逗号分割批量查询查询")
    public Result<List<OssFile>> queryByIdList(@RequestParam(name = "ids") String ids) {
        Result<List<OssFile>> result = new Result<>();
        String[] idList = ids.split(",");
        List<OssFile> resultList = new ArrayList<>();
        for (int i = 0; i < idList.length; i++) {
            OssFile file = ossFileService.getById(idList[i]);
            resultList.add(file);
        }
        if (CollectionUtil.isEmpty(resultList)) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(resultList);
            result.setSuccess(true);
        }
        return result;
    }

}
