service/app/mix/service/logic/accountrelation.go

195 lines
5.6 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package logic
import (
"fmt"
"service/api/consts"
accountrelationproto "service/api/proto/accountrelation/proto"
"service/app/mix/dao"
"service/bizcommon/util"
"service/dbstruct"
"service/library/logger"
"time"
"github.com/gin-gonic/gin"
"github.com/qiniu/qmgo"
goproto "google.golang.org/protobuf/proto"
)
type AccountRelation struct {
store *dao.Store
}
func NewAccountRelation(store *dao.Store) (a *AccountRelation) {
a = &AccountRelation{
store: store,
}
return
}
func (p *AccountRelation) OpCreate(ctx *gin.Context, req *accountrelationproto.OpCreateReq) error {
for _, accountrelation := range req.AccountRelations {
accountRelationIdSeqId, err := p.store.GetAndUpdateAccountRelationIdSeq(ctx)
if err != nil {
logger.Error("GetAndUpdateAccountRelationIdSeq fail, err: %v", err)
return err
}
accountrelation.Id = goproto.Int64(accountRelationIdSeqId.Seq)
accountrelation.Ct = goproto.Int64(time.Now().Unix())
accountrelation.Ut = goproto.Int64(time.Now().Unix())
accountrelation.DelFlag = goproto.Int64(consts.Exist)
}
err := p.store.CreateAccountRelation(ctx, req.AccountRelations)
if err != nil {
logger.Error("CreateAccountRelation fail, err: %v", err)
return err
}
return nil
}
func (p *AccountRelation) OpUpdate(ctx *gin.Context, req *accountrelationproto.OpUpdateReq) error {
err := p.store.UpdateAccountRelationBySentence(ctx, req.AccountRelation, req.Sentence)
if err != nil {
logger.Error("UpdateAccountRelationBySentence fail, err: %v", err)
return err
}
return nil
}
func (p *AccountRelation) OpDelete(ctx *gin.Context, req *accountrelationproto.OpDeleteReq) error {
//1.查出原有用户关系
list, err := p.store.GetAccountRelationBySentences(ctx, req.Sentences)
if len(list) == 0 {
logger.Error("GetAccountRelationBySentences fail, err: %v", qmgo.ErrNoSuchDocuments)
return qmgo.ErrNoSuchDocuments
}
if err != nil {
logger.Error("GetAccountRelationBySentences fail, err: %v", err)
return err
}
//2.用户关系标志删除后转存到历史表并取出ids
ids := make([]int64, len(list))
for i, accountrelation := range list {
accountrelation.DelFlag = goproto.Int64(consts.Deleted)
ids[i] = util.DerefInt64(accountrelation.Id)
}
err = p.store.CreateAccountRelationHis(ctx, list)
if err != nil {
logger.Error("CreateAccountRelationHis fail, err: %v", err)
return err
}
//3.真删除原有用户关系
err = p.store.DeleteAccountRelationByIds(ctx, ids)
if err != nil {
logger.Error("DeleteAccountRelationByIds fail, err: %v", err)
return err
}
return nil
}
func (p *AccountRelation) OpListBySentence(ctx *gin.Context, req *accountrelationproto.OpListBySentenceReq) (*dbstruct.AccountRelation, error) {
accountrelation, err := p.store.GetAccountRelationBySentence(ctx, req)
if err != nil {
logger.Error("GetAccountRelationBySentence fail, err: %v", err)
return nil, err
}
return accountrelation, nil
}
func (p *AccountRelation) OpListBySubMidAndPredicate(ctx *gin.Context, req *accountrelationproto.OpListBySubMidAndPredicateReq) ([]*dbstruct.AccountRelation, error) {
list, err := p.store.GetAccountRelationListBySubMidAndPredicate(ctx, req)
if err != nil {
logger.Error("GetFollowAccountRelationList fail, err: %v", err)
return make([]*dbstruct.AccountRelation, 0), err
}
return list, nil
}
func (p *AccountRelation) OpListFriend(ctx *gin.Context, req *accountrelationproto.OpListReq) ([]*dbstruct.AccountRelation, error) {
mapList, err := p.store.GetFriendAccountRelationList(ctx, req)
if err != nil {
logger.Error("GetFriendAccountRelationList fail, err: %v", err)
return nil, err
}
list := make([]*dbstruct.AccountRelation, len(mapList))
for i, _map := range mapList {
if objMid, ok := _map["_id"].(int64); ok {
accountRelation := &dbstruct.AccountRelation{
SubMid: req.Mid,
ObjMid: goproto.Int64(objMid),
Predicate: goproto.Int64(consts.Friend),
}
list[i] = accountRelation
} else {
logger.Error("assertion err")
return nil, fmt.Errorf("assertion err")
}
}
return list, err
}
func (p *AccountRelation) OpCount(ctx *gin.Context, req *accountrelationproto.OpCountReq) (map[int64]int64, error) {
countMap := make(map[int64]int64)
mapList, err := p.store.GetAccountRelationCount(ctx, req)
if err != nil {
logger.Error("GetAccountRelationCount fail, err: %v", err)
return nil, err
}
friendMapList, err := p.store.GetFriendAccountRelationCount(ctx, req)
if err != nil {
logger.Error("GetFriendAccountRelationCount fail, err: %v", err)
return nil, err
}
for _, _map := range mapList {
id, ok1 := _map["_id"].(int64)
count, ok2 := _map["count"].(int32)
if ok1 && ok2 {
countMap[id] = int64(count)
} else {
logger.Error("assertion err")
return nil, fmt.Errorf("assertion err")
}
}
if len(friendMapList) > 0 {
if count, ok := friendMapList[0]["count"].(int32); ok {
countMap[consts.Friend] = int64(count)
} else {
logger.Error("assertion err")
return nil, fmt.Errorf("assertion err")
}
}
return countMap, nil
}
func (p *AccountRelation) OpIsFollowedCount(ctx *gin.Context) ([]int64, []int64, error) {
mapList, err := p.store.GetIsFollowedAccountRelationCount(ctx)
if err != nil {
logger.Error("GetIsFollowedAccountRelationCount fail, err: %v", err)
return nil, nil, err
}
midList := make([]int64, len(mapList))
countList := make([]int64, len(mapList))
for i, _map := range mapList {
id, ok1 := _map["_id"].(int64)
count, ok2 := _map["count"].(int32)
if ok1 && ok2 {
midList[i] = id
countList[i] = int64(count)
} else {
logger.Error("assertion err")
return nil, nil, fmt.Errorf("assertion err")
}
}
return midList, countList, nil
}