package org.dromara.system.controller.room;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.system.domain.bo.CommunityBoStr;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.CommunityVo;
import org.dromara.system.domain.bo.CommunityBo;
import org.dromara.system.service.ICommunityService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 社区管理
 *
 * @author Lion Li
 * @date 2025-03-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/community")
public class CommunityController extends BaseController {

    private final ICommunityService communityService;

    /**
     * 查询社区列表
     */
    @SaCheckPermission("system:community:list")
    @GetMapping("/list")
    public TableDataInfo<CommunityVo> list(CommunityBo bo, PageQuery pageQuery) {
        return communityService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出社区列表
     */
    @SaCheckPermission("system:community:export")
    @Log(title = "导出社区", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CommunityBo bo, HttpServletResponse response) {
        List<CommunityVo> list = communityService.queryList(bo);
        ExcelUtil.exportExcel(list, "【请填写功能名称】", CommunityVo.class, response);
    }

    /**
     * 获取社区详细信息
     *
     * @param communityId 主键
     */
    @SaCheckPermission("system:community:query")
    @GetMapping("/{communityId}")
    public R<CommunityVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String communityId) {
        return R.ok(communityService.queryById(communityId));
    }

    /**
     * 新增社区
     */
    @SaCheckPermission("system:community:add")
    @Log(title = "新增社区", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CommunityBoStr bo) {
        return toAjax(communityService.insertByBo(bo));
    }

    /**
     * 修改社区
     */
    @SaCheckPermission("system:community:edit")
    @Log(title = "修改社区", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CommunityBoStr bo) {
        return toAjax(communityService.updateByBo(bo));
    }

    /**
     * 删除社区
     *
     * @param communityIds 主键串
     */
    @SaCheckPermission("system:community:remove")
    @Log(title = "删除社区", businessType = BusinessType.DELETE)
    @DeleteMapping("/{communityIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] communityIds) {
        return toAjax(communityService.deleteWithValidByIds(List.of(communityIds), true));
    }

    @PostMapping("/communityList")
    public R<List<CommunityVo>> communityList(@RequestBody CommunityBo bo) {
        return R.ok(communityService.queryAll(bo));
    }


}
