package com.zsElectric.boot.business.controller;

import com.zsElectric.boot.business.service.AdvertisingService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zsElectric.boot.business.model.form.AdvertisingForm;
import com.zsElectric.boot.business.model.query.AdvertisingQuery;
import com.zsElectric.boot.business.model.vo.AdvertisingVO;
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 wzq
 * @since 2025-11-26 15:57
 */
@Tag(name = "v2.0 广告管理接口")
@RestController
@RequestMapping("/api/v1/advertising")
@RequiredArgsConstructor
public class AdvertisingController  {

    private final AdvertisingService advertisingService;

    @Operation(summary = "广告管理分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('business:advertising:query')")
    public PageResult<AdvertisingVO> getAdvertisingPage(AdvertisingQuery queryParams ) {
        IPage<AdvertisingVO> result = advertisingService.getAdvertisingPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增广告管理")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('business:advertising:add')")
    public Result<Void> saveAdvertising(@RequestBody @Valid AdvertisingForm formData ) {
        boolean result = advertisingService.saveAdvertising(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取广告管理表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('business:advertising:edit')")
    public Result<AdvertisingForm> getAdvertisingForm(
        @Parameter(description = "广告管理ID") @PathVariable Long id
    ) {
        AdvertisingForm formData = advertisingService.getAdvertisingFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改广告管理")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('business:advertising:edit')")
    public Result<Void> updateAdvertising(
            @Parameter(description = "广告管理ID") @PathVariable Long id,
            @RequestBody @Validated AdvertisingForm formData
    ) {
        boolean result = advertisingService.updateAdvertising(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除广告管理")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('business:advertising:delete')")
    public Result<Void> deleteAdvertisings(
        @Parameter(description = "广告管理ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = advertisingService.deleteAdvertisings(ids);
        return Result.judge(result);
    }
}
