package com.njcn.msgpush.module.push.controller.admin.ratelimit;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.msgpush.framework.common.pojo.CommonResult;
import com.njcn.msgpush.module.push.controller.admin.ratelimit.VO.RateLimitConfigReqVO;
import com.njcn.msgpush.module.push.dal.dataobject.ratelimit.RateLimitConfigDO;
import com.njcn.msgpush.module.push.service.ratelimit.RateLimitConfigService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.njcn.msgpush.framework.common.pojo.CommonResult.success;

/**
 * @author caozehui
 * @data 2026-03-13
 */
@Tag(name = "管理后台 - 单人配额")
@Validated
@RestController
@RequestMapping("/push/ratelimit")
public class RateLimitConfigController {
    @Autowired
    private RateLimitConfigService rateLimitConfigService;

    @PostMapping("/page")
    @Operation(summary = "分页单人配额列表")
    @PreAuthorize("@ss.hasPermission('push:ratelimit:page')")
    public CommonResult<Page<RateLimitConfigDO>> pageRateLimitConfig(@Validated @RequestBody RateLimitConfigReqVO reqVO) {
        Page<RateLimitConfigDO> res = rateLimitConfigService.getPage(reqVO);
        return success(res);
    }

    @PostMapping("/add")
    @Operation(summary = "添加单人配额")
    @PreAuthorize("@ss.hasPermission('push:ratelimit:add')")
    public CommonResult<Boolean> add(@Validated @RequestBody RateLimitConfigReqVO reqVO) {
        boolean res = rateLimitConfigService.save(BeanUtil.copyProperties(reqVO, RateLimitConfigDO.class));
        return success(res);
    }

    @PostMapping("/update")
    @Operation(summary = "更新单人配额")
    @PreAuthorize("@ss.hasPermission('push:ratelimit:update')")
    public CommonResult<Boolean> update(@Validated @RequestBody RateLimitConfigReqVO reqVO) {
        boolean res = rateLimitConfigService.updateById(BeanUtil.copyProperties(reqVO, RateLimitConfigDO.class));
        return success(res);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除单人配额")
    @PreAuthorize("@ss.hasPermission('push:ratelimit:delete')")
    @Parameter(name = "ids", description = "id列表", required = true)
    public CommonResult<Boolean> delete(@RequestParam List<Long> ids) {
        return success(rateLimitConfigService.removeByIds(ids));
    }
}
