package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.PromotionCouponTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.PromotionCouponTemplateForm;
import com.zsElectric.boot.business.model.query.PromotionCouponTemplateQuery;
import com.zsElectric.boot.business.model.vo.PromotionCouponTemplateVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zsElectric.boot.core.web.PageResult;
import com.zsElectric.boot.core.web.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 优惠劵模板前端控制层
 *
 * @author zsElectric
 * @since 2025-12-12 15:11
 */
@Tag(name = "优惠劵模板接口")
@RestController
@RequestMapping("/api/v1/promotion-coupon-template")
@RequiredArgsConstructor
public class PromotionCouponTemplateController  {

    private final PromotionCouponTemplateService promotionCouponTemplateService;

    @Operation(summary = "优惠劵模板分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:promotion-coupon-template:query')")
    public PageResult<PromotionCouponTemplateVO> getPromotionCouponTemplatePage(PromotionCouponTemplateQuery queryParams ) {
        IPage<PromotionCouponTemplateVO> result = promotionCouponTemplateService.getPromotionCouponTemplatePage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增优惠劵模板")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:promotion-coupon-template:add')")
    public Result<Void> savePromotionCouponTemplate(@RequestBody @Valid PromotionCouponTemplateForm formData ) {
        boolean result = promotionCouponTemplateService.savePromotionCouponTemplate(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取优惠劵模板表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:promotion-coupon-template:edit')")
    public Result<PromotionCouponTemplateForm> getPromotionCouponTemplateForm(
        @Parameter(description = "优惠劵模板ID") @PathVariable Long id
    ) {
        PromotionCouponTemplateForm formData = promotionCouponTemplateService.getPromotionCouponTemplateFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改优惠劵模板")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:promotion-coupon-template:edit')")
    public Result<Void> updatePromotionCouponTemplate(
            @Parameter(description = "优惠劵模板ID") @PathVariable Long id,
            @RequestBody @Validated PromotionCouponTemplateForm formData
    ) {
        boolean result = promotionCouponTemplateService.updatePromotionCouponTemplate(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除优惠劵模板")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:promotion-coupon-template:delete')")
    public Result<Void> deletePromotionCouponTemplates(
        @Parameter(description = "优惠劵模板ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = promotionCouponTemplateService.deletePromotionCouponTemplates(ids);
        return Result.judge(result);
    }
}
