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.TaBuildingUnit;
import com.huiju.estateagents.service.ITaBuildingUnitService;
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 TaBuildingUnitController extends BaseController {

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

    @Autowired
    public ITaBuildingUnitService iTaBuildingUnitService;


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

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

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

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

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

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