package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.CouponTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.CouponTemplateForm;
import com.zsElectric.boot.business.model.query.CouponTemplateQuery;
import com.zsElectric.boot.business.model.vo.CouponTemplateVO;
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-15 11:14
 */
@Tag(name = "优惠劵模板接口")
@RestController
@RequestMapping("/api/v1/coupon-template")
@RequiredArgsConstructor
public class CouponTemplateController  {

    private final CouponTemplateService couponTemplateService;

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

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

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

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

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