package com.gyee.frame.controller.admin;

import com.github.pagehelper.PageInfo;
import com.gyee.frame.common.base.BaseController;
import com.gyee.frame.common.domain.AjaxResult;
import com.gyee.frame.model.auto.TvueRole;
import com.gyee.frame.model.custom.BootstrapTree;
import com.gyee.frame.model.custom.TableSplitResult;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.model.custom.TitleVo;
import com.gyee.frame.shiro.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * 角色Controller
 * @author gyee
 * @date: 2018年9月2日 下午8:08:21
 */
@Api(value = "用户角色")
@Controller
@RequestMapping("/RoleVueController")
public class RoleVueController extends BaseController{
	
	
	//跳转页面参数
	private String prefix = "admin/rolevue";
	
	/**
	 * 展示页面
	 * @param model
	 * @return
	 * @author gyee
	 * @Date 2019年11月11日 下午4:01:58
	 */
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/view")
	@RequiresPermissions("system:rolevue:view")
    public String view(ModelMap model)
    {	
		String str="VUE角色";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	
	/**
	 * 角色列表
	 * @param tablepar
	 * @param searchText 搜索字符
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping("/list")
	@RequiresPermissions("system:rolevue:list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchText){
		PageInfo<TvueRole> page=vueRoleService.list(tablepar,searchText) ; 
		TableSplitResult<TvueRole> result=new TableSplitResult<TvueRole>(page.getPageNum(), page.getTotal(), page.getList()); 
		return  result;
	}
	
	/**
     * 新增角色
     */
	@ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
	
	
    /**
     * 角色添加
     * @param rolevue
     * @return
     */
	//@Log(title = "角色添加", action = "1")
	@ApiOperation(value = "新增", notes = "新增")
	@PostMapping("/add")
	@RequiresPermissions("system:rolevue:add")
	@ResponseBody
	public AjaxResult add(TvueRole rolevue,String prem){
		int b=vueRoleService.insertRoleAndPrem(rolevue,prem);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除角色
	 * @param ids
	 * @return
	 */
	//@Log(title = "删除角色", action = "1")
	@ApiOperation(value = "删除", notes = "删除")
	@PostMapping("/remove")
	@RequiresPermissions("system:rolevue:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=vueRoleService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 检查角色
	 * @return
	 */
	@ApiOperation(value = "检查Name唯一", notes = "检查Name唯一")
	@PostMapping("/checkNameUnique")
	@ResponseBody
	public int checkNameUnique(TvueRole tsysRole){
		int b=vueRoleService.checkNameUnique(tsysRole);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
	 * 修改角色
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") String id, ModelMap mmap)
    {
        mmap.put("TvueRole", vueRoleService.selectByPrimaryKey(id));
        return prefix + "/edit";
    }
	
	/**
     * 修改保存角色
     */
	//@Log(title = "修改保存角色", action = "1")
	@ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("system:rolevue:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TvueRole tsysRole,String prem,HttpServletRequest request)
    {
    	int i=vueRoleService.updateRoleAndPrem(tsysRole,prem);
    	if(i>0) {
    		//大于0刷新权限
    		ShiroUtils.clearCachedAuthorizationInfo();
    		//获取菜单栏
        	BootstrapTree bootstrapTree= sysPermissionService.getbooBootstrapTreePerm(ShiroUtils.getUserId());
        	request.getSession().setAttribute("bootstrapTree", bootstrapTree);
    	}
		
        return toAjax(i);
    }
}
