package com.backendsys.modules.b2c.good.controller;

import com.backendsys.modules.b2c.good.entity.Good;
import com.backendsys.modules.b2c.good.service.GoodService;
import com.backendsys.modules.common.utils.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@Tag(name = "商品管理")
public class GoodController {

    @Autowired
    private GoodService goodService;

    @Operation(summary = "获取商品列表")
    @PreAuthorize("@sr.hasPermission('11.1')")
    @GetMapping("/api/b2c/good/getGoodList")
    public Result getGood(@Validated Good good) {
        return Result.success().put("data", goodService.selectGoodList(good));
    }

    @Operation(summary = "获取商品详情")
    @PreAuthorize("@sr.hasPermission('11.1.1')")
    @GetMapping("/api/b2c/good/getGoodDetail")
    public Result getGoodDetail(@Validated(Good.Detail.class) Good good) {
        return Result.success().put("data", goodService.selectGoodDetail(good));
    }

    @Operation(summary = "创建商品")
    @PreAuthorize("@sr.hasPermission('11.1.2')")
    @PostMapping("/api/b2c/good/createGood")
    public Result createGood(@Validated(Good.Create.class) @RequestBody Good good) {
        return Result.success().put("data", goodService.insertGood(good));
    }

    @Operation(summary = "编辑商品")
    @PreAuthorize("@sr.hasPermission('11.1.3')")
    @PutMapping("/api/b2c/good/updateGood")
    public Result updateGood(@Validated(Good.Update.class) @RequestBody Good good) {
        return Result.success().put("data", goodService.updateGood(good));
    }

    @Operation(summary = "删除商品")
    @PreAuthorize("@sr.hasPermission('11.1.4')")
    @DeleteMapping("/api/b2c/good/deleteGood")
    public Result deleteGood(@Validated(Good.Delete.class) @RequestBody Good good) {
        return Result.success().put("data", goodService.deleteGood(good));
    }

}
