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.TvuePermission;
import com.gyee.frame.model.custom.TableSplitResult;
import com.gyee.frame.model.custom.Tablepar;
import com.gyee.frame.model.custom.TitleVo;
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 java.util.List;

/**
 * VUE权限Controller
 * @author gyee
 * @date: 2018年9月2日 下午8:08:21
 */
@Api(value = "VUE权限")
@Controller
@RequestMapping("/PermissionVueController")
public class PermissionVueController extends BaseController{
	
	//跳转页面参数
	private String prefix = "admin/permissionvue";
	
	/**
	 * VUE权限列表展示
	 * @param model
	 * @return
	 * @author gyee
	 * @Date 2019年11月11日 下午3:43:51
	 */
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/view")
	@RequiresPermissions("system:permissionvue:view")
    public String view(ModelMap model)
    {	
		String str="VUE权限";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	
	/**
	 * VUE权限列表
	 * @param tablepar
	 * @param searchText 搜索字符
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping("/list")
	@RequiresPermissions("system:permissionvue:list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchText){
		PageInfo<TvuePermission> page= vuePermissionService.list(tablepar, searchText) ;
		TableSplitResult<TvuePermission> result=new TableSplitResult<TvuePermission>(page.getPageNum(), page.getTotal(), page.getList());
		return  result;
	}
	/**
	 * VUE权限列表
	 * @param tablepar
	 * @param searchText 搜索字符
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@PostMapping("/list2")
	@ResponseBody
	public Object list2(Tablepar tablepar,String searchText){
		List<TvuePermission> page= vuePermissionService.list2(searchText) ;
		return  page;
	}
	/**
     * 新增VUE权限
     */
	@ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
	
	
    /**
     * VUE权限添加
     * @param role
     * @return
     */
	//@Log(title = "VUE权限添加", action = "1")
	@ApiOperation(value = "新增", notes = "新增")
	@PostMapping("/add")
	@RequiresPermissions("system:permissionvue:add")
	@ResponseBody
	public AjaxResult add(TvuePermission role){
		int b= vuePermissionService.insertSelective(role);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除VUE权限
	 * @param ids
	 * @return
	 */
	//@Log(title = "删除VUE权限", action = "1")
	@ApiOperation(value = "删除", notes = "删除")
	@PostMapping("/remove")
	@RequiresPermissions("system:permissionvue:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b= vuePermissionService.deleteByPrimaryKey(ids);
		if(b==1){
			return success();
		}else if(b==-1){
			return error("该VUE权限有子VUE权限，请先删除子VUE权限");
		}else if(b==-2){
			return error("该VUE权限绑定了角色，请解除角色绑定");
		}else {
			return error();
		}
	}
	
	/**
	 * 检查VUE权限
	 * @param TvuePermission
	 * @return
	 */
	@ApiOperation(value = "检查VUE权限", notes = "检查VUE权限")
	@PostMapping("/checkNameUnique")
	@ResponseBody
	public int checkNameUnique(TvuePermission TvuePermission){
		int b= vuePermissionService.checkNameUnique(TvuePermission);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * 检查VUE权限URL
	 * @return
	 */
	@ApiOperation(value = "检查VUE权限URL", notes = "检查VUE权限URL")
	@PostMapping("/checkURLUnique")
	@ResponseBody
	public int checkURLUnique(TvuePermission tsysPermission){
		int b= vuePermissionService.checkURLUnique(tsysPermission);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * 检查VUE权限perms字段
	 * @return
	 */
	@ApiOperation(value = "检查VUE权限perms字段", notes = "检查VUE权限perms字段")
	@PostMapping("/checkPermsUnique")
	@ResponseBody
	public int checkPermsUnique(TvuePermission tsysPermission){
		int b= vuePermissionService.checkPermsUnique(tsysPermission);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * 修改VUE权限
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/edit/{roleId}")
    public String edit(@PathVariable("roleId") String id, ModelMap mmap)
    {	
		//获取自己的VUE权限信息
		TvuePermission mytsysPermission = vuePermissionService.selectByPrimaryKey(id);
		//获取父VUE权限信息
		TvuePermission pattsysPermission = vuePermissionService.selectByPrimaryKey(mytsysPermission.getPid());
        mmap.put("TvuePermission", mytsysPermission);
        mmap.put("pattsysPermission", pattsysPermission);
        return prefix + "/edit";
    }
	
	/**
     * 修改保存VUE权限
     */
	//@Log(title = "修改保存VUE权限", action = "1")
	@ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("system:permissionvue:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TvuePermission TvuePermission)
    {
        return toAjax(vuePermissionService.updateByPrimaryKey(TvuePermission));
    }
    
    /**
     * 获取所有的转换成bootstarp的VUE权限数据
     * @return
     */
	@ApiOperation(value = "获取所有的转换成bootstarp的VUE权限数据", notes = "获取所有的转换成bootstarp的VUE权限数据")
    @GetMapping("/getTreePerm")
    @ResponseBody
    public AjaxResult getbooBootstrapTreePerm(){
    	
    	return retobject(200, vuePermissionService.getbooBootstrapTreePerm(null));
    }
    
    
    /**
     * 根据角色id获取bootstarp 所有打勾VUE权限
     * @param roleId 角色id集合
     * @return
     */
	@ApiOperation(value = "根据角色id获取bootstarp 所有打勾VUE权限", notes = "根据角色id获取bootstarp 所有打勾VUE权限")
    @PostMapping("/getCheckPrem")
    @ResponseBody
    public AjaxResult getCheckPrem(String roleId){
    	
    	return retobject(200, vuePermissionService.getCheckPrem(roleId));
    }
    
    
    /**
     * 跳转到菜单树页面
     * @return
     */
	@ApiOperation(value = "跳转到菜单树页面", notes = "跳转到菜单树页面")
    @GetMapping("/tree")
    public String Tree(){
    	 return prefix + "/tree";
    }
    
    /**
     * 获取菜单树
     * @param pid 父id【没用到】
     * @return
     */
	@ApiOperation(value = "获取菜单树", notes = "获取菜单树")
    @PostMapping("/tree/{pid}")
    @ResponseBody
    public AjaxResult Tree(@PathVariable("pid") String pid){
    	return retobject(200, vuePermissionService.getbooBootstrapTreePerm(null));
    }
    
    
}
