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

/**
 * <p>
    * 菜单按钮表  前端控制器
    * </p>
 *
 * @author jobob
 * @since 2019-09-11
 */
@RestController
@RequestMapping("/")
public class SysButtonInMenuController extends BaseController {

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

    @Autowired
    public ISysButtonInMenuService iSysButtonInMenuService;


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

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

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

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

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

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