package role import ( "context" "fmt" "github.com/samber/lo" "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction" "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/contextdata" "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr" "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc" "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types" "github.com/zeromicro/go-zero/core/logx" ) type SystemSaveRoleLogic struct { logx.Logger ctx context.Context svcCtx *svc.ServiceContext } func NewSystemSaveRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SystemSaveRoleLogic { return &SystemSaveRoleLogic{ Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx, } } func (l *SystemSaveRoleLogic) SystemSaveRole(req *types.RoleSaveRequest) (resp *types.RoleSaveResponse, err error) { var ( conn = l.svcCtx.DefaultDBConn() userAddRoleList []*domain.User ) role := NewDomainRole(l.ctx, req.Role) for _, authId := range req.Role.AuthIds { if auth := role.GetAuth(authId); auth == nil { return nil, xerr.NewErrMsg(fmt.Sprintf("unknown auth %d ", authId)) } } for _, item := range req.Role.Users { var user *domain.User if user, err = l.svcCtx.UserRepository.FindOne(l.ctx, conn, item.Id); err != nil { return nil, xerr.NewErrMsgErr("用户存在", err) } userAddRoleList = append(userAddRoleList, user) } if err = transaction.UseTrans(l.ctx, l.svcCtx.DB, func(ctx context.Context, conn transaction.Conn) error { if role, err = l.svcCtx.RoleRepository.Insert(ctx, conn, role); err != nil { return err } for _, user := range userAddRoleList { user.AddRole(role.Id) if user, err = l.svcCtx.UserRepository.UpdateWithVersion(ctx, conn, user); err != nil { return err } if _, err = l.svcCtx.UserRoleRepository.Insert(l.ctx, conn, &domain.UserRole{ CompanyId: user.CompanyId, UserId: user.Id, RoleId: role.Id, }); err != nil { return err } } return nil }, true); err != nil { return nil, xerr.NewErrMsgErr("添加角色失败", err) } resp = &types.RoleSaveResponse{} return } func NewDomainRole(ctx context.Context, item types.RoleItem) *domain.Role { var ( role *domain.Role users []domain.User = make([]domain.User, 0) userToken = contextdata.GetUserTokenFromCtx(ctx) ) lo.ForEach(item.Users, func(item types.RoleUser, index int) { users = append(users, domain.User{ Id: item.Id, Name: item.Name, }) }) role = &domain.Role{ CompanyId: userToken.CompanyId, Name: item.Name, Auths: item.AuthIds, Remark: item.Remark, //Users: users, } return role } func NewTypesRole(item *domain.Role) types.RoleItem { var ( role types.RoleItem users []types.RoleUser = make([]types.RoleUser, 0) ) role = types.RoleItem{ Id: item.Id, CompanyId: item.CompanyId, Name: item.Name, AuthIds: item.Auths, Remark: item.Remark, Users: users, UpdatedAt: item.UpdatedAt, } return role }