package application import ( "mayfly-go/internal/sys/domain/entity" "mayfly-go/internal/sys/domain/repository" "mayfly-go/pkg/base" "mayfly-go/pkg/errorx" "mayfly-go/pkg/gormx" "mayfly-go/pkg/model" "mayfly-go/pkg/utils/cryptox" "gorm.io/gorm" ) type Account interface { base.App[*entity.Account] GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) Create(account *entity.Account) error Update(account *entity.Account) error Delete(id uint64) error } func newAccountApp(accountRepo repository.Account) Account { return &accountAppImpl{base.AppImpl[*entity.Account, repository.Account]{Repo: accountRepo}} } type accountAppImpl struct { base.AppImpl[*entity.Account, repository.Account] } func (a *accountAppImpl) GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) { return a.GetRepo().GetPageList(condition, pageParam, toEntity) } func (a *accountAppImpl) Create(account *entity.Account) error { if a.GetBy(&entity.Account{Username: account.Username}) == nil { return errorx.NewBiz("该账号用户名已存在") } // 默认密码为账号用户名 account.Password = cryptox.PwdHash(account.Username) account.Status = entity.AccountEnableStatus return a.Insert(account) } func (a *accountAppImpl) Update(account *entity.Account) error { if account.Username != "" { unAcc := &entity.Account{Username: account.Username} err := a.GetBy(unAcc) if err == nil && unAcc.Id != account.Id { return errorx.NewBiz("该用户名已存在") } } return a.UpdateById(account) } func (a *accountAppImpl) Delete(id uint64) error { return gormx.Tx( func(db *gorm.DB) error { // 删除account信息 return a.DeleteByIdWithDb(db, id) }, func(db *gorm.DB) error { // 删除账号关联的角色信息 return gormx.DeleteByWithDb(db, &entity.AccountRole{AccountId: id}) }, ) }