package controllers

import (
	"encoding/json"
	"oppmg/common/log"
	"oppmg/models"
	"oppmg/protocol"
	serverbac "oppmg/services/rbac"
)

type RbacController struct {
	BaseController
}

//URLMapping 实现ControllerInterface中的URLMapping
func (c *RbacController) URLMapping() {
	//c.Mapping("AccessToken", c.AccessToken)
}

// RoleAdd 添加部门
// @router /role [post]
func (c *RbacController) RoleAdd() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	var param protocol.RequestRoleAdd
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	param.Types = models.ROLETYPES_ROLE
	param.CompanyID = c.GetCompanyId()
	if param.CompanyID <= 0 {
		log.Error("param.CompanyID <= 0")
		msg = protocol.BadRequestParam("1")
		return
	}

	r, err := serverbac.RoleAdd(param)
	msg = protocol.NewReturnResponse(r, err)
	return
}

// RoleUpdate 编辑部门
// @router /role [put]
func (c *RbacController) RoleUpdate() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	var param protocol.RequestRoleEdit
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	param.CompanyID = c.GetCompanyId()
	if param.CompanyID <= 0 {
		log.Error("param.CompanyID <= 0")
		msg = protocol.BadRequestParam("1")
		return
	}
	param.Types = models.ROLETYPES_ROLE
	r, err := serverbac.RoleEdit(param)
	msg = protocol.NewReturnResponse(r, err)
	return
}

// RoleDelete 删除角色
// @router /role[delete]
func (c *RbacController) RoleDelete() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	var param protocol.RequestRoleDelete
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	param.CompanyID = c.GetCompanyId()
	if param.CompanyID <= 0 {
		log.Error("param.CompanyID <= 0")
		msg = protocol.BadRequestParam("1")
		return
	}

	err := serverbac.RoleDelete(param)

	msg = protocol.NewReturnResponse(nil, err)
	return
}

// RoleGroupAdd 角色组
// @router /role_group [post]
func (c *RbacController) RoleGroupAdd() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	var param protocol.RequestRoleAdd
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	param.CompanyID = c.GetCompanyId()
	if param.CompanyID <= 0 {
		log.Error("param.CompanyID <= 0")
		msg = protocol.BadRequestParam("1")
		return
	}
	param.Pid = 0
	param.Types = models.ROLETYPES_GROUP
	roleinfo, err := serverbac.RoleAdd(param)
	msg = protocol.NewReturnResponse(roleinfo, err)
	return
}

// RoleGroupEdit 角色组
// @router /role_group [put]
func (c *RbacController) RoleGroupUpdate() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	var param protocol.RequestRoleEdit
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	param.CompanyID = c.GetCompanyId()
	if param.CompanyID <= 0 {
		log.Error("param.CompanyID <= 0")
		msg = protocol.BadRequestParam("1")
		return
	}
	param.Pid = 0
	param.Types = models.ROLETYPES_GROUP
	roleinfo, err := serverbac.RoleEdit(param)
	msg = protocol.NewReturnResponse(roleinfo, err)
	return
}

// RoleGroupEdit 角色组
// @router /role_group [delete]
func (c *RbacController) RoleGroupDelete() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	var param protocol.RequestRoleDelete
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	param.CompanyID = c.GetCompanyId()
	if param.CompanyID <= 0 {
		log.Error("param.CompanyID <= 0")
		msg = protocol.BadRequestParam("1")
		return
	}

	err := serverbac.RoleGroupDelete(param)
	msg = protocol.NewReturnResponse(nil, err)
	return
}

// RoleList
// @router /role [get]
func (c *RbacController) RoleList() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	var companyid int64
	companyid = c.GetCompanyId()
	if companyid <= 0 {
		msg = protocol.BadRequestParam("1")
		return
	}
	rolelist, err := serverbac.GetRoleList(companyid)
	msg = protocol.NewReturnResponse(rolelist, err)
	return
}

//PermissionList
//@router  /menu/list [post]
func (c *RbacController) MenuList() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	list, err := serverbac.GetMenuAll()
	msg = protocol.NewReturnResponse(list, err)
	return
}

//GetRoleHasMenu  获取角色下的菜单
//@router /role/menu
func (c *RbacController) RoleHasMenu() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	type Parameter struct {
		RoleId int64 `json:"role_id"`
	}
	var param Parameter
	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
		log.Error("json 解析失败 err:%s", err)
		msg = protocol.BadRequestParam("1")
		return
	}
	companyid := c.GetCompanyId()
	list, err := serverbac.GetRoleHasMenu(param.RoleId, companyid)
	msg = protocol.NewReturnResponse(list, err)
	return
}

func (c *RbacController) RoleMenuEdit() {
	return
}