package com.ruoyi.platform.controller.resources;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.GenericsAjaxResult;
import com.ruoyi.platform.domain.ComputingResource;
import com.ruoyi.platform.domain.ResourceOccupy;
import com.ruoyi.platform.service.ComputingResourceService;
import com.ruoyi.platform.service.ResourceOccupyService;
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 javax.annotation.Resource;
import java.util.Map;

/**
 * (ComputingResource)表控制层
 *
 * @author Xidaray
 * @since 2023-11-29 14:13:20
 */
@RestController
@RequestMapping("computingResource")
@Api("计算资源管理")
public class ComputingResourceController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private ComputingResourceService computingResourceService;

    @Resource
    private ResourceOccupyService resourceOccupyService;

    /**
     * 分页查询
     *
     * @param computingResource 筛选条件
     * @return 查询结果
     */
    @GetMapping
    @ApiOperation("分页查询")
    public GenericsAjaxResult<Page<ComputingResource>> queryByPage(ComputingResource computingResource, @RequestParam("page") int page,
                                                                   @RequestParam("size") int size,
                                                                   @RequestParam(value = "resource_type") String resourceType) {
        computingResource.setComputingResource(resourceType);
        PageRequest pageRequest = PageRequest.of(page, size);
        return genericsSuccess(this.computingResourceService.queryByPage(computingResource, pageRequest));
    }

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

    /**
     * 新增数据
     *
     * @param computingResource 实体
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("新增计算资源")
    public GenericsAjaxResult<ComputingResource> add(@RequestBody ComputingResource computingResource) {
        return genericsSuccess(this.computingResourceService.insert(computingResource));
    }

    /**
     * 编辑数据
     *
     * @param computingResource 实体
     * @return 编辑结果
     */
    @PutMapping
    @ApiOperation("编辑计算资源")
    public GenericsAjaxResult<ComputingResource> edit(@RequestBody ComputingResource computingResource) {
        return genericsSuccess(this.computingResourceService.update(computingResource));
    }

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

    @GetMapping("/resouceOccupy")
    @ApiOperation("分页查询用户资源使用情况")
    public GenericsAjaxResult<Page<ResourceOccupy>> queryResourceOccupyByPage(@RequestParam("page") int page,
                                                                              @RequestParam("size") int size) {
        PageRequest pageRequest = PageRequest.of(page, size);
        return genericsSuccess(resourceOccupyService.queryByPage(pageRequest));
    }

    @GetMapping("/credit")
    @ApiOperation("查询用户积分使用情况")
    public GenericsAjaxResult<Map<String, Double>> queryCredit() {
        return genericsSuccess(resourceOccupyService.queryCredit());
    }
}

