package com.ruoyi.platform.controller.model;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.ModelDependency;
import com.ruoyi.platform.domain.ModelsVersion;
import com.ruoyi.platform.service.ModelDependencyService;
import com.ruoyi.platform.vo.ModelDependcyTreeVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * (ModelDependency)表控制层
 *
 * @author Xidaray
 * @since 2024-05-29 13:51:23
 */
@RestController
@RequestMapping("modelDependency")
public class ModelDependencyController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ModelDependencyService modelDependencyService;

    /**
     * 分页查询
     *
     * @param modelDependency 筛选条件
     * @param page      分页对象
     * @param size      分页对象
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation("分页查询")
    public GenericsAjaxResult<Page<ModelDependency>> queryByPage(ModelDependency modelDependency, int page ,int size) {
        PageRequest pageRequest = PageRequest.of(page,size);
        return genericsSuccess(this.modelDependencyService.queryByPage(modelDependency, pageRequest));
    }

    /**
     * 根据对象查询
     *
     * @param modelDependency 筛选条件
     * @return 查询结果
     */
    @GetMapping("/queryModelDependency")
    @ApiOperation("根据对象查询")
    public GenericsAjaxResult<List<ModelDependency>> queryByModelDependency(@RequestBody ModelDependency modelDependency) throws IOException {
        modelDependency.setState(1);
        return genericsSuccess(this.modelDependencyService.queryByModelDependency(modelDependency));
    }

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

    /**
     * 新增数据
     *
     * @param modelDependency 实体
     * @return 新增结果
     */
    @PostMapping
    public GenericsAjaxResult<ModelDependency> add(@RequestBody ModelDependency modelDependency) {
        return genericsSuccess(this.modelDependencyService.insert(modelDependency));
    }

    /**
     * 编辑数据
     *
     * @param modelDependency 实体
     * @return 编辑结果
     */
    @PutMapping
    public GenericsAjaxResult<ModelDependency> edit(@RequestBody ModelDependency modelDependency) {
        return genericsSuccess(this.modelDependencyService.update(modelDependency));
    }

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


    @PostMapping("/queryModelAtlas")
    @ApiOperation("根据模型id与版本两个属性得到模型的演化图谱")
    public GenericsAjaxResult<ModelDependcyTreeVo> queryModelAtlas(@RequestBody ModelDependency modelDependency) throws Exception {
        return genericsSuccess(this.modelDependencyService.getModelDependencyTree(modelDependency));
    }
}

