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 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 com.huiju.estateagents.service.ITaRaiseInviteService;
import com.huiju.estateagents.entity.TaRaiseInvite;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
    * 认筹被邀请清单  前端控制器
    * </p>
 *
 * @author fxf
 * @since 2020-03-30
 */
@RestController
@RequestMapping("/")
public class TaRaiseInviteController extends BaseController {

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

    @Autowired
    public ITaRaiseInviteService iTaRaiseInviteService;


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

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

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

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

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

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