package com.zcxk.rmcp.web.controller;

import com.zcxk.core.common.pojo.AjaxMessage;
import com.zcxk.core.mysql.pageing.Pagination;
import com.zcxk.rmcp.api.dto.community.CommunityAddDto;
import com.zcxk.rmcp.api.dto.community.CommunityDto;
import com.zcxk.rmcp.api.dto.community.CommunityPageDto;
import com.zcxk.rmcp.api.vo.CommunityVo;
import com.zcxk.rmcp.core.entity.Community;
import com.zcxk.rmcp.web.service.CommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author Andy
 * @version V1.0
 **/
@Controller
@ResponseBody
@RequestMapping("/system/community")
@Api(tags = "系统管理-小区管理")
public class CommunityController {

    @Autowired
    private CommunityService communityService;

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ApiOperation(value = "查询单个小区")
    public AjaxMessage<Community> findCommunity(@ApiParam(value = "小区ID", required = true) @RequestParam("id") Integer id) {
        return AjaxMessage.success(communityService.findById(id));
    }

    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiOperation(value = "分页查询小区列表")
    public AjaxMessage<Pagination<CommunityVo>> pageCommunity(@RequestBody @Valid CommunityPageDto communityDto) {
        return AjaxMessage.success(communityService.pageCommunity(communityDto));
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ApiOperation(value = "查询小区列表")
    public AjaxMessage<List> listCommunity(@RequestBody @Valid CommunityDto communityDto) {
        return AjaxMessage.success(communityService.listCommunity(communityDto));
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加小区")
    public AjaxMessage<Void> addCommunity(@RequestBody @Valid CommunityAddDto communityAddDto) {
        communityService.insertCommunity(communityAddDto);
        return AjaxMessage.success();
    }

    @RequestMapping(value = "/del", method = RequestMethod.GET)
    @ApiOperation(value = "删除小区")
    public AjaxMessage<Void> delCommunity(@ApiParam(value = "小区ID", required = true) @RequestParam("id") Integer id) {
        communityService.deleteCommunity(id);
        return AjaxMessage.success();
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "编辑小区")
    public AjaxMessage<Void> editCommunity(@RequestBody @Valid CommunityAddDto communityDto) {
        communityService.updateByPrimaryKeySelective(communityDto);
        return AjaxMessage.success();
    }

}
