// 文件路径: com/yami/shop/platform/controller/AdvertInfoController.java
package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.AdvertInfo;
import com.yami.shop.bean.param.AdvertInfoParam;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.common.util.R;
import com.yami.shop.service.IAdvertInfoService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 广告信息管理控制器
 *
 * @author wy
 */
@Slf4j
@RestController
@RequestMapping("/platform/advertInfo")
@AllArgsConstructor
public class AdvertInfoController {

    private final IAdvertInfoService advertInfoService;

    /**
     * 分页获取广告列表
     */
    @GetMapping("/page")
//    @PreAuthorize("@pms.hasPermission('platform:advertInfo:page')")
    public R<IPage<AdvertInfo>> page(
            @RequestParam(required = false) String advertName,
            @RequestParam(required = false) Integer status,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {

        ResponseEntity<IPage<AdvertInfo>> response = advertInfoService.search(advertName, status, current, size);
        return R.SUCCESS(response.getBody());
    }

    /**
     * 根据ID获取广告详情
     */
    @GetMapping("/info/{id}")
//    @PreAuthorize("@pms.hasPermission('platform:advertInfo:info')")
    public R<AdvertInfoParam> info(@PathVariable("id") Integer id) {
        ResponseEntity<AdvertInfoParam> response = advertInfoService.get(id);
        return R.SUCCESS(response.getBody());
    }

    /**
     * 添加广告
     */
    @PostMapping
//    @PreAuthorize("@pms.hasPermission('platform:advertInfo:save')")
    public R<Void> save(@Valid @RequestBody AdvertInfoParam param) {
        ResponseEntity<Void> response = advertInfoService.addOrUpdate(param);
        return R.SUCCESS();
    }

    /**
     * 更新广告
     */
    @PutMapping
//    @PreAuthorize("@pms.hasPermission('platform:advertInfo:update')")
    public R<Void> update(@Valid @RequestBody AdvertInfoParam param) {
        ResponseEntity<Void> response = advertInfoService.addOrUpdate(param);
        return R.SUCCESS();
    }

    /**
     * 删除广告
     */
    @DeleteMapping("/{id}")
//    @PreAuthorize("@pms.hasPermission('platform:advertInfo:delete')")
    public R<Void> delete(@PathVariable("id") Integer id) {
        ResponseEntity<Void> response = advertInfoService.del(id);
        return R.SUCCESS();
    }

    /**
     * 操作广告状态（启用/禁用）
     */
    @PutMapping("/operate/{id}")
//    @PreAuthorize("@pms.hasPermission('platform:advertInfo:operate')")
    public R<Void> operate(@PathVariable("id") Integer id) {
        ResponseEntity<Void> response = advertInfoService.operate(id);
        return R.SUCCESS();
    }
}
