package com.tashow.cloud.product.controller;

import com.tashow.cloud.product.dto.ProdDO;
import com.tashow.cloud.product.vo.prod.*;
import jakarta.annotation.security.PermitAll;
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.web.apilog.core.annotation.ApiAccessLog;
import static com.tashow.cloud.web.apilog.core.enums.OperateTypeEnum.*;
import com.tashow.cloud.product.service.ProdService;

@Tag(name = "管理后台 - 商品")
@RestController
@RequestMapping("/prod")
@Validated
public class ProdController {

    @Resource
    private ProdService prodService;

    @PostMapping("/create")
    @Operation(summary = "创建商品")
    @PreAuthorize("@ss.hasPermission('tashow-module-product:prod:create')")
    public CommonResult<Long> createProd(@Valid @RequestBody ProdSaveReqVO createReqVO) {
        return success(prodService.createProd(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品")
    @PreAuthorize("@ss.hasPermission('tashow-module-product:prod:update')")
    public CommonResult<Boolean> updateProd(@Valid @RequestBody ProdSaveReqVO updateReqVO) {
        prodService.updateProd(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('tashow-module-product:prod:delete')")
    public CommonResult<Boolean> deleteProd(@RequestParam("id") Long id) {
        prodService.deleteProd(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('tashow-module-product:prod:query')")
    public CommonResult<ProdRespVO> getProd(@RequestParam("id") Long id) {
        ProdDO prod = prodService.getProd(id);
        return success(BeanUtils.toBean(prod, ProdRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品分页")
    @PreAuthorize("@ss.hasPermission('tashow-module-product:prod:query')")
    @PermitAll
    public CommonResult<PageResult<ProdRespVO>> getProdPage(@Valid ProdPageReqVO pageReqVO) {
        PageResult<ProdDO> pageResult = prodService.getProdPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProdRespVO.class));
    }
}