package cn.iocoder.yudao.module.energy.controller.admin.config;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.energy.controller.admin.config.vo.EnergyStationConfigPageReqVO;
import cn.iocoder.yudao.module.energy.controller.admin.config.vo.EnergyStationConfigRespVO;
import cn.iocoder.yudao.module.energy.controller.admin.config.vo.EnergyStationConfigSaveReqVO;
import cn.iocoder.yudao.module.energy.convert.config.EnergyStationConfigConvert;
import cn.iocoder.yudao.module.energy.dal.dataobject.config.EnergyStationConfigDO;
import cn.iocoder.yudao.module.energy.service.config.EnergyStationConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 加氢站配置")
@RestController
@RequestMapping("/energy/station-config")
@Validated
public class EnergyStationConfigController {

    @Resource
    private EnergyStationConfigService stationConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建站点配置")
    @PreAuthorize("@ss.hasPermission('energy:station-config:create')")
    public CommonResult<Long> createConfig(@Valid @RequestBody EnergyStationConfigSaveReqVO createReqVO) {
        return success(stationConfigService.createConfig(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新站点配置")
    @PreAuthorize("@ss.hasPermission('energy:station-config:update')")
    public CommonResult<Boolean> updateConfig(@Valid @RequestBody EnergyStationConfigSaveReqVO updateReqVO) {
        stationConfigService.updateConfig(updateReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得站点配置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('energy:station-config:query')")
    public CommonResult<EnergyStationConfigRespVO> getConfig(@RequestParam("id") Long id) {
        EnergyStationConfigDO config = stationConfigService.getConfig(id);
        return success(EnergyStationConfigConvert.INSTANCE.convert(config));
    }

    @GetMapping("/page")
    @Operation(summary = "获得站点配置分页")
    @PreAuthorize("@ss.hasPermission('energy:station-config:query')")
    public CommonResult<PageResult<EnergyStationConfigRespVO>> getConfigPage(@Valid EnergyStationConfigPageReqVO pageReqVO) {
        PageResult<EnergyStationConfigDO> pageResult = stationConfigService.getConfigPage(pageReqVO);
        return success(EnergyStationConfigConvert.INSTANCE.convertPage(pageResult));
    }
}
