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,65 @@
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",
"dao.cache.go",
"dao.go",
"kfc.go",
"mc.cache.go",
],
importpath = "go-common/app/interface/main/activity/dao/kfc",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/activity/conf:go_default_library",
"//app/interface/main/activity/model/kfc:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/net/metadata: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",
"dao.cache_test.go",
"dao_test.go",
"kfc_test.go",
"mc.cache_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/activity/conf:go_default_library",
"//app/interface/main/activity/model/kfc:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,36 @@
package kfc
import (
"context"
"fmt"
"go-common/app/interface/main/activity/model/kfc"
)
func kfcKey(id int64) string {
return fmt.Sprintf("b_kfc_c_%d", id)
}
func kfcCodeKey(code string) string {
return fmt.Sprintf("b_kfc_code_%s", code)
}
//go:generate $GOPATH/src/go-common/app/tool/cache/gen
type _cache interface {
// cache: -sync=true
KfcCoupon(c context.Context, id int64) (*kfc.BnjKfcCoupon, error)
}
//go:generate $GOPATH/src/go-common/app/tool/cache/mc
type _mc interface {
// mc: -key=kfcKey
CacheKfcCoupon(c context.Context, id int64) (*kfc.BnjKfcCoupon, error)
// mc: -key=kfcKey -expire=d.mcKfcExpire -encode=pb
AddCacheKfcCoupon(c context.Context, id int64, val *kfc.BnjKfcCoupon) error
// mc: -key=kfcKey
DelCacheKfcCoupon(c context.Context, id int64) error
// mc: -key=kfcCodeKey
CacheKfcCode(c context.Context, code string) (int64, error)
// mc: -key=kfcCodeKey -expire=d.mcKfcCodeExpire -encode=raw
AddCacheKfcCode(c context.Context, code string, val int64) error
}

View File

