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

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njcn.msgpush.framework.common.pojo.CommonResult;
import com.njcn.msgpush.module.push.controller.admin.blacklist.vo.BlacklistReqVO;
import com.njcn.msgpush.module.push.dal.dataobject.blacklist.BlacklistDO;
import com.njcn.msgpush.module.push.service.blacklist.BlacklistService;
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-12
 */
@Tag(name = "管理后台 - 黑名单")
@Validated
@RestController
@RequestMapping("/push/blacklist")
public class BlacklistController {
    @Autowired
    private BlacklistService blacklistService;

    @PostMapping("/page")
    @Operation(summary = "分页查询黑名单列表")
    @PreAuthorize("@ss.hasPermission('push:blacklist:page')")
    public CommonResult<Page<BlacklistDO>> pageBlacklist(@Validated @RequestBody BlacklistReqVO reqVO) {
        Page<BlacklistDO> res = blacklistService.getPage(reqVO);
        return success(res);
    }

    @PostMapping("/add")
    @Operation(summary = "添加黑名单")
    @PreAuthorize("@ss.hasPermission('push:blacklist:add')")
    public CommonResult<Boolean> add(@RequestBody BlacklistReqVO reqVO) {
        return success(blacklistService.save(BeanUtil.copyProperties(reqVO, BlacklistDO.class)));
    }

    @PostMapping("/update")
    @Operation(summary = "更新黑名单")
    @PreAuthorize("@ss.hasPermission('push:blacklist:update')")
    public CommonResult<Boolean> update(@RequestBody BlacklistReqVO reqVO) {
        return success(blacklistService.updateById(BeanUtil.copyProperties(reqVO, BlacklistDO.class)));
    }

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