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.entity.TaBuildingIntention;
import com.huiju.estateagents.entity.TdBizEvent;
import com.huiju.estateagents.service.ITaBuildingIntentionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.huiju.estateagents.base.BaseController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
    * 项目意向表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-07
 */
@RestController
@RequestMapping("/api")
public class TaBuildingIntentionController extends BaseController {

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

    @Autowired
    public ITaBuildingIntentionService iTaBuildingIntentionService;


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

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

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


    /**
     *  批量 保存 或者 修改 对象  意向值和列表
     * @param tdBizEventList 实体对象集合
     * @return
     */
    @RequestMapping(value="/admin/taBuildingIntentionAddOrUpdate/{buildingId}",method= RequestMethod.POST)
    public ResponseBean taBuildingIntentionAddOrUpdate(@RequestBody List<TdBizEvent> tdBizEventList, @PathVariable String buildingId, HttpServletRequest request){
        ResponseBean responseBean = new ResponseBean();
        responseBean = iTaBuildingIntentionService.taBuildingIntentionAddOrUpdate(tdBizEventList, buildingId, getOrgId(request));
        return responseBean;
    }


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

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

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