admin_user.go 7.9 KB
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) {
	//实际业务
	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.AdminPermissionFindQuery{
		IdsIn: saveUserCmd.PermissionId,
	})
	if err != nil {
		return nil, lib.ThrowError(lib.INTERNAL_SERVER_ERROR, err.Error())
	}

	permissionBases := []domain.AdminPermissionBase{}
	for i := range permissions {
		p := domain.AdminPermissionBase{
			Id: permissions[i].Id, Code: permissions[i].Code,
		}
		permissionBases = append(permissionBases, p)
	}
	var accountChance 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 {
			accountChance = true
		}
		adminuser.Account = saveUserCmd.Account
		adminuser.AdminName = saveUserCmd.Name
		adminuser.IsUsable = saveUserCmd.IsUsable
		adminuser.Permission = permissionBases

	} else {
		accountChance = true
		defaultPwd := fmt.Sprintf("%x", sha1.Sum([]byte("123456")))
		adminuser = &domain.AdminUser{
			Id:         saveUserCmd.Id,
			Account:    saveUserCmd.Account,
			Password:   defaultPwd,
			AdminName:  saveUserCmd.Name,
			IsUsable:   true,
			Permission: permissionBases,
		}
	}
	if accountChance {
		//检查账号是否已存在
		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 {
	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.TRANSACTION_ERROR, err.Error())
	} else if ok {
		return lib.ThrowError(lib.BUSINESS_ERROR, "请勿禁用超级管理员")
	}
	err = adminuserDao.UpdateIsUsable(uid, isUsable)
	if err != nil {
		return lib.ThrowError(lib.TRANSACTION_ERROR, err.Error())
	}
	transactionContext.CommitTransaction()
	return nil
}