package com.huiju.estateagents.redpack.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.redpack.entity.TaMpMiniapp;
import com.huiju.estateagents.redpack.service.ITaMpMiniappService;
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 TaMpMiniappController extends BaseController {

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

    @Autowired
    public ITaMpMiniappService iTaMpMiniappService;


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

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

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

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

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

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