package controllers

import (
	"gdyb/enums"
	"gdyb/models"
	"github.com/astaxie/beego"
	//"XT_New/models"

	//"XT_New/models"
	"gdyb/service"
)

type BaseAPIController struct {
	BaseController
}

// func (this *BaseAPIController) Prepare() {
// 	this.BaseController.Prepare()
// 	beego.Trace("============================================================")
// 	beego.Trace("session ID: %v", this.Ctx.Input.Cookie("beegosessionID"))
// 	beego.Trace("session : %v", this.GetSession("info"))
// 	this.SetSession("info", time.Now().Format("2006/01/02 15:04:05"))
// 	beego.Trace("============================================================")
// }

// 输出数据格式化
/*
	success json:
	{
		"state": 1,
		"code": 0,
		"data": json,
	}

	fail json:
	{
		"state": 0,
		"code": int,
		"msg": string,
	}
*/
func (this *BaseAPIController) ServeSuccessJSON(data map[string]interface{}) {
	this.Data["json"] = enums.MakeSuccessResponseJSON(data)
	this.ServeJSON()
}

func (this *BaseAPIController) ServeFailJSONWithSGJErrorCode(code int) {
	this.Data["json"] = enums.MakeFailResponseJSONWithSGJErrorCode(code)
	this.ServeJSON()
}

func (this *BaseAPIController) ServeFailJSONWithSGJError(err *enums.SGJError) {
	this.Data["json"] = enums.MakeFailResponseJSONWithSGJError(err)
	this.ServeJSON()
}

func (this *BaseAPIController) ServeFailJsonSend(code int, msg string) {
	this.Data["json"] = enums.MakeFailResponseJSON(msg, code)
	this.ServeJSON()
}

type BaseAuthAPIController struct {
	BaseAPIController
}

func (this *BaseAuthAPIController) Prepare() {
	this.BaseAPIController.Prepare()
	if this.GetAdminUserInfo() == nil {

		//org_id, _ := beego.AppConfig.Int64("org_id")
		org_id, _ := beego.AppConfig.Int64("org_id")

		var userAdmin models.AdminUser
		userAdmin.Id = 1448
		userAdmin.Mobile = "13318599895"
		//userAdmin.Id = 597
		//userAdmin.Mobile = "19874122664"
		userAdmin.IsSuperAdmin = false
		userAdmin.Status = 1
		userAdmin.CreateTime = 1530786071
		userAdmin.ModifyTime = 1530786071
		var subscibe models.ServeSubscibe
		subscibe.ID = 1
		subscibe.OrgId = org_id
		subscibe.PeriodStart = 1538035409
		subscibe.PeriodEnd = 1569571409
		subscibe.State = 1
		subscibe.Status = 1
		subscibe.CreatedTime = 1538035409
		subscibe.UpdatedTime = 1538035409
		subscibes := make(map[int64]*models.ServeSubscibe, 0)
		subscibes[4] = &subscibe
		var adminUserInfo service.AdminUserInfo
		adminUserInfo.CurrentOrgId = org_id
		adminUserInfo.CurrentAppId = 8642
		adminUserInfo.AdminUser = &userAdmin
		this.SetSession("admin_user_info", &adminUserInfo)
	}

	//if this.Ctx.Request.Header.Get("Permission") == "5" { //医保相关权限
	//	org_id, _ := beego.AppConfig.Int64("org_id")
	//	org, _ := service.GetOrgById(org_id)
	//	admin_user_id, _ := this.GetInt64("admin_user_id")
	//	if admin_user_id != org.Creator { //超级管理员不受此限制
	//		isPermission := false
	//		//该机构下该用户有多少个
	//		role, _ := service.GetUserAllRole(org_id, admin_user_id)
	//		var roles []string
	//		if len(role.RoleIds) <= 0 { //该用户没有设置角色
	//
	//		} else {
	//			roles = strings.Split(role.RoleIds, ",")
	//		}
	//		fmt.Println(roles)
	//
	//		//获取该用户下所有角色的权限总集
	//		var userRolePurviews string
	//		var userRolePurviewsArr []string
	//		for _, item := range roles {
	//			role_id, _ := strconv.ParseInt(item, 10, 64)
	//			purviews, _ := service.GetRoleFuncPurviewIds(role_id)
	//			if len(userRolePurviews) == 0 {
	//				userRolePurviews = purviews
	//			} else {
	//				userRolePurviews = userRolePurviews + "," + purviews
	//			}
	//		}
	//		//该用户所拥有角色的权限的总集
	//		userRolePurviewsArr = RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
	//		fmt.Println(userRolePurviewsArr)
	//		//系统所记录的权限列表
	//		allPermission, _ := service.GetAllFunctionPurview()
	//
	//		for _, item := range allPermission {
	//			//获取该角色的所有权限
	//			for _, items := range userRolePurviewsArr {
	//				id, _ := strconv.ParseInt(items, 10, 64)
	//				if id == item.ID {
	//					isPermission = true
	//				}
	//			}
	//			if !isPermission {
	//				msg, _ := service.FindErrorMsgByStr(item.ID)
	//				json := make(map[string]interface{})
	//				json["msg"] = msg
	//				json["code"] = 0
	//				json["state"] = 0
	//				this.Data["json"] = json
	//				this.ServeJSON()
	//				this.StopRun()
	//			}
	//
	//		}
	//	}
	//}

}

