package com.yunzhi.marketing.center.sysUser.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.center.sysUser.entity.SysToken;
import com.yunzhi.marketing.center.sysUser.service.ISysTokenService;
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 SysTokenController extends BaseController {

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

    @Autowired
    public ISysTokenService iSysTokenService;


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

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

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

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

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

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