作者 唐旭辉

清理废旧代码

package command
import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
type LoginBySecretKeyCommand struct {
Secret string `json:"secret"`
}
func (login LoginBySecretKeyCommand) ValidateCommand() error {
if len(login.Secret) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "登录参数错误")
}
return nil
}
package command
import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
type SaveAdminUserCommand struct {
Id int64 `json:"id"`
// 员工姓名
Name string `json:"name"`
// 员工账号
Account string `json:"account" `
//密码
Password string `json:"password"`
// 员工角色
PermissionId []int64 `json:"PermissionId"`
IsUsable bool `json:"isUsable"`
}
func (command SaveAdminUserCommand) ValidateCommand() error {
if len(command.Name) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "用户名称必填")
}
if len(command.Account) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "账号必填")
}
return nil
}
package command
import "gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
//UpdateAdminUserPwdCommand 修改密码
type UpdateAdminUserPwdCommand struct {
Id int64 `json:"id"`
//密码
Password string `json:"password"`
}
func (command UpdateAdminUserPwdCommand) ValidateCommand() error {
if len(command.Password) == 0 {
return lib.ThrowError(lib.ARG_ERROR, "密码必填")
}
return nil
}
package query
type GetAdminUserQuery struct {
Id int64 `json:"id"`
AdminAccount string `json:"adminAccount"`
}
package query
//ListAdminUserQuery 获取用户列表
type ListAdminUserQuery struct {
//账号匹配
AdminAccountMatch string `json:"adminAccountMatch" `
// 查询偏离量
Offset int `json:"offset" `
// 查询限制
Limit int `json:"limit"`
}
package service
import (
"crypto/sha1"
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/command"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/adminUser/query"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/application/factory"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/dao"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/lib"
)
//AdminUserService 管理员相关服务
type AdminUserService struct {
}
func NewAdminUserService(option map[string]interface{}) *AdminUserService {
newAdminUserService := new(AdminUserService)
return newAdminUserService
}
func (adminUserSrv AdminUserService) GetAdminUser(getAdminUserQuery *query.GetAdminUserQuery) (*domain.AdminUser, error) {
//实际业务
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
adminuserRepository domain.AdminUserRepository
adminuser *domain.AdminUser
)
if value, err := factory.CreateAdminUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
adminuserRepository = value
}
adminuser, err = adminuserRepository.FindOne(domain.AdminUserFindOneQuery{
AccountEqual: getAdminUserQuery.AdminAccount,
AdminUserId: getAdminUserQuery.Id,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return adminuser, nil
}
func (adminUserSrv AdminUserService) SaveAdminUser(saveUserCmd *command.SaveAdminUserCommand) (*domain.AdminUser, error) {
if err := saveUserCmd.ValidateCommand(); err != nil {
return nil, lib.ThrowError(lib.ARG_ERROR, err.Error())
}
//实际业务
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
adminuserRepository domain.AdminUserRepository
adminuser *domain.AdminUser
)
if value, err := factory.CreateAdminUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
adminuserRepository = value
}
//获取权限
var (
permissionRepository domain.AdminPermissionRepository
permissions []domain.AdminPermission
)
if value, err := factory.CreateAdminPermissionRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
permissionRepository = value
}
permissions, err = permissionRepository.Find(domain.PermissionFindOption{
Ids: saveUserCmd.PermissionId,
})
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
for i := range permissions {
if permissions[i].Code == domain.PERMINSSION_ADMIN_USER {
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "操作异常")
}
}
permissionBases := []domain.AdminPermissionBase{}
for i := range permissions {
p := domain.AdminPermissionBase{
Id: permissions[i].Id, Code: permissions[i].Code,
}
permissionBases = append(permissionBases, p)
}
//账号是否有变更
var accountChange bool
if saveUserCmd.Id > 0 {
//更新数据
adminuser, err = adminuserRepository.FindOne(domain.AdminUserFindOneQuery{
AdminUserId: saveUserCmd.Id,
})
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if adminuser.Account != saveUserCmd.Account {
accountChange = true
}
adminuser.Account = saveUserCmd.Account
adminuser.AdminName = saveUserCmd.Name
adminuser.IsUsable = saveUserCmd.IsUsable
if !adminuser.IsDefault {
adminuser.Permission = permissionBases
}
} else {
//添加新数据
accountChange = true
defaultPwd := fmt.Sprintf("%x", sha1.Sum([]byte("123456")))
adminuser = &domain.AdminUser{
Id: saveUserCmd.Id,
Account: saveUserCmd.Account,
Password: defaultPwd,
AdminName: saveUserCmd.Name,
IsUsable: saveUserCmd.IsUsable,
Permission: permissionBases,
}
}
if accountChange {
//检查账号是否已存在
var (
adminuserDao *dao.AdminUserDao
)
if v, err := factory.CreateAdminUserkDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else {
adminuserDao = v
}
ok, err := adminuserDao.AdminUserAccountExist(saveUserCmd.Account)
if err != nil {
return nil, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if ok {
return nil, lib.ThrowError(lib.BUSINESS_ERROR, "账号已存在")
}
}
adminuser, err = adminuserRepository.Save(*adminuser)
if err != nil {
return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return adminuser, nil
}
func (adminUserSrv AdminUserService) PageListAdminUser(listAdminUserQuery *query.ListAdminUserQuery) ([]domain.AdminUser, int, error) {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return nil, 0, lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
var (
adminuserRepository domain.AdminUserRepository
adminusers []domain.AdminUser
cnt int
)
if value, err := factory.CreateAdminUserRepository(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else {
adminuserRepository = value
}
adminusers, err = adminuserRepository.Find(domain.AdminUserFindQuery{
AccountLike: listAdminUserQuery.AdminAccountMatch,
Offset: listAdminUserQuery.Offset,
Limit: listAdminUserQuery.Limit,
})
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
cnt, err = adminuserRepository.CountAll(domain.AdminUserFindQuery{
AccountLike: listAdminUserQuery.AdminAccountMatch,
})
if err != nil {
return nil, 0, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
return adminusers, cnt, nil
}
func (adminUserSrv AdminUserService) UpdateAdminPassword(updatecmd command.UpdateAdminUserPwdCommand) error {
if err := updatecmd.ValidateCommand(); err != nil {
return lib.ThrowError(lib.ARG_ERROR, err.Error())
}
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
adminuserDao *dao.AdminUserDao
)
if v, err := factory.CreateAdminUserkDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else {
adminuserDao = v
}
err = adminuserDao.UpdatePassword(updatecmd.Id, updatecmd.Password)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return nil
}
func (adminUserSrv AdminUserService) UpdateAdminIsUsable(uid int64, isUsable bool) error {
transactionContext, err := factory.CreateTransactionContext(nil)
if err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
if err := transactionContext.StartTransaction(); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
}
defer func() {
transactionContext.RollbackTransaction()
}()
var (
adminuserDao *dao.AdminUserDao
)
if v, err := factory.CreateAdminUserkDao(map[string]interface{}{
"transactionContext": transactionContext,
}); err != nil {
return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
} else {
adminuserDao = v
}
if ok, err := adminuserDao.AdminUserIsDefault(uid); err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
} else if ok {
return lib.ThrowError(lib.BUSINESS_ERROR, "请勿禁用超级管理员")
}
err = adminuserDao.UpdateIsUsable(uid, isUsable)
if err != nil {
return lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
}
transactionContext.CommitTransaction()
return nil
}
... ... @@ -5,14 +5,6 @@ import (
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
func CreateAdminUserkDao(options map[string]interface{}) (*dao.AdminUserDao, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return dao.NewAdminUserDao(transactionContext)
}
func CreatePartnerInfoDao(options map[string]interface{}) (*dao.PartnerInfoDao, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
... ...
... ... @@ -15,15 +15,6 @@ func CreatePartnerInfoRepository(options map[string]interface{}) (domain.Partner
return repository.NewPartnerInfoRepository(transactionContext)
}
//CreateAdminUserRepository 管理员信息
func CreateAdminUserRepository(options map[string]interface{}) (domain.AdminUserRepository, error) {
var transactionContext *transaction.TransactionContext
if value, ok := options["transactionContext"]; ok {
transactionContext = value.(*transaction.TransactionContext)
}
return repository.NewAdminUserRepository(transactionContext)
}
//CreateAdminUserRepository 管理员权限信息
func CreateAdminPermissionRepository(options map[string]interface{}) (domain.AdminPermissionRepository, error) {
var transactionContext *transaction.TransactionContext
... ...
package dao
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type AdminUserDao struct {
transactionContext *transaction.TransactionContext
}
func NewAdminUserDao(transactionContext *transaction.TransactionContext) (*AdminUserDao, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
} else {
return &AdminUserDao{
transactionContext: transactionContext,
}, nil
}
}
//UpdatePassword ....
func (dao *AdminUserDao) UpdatePassword(id int64, pwd string) error {
tx := dao.transactionContext.PgDd
m := &models.AdminUser{}
err := tx.Model(m).Where("id=?", id).First()
if err != nil {
return err
}
_, err = tx.Model(m).Where("id=?", id).
Set("password=?", pwd).
Update()
return err
}
//UpdateIsUsable ....
func (dao *AdminUserDao) UpdateIsUsable(id int64, isUsable bool) error {
tx := dao.transactionContext.PgDd
m := &models.AdminUser{}
err := tx.Model(m).Where("id=?", id).First()
if err != nil {
return err
}
_, err = tx.Model(m).Where("id=?", id).
Set("is_usable=?", isUsable).
Update()
return err
}
//AdminUserAccountExist ...
func (dao *AdminUserDao) AdminUserAccountExist(account string) (bool, error) {
tx := dao.transactionContext.PgDd
m := &models.AdminUser{}
ok, err := tx.Model(m).
Where("account=?", account).
Exists()
return ok, err
}
func (dao *AdminUserDao) AdminUserIsDefault(id int64) (bool, error) {
tx := dao.transactionContext.PgDd
m := &models.AdminUser{}
err := tx.Model(m).
Where("id=?", id).
Column("is_default").
First()
return m.IsDefault, err
}
package models
import (
"context"
"time"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"github.com/go-pg/pg/v10"
)
type AdminUser struct {
tableName struct{} `pg:"admin_user,alias:admin_user"`
//id
Id int64 `pg:",pk"`
//用户账号
Account string `pg:",unique"`
//用户名称
AdminName string
//账号密码
Password string
//是否是默认账号
IsDefault bool `pg:",use_zero"`
//账号是否可用
IsUsable bool `pg:",use_zero"`
//用户的权限
Permission []domain.AdminPermissionBase
CreateAt time.Time
UpdateAt time.Time
}
var _ pg.BeforeUpdateHook = (*AdminUser)(nil)
func (user *AdminUser) BeforeUpdate(ctx context.Context) (context.Context, error) {
user.UpdateAt = time.Now()
return ctx, nil
}
var _ pg.BeforeInsertHook = (*AdminUser)(nil)
func (user *AdminUser) BeforeInsert(ctx context.Context) (context.Context, error) {
user.CreateAt = time.Now()
user.UpdateAt = time.Now()
return ctx, nil
}
package repository
import (
"fmt"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/domain"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/models"
"gitlab.fjmaimaimai.com/mmm-go/partnermg/pkg/infrastructure/pg/transaction"
)
type AdminUserRepository struct {
transactionContext *transaction.TransactionContext
}
var (
_ domain.AdminUserRepository = (*AdminUserRepository)(nil)
)
func NewAdminUserRepository(transactionContext *transaction.TransactionContext) (*AdminUserRepository, error) {
if transactionContext == nil {
return nil, fmt.Errorf("transactionContext参数不能为nil")
}
return &AdminUserRepository{transactionContext: transactionContext}, nil
}
func (reponsitory AdminUserRepository) transformPgModelToDomainModel(adminuserModel *models.AdminUser) (domain.AdminUser, error) {
result := domain.AdminUser{
Id: adminuserModel.Id,
Account: adminuserModel.Account,
AdminName: adminuserModel.AdminName,
IsDefault: adminuserModel.IsDefault,
CreateAt: adminuserModel.CreateAt,
IsUsable: adminuserModel.IsUsable,
Password: adminuserModel.Password,
Permission: adminuserModel.Permission,
}
return result, nil
}
func (reponsitory AdminUserRepository) FindOne(queryOption domain.AdminUserFindOneQuery) (*domain.AdminUser, error) {
db := reponsitory.transactionContext.PgDd
adminuserModel := new(models.AdminUser)
query := db.Model(adminuserModel)
if queryOption.AdminUserId > 0 {
query = query.Where("id=?", queryOption.AdminUserId)
}
if len(queryOption.AccountEqual) > 0 {
query = query.Where("account=?", queryOption.AccountEqual)
}
err := query.First()
if err != nil {
return nil, err
}
adminUser, err := reponsitory.transformPgModelToDomainModel(adminuserModel)
return &adminUser, err
}
func (reponsitory AdminUserRepository) updateAdminUser(adminuser domain.AdminUser) (*domain.AdminUser, error) {
tx := reponsitory.transactionContext.PgTx
adminUserModel := &models.AdminUser{
Id: adminuser.Id,
Account: adminuser.Account,
AdminName: adminuser.AdminName,
Password: adminuser.Password,
IsUsable: adminuser.IsUsable,
Permission: adminuser.Permission,
}
_, err := tx.Model(adminUserModel).
Where("id=?", adminUserModel.Id).
Column("admin_name", "account", "password", "is_usable", "permission").
Update()
if err != nil {
return nil, fmt.Errorf("更新用户数据失败:%s", err)
}
return &adminuser, nil
}
func (reponsitory AdminUserRepository) addAdminUser(adminuser domain.AdminUser) (*domain.AdminUser, error) {
tx := reponsitory.transactionContext.PgTx
adminuserModel := &models.AdminUser{
Account: adminuser.Account,
AdminName: adminuser.AdminName,
Password: adminuser.Password,
IsDefault: false,
IsUsable: adminuser.IsUsable,
Permission: adminuser.Permission,
}
//添加用户数据
_, err := tx.Model(adminuserModel).Insert()
if err != nil {
return nil, err
}
return nil, nil
}
func (reponsitory AdminUserRepository) Save(adminuser domain.AdminUser) (*domain.AdminUser, error) {
if adminuser.Id == 0 {
return reponsitory.addAdminUser(adminuser)
}
return reponsitory.updateAdminUser(adminuser)
}
func (reponsitory AdminUserRepository) Find(queryOption domain.AdminUserFindQuery) ([]domain.AdminUser, error) {
db := reponsitory.transactionContext.PgDd
adminuserModels := []models.AdminUser{}
query := db.Model(&adminuserModels)
if len(queryOption.AccountLike) > 0 {
query = query.Where("account like ?", "%"+queryOption.AccountLike+"%")
}
if queryOption.Offset > -1 {
query = query.Offset(queryOption.Offset)
}
if queryOption.Limit > 0 {
query = query.Limit(queryOption.Limit)
} else {
query = query.Limit(20)
}
var (
err error
adminuserReturn = make([]domain.AdminUser, 0)
)
query = query.Order("admin_user.id DESC")
err = query.Select()
if err != nil {
return adminuserReturn, err
}
for i := range adminuserModels {
domainAdminUser, err := reponsitory.transformPgModelToDomainModel(&adminuserModels[i])
if err != nil {
return adminuserReturn, err
}
adminuserReturn = append(adminuserReturn, domainAdminUser)
}
return adminuserReturn, nil
}
func (reponsitory AdminUserRepository) CountAll(queryOption domain.AdminUserFindQuery) (int, error) {
db := reponsitory.transactionContext.PgDd
adminuserModels := models.AdminUser{}
query := db.Model(&adminuserModels)
if len(queryOption.AccountLike) > 0 {
query = query.Where("account like ?", "%"+queryOption.AccountLike+"%")
}
cnt, err := query.Count()
return cnt, err
}
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 []int64 `json:"permissionType"`
Status int `json:"status"`
Account string `json:"account"`
IsAdmin int `json:"isAdmin"`
}
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,
IsAdmin: 0,
}
for _, v := range adminuser.Permission {
rspData.PermissionType = append(rspData.PermissionType, v.Id)
}
if adminuser.IsUsable {
rspData.Status = 1
}
if adminuser.IsDefault {
rspData.IsAdmin = 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[int64]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,
"statue": 0,
"isAdmin": 0,
}
if adminusers[i].IsUsable {
m["statue"] = 1
}
if adminusers[i].IsDefault {
m["isAdmin"] = 1
}
listData = append(listData, m)
}
c.ResponsePageList(listData, cnt, param.PageNumber)
return
}
func (c *AdminUserController) ForbiddenAdminUser() {
//用与适配前端定义的数据结构
type Paramter struct {
Id int64 `json:"id"`
Statue int `json:"statue"`
}
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)
var isUsable bool
if param.Statue == 1 {
isUsable = true
} else if param.Statue == 0 {
isUsable = false
} else {
c.ResponseError(errors.New("参数错误"))
return
}
err = newAdminUserService.UpdateAdminIsUsable(param.Id, isUsable)
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 {
if allPermission[i].Code == domain.PERMINSSION_ADMIN_USER {
continue
}
m := map[string]interface{}{
"id": allPermission[i].Id,
"permissionName": allPermission[i].Name,
}
rspData = append(rspData, m)
}
c.ResponseData(rspData)
return
}