60 lines
1.3 KiB
Go
Executable File

package admin
import (
"context"
"fmt"
"time"
"bindbox-game/internal/pkg/utils"
"gorm.io/gorm"
)
type ModifyInput struct {
Username string
Nickname string
Mobile string
Password string
Avatar string
UpdatedBy string
}
func (s *service) Modify(ctx context.Context, id int, in ModifyInput) error {
checkIdInfo, err := s.readDB.Admin.WithContext(ctx).
Where(s.readDB.Admin.ID.Eq(int32(id))).
First()
if err != nil && err != gorm.ErrRecordNotFound {
return err
}
if checkIdInfo == nil {
return fmt.Errorf("该账号不存在")
}
checkUserNameInfo, err := s.readDB.Admin.WithContext(ctx).
Where(s.readDB.Admin.ID.Neq(int32(id))).
Where(s.readDB.Admin.Username.Eq(in.Username)).
First()
if err != nil && err != gorm.ErrRecordNotFound {
return err
}
if checkUserNameInfo != nil {
return fmt.Errorf("该账号已存在")
}
if in.Password != "" {
hashedPassword, err := utils.GenerateAdminHashedPassword(in.Password)
if err != nil {
return err
}
checkIdInfo.Password = hashedPassword
}
checkIdInfo.Username = in.Username
checkIdInfo.Nickname = in.Nickname
checkIdInfo.Mobile = in.Mobile
checkIdInfo.Avatar = in.Avatar
checkIdInfo.UpdatedUser = in.UpdatedBy
checkIdInfo.UpdatedAt = time.Now()
return s.writeDB.Admin.WithContext(ctx).Save(checkIdInfo)
}