Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"audit_log.go",
"mcn.go",
"msg.go",
"recommend.go",
"service.go",
"statistics.go",
"upload.go",
],
importpath = "go-common/app/admin/main/mcn/service",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/mcn/conf:go_default_library",
"//app/admin/main/mcn/dao/bfs:go_default_library",
"//app/admin/main/mcn/dao/msg:go_default_library",
"//app/admin/main/mcn/dao/up:go_default_library",
"//app/admin/main/mcn/dao/videoup:go_default_library",
"//app/admin/main/mcn/model:go_default_library",
"//app/interface/main/mcn/model/datamodel:go_default_library",
"//app/interface/main/mcn/tool/worker:go_default_library",
"//app/service/main/account/api:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/member/api:go_default_library",
"//app/service/main/member/model/block:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/metadata:go_default_library",
"//library/queue/databus/report:go_default_library",
"//library/sync/errgroup:go_default_library",
"//library/time:go_default_library",
"//library/xstr:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
go_test(
name = "go_default_test",
srcs = [
"audit_log_test.go",
"mcn_test.go",
"msg_test.go",
"recommend_test.go",
"service_test.go",
"statistics_test.go",
"upload_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/mcn/conf:go_default_library",
"//app/admin/main/mcn/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,33 @@
package service
import (
"context"
"time"
"go-common/library/log"
manager "go-common/library/queue/databus/report"
)
// AddAuditLog .
func (s *Service) AddAuditLog(c context.Context, bizID int, tp int8, action string, uid int64, uname string, oids []int64, index []interface{}, content map[string]interface{}) error {
var err error
for _, oid := range oids {
managerInfo := &manager.ManagerInfo{
UID: uid,
Uname: uname,
Business: bizID,
Type: int(tp),
Action: action,
Oid: oid,
Ctime: time.Now(),
Index: index,
Content: content,
}
if err = manager.Manager(managerInfo); err != nil {
log.Error("manager.Manager(%+v) error(%+v)", managerInfo, err)
continue
}
log.Info("s.managerSendLog(%+v)", managerInfo)
}
return err
}

View File

@@ -0,0 +1,30 @@
package service
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestServiceAddAuditLog(t *testing.T) {
convey.Convey("AddAuditLog", t, func(ctx convey.C) {
var (
c = context.Background()
bizID = int(0)
tp = int8(0)
action = ""
uid = int64(0)
uname = ""
oids = []int64{}
index = []interface{}{}
content map[string]interface{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.AddAuditLog(c, bizID, tp, action, uid, uname, oids, index, content)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,297 @@
package service
import (
"context"
"fmt"
"testing"
"go-common/app/admin/main/mcn/model"
"github.com/smartystreets/goconvey/convey"
)
func TestServiceMcnSignEntry(t *testing.T) {
convey.Convey("McnSignEntry", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNSignEntryReq{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.McnSignEntry(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceMcnSignList(t *testing.T) {
convey.Convey("McnSignList", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNSignStateReq{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := s.McnSignList(c, arg)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestServiceMcnSignOP(t *testing.T) {
convey.Convey("McnSignOP", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNSignStateOpReq{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.McnSignOP(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceMcnUPReviewList(t *testing.T) {
convey.Convey("McnUPReviewList", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNUPStateReq{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := s.McnUPReviewList(c, arg)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestServiceMcnUPOP(t *testing.T) {
convey.Convey("McnUPOP", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNUPStateOpReq{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.McnUPOP(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceMcnPermitOP(t *testing.T) {
convey.Convey("McnPermitOP", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNSignPermissionReq{}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := s.McnPermitOP(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServicegetPermitOpenOrClosed(t *testing.T) {
convey.Convey("getPermitOpenOrClosed", t, func(ctx convey.C) {
var (
a = uint32(5)
b = uint32(5)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
open, closed := s.getPermitOpenOrClosed(a, b)
ctx.Convey("Then open,closed should not be nil.", func(ctx convey.C) {
ctx.So(closed, convey.ShouldNotBeNil)
ctx.So(open, convey.ShouldNotBeNil)
})
})
})
}
func TestServicegetUpPermitString(t *testing.T) {
convey.Convey("getPermitOpenOrClosed", t, func(ctx convey.C) {
var (
a = uint32(5)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
ps := s.getUpPermitString(a)
ctx.Convey("Then open,closed should not be nil.", func(ctx convey.C) {
ctx.So(ps, convey.ShouldNotBeNil)
})
})
})
}
func TestServiceMcnUPPermitList(t *testing.T) {
convey.Convey("McnUPPermitList", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNUPPermitStateReq{}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := s.McnUPPermitList(c, arg)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestServiceMcnUPPermitOP(t *testing.T) {
convey.Convey("McnUPPermitOP", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNUPPermitOPReq{}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := s.McnUPPermitOP(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceMCNList(t *testing.T) {
convey.Convey("MCNList", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNListReq{State: -1}
)
arg.MCNMID = 12345
arg.Export = "csv"
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := s.MCNList(c, arg)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
for k, v := range res.List {
fmt.Printf("re[%d]:%+v", k, v)
}
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestServiceMCNPayEdit(t *testing.T) {
convey.Convey("MCNPayEdit", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNPayEditReq{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.MCNPayEdit(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceMCNPayStateEdit(t *testing.T) {
convey.Convey("MCNPayStateEdit", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNPayStateEditReq{ID: 1, MCNMID: 212895899, SignID: 1}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.MCNPayStateEdit(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceMCNStateEdit(t *testing.T) {
convey.Convey("MCNStateEdit", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNStateEditReq{ID: 1, MCNMID: 1212, Action: model.McnAccountRestore}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.MCNStateEdit(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceMCNRenewal(t *testing.T) {
convey.Convey("MCNRenewal", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNRenewalReq{ID: 5, MCNMID: 27515432, BeginDate: "2018-09-22", EndDate: "2019-09-23", ContractLink: "ContractLink"}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.MCNRenewal(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceMCNInfo(t *testing.T) {
convey.Convey("MCNInfo", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNInfoReq{ID: 9}
)
arg.MCNMID = 27515432
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := s.MCNInfo(c, arg)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
fmt.Println(res)
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestServiceMCNUPList(t *testing.T) {
convey.Convey("McnUPList", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNUPListReq{SignID: 3, State: -1, SortFansCountActive: "asc"}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := s.MCNUPList(c, arg)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
for k, v := range res.List {
fmt.Printf("re[%d]:%+v \n", k, v)
}
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestServiceMCNUPStateEdit(t *testing.T) {
convey.Convey("MCNUPStateEdit", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNUPStateEditReq{ID: 1, SignID: 1}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.MCNUPStateEdit(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,30 @@
package service
import (
"context"
"go-common/app/admin/main/mcn/model"
"go-common/library/log"
"go-common/library/net/metadata"
)
// sendMsg send msg
func (s *Service) sendMsg(c context.Context, arg *model.ArgMsg) error {
var err error
mids, title, content, code := arg.MsgInfo(s.msgMap[arg.MSGType])
if len(mids) == 0 || title == "" || content == "" || code == "" {
log.Warn("mid(%+v) title(%s) content(%s) code(%s) sth is empty!", mids, title, content, code)
return nil
}
if err = s.msg.MutliSendSysMsg(c, mids, code, title, content, metadata.String(c, metadata.RemoteIP)); err != nil {
log.Error("s.msg.MutliSendSysMsg(%+v,%s,%s,%s,%s) error(%+v)", mids, code, title, content, metadata.String(c, metadata.RemoteIP), err)
}
return err
}
func (s *Service) setMsgTypeMap() {
s.msgMap = make(map[model.MSGType]*model.MSG, len(s.c.Property.MSG))
for _, msg := range s.c.Property.MSG {
s.msgMap[msg.MSGType] = msg
}
}

View File

@@ -0,0 +1,35 @@
package service
import (
"context"
"testing"
"go-common/app/admin/main/mcn/model"
"github.com/smartystreets/goconvey/convey"
)
func TestServicesendMsg(t *testing.T) {
convey.Convey("sendMsg", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.ArgMsg{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.sendMsg(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServicesetMsgTypeMap(t *testing.T) {
convey.Convey("setMsgTypeMap", t, func(ctx convey.C) {
ctx.Convey("When everything goes positive", func(ctx convey.C) {
s.setMsgTypeMap()
ctx.Convey("No return values", func(ctx convey.C) {
})
})
})
}

View File

@@ -0,0 +1,192 @@
package service
import (
"context"
"fmt"
"go-common/app/admin/main/mcn/model"
accgrpc "go-common/app/service/main/account/api"
memgrpc "go-common/app/service/main/member/api"
blkmdl "go-common/app/service/main/member/model/block"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/xstr"
)
// RecommendAdd .
func (s *Service) RecommendAdd(c context.Context, arg *model.RecommendUpReq) error {
var (
err error
ok bool
bindMids []int64
blockInfo *memgrpc.BlockInfoReply
bi *model.UpBaseInfo
pi *model.UpPlayInfo
mbi map[int64]*model.UpBaseInfo
mpi map[int64]*model.UpPlayInfo
rpp *model.McnUpRecommendPool
)
if blockInfo, err = s.memGRPC.BlockInfo(c, &memgrpc.MemberMidReq{Mid: arg.UpMid}); err != nil {
return err
}
if blockInfo.BlockStatus > int32(blkmdl.BlockStatusFalse) {
return fmt.Errorf("添加到推荐池的mid为(%d)的up主已经被封禁", arg.UpMid)
}
if bindMids, err = s.dao.McnUpBindMids(c, []int64{arg.UpMid}); err != nil {
return err
}
if len(bindMids) > 0 {
return fmt.Errorf("添加到推荐池的mid为(%s)的up主已经被绑定", xstr.JoinInts(bindMids))
}
if rpp, err = s.dao.McnUpRecommendMid(c, arg.UpMid); err != nil {
return err
}
if rpp != nil && rpp.State != model.MCNUPRecommendStateDel {
return ecode.MCNRecommendUpInPool
}
if mbi, err = s.dao.UpBaseInfoMap(c, []int64{arg.UpMid}); err != nil {
return err
}
if mpi, err = s.dao.UpPlayInfoMap(c, []int64{arg.UpMid}); err != nil {
return err
}
rp := &model.McnUpRecommendPool{UpMid: arg.UpMid}
if bi, ok = mbi[arg.UpMid]; ok {
rp.FansCount = bi.FansCount
rp.ActiveTid = bi.ActiveTid
}
if pi, ok = mpi[arg.UpMid]; ok {
rp.ArchiveCount = pi.ArticleCount
rp.PlayCountAccumulate = pi.PlayCountAccumulate
rp.PlayCountAverage = pi.PlayCountAverage
}
if _, err = s.dao.AddMcnUpRecommend(c, rp); err != nil {
return err
}
s.worker.Add(func() {
index := []interface{}{int8(model.MCNUPRecommendStateOff), arg.UpMid}
content := map[string]interface{}{
"up_mid": arg.UpMid,
"fans_count": rp.FansCount,
"archive_count": rp.ArchiveCount,
"play_count_accumulate": rp.PlayCountAccumulate,
"play_count_average": rp.PlayCountAverage,
"active_tid": rp.ActiveTid,
"source": model.MCNUPRecommendStateManual,
}
s.AddAuditLog(context.Background(), model.MCNRecommendLogBizID, int8(model.MCNUPRecommendActionAdd), model.MCNUPRecommendActionAdd.String(), arg.UID, arg.UserName, []int64{arg.UpMid}, index, content)
})
return nil
}
// RecommendOP .
func (s *Service) RecommendOP(c context.Context, arg *model.RecommendStateOpReq) error {
var (
err error
blockInfosReply *memgrpc.BlockBatchInfoReply
blockMids, bindMids, banMids, recMids []int64
mrp map[int64]*model.McnUpRecommendPool
)
if len(arg.UpMids) == 0 {
return ecode.MCNRecommendUpMidsIsEmpty
}
if arg.Action == model.MCNUPRecommendActionOn || arg.Action == model.MCNUPRecommendActionRestore {
if blockInfosReply, err = s.memGRPC.BlockBatchInfo(c, &memgrpc.MemberMidsReq{Mids: arg.UpMids}); err != nil {
return err
}
for _, v := range blockInfosReply.BlockInfos {
if v.BlockStatus > int32(blkmdl.BlockStatusFalse) {
blockMids = append(blockMids, v.MID)
}
}
if len(blockMids) > 0 {
return fmt.Errorf("推荐的mid为(%s)的up主已经被封禁", xstr.JoinInts(blockMids))
}
if bindMids, err = s.dao.McnUpBindMids(c, arg.UpMids); err != nil {
return err
}
if len(bindMids) > 0 {
return fmt.Errorf("推荐的mid为(%s)的up主已经被绑定", xstr.JoinInts(bindMids))
}
}
state := arg.Action.GetState()
if state == model.MCNUPRecommendStateUnKnown {
return ecode.MCNRecommendUpStateFlowErr
}
if mrp, err = s.dao.McnUpRecommendMids(c, arg.UpMids); err != nil {
return err
}
for _, upMids := range arg.UpMids {
if rp, ok := mrp[upMids]; ok {
if rp.State == model.MCNUPRecommendStateBan && arg.Action == model.MCNUPRecommendActionOn {
banMids = append(banMids, upMids)
}
if rp.State == model.MCNUPRecommendStateOn && arg.Action == model.MCNUPRecommendActionRestore {
recMids = append(recMids, upMids)
}
}
}
if len(banMids) > 0 {
return fmt.Errorf("推荐的mid为(%s)的up主已被禁止推荐,不能推荐", xstr.JoinInts(banMids))
}
if len(recMids) > 0 {
return fmt.Errorf("推荐的mid为(%s)的up主已被推荐,不需要恢复", xstr.JoinInts(recMids))
}
if _, err = s.dao.UpMcnUpsRecommendOP(c, arg.UpMids, state); err != nil {
return err
}
for _, mid := range arg.UpMids {
s.worker.Add(func() {
index := []interface{}{int8(state), mid}
content := map[string]interface{}{
"up_mid": mid,
}
s.AddAuditLog(context.Background(), model.MCNRecommendLogBizID, int8(state), arg.Action.String(), arg.UID, arg.UserName, []int64{mid}, index, content)
})
}
return nil
}
// RecommendList .
func (s *Service) RecommendList(c context.Context, arg *model.MCNUPRecommendReq) (res *model.McnUpRecommendListReply, err error) {
var (
mids, tids []int64
tpNames map[int64]string
accsReply *accgrpc.InfosReply
)
res = new(model.McnUpRecommendListReply)
res.Page = arg.Page
if res.TotalCount, err = s.dao.McnUpRecommendTotal(c, arg); err != nil {
return
}
if res.TotalCount <= 0 {
return
}
if res.List, err = s.dao.McnUpRecommends(c, arg); err != nil {
return
}
if len(res.List) <= 0 {
return
}
for _, v := range res.List {
mids = append(mids, v.UpMid)
tids = append(tids, int64(v.ActiveTid))
}
if accsReply, err = s.accGRPC.Infos3(c, &accgrpc.MidsReq{Mids: mids}); err != nil {
log.Error("s.accGRPC.Infos3(%+v) err(%v)", mids, err)
err = nil
}
tpNames = s.videoup.GetTidName(tids)
infos := accsReply.Infos
for _, v := range res.List {
if info, ok := infos[v.UpMid]; ok {
v.UpName = info.Name
}
if tyName, ok := tpNames[int64(v.ActiveTid)]; ok {
v.TpName = tyName
} else {
v.TpName = model.DefaultTyName
}
}
return
}

View File

@@ -0,0 +1,56 @@
package service
import (
"context"
"testing"
"go-common/app/admin/main/mcn/model"
"github.com/smartystreets/goconvey/convey"
)
func TestServiceRecommendAdd(t *testing.T) {
convey.Convey("RecommendAdd", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.RecommendUpReq{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.RecommendAdd(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceRecommendOP(t *testing.T) {
convey.Convey("RecommendOP", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.RecommendStateOpReq{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := s.RecommendOP(c, arg)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestServiceRecommendList(t *testing.T) {
convey.Convey("RecommendList", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.MCNUPRecommendReq{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := s.RecommendList(c, arg)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,68 @@
package service
import (
"context"
"go-common/app/admin/main/mcn/conf"
bfs "go-common/app/admin/main/mcn/dao/bfs"
msg "go-common/app/admin/main/mcn/dao/msg"
dao "go-common/app/admin/main/mcn/dao/up"
videoup "go-common/app/admin/main/mcn/dao/videoup"
"go-common/app/admin/main/mcn/model"
"go-common/app/interface/main/mcn/tool/worker"
accgrpc "go-common/app/service/main/account/api"
arcgrpc "go-common/app/service/main/archive/api"
memgrpc "go-common/app/service/main/member/api"
"github.com/pkg/errors"
)
// Service struct
type Service struct {
c *conf.Config
dao *dao.Dao
bfs *bfs.Dao
msg *msg.Dao
videoup *videoup.Dao
msgMap map[model.MSGType]*model.MSG
memGRPC memgrpc.MemberClient
accGRPC accgrpc.AccountClient
arcGRPC arcgrpc.ArchiveClient
worker *worker.Pool
}
// New init
func New(c *conf.Config) (s *Service) {
s = &Service{
c: c,
dao: dao.New(c),
bfs: bfs.New(c),
msg: msg.New(c),
videoup: videoup.New(c),
worker: worker.New(nil),
}
var err error
if s.accGRPC, err = accgrpc.NewClient(c.GRPCClient.Account); err != nil {
panic(errors.WithMessage(err, "Failed to dial account service"))
}
if s.arcGRPC, err = arcgrpc.NewClient(c.GRPCClient.Archive); err != nil {
panic(errors.WithMessage(err, "Failed to dial archive service"))
}
if s.memGRPC, err = memgrpc.NewClient(c.GRPCClient.Member); err != nil {
panic(errors.WithMessage(err, "Failed to dial member service"))
}
s.setMsgTypeMap()
return s
}
// Ping Service
func (s *Service) Ping(c context.Context) (err error) {
return s.dao.Ping(c)
}
// Close Service
func (s *Service) Close() {
s.dao.Close()
s.worker.Close()
s.worker.Wait()
}

View File

@@ -0,0 +1,33 @@
package service
import (
"flag"
"os"
"go-common/app/admin/main/mcn/conf"
)
var (
s *Service
)
func init() {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.mcn-admin")
flag.Set("conf_token", "BVWgBtBvS2pkTBbmxAl0frX6KRA14d5P")
flag.Set("tree_id", "6813")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../cmd/mcn-admin-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
s = New(conf.Conf)
}

View File

@@ -0,0 +1,215 @@
package service
import (
"context"
"fmt"
"time"
"go-common/app/admin/main/mcn/model"
dtmdl "go-common/app/interface/main/mcn/model/datamodel"
accgrpc "go-common/app/service/main/account/api"
arcgrpc "go-common/app/service/main/archive/api"
"go-common/library/log"
"go-common/library/sync/errgroup"
xtime "go-common/library/time"
"github.com/pkg/errors"
)
// ArcTopDataStatistics .
func (s *Service) ArcTopDataStatistics(c context.Context, arg *model.McnGetRankReq) (res *model.McnGetRankUpFansReply, err error) {
return s.dao.ArcTopDataStatistics(c, arg)
}
// McnsTotalDatas .
func (s *Service) McnsTotalDatas(c context.Context, arg *model.TotalMcnDataReq) (res *model.TotalMcnDataInfo, err error) {
var (
signUpsTotal, FanTotal, videoupTotal, playTotal int64
mids, fansMids, ArcsMids, avids, tids, arcsTids, typeTids []int64
m *model.McnDataOverview
mrf map[int8][]*model.McnRankFansOverview
ras []*model.McnRankArchiveLikesOverview
mmd map[string][]*model.McnDataTypeSummary
)
td := new(thirdDataMap)
res = new(model.TotalMcnDataInfo)
res.TopInfo = new(model.McnDataTopInfo)
res.TypesInfo = new(model.McnDataTypesInfo)
date := xtime.Time(time.Date(arg.Date.Time().Year(), arg.Date.Time().Month(), arg.Date.Time().Day()-1, 0, 0, 0, 0, time.Local).Unix())
if m, err = s.dao.McnDataOverview(c, date); err != nil {
return
}
res.BaseInfo = m
if mrf, fansMids, err = s.dao.McnRankFansOverview(c, model.DataTypeDay, date, model.TopDataLenth); err != nil {
return
}
if ras, ArcsMids, avids, arcsTids, err = s.dao.McnRankArchiveLikesOverview(c, model.DataTypeDay, date, model.TopDataLenth); err != nil {
return
}
if mmd, typeTids, err = s.dao.McnDataTypeSummary(c, date); err != nil {
return
}
mids = append(mids, fansMids...)
mids = append(mids, ArcsMids...)
tids = append(tids, arcsTids...)
tids = append(tids, typeTids...)
if td.infosReply, err = s.accGRPC.Infos3(c, &accgrpc.MidsReq{Mids: mids}); err != nil {
log.Error("s.accGRPC.Infos3(%+v) error(%+v)", mids, err)
err = nil
}
if td.archivesReply, err = s.arcGRPC.Arcs(c, &arcgrpc.ArcsRequest{Aids: avids}); err != nil {
log.Error("s.arcGRPC.Arcs(%+v) error(%+v)", avids, err)
err = nil
}
td.tpNames = s.videoup.GetTidName(tids)
for view, data := range mrf {
for _, v := range data {
var (
rateIncr int64
name, _, _, _ = td.getTypeName(v.Mid, 0, 0, 0)
)
if v.Fans != 0 {
rateIncr = (10000 * v.FansIncr) / v.Fans
}
fr := &model.FansRankIncr{Mid: v.Mid, Name: name, Rank: v.Rank, FansIncr: v.FansIncr, Fans: v.Fans, RateIncr: rateIncr, SignID: v.SignID}
switch model.DataViewFansTop(view) {
case model.McnFansIncr:
res.TopInfo.McnFansIncr = append(res.TopInfo.McnFansIncr, fr)
case model.McnFansIncrRate:
res.TopInfo.McnFansRateIncr = append(res.TopInfo.McnFansRateIncr, fr)
case model.UpFansIncr:
res.TopInfo.UpFansIncr = append(res.TopInfo.UpFansIncr, fr)
case model.UpFansIncrRate:
res.TopInfo.UpFansRateIncr = append(res.TopInfo.UpFansRateIncr, fr)
}
}
}
for _, v := range ras {
var mcnName, upName, avTitle, tpName = td.getTypeName(v.McnMid, v.UpMid, v.Avid, int64(v.Tid))
lr := &model.LikesRankIncr{McnMid: v.McnMid, McnName: mcnName,
UpMid: v.UpMid, UpName: upName, AVID: v.Avid, AVTitle: avTitle,
TID: v.Tid, TypeName: tpName, LikesIncr: v.Likes, PlayIncr: v.Plays, SignID: v.SignID}
res.TopInfo.ArcLikesIncr = append(res.TopInfo.ArcLikesIncr, lr)
}
for vt, data := range mmd {
for _, v := range data {
var _, _, _, tpName = td.getTypeName(0, 0, 0, int64(v.Tid))
dt := &model.DataTypes{TID: v.Tid, TypeName: tpName, Amount: v.Amount}
switch vt {
case fmt.Sprintf("%d-%d", model.SignUpsAccumulate, model.DataTypeAccumulate):
signUpsTotal += v.Amount
res.TypesInfo.SignUps = append(res.TypesInfo.SignUps, dt)
case fmt.Sprintf("%d-%d", model.FansIncr, model.DataTypeDay):
FanTotal += v.Amount
res.TypesInfo.FansIncr = append(res.TypesInfo.FansIncr, dt)
case fmt.Sprintf("%d-%d", model.VideoUpsIncr, model.DataTypeDay):
videoupTotal += v.Amount
res.TypesInfo.VideoupIncr = append(res.TypesInfo.VideoupIncr, dt)
case fmt.Sprintf("%d-%d", model.PlaysIncr, model.DataTypeDay):
playTotal += v.Amount
res.TypesInfo.PlayIncr = append(res.TypesInfo.PlayIncr, dt)
}
}
}
statTypeRate(res.TypesInfo.SignUps, signUpsTotal)
statTypeRate(res.TypesInfo.FansIncr, FanTotal)
statTypeRate(res.TypesInfo.VideoupIncr, videoupTotal)
statTypeRate(res.TypesInfo.PlayIncr, playTotal)
return
}
type thirdDataMap struct {
tpNames map[int64]string
infosReply *accgrpc.InfosReply
archivesReply *arcgrpc.ArcsReply
}
func (td *thirdDataMap) getTypeName(mcnMid, upMid, avid, tid int64) (mcnName, upName, avTitle, tpName string) {
if td.infosReply != nil {
infos := td.infosReply.Infos
if info, ok := infos[mcnMid]; ok {
mcnName = info.Name
}
if info, ok := infos[upMid]; ok {
upName = info.Name
}
}
if td.archivesReply != nil {
archives := td.archivesReply.Arcs
if archive, ok := archives[avid]; ok {
avTitle = archive.Title
}
}
tpName = td.tpNames[tid]
return
}
func statTypeRate(dts []*model.DataTypes, total int64) {
for _, v := range dts {
if total != 0 {
v.Rate = (10000 * v.Amount) / total
}
v.Total = total
}
}
// McnFansAnalyze .
func (s *Service) McnFansAnalyze(c context.Context, arg *model.McnCommonReq) (res *model.McnGetMcnFansReply, err error) {
var (
f *dtmdl.DmConMcnFansD
sex *dtmdl.DmConMcnFansSexW
age *dtmdl.DmConMcnFansAgeW
playWay *dtmdl.DmConMcnFansPlayWayW
areas []*dtmdl.DmConMcnFansAreaW
types []*dtmdl.DmConMcnFansTypeW
tags []*dtmdl.DmConMcnFansTagW
g errgroup.Group
)
res = new(model.McnGetMcnFansReply)
g.Go(func() (err error) {
if f, err = s.dao.DataFans(c, arg); err != nil {
log.Error("s.dao.DataFans(%+v) error(%v)", arg, err)
return
}
res.FansOverview = f
return
})
g.Go(func() (err error) {
if sex, age, playWay, err = s.dao.DataFansBaseAttr(c, arg); err != nil {
log.Error("s.dao.DataFansBaseAttr(%+v) error(%v)", arg, err)
return
}
res.FansSex = sex
res.FansAge = age
res.FansPlayWay = playWay
return
})
g.Go(func() (err error) {
if areas, err = s.dao.DataFansArea(c, arg); err != nil {
log.Error("s.dao.DataFansArea(%+v) error(%v)", arg, err)
return
}
res.FansArea = areas
return
})
g.Go(func() (err error) {
if types, err = s.dao.DataFansType(c, arg); err != nil {
log.Error("s.dao.DataFansType(%+v) error(%v)", arg, err)
return
}
res.FansType = types
return
})
g.Go(func() (err error) {
if tags, err = s.dao.DataFansTag(c, arg); err != nil {
log.Error("s.dao.DataFansTag(%+v) error(%v)", arg, err)
return
}
res.FansTag = tags
return
})
if err = g.Wait(); err != nil {
err = errors.WithStack(err)
}
return
}

View File

@@ -0,0 +1,57 @@
package service
import (
"context"
"testing"
"go-common/app/admin/main/mcn/model"
"github.com/smartystreets/goconvey/convey"
)
func TestServiceArcTopDataStatistics(t *testing.T) {
convey.Convey("ArcTopDataStatistics", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.McnGetRankReq{}
)
arg.SignID = 214
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := s.ArcTopDataStatistics(c, arg)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestServiceMcnsTotalDatas(t *testing.T) {
convey.Convey("McnsTotalDatas", t, func(ctx convey.C) {
var (
c = context.Background()
arg = &model.TotalMcnDataReq{Date: 1542211200}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := s.McnsTotalDatas(c, arg)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestServicestatTypeRate(t *testing.T) {
convey.Convey("statTypeRate", t, func(ctx convey.C) {
var (
dts = []*model.DataTypes{}
total = int64(111)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
statTypeRate(dts, total)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
})
}

View File

@@ -0,0 +1,11 @@
package service
import (
"context"
"io"
)
// Upload http upload file.
func (s *Service) Upload(c context.Context, fileName, fileType string, expire int64, body io.Reader) (location string, err error) {
return s.bfs.Upload(c, fileName, fileType, expire, body)
}

View File

@@ -0,0 +1,28 @@
package service
import (
"context"
"io"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestServiceUpload(t *testing.T) {
convey.Convey("Upload", t, func(ctx convey.C) {
var (
c = context.Background()
fileName = ""
fileType = ""
expire = int64(0)
body io.Reader
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
location, err := s.Upload(c, fileName, fileType, expire, body)
ctx.Convey("Then err should be nil.location should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(location, convey.ShouldNotBeNil)
})
})
})
}