package com.yunzhi.marketing.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.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.entity.TaRoleMenu;
import com.yunzhi.marketing.service.ITaRoleMenuService;
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 TaRoleMenuController extends BaseController {

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

    @Autowired
    public ITaRoleMenuService iTaRoleMenuService;


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

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

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

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

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

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