package co.yixiang.yshop.module.product.controller.admin.productcoupon;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.product.controller.admin.productcoupon.vo.*;
import co.yixiang.yshop.module.product.service.productcoupon.ProductCouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 商品优惠券")
@RestController
@RequestMapping("/product/coupon")
@Validated
public class ProductCouponController {

    @Resource
    private ProductCouponService couponService;


    @PostMapping("/create")
    @Operation(summary = "创建优惠券")
    @PreAuthorize("@ss.hasPermission('product:coupon:create')")
    public CommonResult<Long> createCoupon(@Validated @RequestBody ProductCouponCreateReqVO vo) {
        return success(couponService.createCoupon(vo));
    }

    @PostMapping("/update")
    @Operation(summary = "修改优惠券")
    @PreAuthorize("@ss.hasPermission('product:coupon:update')")
    public CommonResult<Boolean> updateCoupon(@Validated @RequestBody ProductCouponUpdateReqVO vo) {
        couponService.updateCoupon(vo);
        return success(true);
    }

    @GetMapping("/detail/{id}")
    @Operation(summary = "优惠券详情")
    @PreAuthorize("@ss.hasPermission('product:coupon:detail')")
    public CommonResult<ProductCouponDetailRespVO> getCouponDetail(@PathVariable Long id) {
        return success(couponService.getCouponDetail(id));
    }

    @GetMapping("/page")
    @Operation(summary = "优惠券分页")
    @PreAuthorize("@ss.hasPermission('product:coupon:query')")
    public CommonResult<PageResult<ProductCouponDetailRespVO>> getStoreProductPage(@Valid StoreProductCouponPageReqVO pageVO) {
        return success(couponService.getStoreProductPage(pageVO));
    }

    @GetMapping("/delete/{id}")
    @Operation(summary = "删除优惠券")
    @PreAuthorize("@ss.hasPermission('product:coupon:delete')")
    public CommonResult<Boolean> deleteCoupon(@PathVariable Long id) {
        return success(couponService.deleteCoupon(id));
    }

    @GetMapping("/canvas/page")
    @Operation(summary = "画布优惠券分页")
    @PreAuthorize("@ss.hasPermission('product:coupon:canvas:page')")
    public CommonResult<PageResult<CanvasProductCouponRespVO>> getCanvasProductPage(@Valid CanvasProductCouponPageReqVO pageVO) {
        return success(couponService.getCanvasProductPage(pageVO));
    }

}
