package com.ruoyi.platform.controller.image;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.Image;
import com.ruoyi.platform.service.ImageService;
import com.ruoyi.platform.vo.ImageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * (Image)表控制层
 *
 * @author Xidaray
 * @since 2024-03-05 14:46:47
 */
@RestController
@RequestMapping("image")
@Api("镜像管理")
public class ImageController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ImageService imageService;

    /**
     * 分页查询
     *
     * @param image 筛选条件
     * @param page  分页对象
     * @param size  分页对象
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation("分页查询，根据image_type查询公开镜像和自定义镜像; 1公开0私有")
    public GenericsAjaxResult<Page<Image>> queryByPage(Image image, @RequestParam("page") int page,
                                                       @RequestParam("size") int size,
                                                       @RequestParam(value = "image_type") int imageType) {
        image.setImageType(imageType);
        PageRequest pageRequest = PageRequest.of(page, size);
        return genericsSuccess(this.imageService.queryByPage(image, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("按id查询镜像")
    public GenericsAjaxResult<Image> queryById(@PathVariable("id") Integer id) {
        return genericsSuccess(this.imageService.queryById(id));
    }

    /**
     * 按镜像名字模糊查询
     *
     * @param name 筛选条件
     * @return 查询结果
     */
    @GetMapping("name/{name}")
    @ApiOperation("按名字模糊查询镜像")
    public GenericsAjaxResult<Page<Image>> queryByName(@PathVariable("name") String name) {
        return genericsSuccess(this.imageService.queryByName(name));
    }

    /**
     * 新增数据
     *
     * @param image 实体
     * @return 新增结果
     */
    @ApiOperation("新增镜像,不包含镜像版本")
    public GenericsAjaxResult<Image> add(@RequestBody Image image) {
        return genericsSuccess(this.imageService.insert(image));
    }

    /**
     * @param imageVo 实体
     *                新增镜像和版本    @PostMapping
     * @return 新增结果
     */
    @PostMapping("/addImageAndVersion")
    @ApiOperation("添加镜像和版本")
    public GenericsAjaxResult<String> addImageAndVersion(@RequestBody ImageVo imageVo) throws Exception {
        return this.imageService.insertImageAndVersion(imageVo);

    }

    /**
     * 编辑数据
     *
     * @param image 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑镜像")
    public GenericsAjaxResult<Image> edit(@RequestBody Image image) {
        return genericsSuccess(this.imageService.update(image));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除镜像")
    public GenericsAjaxResult<String> deleteById(@PathVariable("id") Integer id) throws Exception {
        return genericsSuccess(this.imageService.removeById(id));
    }

    @PostMapping("/net")
    @ApiOperation("从网络上传构建镜像")
    public GenericsAjaxResult<Map<String, String>> createImageFromNet(@RequestParam("name") String imageName,
                                                                      @RequestParam("tag") String imageTag,
                                                                      @RequestParam("path") String path) throws Exception {
        return genericsSuccess(this.imageService.createImageFromNet(imageName, imageTag, path));
    }

    @PostMapping("/local")
    @ApiOperation("从本地上传构建镜像")
    public GenericsAjaxResult<Map<String, String>> createImageFromLocal(@RequestParam("name") String imageName,
                                                                        @RequestParam("tag") String imageTag,
                                                                        @RequestParam("fileName") String fileName) throws Exception {
        return genericsSuccess(this.imageService.createImageFromLocal(imageName, imageTag, fileName));
    }


    /**
     * 镜像上传
     *
     * @return 上传结果
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传镜像文件", notes = "上传镜像tar包,返回存储路径")
    public GenericsAjaxResult<Map<String, String>> uploadImageFiles(@RequestParam("file") MultipartFile file) throws Exception {
        return genericsSuccess(this.imageService.uploadImageFiles(file));
    }


    @PostMapping("/saveImage")
    @ApiOperation(value = "保存环境为镜像", notes = "docker commit方式保存，并推送到horbor")
    public GenericsAjaxResult<String> saveImage(@RequestBody ImageVo imageVo) {
        this.imageService.saveImage(imageVo);
        return genericsSuccess("操作成功");
    }
}

