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.common.CommConstant;
import com.huiju.estateagents.entity.TaBuildingBlock;
import com.huiju.estateagents.service.ITaBuildingBlockService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

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

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

    @Autowired
    public ITaBuildingBlockService iTaBuildingBlockService;


    /**
     * author fxf
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/admin/taBuildingBlock",method= RequestMethod.GET)
    public ResponseBean taBuildingBlockList(@RequestParam(value ="buildingId",required = true) String buildingId,
                                            @RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                            @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                            HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaBuildingBlock> pg = new Page<>(pageNum, pageSize);
            QueryWrapper<TaBuildingBlock> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("org_id",getOrgId(request));
            queryWrapper.eq("building_id",buildingId);
            queryWrapper.eq("status", CommConstant.STATUS_NORMAL);
            queryWrapper.orderByDesc("create_date");

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

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

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

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

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