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,67 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"depend_test.go",
"localcache_test.go",
"memcache_test.go",
"mysql_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/usersuit/conf:go_default_library",
"//app/service/main/usersuit/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"depend.go",
"localcache.go",
"memcache.go",
"mysql.go",
],
importmap = "go-common/app/service/main/usersuit/dao/medal",
importpath = "go-common/app/service/main/usersuit/dao/medal",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/usersuit/conf:go_default_library",
"//app/service/main/usersuit/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/stat/prom:go_default_library",
"//vendor/github.com/bluele/gcache: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"],
)

View File

@@ -0,0 +1,68 @@
package medal
import (
"context"
"time"
"go-common/app/service/main/usersuit/conf"
"go-common/library/cache/memcache"
"go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
"github.com/bluele/gcache"
)
const (
_sendMsgPath = "/api/notify/send.user.notify.do"
_getWaredFansMedalPath = "/fans_medal/v1/fans_medal/get_weared_medal"
)
// Dao struct info of Dao.
type Dao struct {
db *sql.DB
c *conf.Config
client *bm.Client
// memcache
mc *memcache.Pool
mcExpire int32
pointExpire int32
// send message URI.
sendMsgURI string
// get weared fans medal URI.
getWaredFansMedalURI string
// medalStore
medalStore gcache.Cache
}
// New new a Dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.MySQL),
client: bm.NewClient(c.HTTPClient),
// memcache
mc: memcache.NewPool(c.Memcache.Config),
mcExpire: int32(time.Duration(c.Memcache.MedalExpire) / time.Second),
pointExpire: int32(time.Duration(c.Memcache.PointExpire) / time.Second),
sendMsgURI: c.Host.MessageCo + _sendMsgPath,
getWaredFansMedalURI: c.Host.LiveAPICo + _getWaredFansMedalPath,
medalStore: gcache.New(c.MedalCache.Size).LFU().Build(),
}
return
}
// Ping ping health.
func (d *Dao) Ping(c context.Context) (err error) {
return d.pingMC(c)
}
// Close close connections of mc, redis, db.
func (d *Dao) Close() {
if d.mc != nil {
d.mc.Close()
}
if d.db != nil {
d.db.Close()
}
}

View File

@@ -0,0 +1,47 @@
package medal
import (
"context"
"flag"
"os"
"testing"
"go-common/app/service/main/usersuit/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
c = context.Background()
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.usersuit-service")
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/convey-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
d.client.SetTransport(gock.DefaultTransport)
m.Run()
os.Exit(0)
}
// func httpMock(method, url string) *gock.Request {
// r := gock.New(url)
// r.Method = strings.ToUpper(method)
// return r
// }

View File

