package com.seer.teach.mall.good.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.annotation.LogPrint;
import com.seer.teach.common.enums.GoodsAttributeScopeEnum;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.mall.request.GoodsAttributePageReq;
import com.seer.teach.mall.request.GoodsAttributeReq;
import com.seer.teach.mall.response.GoodsAttributeResq;
import com.seer.teach.mall.service.IGoodsAttributeService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.constraints.NotEmpty;
import java.util.List;

/**
 * <p>
 *  商家商品属性控制器
 * </p>
 *
 * @author 
 * @since 2025-06-12
 */
@RestController
@RequestMapping("/mall/merchant/goods/attribute")
@Tag(name = "商家端 - 商品属性管理")
@Validated
@RequiredArgsConstructor
@SaCheckLogin(type = StpUtil.TYPE)
public class MerchantGoodsAttributeController {

    private final IGoodsAttributeService attributeService;

    @PostMapping("/list")
    @LogPrint
    @Operation(summary = "商品属性分页查询")
    @SaCheckPermission("merchant:goods:attribute:list")
    public ResultBean<PageListBean<GoodsAttributeResq>> getAttributePage(@RequestBody @Validated GoodsAttributePageReq pageReq){
        // 商家可以查看平台属性和自己的属性
        PageListBean<GoodsAttributeResq> result = attributeService.getAttributePage(pageReq);
        return ResultBean.success(result);
    }

    @GetMapping("/info/{id}")
    @LogPrint
    @Operation(summary = "根据id查询商品属性信息")
    @SaCheckPermission("merchant:goods:attribute:info")
    public ResultBean<GoodsAttributeResq> getAttributeInfo(@PathVariable Integer id){
        return ResultBean.success(attributeService.getAttributeInfo(id));
    }

    @PostMapping("/save")
    @LogPrint
    @Operation(summary = "新增商品属性")
    @SaCheckPermission("merchant:goods:attribute:save")
    public ResultBean<Boolean> saveAttribute(@RequestBody @Validated GoodsAttributeReq params){
        params.setScope(GoodsAttributeScopeEnum.MERCHANT.getCode());
        params.setMerchantId(StpUtil.getLoginIdAsInt());
        return ResultBean.success(attributeService.saveAttribute(params));
    }

    @PutMapping("/update")
    @LogPrint
    @Operation(summary = "修改商品信息")
    @SaCheckPermission("merchant:goods:attribute:update")
    public ResultBean<Boolean> updateAttribute(@RequestBody @Validated GoodsAttributeReq params){
        return ResultBean.success(attributeService.updateAttribute(params));
    }

    @PostMapping("/delete")
    @LogPrint
    @Operation(summary = "删除商品信息")
    @SaCheckPermission("merchant:goods:attribute:delete")
    public ResultBean<Boolean> deleteAttribute(@Parameter(description = "商品id") @RequestBody @NotEmpty List<Integer> ids){
        return ResultBean.success(attributeService.deleteAttribute(ids));
    }
}