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

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.huiju.estateagents.base.BaseController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 项目户型表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@RestController
@RequestMapping("/api")
public class TaBuildingApartmentController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(TaBuildingApartmentController.class);
    @Autowired
    private ITaBuildingApartmentService taBuildingApartmentService;

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

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