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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.energy.controller.admin.price.vo.*;
import cn.iocoder.yudao.module.energy.convert.price.EnergyStationPriceConvert;
import cn.iocoder.yudao.module.energy.dal.dataobject.price.EnergyStationPriceDO;
import cn.iocoder.yudao.module.energy.service.price.EnergyStationPriceService;
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 java.time.LocalDate;

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

@Tag(name = "管理后台 - 加氢站价格管理")
@RestController
@RequestMapping("/energy/station-price")
@Validated
public class EnergyStationPriceController {

    @Resource
    private EnergyStationPriceService stationPriceService;

    @PostMapping("/create")
    @Operation(summary = "创建价格")
    @PreAuthorize("@ss.hasPermission('energy:station-price:create')")
    public CommonResult<Long> createPrice(@Valid @RequestBody EnergyStationPriceSaveReqVO createReqVO) {
        return success(stationPriceService.createPrice(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新价格")
    @PreAuthorize("@ss.hasPermission('energy:station-price:update')")
    public CommonResult<Boolean> updatePrice(@Valid @RequestBody EnergyStationPriceSaveReqVO updateReqVO) {
        stationPriceService.updatePrice(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除价格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('energy:station-price:delete')")
    public CommonResult<Boolean> deletePrice(@RequestParam("id") Long id) {
        stationPriceService.deletePrice(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得价格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('energy:station-price:query')")
    public CommonResult<EnergyStationPriceRespVO> getPrice(@RequestParam("id") Long id) {
        EnergyStationPriceDO price = stationPriceService.getPrice(id);
        return success(EnergyStationPriceConvert.INSTANCE.convert(price));
    }

    @GetMapping("/page")
    @Operation(summary = "获得价格分页")
    @PreAuthorize("@ss.hasPermission('energy:station-price:query')")
    public CommonResult<PageResult<EnergyStationPriceRespVO>> getPricePage(@Valid EnergyStationPricePageReqVO pageReqVO) {
        PageResult<EnergyStationPriceDO> pageResult = stationPriceService.getPricePage(pageReqVO);
        return success(EnergyStationPriceConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/get-effective")
    @Operation(summary = "获取生效价格")
    @PreAuthorize("@ss.hasPermission('energy:station-price:query')")
    public CommonResult<EnergyStationPriceRespVO> getEffectivePrice(
            @RequestParam("stationId") Long stationId,
            @RequestParam("customerId") Long customerId,
            @RequestParam("date") LocalDate date) {
        EnergyStationPriceDO price = stationPriceService.getEffectivePrice(stationId, customerId, date);
        return success(EnergyStationPriceConvert.INSTANCE.convert(price));
    }
}
