package controllers

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

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
	}
	if param.CompanyID <= 0 {
		log.Error("param.CompanyID <= 0")
		msg = protocol.BadRequestParam("1")
		return
	}
	param.Types = models.ROLETYPES_ROLE
	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
	}
	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
	}
	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
	}
	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
	}
	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
	}
	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 /:companyid/role [get]
func (c *RbacController) RoleList() {
	var msg *protocol.ResponseMessage
	defer func() {
		c.ResposeJson(msg)
	}()
	var companyid int64
	param := c.Ctx.Input.Param(":companyid")
	companyid, _ = strconv.ParseInt(param, 10, 64)
	if companyid <= 0 {
		msg = protocol.BadRequestParam("1")
		return
	}
	rolelist, err := serverbac.GetRoleList(companyid)
	msg = protocol.NewReturnResponse(rolelist, err)
	return
}