package com.mall.product.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.domain.vo.result.PageResult;
import com.mall.common.domain.vo.result.Result;
import com.mall.common.domain.vo.result.ResultCodeEnum;
import com.mall.product.domain.dto.AttrDto;
import com.mall.product.domain.entity.Attr;
import com.mall.product.domain.vo.AttrVo;
import com.mall.product.service.AttrService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品属性 前端控制器
 * </p>
 *
 * @author Bunny
 * @since 2025-07-04 15:19:19
 */
@Tag(name = "商品属性", description = "商品属性相关接口")
@RestController
@RequestMapping("/api/product/attr")
@RequiredArgsConstructor
public class AttrController {

    private final AttrService attrService;

    @Operation(summary = "分页查询商品属性", description = "分页商品属性")
    @GetMapping("{page}/{limit}")
    public Result<PageResult<AttrVo>> getAttrPage(
            @Parameter(name = "page", description = "当前页", required = true)
            @PathVariable("page") Integer page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable("limit") Integer limit,
            AttrDto dto) {
        Page<Attr> pageParams = new Page<>(page, limit);
        PageResult<AttrVo> pageResult = attrService.getAttrPage(pageParams, dto);
        return Result.success(pageResult);
    }

    @Operation(summary = "添加商品属性", description = "添加商品属性")
    @PostMapping()
    public Result<String> addAttr(@Valid @RequestBody AttrDto dto) {
        attrService.addAttr(dto);
        return Result.success(ResultCodeEnum.ADD_SUCCESS);
    }

    @Operation(summary = "更新商品属性", description = "更新商品属性")
    @PutMapping()
    public Result<String> updateAttr(@Valid @RequestBody AttrDto dto) {
        attrService.updateAttr(dto);
        return Result.success(ResultCodeEnum.UPDATE_SUCCESS);
    }

    @Operation(summary = "删除商品属性", description = "删除商品属性")
    @DeleteMapping()
    public Result<String> deleteAttr(@RequestBody List<Long> ids) {
        attrService.deleteAttr(ids);
        return Result.success(ResultCodeEnum.DELETE_SUCCESS);
    }
}