package com.tashow.cloud.product.controller.admin;

import com.tashow.cloud.common.pojo.CommonResult;
import com.tashow.cloud.common.pojo.PageParam;
import com.tashow.cloud.common.pojo.PageResult;
import com.tashow.cloud.common.util.object.BeanUtils;
import com.tashow.cloud.excel.excel.core.util.ExcelUtils;
import com.tashow.cloud.productapi.api.product.dto.ProductOrderLimitDO;
import com.tashow.cloud.product.service.ProductOrderLimitService;
import com.tashow.cloud.productapi.api.product.vo.productorderlimit.ProductOrderLimitPageReqVO;
import com.tashow.cloud.productapi.api.product.vo.productorderlimit.ProductOrderLimitRespVO;
import com.tashow.cloud.productapi.api.product.vo.productorderlimit.ProductOrderLimitSaveReqVO;
import com.tashow.cloud.web.apilog.core.annotation.ApiAccessLog;
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.servlet.http.HttpServletResponse;
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.io.IOException;
import java.util.List;

import static com.tashow.cloud.common.pojo.CommonResult.success;
import static com.tashow.cloud.web.apilog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 商品接单上限设置")
@RestController
@RequestMapping("/tz/product-order-limit")
@Validated
public class ProductOrderLimitController {

    @Resource
    private ProductOrderLimitService productOrderLimitService;

    @PostMapping("/create")
    @Operation(summary = "创建商品接单上限设置")
    @PreAuthorize("@ss.hasPermission('tz:product-order-limit:create')")
    public CommonResult<Long> createProductOrderLimit(@Valid @RequestBody ProductOrderLimitSaveReqVO createReqVO) {
        return success(productOrderLimitService.createProductOrderLimit(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品接单上限设置")
    @PreAuthorize("@ss.hasPermission('tz:product-order-limit:update')")
    public CommonResult<Boolean> updateProductOrderLimit(@Valid @RequestBody ProductOrderLimitSaveReqVO updateReqVO) {
        productOrderLimitService.updateProductOrderLimit(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品接单上限设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:product-order-limit:delete')")
    public CommonResult<Boolean> deleteProductOrderLimit(@RequestParam("id") Long id) {
        productOrderLimitService.deleteProductOrderLimit(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品接单上限设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:product-order-limit:query')")
    public CommonResult<ProductOrderLimitRespVO> getProductOrderLimit(@RequestParam("id") Long id) {
        ProductOrderLimitDO productOrderLimit = productOrderLimitService.getProductOrderLimit(id);
        return success(BeanUtils.toBean(productOrderLimit, ProductOrderLimitRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品接单上限设置分页")
    @PreAuthorize("@ss.hasPermission('tz:product-order-limit:query')")
    public CommonResult<PageResult<ProductOrderLimitRespVO>> getProductOrderLimitPage(@Valid ProductOrderLimitPageReqVO pageReqVO) {
        PageResult<ProductOrderLimitDO> pageResult = productOrderLimitService.getProductOrderLimitPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProductOrderLimitRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品接单上限设置 Excel")
    @PreAuthorize("@ss.hasPermission('tz:product-order-limit:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProductOrderLimitExcel(@Valid ProductOrderLimitPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProductOrderLimitDO> list = productOrderLimitService.getProductOrderLimitPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品接单上限设置.xls", "数据", ProductOrderLimitRespVO.class,
                        BeanUtils.toBean(list, ProductOrderLimitRespVO.class));
    }

}