package controllers

import (
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxstore/permission"
	"git.rosy.net.cn/jx-callback/business/jxutils"
	"git.rosy.net.cn/jx-callback/business/model"
	"github.com/astaxie/beego/server/web"
)

type PowerController struct {
	web.Controller
}

// @Title 添加功能（菜单）
// @Description 添加功能（菜单）
// @Param	token			header		string	true	"认证token"
// @Param	payload			formData	string	true	"Menu json实体"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AddMenu [post]
func (c *PowerController) AddMenu() {
	c.callAddMenu(func(params *tPowerAddMenuParams) (retVal interface{}, errCode string, err error) {
		menu := &model.Menu{}
		if err = utils.UnmarshalUseNumber([]byte(params.Payload), menu); err == nil {
			err = permission.AddMenu(params.Ctx, menu)
		}
		return retVal, "", err
	})
}

// @Title 查询功能（菜单）
// @Description 查询功能（菜单）
// @Param	token			header		string	true	"认证token"
// @Param	userID			query		string	false	"用户ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetMenu [get]
func (c *PowerController) GetMenu() {
	c.callGetMenu(func(params *tPowerGetMenuParams) (retVal interface{}, errCode string, err error) {
		retVal, err = permission.GetMenu(params.Ctx, params.UserID)
		return retVal, "", err
	})
}

// @Title 修改功能（菜单）
// @Description 修改功能（菜单）
// @Param	token			header		string	true	"认证token"
// @Param	payload			formData	string	true	"Menu json实体"
// @Param	menuID			formData	int		true	"菜单ID"
// @Param	isDelete		formData	bool	true	"是否是删除"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateMenu [put]
func (c *PowerController) UpdateMenu() {
	c.callUpdateMenu(func(params *tPowerUpdateMenuParams) (retVal interface{}, errCode string, err error) {
		payload := make(map[string]interface{})
		if err = utils.UnmarshalUseNumber([]byte(params.Payload), &payload); err == nil {
			retVal, err = permission.UpdateMenu(params.Ctx, params.MenuID, payload, params.IsDelete)
		}
		return retVal, "", err
	})
}

// @Title 添加角色
// @Description 添加角色
// @Param	token			header		string	true	"认证token"
// @Param	name			formData	string	true	"角色名"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /AddRole [post]
func (c *PowerController) AddRole() {
	c.callAddRole(func(params *tPowerAddRoleParams) (retVal interface{}, errCode string, err error) {
		err = permission.AddRole(params.Ctx, params.Name)
		return retVal, "", err
	})
}

// @Title 查询角色
// @Description 查询角色
// @Param	token			header		string	true	"认证token"
// @Param	name			query		string	false	"角色名"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetRole [get]
func (c *PowerController) GetRole() {
	c.callGetRole(func(params *tPowerGetRoleParams) (retVal interface{}, errCode string, err error) {
		retVal, err = permission.GetRole(params.Ctx, params.Name)
		return retVal, "", err
	})
}

// @Title 修改角色
// @Description 修改角色
// @Param	token			header		string	true	"认证token"
// @Param	name			formData	string	true	"角色名"
// @Param	roleID			formData	int		true	"角色ID"
// @Param	isDelete		formData	bool	true	"是否是删除"
// @Param	brandID			formData	int		false	"品牌ID"
// @Param	cityCodes		formData	string	false	"城市IDs"
// @Param	storeIDs		formData	string	false	"门店IDs"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateRole [put]
func (c *PowerController) UpdateRole() {
	c.callUpdateRole(func(params *tPowerUpdateRoleParams) (retVal interface{}, errCode string, err error) {
		var (
			cityCodes, storeIDs []int
		)
		if err = jxutils.Strings2Objs(params.CityCodes, &cityCodes, params.StoreIDs, &storeIDs); err == nil {
			retVal, err = permission.UpdateRole(params.Ctx, params.RoleID, params.Name, params.IsDelete, params.BrandID, cityCodes, storeIDs)
		}
		return retVal, "", err
	})
}

// @Title 查询用户角色
// @Description查询用户角色
// @Param	token			header		string	true	"认证token"
// @Param	userID			query		string	false	"用户ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetUserRole [get]
func (c *PowerController) GetUserRole() {
	c.callGetUserRole(func(params *tPowerGetUserRoleParams) (retVal interface{}, errCode string, err error) {
		retVal, err = permission.GetUserRole(params.Ctx, params.UserID)
		return retVal, "", err
	})
}

// @Title 修改用户角色
// @Description 修改用户角色
// @Param	token			header		string	true	"认证token"
// @Param	userIDs			formData	string	true	"用户IDs"
// @Param	roleIDs			formData	string	true	"角色IDs"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateUserRole [put]
func (c *PowerController) UpdateUserRole() {
	c.callUpdateUserRole(func(params *tPowerUpdateUserRoleParams) (retVal interface{}, errCode string, err error) {
		var (
			userIDs []string
			roleIDs []int
		)
		if err = jxutils.Strings2Objs(params.UserIDs, &userIDs, params.RoleIDs, &roleIDs); err == nil {
			err = permission.UpdateUserRole(params.Ctx, userIDs, roleIDs)
		}
		return retVal, "", err
	})
}

// @Title 查询角色菜单
// @Description 查询角色菜单
// @Param	token			header		string	true	"认证token"
// @Param	roleID			query		int		false	"用户ID"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /GetRoleMenu [get]
func (c *PowerController) GetRoleMenu() {
	c.callGetRoleMenu(func(params *tPowerGetRoleMenuParams) (retVal interface{}, errCode string, err error) {
		retVal, err = permission.GetRoleMenu(params.Ctx, params.RoleID)
		return retVal, "", err
	})
}

// @Title 修改角色菜单
// @Description 修改角色菜单
// @Param	token			header		string	true	"认证token"
// @Param	roleIDs			formData	string	true	"角色IDs"
// @Param	menuIDs			formData	string	true	"菜单IDs"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /UpdateRoleMenu [put]
func (c *PowerController) UpdateRoleMenu() {
	c.callUpdateRoleMenu(func(params *tPowerUpdateRoleMenuParams) (retVal interface{}, errCode string, err error) {
		var (
			roleIDs, menuIDs []int
		)
		if err = jxutils.Strings2Objs(params.RoleIDs, &roleIDs, params.MenuIDs, &menuIDs); err == nil {
			err = permission.UpdateRoleMenu(params.Ctx, roleIDs, menuIDs)
		}
		return retVal, "", err
	})
}
