package com.backendsys.controller.B2c;

import com.backendsys.aspect.QueryNullCheck;
import com.backendsys.aspect.QueryNullCheckAspect;
import com.backendsys.entity.B2c.B2cGoodSpecSkuDTO;
import com.backendsys.entity.PageDTO;
import com.backendsys.service.B2c.B2cGoodSpecSkuService;
import com.backendsys.utils.response.Result;
import com.backendsys.utils.response.ResultEnum;
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.*;

/**
 * 商品规格SKU
 */
@Validated
@RestController
public class B2cGoodSpecSkuController {

    @Autowired
    private B2cGoodSpecSkuService b2cGoodSpecSkuService;

    @PreAuthorize("@sr.hasPermission('11.6')")
    @GetMapping("/api/b2c/good/getGoodSpecSku")
    public Result getGoodSpecSku(@Validated PageDTO pageDTO, @Validated B2cGoodSpecSkuDTO b2cGoodSpecSkuDTO) {
        if (b2cGoodSpecSkuDTO.getGood_id() == null) {
            return Result.error(ResultEnum.PARAMETER_EXCEPTION.getCode(), "good_id 不能为空");
        }
        return Result.success(b2cGoodSpecSkuService.queryGoodSpecSkuList(pageDTO.getPage_num(), pageDTO.getPage_size(), b2cGoodSpecSkuDTO));
    }

    @PreAuthorize("@sr.hasPermission('11.6.1')")
    @QueryNullCheck(serviceClass = B2cGoodSpecSkuService.class, serviceMethod = "queryGoodSpecSkuDetail", argField = "good_spec_sku_id", message = "商品规格SKU不存在")
    @GetMapping("/api/b2c/good/getGoodSpecSkuDetail")
    public Result getGoodSpecSkuDetail(@Validated(B2cGoodSpecSkuDTO.Detail.class) B2cGoodSpecSkuDTO b2cGoodSpecSkuDTO) {
        return Result.success(QueryNullCheckAspect.getQueryResult());
    }

    @PreAuthorize("@sr.hasPermission('11.6.2')")
    @PostMapping("/api/b2c/good/createGoodSpecSku")
    public Result createGoodSpecSku(@Validated(B2cGoodSpecSkuDTO.Create.class) @RequestBody B2cGoodSpecSkuDTO b2cGoodSpecSkuDTO) {
        return Result.success(b2cGoodSpecSkuService.insertGoodSpecSku(b2cGoodSpecSkuDTO), "创建成功");
    }

    @PreAuthorize("@sr.hasPermission('11.6.3')")
    @QueryNullCheck(serviceClass = B2cGoodSpecSkuService.class, serviceMethod = "queryGoodSpecSkuDetail", argField = "good_spec_sku_id", message = "商品规格SKU不存在")
    @PutMapping("/api/b2c/good/updateGoodSpecSku")
    public Result updateGoodSpecSku(@Validated(B2cGoodSpecSkuDTO.Update.class) @RequestBody B2cGoodSpecSkuDTO b2cGoodSpecSkuDTO) {
        return Result.success(b2cGoodSpecSkuService.updateGoodSpecSku(b2cGoodSpecSkuDTO), "更新成功");
    }

    @PreAuthorize("@sr.hasPermission('11.6.4')")
    @QueryNullCheck(serviceClass = B2cGoodSpecSkuService.class, serviceMethod = "queryGoodSpecSkuDetail", argField = "good_spec_sku_id", message = "商品规格SKU不存在")
    @DeleteMapping("/api/b2c/good/deleteGoodSpecSku")
    public Result deleteGoodSpecSku(@Validated(B2cGoodSpecSkuDTO.Delete.class) @RequestBody B2cGoodSpecSkuDTO b2cGoodSpecSkuDTO) {
        return Result.success(b2cGoodSpecSkuService.deleteGoodSpecSku(b2cGoodSpecSkuDTO), "删除成功");
    }
}