@@ -0,0 +1,35 @@
package kfc
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestKfckfcKey(t *testing.T) {
convey.Convey("kfcKey", t, func(convCtx convey.C) {
var (
id = int64(3)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := kfcKey(id)
convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestKfckfcCodeKey(t *testing.T) {
convey.Convey("kfcCodeKey", t, func(convCtx convey.C) {
var (
code = "201812041201"
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := kfcCodeKey(code)
convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,46 @@
// Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
/*
Package kfc is a generated cache proxy package.
It is generated from:
type _cache interface {
// cache: -sync=true
KfcCoupon(c context.Context, id int64) (*kfc.BnjKfcCoupon, error)
}
*/
package kfc
import (
"context"
"go-common/app/interface/main/activity/model/kfc"
"go-common/library/stat/prom"
)
var _ _cache
// KfcCoupon get data from cache if miss will call source method, then add to cache.
func (d *Dao) KfcCoupon(c context.Context, id int64) (res *kfc.BnjKfcCoupon, err error) {
addCache := true
res, err = d.CacheKfcCoupon(c, id)
if err != nil {
addCache = false
err = nil
}
if res != nil {
prom.CacheHit.Incr("KfcCoupon")
return
}
prom.CacheMiss.Incr("KfcCoupon")
res, err = d.RawKfcCoupon(c, id)
if err != nil {
return
}
miss := res
if !addCache {
return
}
d.AddCacheKfcCoupon(c, id, miss)
return
}

View File

@@ -0,0 +1,24 @@
package kfc
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestKfcKfcCoupon(t *testing.T) {
convey.Convey("KfcCoupon", t, func(convCtx convey.C) {
var (
c = context.Background()
id = int64(3)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.KfcCoupon(c, id)
convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,55 @@
package kfc
import (
"time"
"go-common/app/interface/main/activity/conf"
"go-common/library/cache/memcache"
xsql "go-common/library/database/sql"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
"go-common/library/stat/prom"
)
const (
_kfcWinnerURI = "/gift/v4/Smalltv/getKfcWinnerById"
)
// PromError stat and log.
func PromError(name string, format string, args ...interface{}) {
prom.BusinessErrCount.Incr(name)
log.Error(format, args...)
}
// Dao dao.
type Dao struct {
mc *memcache.Pool
db *xsql.DB
client *httpx.Client
mcKfcExpire int32
mcKfcCodeExpire int32
kfcWinnerURL string
}
// New dao new.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
mc: memcache.NewPool(c.Memcache.Like),
db: xsql.NewMySQL(c.MySQL.Like),
client: httpx.NewClient(c.HTTPClientKfc),
mcKfcExpire: int32(time.Duration(c.Memcache.KfcExpire) / time.Second),
mcKfcCodeExpire: int32(time.Duration(c.Memcache.KfcCodeExpire) / time.Second),
kfcWinnerURL: c.Host.LiveCo + _kfcWinnerURI,
}
return
}
// Close Dao
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
if d.mc != nil {
d.mc.Close()
}
}

View File

@@ -0,0 +1,35 @@
package kfc
import (
"flag"
"os"
"testing"
"go-common/app/interface/main/activity/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
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)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,99 @@
package kfc
import (
"context"
"database/sql"
"net/url"
"strconv"
"go-common/app/interface/main/activity/model/kfc"
xsql "go-common/library/database/sql"
"go-common/library/ecode"
"go-common/library/net/metadata"
"github.com/pkg/errors"
)
// KfcCodeUsed kfcDao const.
var (
KfcCodeUsed = 1
KfcCodeNotGiveOut = 0
_kfcCouponSQL = "select `id`,`mid`,`ctime`,`mtime`,`coupon_code`,`desc`,`state`,`delete_time` from bnj_kfc_coupon where id = ?"
_kfcCodeSQL = "select `id`,`mid`,`ctime`,`mtime`,`coupon_code`,`desc`,`state`,`delete_time` from bnj_kfc_coupon where coupon_code = ?"
_kfcCodeGiveOuteSQL = "update `bnj_kfc_coupon` set `state` = ? where `id` = ? and `state` = ?"
_kfcDeliverSQL = "update `bnj_kfc_coupon` set mid = ? where id = ? and mid = 0"
)
// RawKfcCoupon get coupon .
func (d *Dao) RawKfcCoupon(c context.Context, id int64) (res *kfc.BnjKfcCoupon, err error) {
res = &kfc.BnjKfcCoupon{}
row := d.db.QueryRow(c, _kfcCouponSQL, id)
if err = row.Scan(&res.ID, &res.Mid, &res.Ctime, &res.Mtime, &res.CouponCode, &res.Desc, &res.State, &res.DeleteTime); err != nil {
if err == xsql.ErrNoRows {
err = nil
} else {
err = errors.Wrap(err, "RawKfcCoupon:row.Scan()")
}
}
return
}
// RawKfcCode .
func (d *Dao) RawKfcCode(c context.Context, code string) (res *kfc.BnjKfcCoupon, err error) {
res = &kfc.BnjKfcCoupon{}
row := d.db.QueryRow(c, _kfcCodeSQL, code)
if err = row.Scan(&res.ID, &res.Mid, &res.Ctime, &res.Mtime, &res.CouponCode, &res.Desc, &res.State, &res.DeleteTime); err != nil {
if err == xsql.ErrNoRows {
err = nil
} else {
err = errors.Wrap(err, "RawKfcCode:row.Scan()")
}
}
return
}
// KfcCodeGiveOut .
func (d *Dao) KfcCodeGiveOut(c context.Context, id int64) (res int64, err error) {
var (
sqlRes sql.Result
)
if sqlRes, err = d.db.Exec(c, _kfcCodeGiveOuteSQL, KfcCodeUsed, id, KfcCodeNotGiveOut); err != nil {
err = errors.Wrap(err, "d.db.Exec()")
return
}
return sqlRes.RowsAffected()
}
// KfcDeliver .
func (d *Dao) KfcDeliver(c context.Context, id, mid int64) (res int64, err error) {
var (
sqlRes sql.Result
)
if sqlRes, err = d.db.Exec(c, _kfcDeliverSQL, mid, id); err != nil {
err = errors.Wrap(err, "d.db.Exec()")
return
}
return sqlRes.RowsAffected()
}
// KfcWinner .
func (d *Dao) KfcWinner(c context.Context, id int64) (uid int64, err error) {
params := url.Values{}
params.Set("id", strconv.FormatInt(id, 10))
var res struct {
Code int `json:"code"`
Data struct {
UID int64 `json:"uid"`
} `json:"data"`
}
if err = d.client.Get(c, d.kfcWinnerURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
err = errors.Wrapf(err, "d.client.Get(%s)", d.kfcWinnerURL+"?"+params.Encode())
return
}
if res.Code != ecode.OK.Code() {
err = ecode.Int(res.Code)
return
}
uid = res.Data.UID
return
}

View File

@@ -0,0 +1,73 @@
package kfc
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestKfcRawKfcCoupon(t *testing.T) {
convey.Convey("RawKfcCoupon", t, func(convCtx convey.C) {
var (
c = context.Background()
id = int64(4)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.RawKfcCoupon(c, id)
convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestKfcRawKfcCode(t *testing.T) {
convey.Convey("RawKfcCode", t, func(convCtx convey.C) {
var (
c = context.Background()
code = "201812041203"
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.RawKfcCode(c, code)
convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestKfcKfcCodeGiveOut(t *testing.T) {
convey.Convey("KfcCodeGiveOut", t, func(convCtx convey.C) {
var (
c = context.Background()
id = int64(3)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.KfcCodeGiveOut(c, id)
convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestKfcDeliver(t *testing.T) {
convey.Convey("KfcCodeGiveOut", t, func(convCtx convey.C) {
var (
c = context.Background()
id = int64(3)
mid = int64(5874874)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.KfcDeliver(c, id, mid)
convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,139 @@
// Code generated by $GOPATH/src/go-common/app/tool/cache/mc. DO NOT EDIT.
/*
Package kfc is a generated mc cache package.
It is generated from:
type _mc interface {
// mc: -key=kfcKey
CacheKfcCoupon(c context.Context, id int64) (*kfc.BnjKfcCoupon, error)
// mc: -key=kfcKey -expire=d.mcKfcExpire -encode=pb
AddCacheKfcCoupon(c context.Context, id int64, val *kfc.BnjKfcCoupon) error
// mc: -key=kfcKey
DelCacheKfcCoupon(c context.Context, id int64) error
// mc: -key=kfcCodeKey
CacheKfcCode(c context.Context, code string) (int64, error)
// mc: -key=kfcCodeKey -expire=d.mcKfcCodeExpire -encode=raw
AddCacheKfcCode(c context.Context, code string, val int64) error
}
*/
package kfc
import (
"context"
"fmt"
"strconv"
"go-common/app/interface/main/activity/model/kfc"
"go-common/library/cache/memcache"
"go-common/library/log"
"go-common/library/stat/prom"
)
var _ _mc
// CacheKfcCoupon get data from mc
func (d *Dao) CacheKfcCoupon(c context.Context, id int64) (res *kfc.BnjKfcCoupon, err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := kfcKey(id)
reply, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:CacheKfcCoupon")
log.Errorv(c, log.KV("CacheKfcCoupon", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
res = &kfc.BnjKfcCoupon{}
err = conn.Scan(reply, res)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheKfcCoupon")
log.Errorv(c, log.KV("CacheKfcCoupon", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// AddCacheKfcCoupon Set data to mc
func (d *Dao) AddCacheKfcCoupon(c context.Context, id int64, val *kfc.BnjKfcCoupon) (err error) {
if val == nil {
return
}
conn := d.mc.Get(c)
defer conn.Close()
key := kfcKey(id)
item := &memcache.Item{Key: key, Object: val, Expiration: d.mcKfcExpire, Flags: memcache.FlagProtobuf}
if err = conn.Set(item); err != nil {
prom.BusinessErrCount.Incr("mc:AddCacheKfcCoupon")
log.Errorv(c, log.KV("AddCacheKfcCoupon", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// DelCacheKfcCoupon delete data from mc
func (d *Dao) DelCacheKfcCoupon(c context.Context, id int64) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := kfcKey(id)
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:DelCacheKfcCoupon")
log.Errorv(c, log.KV("DelCacheKfcCoupon", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// CacheKfcCode get data from mc
func (d *Dao) CacheKfcCode(c context.Context, id string) (res int64, err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := kfcCodeKey(id)
reply, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:CacheKfcCode")
log.Errorv(c, log.KV("CacheKfcCode", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
var v string
err = conn.Scan(reply, &v)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheKfcCode")
log.Errorv(c, log.KV("CacheKfcCode", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
r, err := strconv.ParseInt(v, 10, 64)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheKfcCode")
log.Errorv(c, log.KV("CacheKfcCode", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
res = int64(r)
return
}
// AddCacheKfcCode Set data to mc
func (d *Dao) AddCacheKfcCode(c context.Context, id string, val int64) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := kfcCodeKey(id)
bs := []byte(strconv.FormatInt(int64(val), 10))
item := &memcache.Item{Key: key, Value: bs, Expiration: d.mcKfcCodeExpire, Flags: memcache.FlagRAW}
if err = conn.Set(item); err != nil {
prom.BusinessErrCount.Incr("mc:AddCacheKfcCode")
log.Errorv(c, log.KV("AddCacheKfcCode", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}

View File

@@ -0,0 +1,89 @@
package kfc
import (
"context"
"testing"
"go-common/app/interface/main/activity/model/kfc"
"github.com/smartystreets/goconvey/convey"
)
func TestKfcCacheKfcCoupon(t *testing.T) {
convey.Convey("CacheKfcCoupon", t, func(convCtx convey.C) {
var (
c = context.Background()
id = int64(3)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.CacheKfcCoupon(c, id)
convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestKfcAddCacheKfcCoupon(t *testing.T) {
convey.Convey("AddCacheKfcCoupon", t, func(convCtx convey.C) {
var (
c = context.Background()
id = int64(3)
val = &kfc.BnjKfcCoupon{ID: 3, CouponCode: "201812041201", Mid: 2089809}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.AddCacheKfcCoupon(c, id, val)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestKfcDelCacheKfcCoupon(t *testing.T) {
convey.Convey("DelCacheKfcCoupon", t, func(convCtx convey.C) {
var (
c = context.Background()
id = int64(3)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.DelCacheKfcCoupon(c, id)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestKfcCacheKfcCode(t *testing.T) {
convey.Convey("CacheKfcCode", t, func(convCtx convey.C) {
var (
c = context.Background()
id = "201812041201"
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.CacheKfcCode(c, id)
convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestKfcAddCacheKfcCode(t *testing.T) {
convey.Convey("AddCacheKfcCode", t, func(convCtx convey.C) {
var (
c = context.Background()
id = "201812041201"
val = int64(3)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.AddCacheKfcCode(c, id, val)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}