package com.huiju.estateagents.third.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.third.entity.TaMpInfo;
import com.huiju.estateagents.third.service.ITaMpInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
    *   前端控制器
    * </p>
 *
 * @author fxf
 * @since 2020-06-02
 */
@RestController
@RequestMapping("/")
public class TaMpInfoController extends BaseController {

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

    @Autowired
    public ITaMpInfoService iTaMpInfoService;


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

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

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

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

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

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