2021-06-07 17:22:07 +08:00
|
|
|
package application
|
|
|
|
|
|
|
|
|
|
import (
|
2022-06-02 17:41:11 +08:00
|
|
|
"mayfly-go/internal/sys/domain/entity"
|
|
|
|
|
"mayfly-go/internal/sys/domain/repository"
|
2023-10-26 17:15:49 +08:00
|
|
|
"mayfly-go/pkg/base"
|
|
|
|
|
"mayfly-go/pkg/errorx"
|
2023-07-01 14:34:42 +08:00
|
|
|
"mayfly-go/pkg/gormx"
|
2022-06-02 17:41:11 +08:00
|
|
|
"mayfly-go/pkg/model"
|
2023-07-21 17:07:04 +08:00
|
|
|
"mayfly-go/pkg/utils/cryptox"
|
2021-07-28 18:03:19 +08:00
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
2021-06-07 17:22:07 +08:00
|
|
|
)
|
|
|
|
|
|
2021-07-28 18:03:19 +08:00
|
|
|
type Account interface {
|
2023-10-26 17:15:49 +08:00
|
|
|
base.App[*entity.Account]
|
2021-06-07 17:22:07 +08:00
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
GetPageList(condition *entity.Account, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
2023-07-24 22:36:07 +08:00
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
Create(account *entity.Account) error
|
2021-07-28 18:03:19 +08:00
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
Update(account *entity.Account) error
|
2021-07-28 18:03:19 +08:00
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
Delete(id uint64) error
|
2021-06-07 17:22:07 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func newAccountApp(accountRepo repository.Account) Account {
|
2023-10-26 17:15:49 +08:00
|
|
|
return &accountAppImpl{base.AppImpl[*entity.Account, repository.Account]{Repo: accountRepo}}
|
2021-06-07 17:22:07 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
type accountAppImpl struct {
|
2023-10-26 17:15:49 +08:00
|
|
|
base.AppImpl[*entity.Account, repository.Account]
|
2021-06-07 17:22:07 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
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)
|
2023-07-24 22:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
func (a *accountAppImpl) Create(account *entity.Account) error {
|
|
|
|
|
if a.GetBy(&entity.Account{Username: account.Username}) == nil {
|
|
|
|
|
return errorx.NewBiz("该账号用户名已存在")
|
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
// 默认密码为账号用户名
|
2023-07-21 17:07:04 +08:00
|
|
|
account.Password = cryptox.PwdHash(account.Username)
|
2021-07-28 18:03:19 +08:00
|
|
|
account.Status = entity.AccountEnableStatus
|
2023-10-26 17:15:49 +08:00
|
|
|
return a.Insert(account)
|
2021-07-28 18:03:19 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
func (a *accountAppImpl) Update(account *entity.Account) error {
|
2023-07-24 22:36:07 +08:00
|
|
|
if account.Username != "" {
|
|
|
|
|
unAcc := &entity.Account{Username: account.Username}
|
2023-10-26 17:15:49 +08:00
|
|
|
err := a.GetBy(unAcc)
|
|
|
|
|
if err == nil && unAcc.Id != account.Id {
|
|
|
|
|
return errorx.NewBiz("该用户名已存在")
|
|
|
|
|
}
|
2023-07-24 22:36:07 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
return a.UpdateById(account)
|
2021-07-28 18:03:19 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-26 17:15:49 +08:00
|
|
|
func (a *accountAppImpl) Delete(id uint64) error {
|
|
|
|
|
return gormx.Tx(
|
2021-07-28 18:03:19 +08:00
|
|
|
func(db *gorm.DB) error {
|
2023-10-26 17:15:49 +08:00
|
|
|
// 删除account信息
|
|
|
|
|
return a.DeleteByIdWithDb(db, id)
|
2021-07-28 18:03:19 +08:00
|
|
|
},
|
|
|
|
|
func(db *gorm.DB) error {
|
|
|
|
|
// 删除账号关联的角色信息
|
2023-10-26 17:15:49 +08:00
|
|
|
return gormx.DeleteByWithDb(db, &entity.AccountRole{AccountId: id})
|
2021-07-28 18:03:19 +08:00
|
|
|
},
|
|
|
|
|
)
|
|
|
|
|
}
|