package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.dto.AreaDto;
import com.zcxk.entity.Area;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.AreaService;
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.web.bind.annotation.*;

import java.util.List;

/**
 * (Area)控制层
 *
 * @author makejava
 * @since 2020-10-28 16:51:27
 */
@RestController
@RequestMapping("/area")
@Api(tags = "区域接口")
public class AreaController {
    /**
     * 服务对象
     */
    @Autowired
    private AreaService areaService;

    /**
     * 通过主键查询单条数据
     *
     * @param area 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    @ApiOperation(value = "查询区域列表")
    public AjaxMessage<Area> selectOne(
            @ApiParam(value = "设置配置", required = true) @RequestBody Area area) {
        Area result = areaService.selectById(area.getId());

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 新增一条数据
     *
     * @param area 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "查询区域列表")
    public AjaxMessage<Integer> insert(@ApiParam(value = "设置配置", required = true) @RequestBody Area area) {
        int result = areaService.insert(area);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改一条数据
     *
     * @param area 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "查询区域列表")
    public AjaxMessage<Integer> update(@ApiParam(value = "设置配置", required = true) @RequestBody Area area) {
        int result = areaService.update(area);
        return new AjaxMessage<>(ResultStatus.OK, result);

    }

    /**
     * 删除一条数据
     *
     * @param area 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ApiOperation(value = "查询区域列表")
    public AjaxMessage<Integer> delete(@ApiParam(value = "设置配置", required = true) @RequestBody Area area) {
        int result = areaService.deleteById(area.getId());
        return new AjaxMessage<>(ResultStatus.OK, result);
    }
    /**
     * 修改一条数据
     *
     * @param id 父类id
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.POST)
    @ApiOperation(value = "查询区域列表")
    public AjaxMessage<List<AreaDto>> selectAll(@ApiParam(value = "设置配置", required = true)
                                              @RequestParam Integer id) {
        Area area=new Area();

        return new AjaxMessage<>(ResultStatus.OK, areaService.selectList(area));

    }

    /**
     * 分页查询
     *
     * @param pageNum  偏移
     * @param pageSize 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询区域列表")
    public AjaxMessage<Pagination<Area>> selectPage(Integer pageNum, Integer pageSize) {
        Area area = new Area();
        IPage<Area> iPage = new Page<>(pageNum, pageSize);
        iPage = areaService.selectPage(area, iPage);
        Pagination<Area> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

}