// 给角色分配资源
func (this *RoleController) AllocationRes() {
	Id, _ := this.GetInt64("Id")
	fmt.Println("给角色分配资源权限")
	fmt.Printf("Id is %d\n", Id)

	res, _ := m.GetResourceByRoleId(Id)
	_, allRes := m.GetAllResource()
	tree := make([]PermissionTree, len(allRes))
	for k, allResOne := range allRes {
		var flag bool = false
		allId := allResOne["Id"].(int64)
		for _, resOne := range res {

			if resOne["Id"].(int64) == allId {
				tree[k].Fid = allId
				tree[k].Pfid = allResOne["Pid"].(int64)
				tree[k].Fname = allResOne["Name"].(string)
				tree[k].Ischecked = true
				flag = true
			}
		}
		if !flag {
			tree[k].Fid = allId
			tree[k].Pfid = allResOne["Pid"].(int64)
			tree[k].Fname = allResOne["Name"].(string)
		}
	}

	this.Data["roleId"] = Id

	this.Data["list"] = &tree
	fmt.Printf("String is %s", &tree)
	this.TplNames = "role/permission.html"
}
// 资源列表
func (this *ResController) List() {
	if this.IsAjax() {
		sEcho := this.GetString("sEcho")
		iDisplayStart := this.GetString("iDisplayStart")
		iDisplayLength := this.GetString("iDisplayLength")
		iStart, _ := strconv.Atoi(iDisplayStart)
		iLength, _ := strconv.Atoi(iDisplayLength)
		page := iStart / iLength
		count, _ := m.GetAllResource()
		res, _ := m.GetResources(int64(page+1), int64(iLength), "Level")

		for _, resource := range res {
			switch resource["Type"] {
			case "0":
				resource["Typename"] = "目录"
			case "1":
				resource["Typename"] = "菜单"
			case "2":
				resource["Typename"] = "按钮"
			}
		}

		data := make(map[string]interface{})
		data["aaData"] = res
		data["iTotalDisplayRecords"] = count
		data["iTotalRecords"] = iLength
		data["sEcho"] = sEcho
		this.Data["json"] = &data
		this.ServeJson()

	} else {
		this.Data["ActionUrl"] = "localhost:8080"
		this.TplNames = "res/index.html"
	}
}
func (this *CommonController) GetResList(uname string, Id int64) []Tree {
	var cnt, length int = 0, 0
	var resources []orm.Params
	adminUser := beego.AppConfig.String("admin_user")
	if uname == adminUser {
		_, resources = m.GetAllResource()
	} else {
		resources, _ = m.GetResourcesByRoleId(Id)
	}

	for _, v := range resources {
		if v["Pid"].(int64) == 0 {
			length = length + 1
		}
	}
	tree := make([]Tree, length)
	for k, v := range resources {
		fmt.Printf("\n key is %d and id  is %d Pid is %d\n", k, v["Id"], v["Pid"])
		if v["Pid"].(int64) == 0 {
			k = cnt
			cnt = cnt + 1
			tree[k].Id = v["Id"].(int64)
			tree[k].Index = cnt
			tree[k].Text = v["Name"].(string)
			// 1代表菜单(目录下面的所有资源)没有把一些不需要的权限去掉
			// children, _ := m.GetResourceTree(v["Id"].(int64), 1)

			var childCnt int = 0
			children := make([]map[string]interface{}, 3)
			for _, v3 := range resources {
				if v3["Pid"].(int64) == v["Id"].(int64) {
					children[childCnt] = v3
					childCnt++
				}
			}

			tree[k].Children = make([]Tree, childCnt)
			for k1, v1 := range children {
				fmt.Printf("\n kv1 is %d\n", v1)
				if v1 == nil {
					fmt.Printf("\n data is over is %d\n")
				} else {
					if v1["Pid"].(int64) == v["Id"].(int64) {
						tree[k].Children[k1].Id = v1["Id"].(int64)
						tree[k].Children[k1].Text = v1["Name"].(string)
						tree[k].Children[k1].Url = "/" + v1["Url"].(string)
					}
				}
			}

		}

	}
	return tree
}