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,63 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"cache.go",
"coupon.go",
"dao.go",
"mc.cache.go",
"redis.go",
],
importpath = "go-common/app/interface/main/activity/dao/bnj",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/activity/conf:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/cache/redis:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/stat/prom: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 = [
"cache_test.go",
"coupon_test.go",
"dao_test.go",
"mc.cache_test.go",
"redis_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/activity/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)

View File

@ -0,0 +1,21 @@
package bnj
import "context"
func timeFinishKey() string {
return "time_finish"
}
func timeLessKey() string {
return "time_less"
}
//go:generate $GOPATH/src/go-common/app/tool/cache/mc
type _mc interface {
// mc: -key=timeFinishKey
CacheTimeFinish(c context.Context) (int64, error)
// mc: -key=timeFinishKey
DelCacheTimeFinish(c context.Context) (int64, error)
// mc: -key=timeLessKey
DelCacheTimeLess(c context.Context) (int64, error)
}

View File

@ -0,0 +1,18 @@
package bnj
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestBnjtimeFinishKey(t *testing.T) {
convey.Convey("timeFinishKey", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := timeFinishKey()
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}

View File

@ -0,0 +1,47 @@
package bnj
import (
"context"
"encoding/json"
"net/http"
"strings"
"go-common/library/ecode"
"github.com/pkg/errors"
)
const _grantCouponURL = "/mall-marketing/coupon_code/create"
// GrantCoupon grant coupon to mid.
func (d *Dao) GrantCoupon(c context.Context, mid int64, couponID string) (err error) {
var (
bs []byte
req *http.Request
)
param := &struct {
Mid int64 `json:"mid"`
CouponID string `json:"couponId"`
}{
Mid: mid,
CouponID: couponID,
}
if bs, err = json.Marshal(param); err != nil {
return
}
if req, err = http.NewRequest(http.MethodPost, d.grantCouponURL, strings.NewReader(string(bs))); err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
var res struct {
Code int `json:"code"`
Msg string `json:"message"`
}
if err = d.client.Do(c, req, &res); err != nil {
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(res.Code), d.grantCouponURL+"msg:"+res.Msg)
}
return
}

View File

@ -0,0 +1,28 @@
package bnj
import (
"context"
"testing"
"gopkg.in/h2non/gock.v1"
"github.com/smartystreets/goconvey/convey"
)
func TestBnjGrantCoupon(t *testing.T) {
convey.Convey("GrantCoupon", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2080809)
couponID = "3d005e8ba01c5cb0"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("POST", d.grantCouponURL).Reply(200).JSON(`{"code":0}`)
err := d.GrantCoupon(c, mid, couponID)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@ -0,0 +1,45 @@
package bnj
import (
"time"
"go-common/app/interface/main/activity/conf"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
xhttp "go-common/library/net/http/blademaster"
)
// Dao bnj dao.
type Dao struct {
c *conf.Config
mc *memcache.Pool
redis *redis.Pool
client *xhttp.Client
resetExpire int32
rewardExpire int32
grantCouponURL string
}
// New init bnj dao.
func New(c *conf.Config) *Dao {
d := &Dao{
c: c,
mc: memcache.NewPool(c.Memcache.Like),
redis: redis.NewPool(c.Redis.Config),
client: xhttp.NewClient(c.HTTPClientBnj),
resetExpire: int32(time.Duration(c.Redis.ResetExpire) / time.Second),
rewardExpire: int32(time.Duration(c.Redis.RewardExpire) / time.Second),
}
d.grantCouponURL = d.c.Host.Mall + _grantCouponURL
return d
}
// Close .
func (d *Dao) Close() {
if d.mc != nil {
d.mc.Close()
}
if d.redis != nil {
d.redis.Close()
}
}

View File

@ -0,0 +1,45 @@
package bnj
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/interface/main/activity/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "dev" {
flag.Set("app_id", "main.web-svr.activity")
flag.Set("conf_token", "22edc93e2998bf0cb0bbee661b03d41f")
flag.Set("tree_id", "2873")
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/activity-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
d.client.SetTransport(gock.DefaultTransport)
os.Exit(m.Run())
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}

View File

@ -0,0 +1,94 @@
// Code generated by $GOPATH/src/go-common/app/tool/cache/mc. DO NOT EDIT.
/*
Package bnj is a generated mc cache package.
It is generated from:
type _mc interface {
// mc: -key=timeFinishKey
CacheTimeFinish(c context.Context) (int64, error)
// mc: -key=timeFinishKey
DelCacheTimeFinish(c context.Context) (int64, error)
// mc: -key=timeLessKey
DelCacheTimeLess(c context.Context) (int64, error)
}
*/
package bnj
import (
"context"
"fmt"
"strconv"
"go-common/library/cache/memcache"
"go-common/library/log"
"go-common/library/stat/prom"
)
var _ _mc
// CacheTimeFinish get data from mc
func (d *Dao) CacheTimeFinish(c context.Context) (res int64, err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := timeFinishKey()
reply, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:CacheTimeFinish")
log.Errorv(c, log.KV("CacheTimeFinish", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
var v string
err = conn.Scan(reply, &v)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheTimeFinish")
log.Errorv(c, log.KV("CacheTimeFinish", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
r, err := strconv.ParseInt(v, 10, 64)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheTimeFinish")
log.Errorv(c, log.KV("CacheTimeFinish", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
res = int64(r)
return
}
// DelCacheTimeFinish delete data from mc
func (d *Dao) DelCacheTimeFinish(c context.Context) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := timeFinishKey()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:DelCacheTimeFinish")
log.Errorv(c, log.KV("DelCacheTimeFinish", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// DelCacheTimeLess delete data from mc
func (d *Dao) DelCacheTimeLess(c context.Context) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := timeLessKey()
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:DelCacheTimeLess")
log.Errorv(c, log.KV("DelCacheTimeLess", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}

View File

@ -0,0 +1,51 @@
package bnj
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestBnjCacheTimeFinish(t *testing.T) {
convey.Convey("CacheTimeFinish", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.CacheTimeFinish(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 TestBnjDelCacheTimeFinish(t *testing.T) {
convey.Convey("DelCacheTimeFinish", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.DelCacheTimeFinish(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestBnjDelCacheTimeLess(t *testing.T) {
convey.Convey("DelCacheTimeLess", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.DelCacheTimeLess(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@ -0,0 +1,89 @@
package bnj
import (
"context"
"fmt"
"go-common/library/cache/redis"
"go-common/library/log"
)
func resetKey(mid int64) string {
return fmt.Sprintf("bnj_%d", mid)
}
func rewardKey(mid, subID int64, step int) string {
return fmt.Sprintf("bnj_rwd_%d_%d_%d", mid, subID, step)
}
// CacheResetCD .
func (d *Dao) CacheResetCD(c context.Context, mid int64, cd int32) (bool, error) {
resetCD := d.resetExpire
if cd > 0 {
resetCD = cd
}
return d.setNXLockCache(c, resetKey(mid), resetCD)
}
// TTLResetCD get reset cd ttl
func (d *Dao) TTLResetCD(c context.Context, mid int64) (ttl int64, err error) {
key := resetKey(mid)
conn := d.redis.Get(c)
defer conn.Close()
if ttl, err = redis.Int64(conn.Do("TTL", key)); err != nil {
log.Error("TTLResetCD conn.Do(TTL, %s), error(%v)", key, err)
}
return
}
// CacheHasReward .
func (d *Dao) CacheHasReward(c context.Context, mid, subID int64, step int) (bool, error) {
return d.setNXLockCache(c, rewardKey(mid, subID, step), d.rewardExpire)
}
// DelCacheHasReward .
func (d *Dao) DelCacheHasReward(c context.Context, mid, subID int64, step int) error {
return d.delNXLockCache(c, rewardKey(mid, subID, step))
}
// HasReward .
func (d *Dao) HasReward(c context.Context, mid, subID int64, step int) (res bool, err error) {
conn := d.redis.Get(c)
defer conn.Close()
key := rewardKey(mid, subID, step)
if res, err = redis.Bool(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
err = nil
return
}
log.Error("HasReward conn.Do(GET(%s)) error(%v)", key, err)
}
return
}
func (d *Dao) setNXLockCache(c context.Context, key string, times int32) (res bool, err error) {
conn := d.redis.Get(c)
defer conn.Close()
if res, err = redis.Bool(conn.Do("SETNX", key, "1")); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(SETNX(%s)) error(%v)", key, err)
return
}
}
if res {
if _, err = redis.Bool(conn.Do("EXPIRE", key, times)); err != nil {
log.Error("conn.Do(EXPIRE, %s, %d) error(%v)", key, times, err)
return
}
}
return
}
func (d *Dao) delNXLockCache(c context.Context, key string) (err error) {
conn := d.redis.Get(c)
defer conn.Close()
_, err = conn.Do("DEL", key)
return
}

View File

@ -0,0 +1,157 @@
package bnj
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestBnjresetKey(t *testing.T) {
convey.Convey("resetKey", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := resetKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestBnjrewardKey(t *testing.T) {
convey.Convey("rewardKey", t, func(ctx convey.C) {
var (
mid = int64(0)
subID = int64(0)
step = int(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := rewardKey(mid, subID, step)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestBnjCacheResetCD(t *testing.T) {
convey.Convey("CacheResetCD", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
expire = int32(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1, err := d.CacheResetCD(c, mid, expire)
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 TestBnjTTLResetCD(t *testing.T) {
convey.Convey("CacheTTLResetCD", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1, err := d.TTLResetCD(c, 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 TestBnjCacheHasReward(t *testing.T) {
convey.Convey("CacheHasReward", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
subID = int64(0)
step = int(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1, err := d.CacheHasReward(c, mid, subID, step)
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 TestBnjDelCacheHasReward(t *testing.T) {
convey.Convey("DelCacheHasReward", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
subID = int64(0)
step = int(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.DelCacheHasReward(c, mid, subID, step)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestBnjHasReward(t *testing.T) {
convey.Convey("HasReward", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(27515251)
subID = int64(0)
step = int(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.HasReward(c, mid, subID, step)
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)
ctx.Printf("%+v", res)
})
})
})
}
func TestBnjsetNXLockCache(t *testing.T) {
convey.Convey("setNXLockCache", t, func(ctx convey.C) {
var (
c = context.Background()
key = ""
times = int32(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.setNXLockCache(c, key, times)
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 TestBnjdelNXLockCache(t *testing.T) {
convey.Convey("delNXLockCache", t, func(ctx convey.C) {
var (
c = context.Background()
key = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.delNXLockCache(c, key)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}