package com.tashow.cloud.product.controller;

import com.tashow.cloud.product.dto.SkuDO;
import com.tashow.cloud.product.service.SkuService;
import com.tashow.cloud.product.vo.sku.SkuPageReqVO;
import com.tashow.cloud.product.vo.sku.SkuRespVO;
import com.tashow.cloud.product.vo.sku.SkuSaveReqVO;
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 = "管理后台 - 单品SKU")
@RestController
@RequestMapping("/tz/sku")
@Validated
public class SkuController {

    @Resource
    private SkuService skuService;

    @PostMapping("/create")
    @Operation(summary = "创建单品SKU")
    @PreAuthorize("@ss.hasPermission('tz:sku:create')")
    public CommonResult<Long> createSku(@Valid @RequestBody SkuSaveReqVO createReqVO) {
        return success(skuService.createSku(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新单品SKU")
    @PreAuthorize("@ss.hasPermission('tz:sku:update')")
    public CommonResult<Boolean> updateSku(@Valid @RequestBody SkuSaveReqVO updateReqVO) {
        skuService.updateSku(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除单品SKU")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tz:sku:delete')")
    public CommonResult<Boolean> deleteSku(@RequestParam("id") Long id) {
        skuService.deleteSku(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得单品SKU")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tz:sku:query')")
    public CommonResult<SkuRespVO> getSku(@RequestParam("id") Long id) {
        SkuDO sku = skuService.getSku(id);
        return success(BeanUtils.toBean(sku, SkuRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得单品SKU分页")
    @PreAuthorize("@ss.hasPermission('tz:sku:query')")
    public CommonResult<PageResult<SkuRespVO>> getSkuPage(@Valid SkuPageReqVO pageReqVO) {
        PageResult<SkuDO> pageResult = skuService.getSkuPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SkuRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出单品SKU Excel")
    @PreAuthorize("@ss.hasPermission('tz:sku:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSkuExcel(@Valid SkuPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SkuDO> list = skuService.getSkuPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "单品SKU.xls", "数据", SkuRespVO.class,
                        BeanUtils.toBean(list, SkuRespVO.class));
    }

}