package com.test.bijihoudaun.controller;


import com.test.bijihoudaun.common.response.R;
import com.test.bijihoudaun.entity.Image;
import com.test.bijihoudaun.service.ImageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

@Tag(name = "markdown接口")
@RestController
@RequestMapping("/api/images")
public class ImageController {

    @Autowired
    private ImageService imageService;

    @Operation(summary = "上传图片")
    @Parameters({
            @Parameter(name = "file", description = "图片文件", required = true)
    })
    @PostMapping
    public R<Image> uploadImage(
            @RequestParam("file") MultipartFile file) {

        try {
            Image image = imageService.uploadImage(file);
            return R.success(image);
        } catch (IOException e) {
            return R.fail();
        }
    }

    @Operation(summary = "根据id删除图片")
    @PostMapping("/{id}")
    public R<Void> deleteImage(@PathVariable Long id) {
        boolean result = imageService.deleteImage(id);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    @Operation(summary = "根据url删除图片")
    @PostMapping("/deleteByUrl")
    public R<Void> deleteImageByUrl(@RequestParam String url) {
        boolean result = imageService.deleteImageByUrl(url);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }

    @Operation(summary = "根据url批量删除图片")
    @PostMapping("/batch")
    public R<Void> deleteImageByUrls(@RequestBody List<String> urls) {
        boolean result = imageService.deleteImageByUrls(urls);
        if (result) {
            return R.success();
        } else {
            return R.fail();
        }
    }


}
