package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.DiscountsActivityService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.DiscountsActivityForm;
import com.zsElectric.boot.business.model.query.DiscountsActivityQuery;
import com.zsElectric.boot.business.model.vo.DiscountsActivityVO;
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-20 16:16
 */
@Tag(name = "优惠活动接口")
@RestController
@RequestMapping("/api/v1/discounts-activity")
@RequiredArgsConstructor
public class DiscountsActivityController {

    private final DiscountsActivityService discountsActivityService;

    @Operation(summary = "优惠活动分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:discounts-activity:query')")
    public PageResult<DiscountsActivityVO> getDiscountsActivityPage(DiscountsActivityQuery queryParams) {
        IPage<DiscountsActivityVO> result = discountsActivityService.getDiscountsActivityPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增优惠活动")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:discounts-activity:add')")
    public Result<Void> saveDiscountsActivity(@RequestBody @Valid DiscountsActivityForm formData) {
        boolean result = discountsActivityService.saveDiscountsActivity(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取优惠活动表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:discounts-activity:edit')")
    public Result<DiscountsActivityForm> getDiscountsActivityForm(
            @Parameter(description = "优惠活动ID") @PathVariable Long id
    ) {
        DiscountsActivityForm formData = discountsActivityService.getDiscountsActivityFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改优惠活动")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:discounts-activity:edit')")
    public Result<Void> updateDiscountsActivity(
            @Parameter(description = "优惠活动ID") @PathVariable Long id,
            @RequestBody @Validated DiscountsActivityForm formData
    ) {
        boolean result = discountsActivityService.updateDiscountsActivity(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除优惠活动")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:discounts-activity:delete')")
    public Result<Void> deleteDiscountsActivitys(
            @Parameter(description = "优惠活动ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = discountsActivityService.deleteDiscountsActivitys(ids);
        return Result.judge(result);
    }
}
