package com.huiju.estateagents.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huiju.estateagents.base.BaseController;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.entity.TaBuildingRoom;
import com.huiju.estateagents.service.ITaBuildingRoomService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
    * 房间表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2020-02-10
 */
@RestController
@RequestMapping("/")
public class TaBuildingRoomController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaBuildingRoomController.class);

    @Autowired
    public ITaBuildingRoomService iTaBuildingRoomService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/taBuildingRoom",method= RequestMethod.GET)
    public ResponseBean taBuildingRoomList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                           @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaBuildingRoom> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaBuildingRoom> queryWrapper = new QueryWrapper<>();
            queryWrapper.orderByDesc("create_date");

            IPage<TaBuildingRoom> result = iTaBuildingRoomService.page(pg, queryWrapper);
            responseBean.addSuccess(result);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taBuildingRoomList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 保存对象
     * @param taBuildingRoom 实体对象
     * @return
     */
    @RequestMapping(value="/taBuildingRoom",method= RequestMethod.POST)
    public ResponseBean taBuildingRoomAdd(@RequestBody TaBuildingRoom taBuildingRoom){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaBuildingRoomService.save(taBuildingRoom)){
                responseBean.addSuccess(taBuildingRoom);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taBuildingRoomAdd -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     */
    @ResponseBody
    @RequestMapping(value="/taBuildingRoom/{id}", method= RequestMethod.DELETE)
    public ResponseBean taBuildingRoomDelete(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            if(iTaBuildingRoomService.removeById(id)){
                responseBean.addSuccess("success");
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taBuildingRoomDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 修改对象
     * @param id  实体ID
     * @param taBuildingRoom 实体对象
     * @return
     */
    @RequestMapping(value="/taBuildingRoom/{id}",method= RequestMethod.PUT)
    public ResponseBean taBuildingRoomUpdate(@PathVariable Integer id,
                                             @RequestBody TaBuildingRoom taBuildingRoom){
        ResponseBean responseBean = new ResponseBean();
        try {
            if (iTaBuildingRoomService.updateById(taBuildingRoom)){
                responseBean.addSuccess(taBuildingRoom);
            }else {
                responseBean.addError("fail");
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taBuildingRoomUpdate -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

    /**
     * 根据id查询对象
     * @param id  实体ID
     */
    @RequestMapping(value="/taBuildingRoom/{id}",method= RequestMethod.GET)
    public ResponseBean taBuildingRoomGet(@PathVariable Integer id){
        ResponseBean responseBean = new ResponseBean();
        try {
            responseBean.addSuccess(iTaBuildingRoomService.getById(id));
        }catch (Exception e){
            e.printStackTrace();
            logger.error("taBuildingRoomDelete -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }
}