type BaseServeAPIController struct {
	BaseAPIController
}

func (this *BaseServeAPIController) Prepare() {
	this.BaseAPIController.Prepare()
	if this.GetAdminUserInfo() == nil {
		org_id, _ := beego.AppConfig.Int64("org_id")
		var userAdmin models.AdminUser
		userAdmin.Id = 1448
		userAdmin.Mobile = "13318599895"
		userAdmin.IsSuperAdmin = false
		userAdmin.Status = 1
		userAdmin.CreateTime = 1530786071
		userAdmin.ModifyTime = 1530786071
		var subscibe models.ServeSubscibe
		subscibe.ID = 1
		subscibe.OrgId = org_id
		subscibe.PeriodStart = 1538035409
		subscibe.PeriodEnd = 1569571409
		subscibe.State = 1
		subscibe.Status = 1
		subscibe.CreatedTime = 1538035409
		subscibe.UpdatedTime = 1538035409
		subscibes := make(map[int64]*models.ServeSubscibe, 0)
		subscibes[4] = &subscibe
		var adminUserInfo service.AdminUserInfo
		adminUserInfo.CurrentOrgId = org_id
		adminUserInfo.AdminUser = &userAdmin
		this.SetSession("admin_user_info", &adminUserInfo)
	}

	//if this.Ctx.Request.Header.Get("Permission") == "5" { //医保相关权限
	//	org_id, _ := beego.AppConfig.Int64("org_id")
	//	org, _ := service.GetOrgById(org_id)
	//	admin_user_id, _ := this.GetInt64("admin_user_id")
	//	if admin_user_id != org.Creator { //超级管理员不受此限制
	//		isPermission := false
	//		//该机构下该用户有多少个
	//		role, _ := service.GetUserAllRole(org_id, admin_user_id)
	//		var roles []string
	//		if len(role.RoleIds) <= 0 { //该用户没有设置角色
	//
	//		} else {
	//			roles = strings.Split(role.RoleIds, ",")
	//		}
	//		fmt.Println(roles)
	//
	//		//获取该用户下所有角色的权限总集
	//		var userRolePurviews string
	//		var userRolePurviewsArr []string
	//		for _, item := range roles {
	//			role_id, _ := strconv.ParseInt(item, 10, 64)
	//			purviews, _ := service.GetRoleFuncPurviewIds(role_id)
	//			if len(userRolePurviews) == 0 {
	//				userRolePurviews = purviews
	//			} else {
	//				userRolePurviews = userRolePurviews + "," + purviews
	//			}
	//		}
	//		//该用户所拥有角色的权限的总集
	//		userRolePurviewsArr = RemoveRepeatedPurviewElement2(strings.Split(userRolePurviews, ","))
	//		fmt.Println(userRolePurviewsArr)
	//		//系统所记录的权限列表
	//		allPermission, _ := service.GetAllFunctionPurview()
	//
	//		for _, item := range allPermission {
	//			//获取该角色的所有权限
	//			for _, items := range userRolePurviewsArr {
	//				id, _ := strconv.ParseInt(items, 10, 64)
	//				if id == item.ID {
	//					isPermission = true
	//				}
	//			}
	//			if !isPermission {
	//				msg, _ := service.FindErrorMsgByStr(item.ID)
	//				json := make(map[string]interface{})
	//				json["msg"] = msg
	//				json["code"] = 0
	//				json["state"] = 0
	//				this.Data["json"] = json
	//				this.ServeJSON()
	//				this.StopRun()
	//			}
	//
	//		}
	//	}
	//}

}

func RemoveRepeatedPurviewElement2(arr []string) (newArr []string) {
	newArr = make([]string, 0)
	for i := 0; i < len(arr); i++ {
		repeat := false
		for j := i + 1; j < len(arr); j++ {
			if arr[i] == arr[j] {
				repeat = true
				break
			}
		}
		if !repeat {
			newArr = append(newArr, arr[i])
		}
	}
	return
}
