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.TaPersonIntentionRecord;
import com.huiju.estateagents.service.ITaPersonIntentionRecordService;
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;

/**
 * <p>
    * 用户意向流水  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-08-07
 */
@RestController
@RequestMapping("/")
public class TaPersonIntentionRecordController extends BaseController {

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

    @Autowired
    public ITaPersonIntentionRecordService iTaPersonIntentionRecordService;


    /**
     * 分页查询列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping(value="/taPersonIntentionRecord",method= RequestMethod.GET)
    public ResponseBean taPersonIntentionRecordList(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                                    @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "buildingId", required = false) String buildingId){
        ResponseBean responseBean = new ResponseBean();
        try {
            //使用分页插件
		    IPage<TaPersonIntentionRecord> pg = new Page<>(pageNum, pageSize);

            responseBean = iTaPersonIntentionRecordService.selectAll(pg, buildingId);
        }catch (Exception e){
            logger.error("taPersonIntentionRecordList -=- {}",e.toString());
            responseBean.addError(e.getMessage());
        }
        return responseBean;
    }

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

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

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

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