198 lines
5.3 KiB
Go
198 lines
5.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"service/api/consts"
|
|
accountproto "service/api/proto/account/proto"
|
|
"service/apollostruct"
|
|
"service/app/mix/dao"
|
|
"service/bizcommon/util"
|
|
"service/dbstruct"
|
|
"service/library/apollo"
|
|
"service/library/logger"
|
|
"service/library/mediafiller"
|
|
interceptor "service/library/taginterceptor"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
goproto "google.golang.org/protobuf/proto"
|
|
)
|
|
|
|
type Account struct {
|
|
store *dao.Store
|
|
}
|
|
|
|
func NewAccount(store *dao.Store) (a *Account) {
|
|
a = &Account{
|
|
store: store,
|
|
}
|
|
return
|
|
}
|
|
|
|
// Account
|
|
func (p *Account) OpCreate(ctx *gin.Context, req *accountproto.OpCreateReq) error {
|
|
|
|
//产生mid
|
|
accountIdSeq, err := p.store.GetAndUpdateAccountIdSeq(ctx)
|
|
if err != nil {
|
|
logger.Error("GetAndUpdateAccountIdSeq failed : %v", err)
|
|
return err
|
|
}
|
|
|
|
req.Account.Mid = goproto.Int64(accountIdSeq.Seq)
|
|
req.Account.Ct = goproto.Int64(time.Now().Unix())
|
|
req.Account.Ut = goproto.Int64(time.Now().Unix())
|
|
req.Account.DelFlag = goproto.Int64(consts.Exist)
|
|
|
|
err = interceptor.EncryptTagInterceptorInstance().Intercept(req.Account, "bcrypto")
|
|
if err != nil {
|
|
logger.Error("Account encryption err : %v", err)
|
|
return err
|
|
}
|
|
|
|
err = p.store.CreateAccount(ctx, req.Account)
|
|
if err != nil {
|
|
logger.Error("CreateAccount fail, err: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Account) OpUpdate(ctx *gin.Context, req *accountproto.OpUpdateReq) error {
|
|
err := p.store.UpdateAccount(ctx, req.Account)
|
|
if err != nil {
|
|
logger.Error("UpdateAccount fail, err: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Account) OpDelete(ctx *gin.Context, id int64) error {
|
|
err := p.store.DeleteAccount(ctx, id)
|
|
if err != nil {
|
|
logger.Error("DeleteAccount fail, err: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Account) OpListByMid(ctx *gin.Context, req *accountproto.OpListByMidReq) (*dbstruct.Account, error) {
|
|
account, err := p.store.GetAccountListByMid(ctx, req)
|
|
if err != nil {
|
|
logger.Error("GetAccountListByMid fail, err: %v", err)
|
|
return nil, err
|
|
}
|
|
return account, nil
|
|
}
|
|
|
|
func (p *Account) OpListByMids(ctx *gin.Context, req *accountproto.OpListByMidsReq) ([]*dbstruct.Account, error) {
|
|
list, err := p.store.GetAccountListByMids(ctx, req)
|
|
if err != nil {
|
|
logger.Error("GetAccountListByMids fail, err: %v", err)
|
|
return make([]*dbstruct.Account, 0), err
|
|
}
|
|
for _, v := range list {
|
|
_ = mediafiller.FillEntity(ctx, v.Avatar)
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func (p *Account) GetAccountMapByMids(ctx *gin.Context, mids []int64) (map[int64]*dbstruct.Account, error) {
|
|
list, err := p.store.GetAccountListByMids(ctx, &accountproto.OpListByMidsReq{
|
|
Mids: mids,
|
|
})
|
|
if err != nil {
|
|
logger.Error("GetAccountListByMids fail, err: %v", err)
|
|
return make(map[int64]*dbstruct.Account), err
|
|
}
|
|
for _, v := range list {
|
|
_ = mediafiller.FillEntity(ctx, v.Avatar)
|
|
}
|
|
|
|
mp := make(map[int64]*dbstruct.Account)
|
|
for _, v := range list {
|
|
mid := util.DerefInt64(v.Mid)
|
|
if mid > 0 {
|
|
mp[mid] = v
|
|
}
|
|
}
|
|
return mp, nil
|
|
}
|
|
|
|
func (p *Account) OpListByUserId(ctx *gin.Context, req *accountproto.OpListByUserIdReq) (*dbstruct.Account, error) {
|
|
list, err := p.store.GetAccountListByUserId(ctx, req)
|
|
if err != nil {
|
|
logger.Error("GetAccountListByUserId fail, err: %v", err)
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func (p *Account) OpListFuzzilyByUserId(ctx *gin.Context, req *accountproto.OpListFuzzilyByUserIdReq) ([]*dbstruct.Account, error) {
|
|
list, err := p.store.GetAccountListFuzzilyByUserId(ctx, req)
|
|
if err != nil {
|
|
logger.Error("GetAccountListFuzzilyByUserId fail, err: %v", err)
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func (p *Account) OpListFuzzilyByName(ctx *gin.Context, req *accountproto.OpListFuzzilyByNameReq) ([]*dbstruct.Account, error) {
|
|
list, err := p.store.GetAccountListFuzzilyByName(ctx, req)
|
|
if err != nil {
|
|
logger.Error("GetAccountListFuzzilyByName fail, err: %v", err)
|
|
return nil, err
|
|
}
|
|
return list, nil
|
|
}
|
|
|
|
func (p *Account) OpUpdateByIds(ctx *gin.Context, req *accountproto.OpUpdateByIdsReq) error {
|
|
err := p.store.UpdateAccountByIds(ctx, req.Account, req.Ids)
|
|
if err != nil {
|
|
logger.Error("UpdateAccountByIds fail, err: %v", err)
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (p *Account) OpCount(ctx *gin.Context, req *accountproto.OpCountReq) (int64, error) {
|
|
count, err := p.store.GetAccountCount(ctx, req)
|
|
if err != nil {
|
|
logger.Error("GetAccountCount fail, err: %v", err)
|
|
return -1, err
|
|
}
|
|
return count, err
|
|
}
|
|
|
|
func (p *Account) GenerateOriginalAccount() (*dbstruct.Account, error) {
|
|
key := "account_init"
|
|
cfg := apollostruct.AccountInitCfg{}
|
|
err := apollo.GetJson(key, &cfg, apollo.ApolloOpts().SetNamespace("account_init"))
|
|
if err != nil {
|
|
logger.Error("Apollo read failed : %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
//默认头像
|
|
imageIds := make([]int64, 1)
|
|
imageIds[0] = cfg.DefaultAvatarImageId
|
|
avatar := &dbstruct.MediaComponent{
|
|
ImageIds: util.Int64Slice(imageIds),
|
|
VideoIds: nil,
|
|
}
|
|
|
|
//默认信息
|
|
originalAccount := &dbstruct.Account{
|
|
Name: goproto.String(cfg.DefaultName),
|
|
Avatar: avatar,
|
|
Level: goproto.Int64(cfg.LevelLowerBound),
|
|
Role: goproto.Int64(cfg.DefaultRole),
|
|
CurrentExp: goproto.Int64(0),
|
|
CurrentLevelExp: goproto.Int64(cfg.LevelUpExpVector1[0]),
|
|
IsDndModeEnabled: goproto.Int64(cfg.DefaultIsDndModeEnabled),
|
|
GoldNum: goproto.Int64(0),
|
|
DiamondNum: goproto.Int64(0),
|
|
}
|
|
|
|
return originalAccount, nil
|
|
}
|