package com.mcwl.web.controller.communityCenter;


import cn.hutool.core.bean.BeanUtil;
import com.mcwl.common.core.domain.AjaxResult;
import com.mcwl.common.core.domain.R;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.communityCenter.domain.Community;
import com.mcwl.communityCenter.domain.dto.CommunityRes;
import com.mcwl.communityCenter.service.CommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 社区
 */
@Api(tags = "社区")
@RestController
@RequestMapping("community")
@RequiredArgsConstructor
public class CommunityController {

    private final CommunityService communityService;


    /**
     * 社区列表
     *
     * @return 社区列表
     */
    @ApiOperation(value = "社区列表")
    @PostMapping("list")
    public TableDataInfo getCommunityList(@RequestBody @Valid PageDomain pageDomain) {
        return communityService.listByPage(pageDomain);
    }


    /**
     * 添加社区
     *
     * @param communityRes 社区参数
     * @return 添加结果
     */
    @ApiOperation(value = "添加社区")
    @PostMapping("add")
    public R<Object> addCommunity(@RequestBody @Valid CommunityRes communityRes) {
        communityService.addCommunity(communityRes);
        return R.ok();
    }

    /**
     * 删除社区
     *
     * @param id 社区id
     * @return 删除结果
     */
    @ApiOperation(value = "删除社区")
    @GetMapping("delete")
    public R<Object> deleteCommunity(@NotNull(message = "id不能为空")
                                      @ApiParam(value = "id", required = true)
                                      @Valid
                                      Long id) {
        communityService.removeById(id);
        return R.ok();
    }

}
