package com.tashow.cloud.product.controller;

import com.tashow.cloud.product.dto.ProdWeightRangePricesDO;
import com.tashow.cloud.product.service.ProdWeightRangePricesService;
import com.tashow.cloud.product.vo.prodweightrangeprices.ProdWeightRangePricesPageReqVO;
import com.tashow.cloud.product.vo.prodweightrangeprices.ProdWeightRangePricesRespVO;
import com.tashow.cloud.product.vo.prodweightrangeprices.ProdWeightRangePricesSaveReqVO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import static com.tashow.cloud.common.pojo.CommonResult.success;

import com.tashow.cloud.excel.excel.core.util.ExcelUtils;

import com.tashow.cloud.web.apilog.core.annotation.ApiAccessLog;
import static com.tashow.cloud.web.apilog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - 体重区间价格")
@RestController
@RequestMapping("/tz/prod-weight-range-prices")
@Validated
public class ProdWeightRangePricesController {

    @Resource
    private ProdWeightRangePricesService prodWeightRangePricesService;

    @PostMapping("/create")
    @Operation(summary = "创建体重区间价格")
    @PreAuthorize("@ss.hasPermission('tz:prod-weight-range-prices:create')")
    public CommonResult<Long> createProdWeightRangePrices(@Valid @RequestBody ProdWeightRangePricesSaveReqVO createReqVO) {
        return success(prodWeightRangePricesService.createProdWeightRangePrices(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新体重区间价格")
    @PreAuthorize("@ss.hasPermission('tz:prod-weight-range-prices:update')")
    public CommonResult<Boolean> updateProdWeightRangePrices(@Valid @RequestBody ProdWeightRangePricesSaveReqVO updateReqVO) {
        prodWeightRangePricesService.updateProdWeightRangePrices(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除体重区间价格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:prod-weight-range-prices:delete')")
    public CommonResult<Boolean> deleteProdWeightRangePrices(@RequestParam("id") Long id) {
        prodWeightRangePricesService.deleteProdWeightRangePrices(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得体重区间价格")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:prod-weight-range-prices:query')")
    public CommonResult<ProdWeightRangePricesRespVO> getProdWeightRangePrices(@RequestParam("id") Long id) {
        ProdWeightRangePricesDO prodWeightRangePrices = prodWeightRangePricesService.getProdWeightRangePrices(id);
        return success(BeanUtils.toBean(prodWeightRangePrices, ProdWeightRangePricesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得体重区间价格分页")
    @PreAuthorize("@ss.hasPermission('tz:prod-weight-range-prices:query')")
    public CommonResult<PageResult<ProdWeightRangePricesRespVO>> getProdWeightRangePricesPage(@Valid ProdWeightRangePricesPageReqVO pageReqVO) {
        PageResult<ProdWeightRangePricesDO> pageResult = prodWeightRangePricesService.getProdWeightRangePricesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdWeightRangePricesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出体重区间价格 Excel")
    @PreAuthorize("@ss.hasPermission('tz:prod-weight-range-prices:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProdWeightRangePricesExcel(@Valid ProdWeightRangePricesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProdWeightRangePricesDO> list = prodWeightRangePricesService.getProdWeightRangePricesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "体重区间价格.xls", "数据", ProdWeightRangePricesRespVO.class,
                        BeanUtils.toBean(list, ProdWeightRangePricesRespVO.class));
    }

}