admin_user.go 5.7 KB
package controllers

import (
	"errors"

	"github.com/astaxie/beego/logs"
	adminPermissionquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/query"
	adminPermissionService "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminPermission/service"
	adminuserCmd "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
	adminuserquery "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
	adminuserservice "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/service"
	"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
)

type AdminUserController struct {
	BaseController
}

////Prepare 重写 BaseController 的Prepare方法
func (c *AdminUserController) Prepare() {
	c.BaseController.Prepare()
	if ok := c.ValidJWTToken(); !ok {
		return
	}
	if ok := c.ValidAdminPermission(domain.PERMINSSION_ADMIN_USER); !ok {
		return
	}
}

type adminDetailParam struct {
	Id             int64  `json:"id"`
	PermissionType []int  `json:"permissionType"`
	Status         int    `json:"status"`
	Account        string `json:"account"`
}

func (c *AdminUserController) SaveAdminUser() {
	//用与适配前端定义的数据结构
	var (
		param adminDetailParam
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	newAdminUserService := adminuserservice.NewAdminUserService(nil)
	cmd := &adminuserCmd.SaveAdminUserCommand{
		Id:           param.Id,
		Name:         param.Account,
		Account:      param.Account,
		PermissionId: param.PermissionType,
		IsUsable:     false,
	}
	if param.Status == 1 {
		cmd.IsUsable = true
	}
	_, err = newAdminUserService.SaveAdminUser(cmd)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}

func (c *AdminUserController) GetAdminUser() {
	//用与适配前端定义的数据结构
	type Paramter struct {
		Id int64 `json:"id"`
	}
	var (
		param Paramter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	newAdminUserService := adminuserservice.NewAdminUserService(nil)
	adminuser, err := newAdminUserService.GetAdminUser(&adminuserquery.GetAdminUserQuery{
		Id: param.Id,
	})
	if err != nil {
		c.ResponseError(err)
		return
	}
	rspData := adminDetailParam{
		Id:      adminuser.Id,
		Account: adminuser.Account,
		Status:  0,
	}
	for _, v := range adminuser.Permission {
		rspData.PermissionType = append(rspData.PermissionType, v.Id)
	}
	if adminuser.IsUsable {
		rspData.Status = 1
	}
	c.ResponseData(rspData)
	return
}

func (c *AdminUserController) ListAdminUser() {
	//用与适配前端定义的数据结构
	type Paramter struct {
		SearchText string `json:"searchText"`
		PageSize   int    `json::"pageSize"`
		PageNumber int    `json:"pageNumber"`
	}
	var (
		param Paramter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	if param.PageSize == 0 {
		param.PageSize = 20
	}
	if param.PageNumber == 0 {
		param.PageNumber = 1
	}
	newAdminUserService := adminuserservice.NewAdminUserService(nil)
	queryOption := &adminuserquery.ListAdminUserQuery{
		AdminAccountMatch: param.SearchText,
		Limit:             param.PageSize,
		Offset:            param.PageSize * (param.PageNumber - 1),
	}
	adminusers, cnt, err := newAdminUserService.PageListAdminUser(queryOption)
	if err != nil {
		c.ResponseError(err)
		return
	}
	newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
	allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{
		ParentId: 0,
	})
	if err != nil {
		logs.Error("获取权限数据失败:%s", err)
		c.ResponseError(errors.New("服务异常"))
		return
	}
	permissionMap := map[int]domain.AdminPermission{}
	for i := range allPermission {
		permissionMap[allPermission[i].Id] = allPermission[i]
	}
	listData := []map[string]interface{}{}
	//前端数据格式适配
	for i := range adminusers {
		permissionTypes := []string{}
		for _, vv := range adminusers[i].Permission {
			if pm, ok := permissionMap[vv.Id]; ok {
				permissionTypes = append(permissionTypes, pm.Name)
			}
		}
		m := map[string]interface{}{
			"id":         adminusers[i].Id,
			"account":    adminusers[i].Account,
			"permission": permissionTypes,
		}
		listData = append(listData, m)
	}
	c.ResponsePageList(listData, cnt, param.PageNumber)
	return
}

func (c *AdminUserController) ForbiddenAdminUser() {
	//用与适配前端定义的数据结构
	type Paramter struct {
		Id int64 `json:"id"`
	}
	var (
		param Paramter
		err   error
	)
	if err = c.BindJsonData(&param); err != nil {
		logs.Error(err)
		c.ResponseError(errors.New("json数据解析失败"))
		return
	}
	newAdminUserService := adminuserservice.NewAdminUserService(nil)
	err = newAdminUserService.UpdateAdminIsUsable(param.Id, false)
	if err != nil {
		c.ResponseError(err)
		return
	}
	c.ResponseData(nil)
	return
}

//BeforeEditAdminUser 编辑管理员操作的前置接口
func (c *AdminUserController) BeforeEditAdminUser() {
	newPermissionSrv := adminPermissionService.NewAdminPermissionService(nil)
	allPermission, err := newPermissionSrv.ListAdminPermission(adminPermissionquery.ListAdminPermissionQuery{
		ParentId: 0,
		NotCode:  []string{domain.PERMINSSION_ADMIN_USER},
	})
	if err != nil {
		logs.Error("获取权限数据失败:%s", err)
		c.ResponseError(errors.New("服务异常"))
		return
	}
	var rspData []map[string]interface{}
	for i := range allPermission {
		m := map[string]interface{}{
			"id":             allPermission[i].Id,
			"permissionName": allPermission[i].Name,
		}
		rspData = append(rspData, m)
	}
	c.ResponseData(rspData)
	return
}