package com.backendsys.controller.B2c;

import com.backendsys.aspect.QueryNullCheck;
import com.backendsys.aspect.QueryNullCheckAspect;
import com.backendsys.entity.B2c.B2cGoodSpecDTO;
import com.backendsys.entity.PageDTO;
import com.backendsys.service.B2c.B2cGoodSpecService;
import com.backendsys.utils.response.Result;
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
public class B2cGoodSpecController {

    @Autowired
    private B2cGoodSpecService b2cGoodSpecService;

    @PreAuthorize("@sr.hasPermission('11.5')")
    @GetMapping("/api/b2c/good/getGoodSpec")
    public Result getGoodSpec(@Validated PageDTO pageDTO, @Validated B2cGoodSpecDTO b2cGoodSpecDTO) {
        return Result.success(b2cGoodSpecService.queryGoodSpecList(pageDTO.getPage_num(), pageDTO.getPage_size(), b2cGoodSpecDTO));
    }

    @PreAuthorize("@sr.hasPermission('11.5.1')")
    @QueryNullCheck(serviceClass = B2cGoodSpecService.class, serviceMethod = "queryGoodSpecDetail", argField = "good_spec_id", message = "商品规格不存在")
    @GetMapping("/api/b2c/good/getGoodSpecDetail")
    public Result getGoodSpecDetail(@Validated(B2cGoodSpecDTO.Detail.class) B2cGoodSpecDTO b2cGoodSpecDTO) {
        return Result.success(QueryNullCheckAspect.getQueryResult());
    }

    @PreAuthorize("@sr.hasPermission('11.5.2')")
    @PostMapping("/api/b2c/good/createGoodSpec")
    public Result createGoodSpec(@Validated(B2cGoodSpecDTO.Create.class) @RequestBody B2cGoodSpecDTO b2cGoodSpecDTO) {
        return Result.success(b2cGoodSpecService.insertGoodSpec(b2cGoodSpecDTO), "创建成功");
    }

    @PreAuthorize("@sr.hasPermission('11.5.3')")
    @QueryNullCheck(serviceClass = B2cGoodSpecService.class, serviceMethod = "queryGoodSpecDetail", argField = "good_spec_id", message = "商品规格不存在")
    @PutMapping("/api/b2c/good/updateGoodSpec")
    public Result updateGoodSpec(@Validated(B2cGoodSpecDTO.Update.class) @RequestBody B2cGoodSpecDTO b2cGoodSpecDTO) {
        return Result.success(b2cGoodSpecService.updateGoodSpec(b2cGoodSpecDTO), "更新成功");
    }

    @PreAuthorize("@sr.hasPermission('11.5.4')")
    @QueryNullCheck(serviceClass = B2cGoodSpecService.class, serviceMethod = "queryGoodSpecDetail", argField = "good_spec_id", message = "商品规格不存在")
    @DeleteMapping("/api/b2c/good/deleteGoodSpec")
    public Result deleteGoodSpec(@Validated(B2cGoodSpecDTO.Delete.class) @RequestBody B2cGoodSpecDTO b2cGoodSpecDTO) {
        return Result.success(b2cGoodSpecService.deleteGoodSpec(b2cGoodSpecDTO), "删除成功");
    }
}