@@ -0,0 +1,66 @@
package medal
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/library/log"
)
// SendMsg send message.
func (d *Dao) SendMsg(c context.Context, mid int64, title string, context string) (err error) {
params := url.Values{}
params.Set("mc", "2_1_13")
params.Set("title", title)
params.Set("data_type", "4")
params.Set("context", context)
params.Set("mid_list", fmt.Sprintf("%d", mid))
var res struct {
Code int `json:"code"`
Data *struct {
Status int8 `json:"status"`
Remark string `json:"remark"`
} `json:"data"`
}
if err = d.client.Post(c, d.sendMsgURI, "", params, &res); err != nil {
log.Error("sendMsgURL(%s) error(%v)", d.sendMsgURI+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("sendMsgURL(%s) res(%d)", d.sendMsgURI+"?"+params.Encode(), res.Code)
}
log.Info("d.sendMsgURL url(%s) res(%d)", d.sendMsgURI+"?"+params.Encode(), res.Code)
return
}
// GetWearedfansMedal get weared fans medals from live.
func (d *Dao) GetWearedfansMedal(c context.Context, mid int64, source int8) (isLove bool, err error) {
params := url.Values{}
params.Set("uid", strconv.FormatInt(mid, 10))
params.Set("source", strconv.FormatInt(int64(source), 10))
var res struct {
Code int `json:"code"`
Data *struct {
Max int8 `json:"max"`
Cnt int8 `json:"cnt"`
MasterMax int8 `json:"master_max"`
} `json:"data"`
}
if err = d.client.Post(c, d.getWaredFansMedalURI, "", params, &res); err != nil {
log.Error("GetWearedfansMedal(%s) error(%v)", d.getWaredFansMedalURI+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("GetWearedfansMedal(%s) res(%d)", d.getWaredFansMedalURI+"?"+params.Encode(), res.Code)
}
log.Info("GetWearedfansMedal(%s) res(%+v)", d.getWaredFansMedalURI+"?"+params.Encode(), res)
if res.Code == 0 {
if res.Data.Cnt > 0 {
isLove = true
return
}
}
return
}

View File

@@ -0,0 +1,39 @@
package medal
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestMedalSendMsg(t *testing.T) {
convey.Convey("SendMsg", t, func(ctx convey.C) {
var (
mid = int64(88889017)
title = ""
context = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SendMsg(c, mid, title, context)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalGetWearedfansMedal(t *testing.T) {
convey.Convey("GetWearedfansMedal", t, func(ctx convey.C) {
var (
mid = int64(88889017)
source = int8(2)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
isLove, err := d.GetWearedfansMedal(c, mid, source)
ctx.Convey("Then err should be nil.isLove should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(isLove, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,84 @@
package medal
import (
"context"
"time"
"go-common/library/log"
"go-common/library/stat/prom"
"github.com/bluele/gcache"
"github.com/pkg/errors"
)
func (d *Dao) loadMedal(c context.Context, mid int64) (int64, bool, error) {
nid, nofound, err := d.medalActivatedCache(c, mid)
if err != nil {
return 0, nofound, err
}
d.storeMedal(mid, nid, nofound)
return nid, nofound, nil
}
func (d *Dao) storeMedal(mid int64, nid int64, nofound bool) {
if nofound {
return
}
d.medalStore.SetWithExpire(mid, nid, time.Duration(d.c.MedalCache.Expire))
}
func (d *Dao) localMedal(mid int64) (int64, error) {
prom.CacheHit.Incr("local_medal_cache")
item, err := d.medalStore.Get(mid)
if err != nil {
prom.CacheMiss.Incr("local_medal_cache")
return 0, err
}
nid, ok := item.(int64)
if !ok {
prom.CacheMiss.Incr("local_medal_cache")
return 0, errors.New("Not a medal")
}
return nid, nil
}
// MedalActivatedCache get medal cache.
func (d *Dao) MedalActivatedCache(c context.Context, mid int64) (int64, bool, error) {
nid, err := d.localMedal(mid)
if err != nil {
if err != gcache.KeyNotFoundError {
log.Error("Failed to get medal from local: mid: %d: %+v", mid, err)
}
return d.loadMedal(c, mid)
}
return nid, false, nil
}
// MedalsActivatedCache get multi medals cache.
func (d *Dao) MedalsActivatedCache(c context.Context, mids []int64) (map[int64]int64, []int64, error) {
nids := make(map[int64]int64, len(mids))
lcMissed := make([]int64, 0, len(mids))
for _, mid := range mids {
nid, err := d.localMedal(mid)
if err != nil {
if err != gcache.KeyNotFoundError {
log.Error("Failed to get medal from local: mid: %d: %+v", mid, err)
}
lcMissed = append(lcMissed, mid)
continue
}
nids[mid] = nid
}
if len(lcMissed) == 0 {
return nids, nil, nil
}
mcMedals, mcMissed, err := d.medalsActivatedCache(c, lcMissed)
if err != nil {
return nil, nil, err
}
for mid, nid := range mcMedals {
d.storeMedal(mid, nid, false)
nids[mid] = nid
}
return nids, mcMissed, nil
}

View File

@@ -0,0 +1,85 @@
package medal
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestMedalloadMedal(t *testing.T) {
convey.Convey("loadMedal", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1, p2, err := d.loadMedal(c, mid)
ctx.Convey("Then err should be nil.p1,p2 should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(p2, convey.ShouldNotBeNil)
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalstoreMedal(t *testing.T) {
convey.Convey("storeMedal", t, func(ctx convey.C) {
var (
mid = int64(88889017)
nid = int64(1)
nofound bool
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.storeMedal(mid, nid, nofound)
ctx.Convey("No return values", func(ctx convey.C) {
})
})
})
}
func TestMedallocalMedal(t *testing.T) {
convey.Convey("localMedal", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1, err := d.localMedal(mid)
ctx.Convey("Then err should be nil.p1 should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalMedalActivatedCache(t *testing.T) {
convey.Convey("MedalActivatedCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1, p2, err := d.MedalActivatedCache(c, mid)
ctx.Convey("Then err should be nil.p1,p2 should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(p2, convey.ShouldNotBeNil)
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalMedalsActivatedCache(t *testing.T) {
convey.Convey("MedalsActivatedCache", t, func(ctx convey.C) {
var (
mids = []int64{88889017, 1}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1, p2, err := d.MedalsActivatedCache(c, mids)
ctx.Convey("Then err should be nil.p1,p2 should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(p1, convey.ShouldNotBeNil)
ctx.So(p2, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,273 @@
package medal
import (
"context"
"strconv"
"github.com/pkg/errors"
"go-common/app/service/main/usersuit/model"
gmc "go-common/library/cache/memcache"
"go-common/library/log"
)
const (
_prefixActivatedNid = "usma:" // key of activated medal nid
_prefixOwners = "umos:" // key of owners info
_prefixRedPoint = "usrp:" // key of red point
_prefixPopup = "uspp:" // key of new medal popup
)
// medalactivated medal nid key.
func activatedNidKey(mid int64) string {
return _prefixActivatedNid + strconv.FormatInt(mid, 10)
}
// ownersKey medal_owner key.
func ownersKey(mid int64) string {
return _prefixOwners + strconv.FormatInt(mid, 10)
}
//RedPointKey new medal RedPoint key.
func RedPointKey(mid int64) string {
return _prefixRedPoint + strconv.FormatInt(mid, 10)
}
// PopupKey new medal popup key.
func PopupKey(mid int64) string {
return _prefixPopup + strconv.FormatInt(mid, 10)
}
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Set(&gmc.Item{Key: "ping", Value: []byte{1}, Expiration: d.mcExpire}); err != nil {
err = errors.WithStack(err)
}
return
}
// MedalOwnersCache get medal_owner cache.
func (d *Dao) MedalOwnersCache(c context.Context, mid int64) (res []*model.MedalOwner, notFound bool, err error) {
conn := d.mc.Get(c)
defer conn.Close()
item, err := conn.Get(ownersKey(mid))
if err != nil {
if err == gmc.ErrNotFound {
res = nil
err = nil
notFound = true
return
}
err = errors.WithStack(err)
return
}
res = make([]*model.MedalOwner, 0)
if err = conn.Scan(item, &res); err != nil {
err = errors.WithStack(err)
}
return
}
// SetMedalOwnersache set medal_owner cache.
func (d *Dao) SetMedalOwnersache(c context.Context, mid int64, nos []*model.MedalOwner) (err error) {
key := ownersKey(mid)
item := &gmc.Item{Key: key, Object: nos, Expiration: d.mcExpire, Flags: gmc.FlagJSON}
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Set(item); err != nil {
err = errors.WithStack(err)
}
return
}
// DelMedalOwnersCache delete medal_owner cache.
func (d *Dao) DelMedalOwnersCache(c context.Context, mid int64) (err error) {
key := ownersKey(mid)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == gmc.ErrNotFound {
err = nil
} else {
err = errors.WithStack(err)
}
}
return
}
// MedalsActivatedCache multi get user activated medal nid from memcache.
func (d *Dao) medalsActivatedCache(c context.Context, mids []int64) (nids map[int64]int64, missed []int64, err error) {
nids = make(map[int64]int64, len(mids))
keys := make([]string, len(mids))
mm := make(map[string]int64, len(mids))
for i, mid := range mids {
var key = activatedNidKey(mid)
keys[i] = key
mm[key] = mid
}
conn := d.mc.Get(c)
defer conn.Close()
items, err := conn.GetMulti(keys)
if err != nil {
if err == gmc.ErrNotFound {
err = nil
}
return
}
for _, item := range items {
var nid int64
if err = conn.Scan(item, &nid); err != nil {
log.Error("conn.Scan(%s) error(%v)", item.Value, err)
continue
}
nids[mm[item.Key]] = nid
delete(mm, item.Key)
}
missed = make([]int64, 0, len(mm))
for _, m := range mm {
missed = append(missed, m)
}
return
}
// MedalActivatedCache get user activated medal nid.
func (d *Dao) medalActivatedCache(c context.Context, mid int64) (nid int64, notFound bool, err error) {
conn := d.mc.Get(c)
defer conn.Close()
item, err := conn.Get(activatedNidKey(mid))
if err != nil {
if err == gmc.ErrNotFound {
nid = 0
err = nil
notFound = true
return
}
err = errors.WithStack(err)
return
}
if err = conn.Scan(item, &nid); err != nil {
err = errors.WithStack(err)
}
return
}
// SetMedalActivatedCache set activated medal cache.
func (d *Dao) SetMedalActivatedCache(c context.Context, mid, nid int64) (err error) {
key := activatedNidKey(mid)
item := &gmc.Item{Key: key, Object: nid, Expiration: d.mcExpire, Flags: gmc.FlagJSON}
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Set(item); err != nil {
err = errors.WithStack(err)
}
return
}
// DelMedalActivatedCache delete activated medal cache.
func (d *Dao) DelMedalActivatedCache(c context.Context, mid int64) (err error) {
key := activatedNidKey(mid)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == gmc.ErrNotFound {
err = nil
} else {
err = errors.WithStack(err)
}
}
return
}
// PopupCache get new medal info popup cache.
func (d *Dao) PopupCache(c context.Context, mid int64) (nid int64, err error) {
conn := d.mc.Get(c)
defer conn.Close()
item, err := conn.Get(PopupKey(mid))
if err != nil {
if err == gmc.ErrNotFound {
nid = 0
err = nil
return
}
err = errors.WithStack(err)
return
}
if err = conn.Scan(item, &nid); err != nil {
err = errors.WithStack(err)
}
return
}
// SetPopupCache set popup cache.
func (d *Dao) SetPopupCache(c context.Context, mid, nid int64) (err error) {
key := PopupKey(mid)
item := &gmc.Item{Key: key, Object: nid, Expiration: d.pointExpire, Flags: gmc.FlagJSON}
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Set(item); err != nil {
err = errors.WithStack(err)
}
return
}
// DelPopupCache delete new medal info popup cache.
func (d *Dao) DelPopupCache(c context.Context, mid int64) (err error) {
key := PopupKey(mid)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == gmc.ErrNotFound {
err = nil
} else {
err = errors.WithStack(err)
}
}
return
}
// RedPointCache get new medal info red point cache.
func (d *Dao) RedPointCache(c context.Context, mid int64) (nid int64, err error) {
conn := d.mc.Get(c)
defer conn.Close()
item, err := conn.Get(RedPointKey(mid))
if err != nil {
if err == gmc.ErrNotFound {
err = nil
return
}
err = errors.WithStack(err)
return
}
if err = conn.Scan(item, &nid); err != nil {
err = errors.WithStack(err)
}
return
}
// SetRedPointCache set red point cache.
func (d *Dao) SetRedPointCache(c context.Context, mid, nid int64) (err error) {
key := RedPointKey(mid)
item := &gmc.Item{Key: key, Object: nid, Expiration: d.pointExpire, Flags: gmc.FlagJSON}
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Set(item); err != nil {
err = errors.WithStack(err)
}
return
}
// DelRedPointCache delete new medal info red point cache.
func (d *Dao) DelRedPointCache(c context.Context, mid int64) (err error) {
key := RedPointKey(mid)
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == gmc.ErrNotFound {
err = nil
} else {
err = errors.WithStack(err)
}
}
return
}

View File

@@ -0,0 +1,271 @@
package medal
import (
"go-common/app/service/main/usersuit/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestMedalactivatedNidKey(t *testing.T) {
convey.Convey("activatedNidKey", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := activatedNidKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalownersKey(t *testing.T) {
convey.Convey("ownersKey", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := ownersKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalRedPointKey(t *testing.T) {
convey.Convey("RedPointKey", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := RedPointKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalPopupKey(t *testing.T) {
convey.Convey("PopupKey", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := PopupKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalpingMC(t *testing.T) {
convey.Convey("pingMC", t, func(ctx convey.C) {
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.pingMC(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalSetMedalOwnersache(t *testing.T) {
convey.Convey("SetMedalOwnersache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
nos = []*model.MedalOwner{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetMedalOwnersache(c, mid, nos)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalMedalOwnersCache(t *testing.T) {
convey.Convey("MedalOwnersCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
nos = []*model.MedalOwner{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.SetMedalOwnersache(c, mid, nos)
res, notFound, err := d.MedalOwnersCache(c, mid)
ctx.Convey("Then err should be nil.res,notFound should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(notFound, convey.ShouldNotBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalDelMedalOwnersCache(t *testing.T) {
convey.Convey("DelMedalOwnersCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelMedalOwnersCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalmedalsActivatedCache(t *testing.T) {
convey.Convey("medalsActivatedCache", t, func(ctx convey.C) {
var (
mids = []int64{88889017}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
nids, missed, err := d.medalsActivatedCache(c, mids)
ctx.Convey("Then err should be nil.nids,missed should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(missed, convey.ShouldNotBeNil)
ctx.So(nids, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalSetMedalActivatedCache(t *testing.T) {
convey.Convey("SetMedalActivatedCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
nid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetMedalActivatedCache(c, mid, nid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalmedalActivatedCache(t *testing.T) {
convey.Convey("medalActivatedCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
nid, notFound, err := d.medalActivatedCache(c, mid)
ctx.Convey("Then err should be nil.nid,notFound should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(notFound, convey.ShouldNotBeNil)
ctx.So(nid, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalDelMedalActivatedCache(t *testing.T) {
convey.Convey("DelMedalActivatedCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelMedalActivatedCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalPopupCache(t *testing.T) {
convey.Convey("PopupCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
nid, err := d.PopupCache(c, mid)
ctx.Convey("Then err should be nil.nid should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(nid, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalSetPopupCache(t *testing.T) {
convey.Convey("SetPopupCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
nid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetPopupCache(c, mid, nid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalDelPopupCache(t *testing.T) {
convey.Convey("DelPopupCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelPopupCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalRedPointCache(t *testing.T) {
convey.Convey("RedPointCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
nid, err := d.RedPointCache(c, mid)
ctx.Convey("Then err should be nil.nid should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(nid, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalSetRedPointCache(t *testing.T) {
convey.Convey("SetRedPointCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
nid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetRedPointCache(c, mid, nid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalDelRedPointCache(t *testing.T) {
convey.Convey("DelRedPointCache", t, func(ctx convey.C) {
var (
mid = int64(88889017)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelRedPointCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,181 @@
package medal
import (
"context"
"fmt"
"go-common/app/service/main/usersuit/model"
"go-common/library/database/sql"
"github.com/pkg/errors"
)
const (
_sharding = 10
_insOwnerSQL = "INSERT INTO medal_owner_%s(mid,nid) VALUES (?,?)"
_updateOwnerSQL = "UPDATE medal_owner_%s SET is_activated=? WHERE mid=? AND nid=?"
_updateOwnerUnallSQL = "UPDATE medal_owner_%s SET is_activated=0 WHERE mid=? AND nid!=?"
_selInfoAllSQL = "SELECT id,name,description,image,image_small,cond,gid,level,level_rank,sort FROM medal_info ORDER BY sort ASC,gid ASC,level ASC"
_selOwnerByMidSQL = "SELECT id,mid,nid,is_activated,ctime,mtime FROM medal_owner_%s WHERE mid=? AND is_del=0 ORDER BY ctime DESC"
_selInfoByNidSQL = "SELECT name FROM medal_info WHERE id=? AND is_online=1"
_selInstalledOwnerBYMidSQL = "SELECT nid FROM medal_owner_%s WHERE mid=? AND is_activated=1 AND is_del=0 LIMIT 1"
_countOwnerBYNidMidSQL = "SELECT COUNT(*) FROM medal_owner_%s WHERE mid=? AND nid=?"
_OwnerBYNidMidSQL = "SELECT id,mid,nid,is_activated,ctime,mtime FROM medal_owner_%s WHERE mid=? AND nid=?"
_selGroupAllSQL = "SELECT id,name,pid,rank FROM medal_group WHERE is_online=1 ORDER BY pid ASC,rank ASC"
)
func (d *Dao) hit(id int64) string {
return fmt.Sprintf("%d", id%_sharding)
}
// AddMedalOwner insert into medal_owner.
func (d *Dao) AddMedalOwner(c context.Context, mid, nid int64) (err error) {
if _, err = d.db.Exec(c, fmt.Sprintf(_insOwnerSQL, d.hit(mid)), mid, nid); err != nil {
err = errors.WithStack(err)
}
return
}
// InstallMedalOwner update medal_owner set is_activated=1.
func (d *Dao) InstallMedalOwner(c context.Context, mid, nid int64) (err error) {
if _, err = d.db.Exec(c, fmt.Sprintf(_updateOwnerSQL, d.hit(mid)), model.OwnerInstall, mid, nid); err != nil {
err = errors.WithStack(err)
}
return
}
// UninstallMedalOwner update medal_owner set is_activated=0.
func (d *Dao) UninstallMedalOwner(c context.Context, mid, nid int64) (err error) {
if _, err = d.db.Exec(c, fmt.Sprintf(_updateOwnerSQL, d.hit(mid)), model.OwnerUninstall, mid, nid); err != nil {
err = errors.WithStack(err)
}
return
}
// UninstallAllMedalOwner uninst all medal_owner set is_activated=0.
func (d *Dao) UninstallAllMedalOwner(c context.Context, mid, nid int64) (err error) {
if _, err = d.db.Exec(c, fmt.Sprintf(_updateOwnerUnallSQL, d.hit(mid)), mid, nid); err != nil {
err = errors.WithStack(err)
}
return
}
// MedalInfoAll retun all medal_info where is_online=1.
func (d *Dao) MedalInfoAll(c context.Context) (res map[int64]*model.MedalInfo, err error) {
res = make(map[int64]*model.MedalInfo)
rows, err := d.db.Query(c, _selInfoAllSQL)
if err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
info := new(model.MedalInfo)
if err = rows.Scan(&info.ID, &info.Name, &info.Description, &info.Image, &info.ImageSmall, &info.Condition, &info.GID, &info.Level, &info.LevelRank, &info.Sort); err != nil {
err = errors.WithStack(err)
return
}
info.Build()
res[info.ID] = info
}
err = rows.Err()
return
}
// MedalOwnerByMid return medal_owner by mid.
func (d *Dao) MedalOwnerByMid(c context.Context, mid int64) (res []*model.MedalOwner, err error) {
res = make([]*model.MedalOwner, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_selOwnerByMidSQL, d.hit(mid)), mid)
if err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.MedalOwner)
if err = rows.Scan(&r.ID, &r.MID, &r.NID, &r.IsActivated, &r.CTime, &r.MTime); err != nil {
err = errors.WithStack(err)
return
}
res = append(res, r)
}
err = rows.Err()
return
}
// MedalInfoByNid return medal_info by nid.
func (d *Dao) MedalInfoByNid(c context.Context, nid int64) (res *model.MedalInfo, err error) {
res = &model.MedalInfo{}
rows := d.db.QueryRow(c, _selInfoByNidSQL, nid)
if err = rows.Scan(&res.Name); err != nil {
if err != sql.ErrNoRows {
err = errors.Wrap(err, "InstalledOwnerBYMid")
return
}
err = nil
}
return
}
// ActivatedOwnerByMid retun nid of medal_owner by mid where is_activated=1.
func (d *Dao) ActivatedOwnerByMid(c context.Context, mid int64) (nid int64, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_selInstalledOwnerBYMidSQL, d.hit(mid)), mid)
if err = row.Scan(&nid); err != nil {
if err != sql.ErrNoRows {
err = errors.Wrap(err, "InstalledOwnerBYMid")
return
}
err = nil
}
return
}
// CountOwnerBYNidMid retun number of medal_owner by mid and nid.
func (d *Dao) CountOwnerBYNidMid(c context.Context, mid, nid int64) (count int64, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_countOwnerBYNidMidSQL, d.hit(mid)), mid, nid)
if err = row.Scan(&count); err != nil {
if err != sql.ErrNoRows {
err = errors.Wrap(err, "CountOwnerBYNidMid")
return
}
count = 0
err = nil
}
return
}
// OwnerBYNidMid retun medal_owner by mid and nid.
func (d *Dao) OwnerBYNidMid(c context.Context, mid, nid int64) (res *model.MedalOwner, err error) {
res = &model.MedalOwner{}
row := d.db.QueryRow(c, fmt.Sprintf(_OwnerBYNidMidSQL, d.hit(mid)), mid, nid)
if err = row.Scan(&res.ID, &res.MID, &res.NID, &res.IsActivated, &res.CTime, &res.MTime); err != nil {
if err != sql.ErrNoRows {
err = errors.Wrap(err, "OwnerBYNidMid")
return
}
res = nil
err = nil
}
return
}
// MedalGroupAll retun all medal_group where is_online=1.
func (d *Dao) MedalGroupAll(c context.Context) (res []*model.MedalGroup, err error) {
rows, err := d.db.Query(c, _selGroupAllSQL)
if err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
info := new(model.MedalGroup)
if err = rows.Scan(&info.ID, &info.Name, &info.PID, &info.Rank); err != nil {
err = errors.WithStack(err)
return
}
res = append(res, info)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,183 @@
package medal
import (
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestMedalhit(t *testing.T) {
convey.Convey("hit", t, func(ctx convey.C) {
var (
id = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := d.hit(id)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalAddMedalOwner(t *testing.T) {
convey.Convey("AddMedalOwner", t, func(ctx convey.C) {
var (
mid = time.Now().Unix()
nid = int64(5)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.AddMedalOwner(c, mid, nid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalInstallMedalOwner(t *testing.T) {
convey.Convey("InstallMedalOwner", t, func(ctx convey.C) {
var (
mid = int64(0)
nid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.InstallMedalOwner(c, mid, nid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalUninstallMedalOwner(t *testing.T) {
convey.Convey("UninstallMedalOwner", t, func(ctx convey.C) {
var (
mid = int64(0)
nid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.UninstallMedalOwner(c, mid, nid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalUninstallAllMedalOwner(t *testing.T) {
convey.Convey("UninstallAllMedalOwner", t, func(ctx convey.C) {
var (
mid = int64(0)
nid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.UninstallAllMedalOwner(c, mid, nid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMedalMedalInfoAll(t *testing.T) {
convey.Convey("MedalInfoAll", t, func(ctx convey.C) {
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.MedalInfoAll(c)
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 TestMedalMedalOwnerByMid(t *testing.T) {
convey.Convey("MedalOwnerByMid", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.MedalOwnerByMid(c, mid)
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 TestMedalMedalInfoByNid(t *testing.T) {
convey.Convey("MedalInfoByNid", t, func(ctx convey.C) {
var (
nid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.MedalInfoByNid(c, nid)
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 TestMedalActivatedOwnerByMid(t *testing.T) {
convey.Convey("ActivatedOwnerByMid", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
nid, err := d.ActivatedOwnerByMid(c, mid)
ctx.Convey("Then err should be nil.nid should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(nid, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalCountOwnerBYNidMid(t *testing.T) {
convey.Convey("CountOwnerBYNidMid", t, func(ctx convey.C) {
var (
mid = int64(0)
nid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
count, err := d.CountOwnerBYNidMid(c, mid, nid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}
func TestMedalOwnerBYNidMid(t *testing.T) {
convey.Convey("OwnerBYNidMid", t, func(ctx convey.C) {
var (
mid = int64(32141)
nid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.OwnerBYNidMid(c, mid, nid)
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 TestMedalMedalGroupAll(t *testing.T) {
convey.Convey("MedalGroupAll", t, func(ctx convey.C) {
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.MedalGroupAll(c)
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)
})
})
})
}