package com.ruoyi.ems.controller;

import com.huashe.common.domain.AjaxResult;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.ems.domain.EmsObjFlowRel;
import com.ruoyi.ems.service.IEmsObjFlowRelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 能源设施能流关系Controller
 *
 * @author ruoyi
 * @date 2024-07-10
 */
@RestController
@RequestMapping("/object/flowrel")
@Api(value = "EmsObjFlowRelController", description = "对象能流关系管理")
public class EmsObjFlowRelController extends BaseController
{
    @Autowired
    private IEmsObjFlowRelService facsFlowRelService;

    /**
     * 查询能源设施能流关系列表
     */
    @RequiresPermissions("basecfg:flowrel:list")
    @GetMapping("/list")
    @ApiOperation("查询能流关系列表")
    public TableDataInfo list(EmsObjFlowRel flowRel)
    {
        startPage();
        List<EmsObjFlowRel> list = facsFlowRelService.selectFlowRelList(flowRel);
        return getDataTable(list);
    }

    /**
     * 获取能源设施能流关系详细信息
     */
    @RequiresPermissions("basecfg:flowrel:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取能流关系详情")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(facsFlowRelService.selectFlowRelById(id));
    }

    /**
     * 新增能源设施能流关系
     */
    @RequiresPermissions("basecfg:flowrel:add")
    @Log(title = "能源设施能流关系", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增能流关系")
    public AjaxResult add(@RequestBody EmsObjFlowRel facsFlowRel)
    {
        return toAjax(facsFlowRelService.insertFlowRel(facsFlowRel));
    }

    /**
     * 修改能源设施能流关系
     */
    @RequiresPermissions("basecfg:flowrel:edit")
    @Log(title = "能源设施能流关系", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改能流关系")
    public AjaxResult edit(@RequestBody EmsObjFlowRel flowRel)
    {
        return toAjax(facsFlowRelService.updateFlowRel(flowRel));
    }

    /**
     * 删除能源设施能流关系
     */
    @RequiresPermissions("basecfg:flowrel:remove")
    @Log(title = "能源设施能流关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除能流关系")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(facsFlowRelService.deleteFlowRelByIds(ids));
    }

    /**
     * 获取能流拓扑数据
     */
    @RequiresPermissions("basecfg:flowrel:list")
    @GetMapping("/topology")
    @ApiOperation("获取能流拓扑数据")
    public AjaxResult getTopology(
        @ApiParam("区域代码") @RequestParam(required = false) String areaCode,
        @ApiParam("能源分类代码") @RequestParam(required = false) String emsClsCode)
    {
        Map<String, Object> topology = facsFlowRelService.getFlowTopology(areaCode, emsClsCode);
        return success(topology);
    }

    /**
     * 验证能流关系是否存在
     */
    @RequiresPermissions("basecfg:flowrel:query")
    @PostMapping("/validate")
    @ApiOperation("验证能流关系是否存在")
    public AjaxResult validate(@RequestBody EmsObjFlowRel flowRel)
    {
        boolean exists = facsFlowRelService.checkFlowRelExists(flowRel);
        return success(exists);
    }

    /**
     * 获取上游能流（输入到该对象的能流）
     */
    @RequiresPermissions("basecfg:flowrel:list")
    @GetMapping("/upstream")
    @ApiOperation("获取上游能流")
    public AjaxResult getUpstream(
        @ApiParam("对象代码") @RequestParam String objCode,
        @ApiParam("对象类型") @RequestParam Integer objType)
    {
        List<EmsObjFlowRel> list = facsFlowRelService.getUpstreamFlow(objCode, objType);
        return success(list);
    }

    /**
     * 获取下游能流（从该对象输出的能流）
     */
    @RequiresPermissions("basecfg:flowrel:list")
    @GetMapping("/downstream")
    @ApiOperation("获取下游能流")
    public AjaxResult getDownstream(
        @ApiParam("对象代码") @RequestParam String objCode,
        @ApiParam("对象类型") @RequestParam Integer objType)
    {
        List<EmsObjFlowRel> list = facsFlowRelService.getDownstreamFlow(objCode, objType);
        return success(list);
    }

    /**
     * 获取指定能源类型的流动关系
     */
    @RequiresPermissions("basecfg:flowrel:list")
    @GetMapping("/energytype")
    @ApiOperation("按能源类型查询能流关系")
    public AjaxResult getByEnergyType(
        @ApiParam("能源分类代码") @RequestParam(required = false) String emsClsCode,
        @ApiParam("区域代码") @RequestParam(required = false) String areaCode)
    {
        List<EmsObjFlowRel> list = facsFlowRelService.getFlowRelByEnergyType(emsClsCode, areaCode);
        return success(list);
    }
}