package com.zsElectric.boot.business.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zsElectric.boot.business.model.entity.FirmStationTimePrice;
import com.zsElectric.boot.business.model.query.ExclusivePriceQuery;
import com.zsElectric.boot.business.model.vo.ExclusivePriceVO;
import com.zsElectric.boot.business.service.FirmStationTimePriceService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.FirmStationTimePriceForm;
import com.zsElectric.boot.business.model.query.FirmStationTimePriceQuery;
import com.zsElectric.boot.business.model.vo.FirmStationTimePriceVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsElectric.boot.core.web.PageResult;
import com.zsElectric.boot.core.web.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.math.BigDecimal;
import java.util.List;

/**
 * 企业专属价格前端控制层
 *
 * @author zsElectric
 * @since 2025-12-11 10:13
 */
@Tag(name = "企业专属价格接口")
@RestController
@RequestMapping("/api/v1/firmStationTimePrice")
@RequiredArgsConstructor
public class FirmStationTimePriceController  {

    private final FirmStationTimePriceService firmStationTimePriceService;

    @Operation(summary = "企业专属价格分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:firmStationTimePrice:query')")
    public PageResult<FirmStationTimePriceVO> getFirmStationTimePricePage(FirmStationTimePriceQuery queryParams ) {
        IPage<FirmStationTimePriceVO> result = firmStationTimePriceService.getFirmStationTimePricePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增企业专属价格")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:firmStationTimePrice:add')")
    public Result<Void> saveFirmStationTimePrice(@RequestBody @Valid FirmStationTimePriceForm formData ) {
        boolean result = firmStationTimePriceService.saveFirmStationTimePrice(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取企业专属价格表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:firmStationTimePrice:edit')")
    public Result<FirmStationTimePriceForm> getFirmStationTimePriceForm(
        @Parameter(description = "企业专属价格ID") @PathVariable Long id
    ) {
        FirmStationTimePriceForm formData = firmStationTimePriceService.getFirmStationTimePriceFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改企业专属价格")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:firmStationTimePrice:edit')")
    public Result<Void> updateFirmStationTimePrice(
            @Parameter(description = "企业专属价格ID") @PathVariable Long id,
            @RequestBody @Validated FirmStationTimePriceForm formData
    ) {
        boolean result = firmStationTimePriceService.updateFirmStationTimePrice(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除企业专属价格")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:firmStationTimePrice:delete')")
    public Result<Void> deleteFirmStationTimePrices(
        @Parameter(description = "企业专属价格ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = firmStationTimePriceService.deleteFirmStationTimePrices(ids);
        return Result.judge(result);
    }

    @Operation(summary = "获取企业专属价格列表")
    @PostMapping("/listPrice")
    @PreAuthorize("@ss.hasPerm('business:firmStationTimePrice:listPrice')")
    public Result<List<ExclusivePriceVO>> getExclusivePriceList(ExclusivePriceQuery queryParams){
        List<ExclusivePriceVO> exclusivePriceList = firmStationTimePriceService.getExclusivePriceList(queryParams);
        return Result.success(exclusivePriceList);
    }

    @Operation(summary = "编辑企业专属价格")
    @PutMapping("/changePrice/{id}/{price}")
    @PreAuthorize("@ss.hasPerm('business:firmStationTimePrice:changePrice')")
    public Result<Void> changePrice(@PathVariable("id")Long id, @PathVariable("price")BigDecimal price){
        boolean result = firmStationTimePriceService.update(Wrappers.lambdaUpdate(FirmStationTimePrice.class).set(FirmStationTimePrice::getFirmPrice, price).eq(FirmStationTimePrice::getId, id));
        return Result.judge(result);
    }

}
