package org.king.modules.oss.controller;

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.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.king.common.api.vo.Result;
import org.king.common.system.query.QueryGenerator;
import org.king.modules.oss.entity.OSSFile;
import org.king.modules.oss.service.IOSSFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/oss/file")
@Api(tags = "OSS文件服务")
public class OSSFileController {

    @Autowired
    private IOSSFileService ossFileService;

    @GetMapping("/list")
    @ApiOperation("查询分页数据")
    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;
    }

    @PostMapping("/upload")
    @ApiOperation("上传")
    @RequiresRoles("admin")
    public Result upload(@RequestParam("file") MultipartFile multipartFile) {
        Result<String> result = new Result<>();
        try {
            String url = ossFileService.upload(multipartFile);
            result.setResult(url);
            result.success("上传成功！");
        } catch (Exception ex) {
            log.info(ex.getMessage(), ex);
            result.error500("上传失败");
        }
        return result;
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除")
    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);
            if (ok) {
                result.success("删除成功!");
            }
        }
        return result;
    }

    /**
     * 通过id查询
     */
    @GetMapping("/queryById")
    @ApiOperation("通过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;
    }

}
