package com.sz.admin.system.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import com.sz.core.common.entity.ApiPageResult;
import com.sz.core.common.entity.ApiResult;
import com.sz.core.common.constant.GlobalConstant;
import com.sz.core.common.entity.PageResult;
import com.sz.core.common.entity.SelectIdsDTO;
import com.sz.admin.system.service.ProductService;
import com.sz.admin.system.pojo.dto.ProductCreateDTO;
import com.sz.admin.system.pojo.dto.ProductUpdateDTO;
import com.sz.admin.system.pojo.dto.ProductListDTO;
import com.sz.admin.system.pojo.vo.ProductVO;
import com.sz.core.common.entity.ImportExcelDTO;
import jakarta.servlet.http.HttpServletResponse;

/**
 * <p>
 * 商品信息表 Controller
 * </p>
 *
 * @author Lz
 * @since 2026-01-22
 */
@Tag(name = "商品信息表")
@RestController
@RequestMapping("sys-product")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @Operation(summary = "新增")
    @SaCheckPermission(value = "sys.product.create")
    @PostMapping
    public ApiResult<Void> create(@RequestBody ProductCreateDTO dto) {
        productService.create(dto);
        return ApiResult.success();
    }

    @Operation(summary = "修改")
    @SaCheckPermission(value = "sys.product.update")
    @PutMapping
    public ApiResult<Void> update(@RequestBody ProductUpdateDTO dto) {
        productService.update(dto);
        return ApiResult.success();
    }

    @Operation(summary = "删除")
    @SaCheckPermission(value = "sys.product.remove")
    @DeleteMapping
    public ApiResult<Void> remove(@RequestBody SelectIdsDTO dto) {
        productService.remove(dto);
        return ApiResult.success();
    }

    @Operation(summary = "列表查询")
    @SaCheckPermission(value = "sys.product.query_table")
    @GetMapping
    public ApiResult<PageResult<ProductVO>> list(ProductListDTO dto) {
        return ApiPageResult.success(productService.page(dto));
    }

    @Operation(summary = "详情")
    @SaCheckPermission(value = "sys.product.query_table")
    @GetMapping("/{id}")
    public ApiResult<ProductVO> detail(@PathVariable Object id) {
        return ApiResult.success(productService.detail(id));
    }

    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "上传文件", schema = @Schema(type = "string", format = "binary"), required = true),
    })
    @SaCheckPermission(value = "sys.product.import")
    @PostMapping("/import")
    public void importExcel(@ModelAttribute ImportExcelDTO dto) {
        productService.importExcel(dto);
    }

    @Operation(summary = "导出")
    @SaCheckPermission(value = "sys.product.export")
    @PostMapping("/export")
    public void exportExcel(@RequestBody ProductListDTO dto, HttpServletResponse response) {
        productService.exportExcel(dto, response);
    }
}