package com.xindazhou.business.controller.admin.community.ban;

import com.xindazhou.business.controller.admin.community.ban.vo.BanPageReqVO;
import com.xindazhou.business.controller.admin.community.ban.vo.BanRespVO;
import com.xindazhou.business.controller.admin.community.ban.vo.BanSaveReqVO;
import com.xindazhou.business.service.community.ban.BanService;
import com.xindazhou.framework.common.pojo.CommonResult;
import com.xindazhou.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.xindazhou.framework.common.pojo.CommonResult.success;

/**
 * 管理后台 - 封禁管理 Controller
 *
 * @author xindazhou
 */
@Tag(name = "管理后台 - 封禁管理")
@RestController("communityBanController")
@RequestMapping("/admin/community/ban")
@Validated
@Slf4j
public class BanController {

    @Resource
    private BanService banService;

    @GetMapping("/page")
    @Operation(summary = "获得封禁分页列表")
    @PreAuthorize("@ss.hasPermission('community:ban:query')")
    public CommonResult<PageResult<BanRespVO>> getBanPage(@Valid BanPageReqVO pageReqVO) {
        PageResult<BanRespVO> pageResult = banService.getBanPage(pageReqVO);
        return success(pageResult);
    }

    @PostMapping("/create")
    @Operation(summary = "封禁用户")
    @PreAuthorize("@ss.hasPermission('community:ban:create')")
    public CommonResult<Long> banUser(@Valid @RequestBody BanSaveReqVO createReqVO) {
        Long id = banService.createBan(createReqVO);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(summary = "更新封禁")
    @PreAuthorize("@ss.hasPermission('community:ban:update')")
    public CommonResult<Boolean> updateBan(@Valid @RequestBody BanSaveReqVO updateReqVO) {
        banService.updateBan(updateReqVO);
        return success(true);
    }

    @PostMapping("/unban")
    @Operation(summary = "解禁用户")
    @PreAuthorize("@ss.hasPermission('community:ban:unban')")
    public CommonResult<Boolean> unbanUser(@RequestParam("userId") Long userId) {
        banService.unbanUser(userId);
        return success(true);
    }

}

