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,60 @@
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",
"memcache_test.go",
"mysql_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/point/conf:go_default_library",
"//app/service/main/point/model:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"memcache.go",
"mysql.go",
],
importpath = "go-common/app/service/main/point/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/point/conf:go_default_library",
"//app/service/main/point/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql:go_default_library",
"//library/stat/prom:go_default_library",
"//library/time: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,55 @@
package dao
import (
"context"
"time"
"go-common/app/service/main/point/conf"
"go-common/library/cache/memcache"
xsql "go-common/library/database/sql"
"go-common/library/stat/prom"
)
// Dao dao
type Dao struct {
c *conf.Config
mc *memcache.Pool
db *xsql.DB
mcExpire int32
errProm *prom.Prom
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
mc: memcache.NewPool(c.Memcache.Config),
mcExpire: int32(time.Duration(c.Memcache.Expire) / time.Second),
db: xsql.NewMySQL(c.MySQL),
errProm: prom.BusinessErrCount,
}
return
}
// Close close the resource.
func (dao *Dao) Close() {
dao.mc.Close()
dao.db.Close()
}
// Ping dao ping
func (dao *Dao) Ping(c context.Context) (err error) {
if err = dao.db.Ping(c); err != nil {
return
}
err = dao.pingMC(c)
return
}
// pingMc ping
func (dao *Dao) pingMC(c context.Context) (err error) {
conn := dao.mc.Get(c)
defer conn.Close()
item := memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 60}
return conn.Set(&item)
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"flag"
"os"
"testing"
"go-common/app/service/main/point/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.point-service")
flag.Set("conf_token", "0b280c9b45b2a8c31463b29fa4612f4e")
flag.Set("tree_id", "21519")
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/point-service.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,84 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/main/point/model"
gmc "go-common/library/cache/memcache"
"github.com/pkg/errors"
)
const (
_pointInfo = "pti:%d"
)
func pointKey(mid int64) string {
return fmt.Sprintf(_pointInfo, mid)
}
//DelPointInfoCache .
func (d *Dao) DelPointInfoCache(c context.Context, mid int64) (err error) {
return d.delCache(c, pointKey(mid))
}
// PointInfoCache .
func (d *Dao) PointInfoCache(c context.Context, mid int64) (pi *model.PointInfo, err error) {
var (
item *gmc.Item
)
key := pointKey(mid)
conn := d.mc.Get(c)
defer conn.Close()
if item, err = conn.Get(key); err != nil {
if err == gmc.ErrNotFound {
err = nil
pi = nil
return
}
err = errors.Wrapf(err, "d.PointInfoCache(%d)", mid)
d.errProm.Incr("get_mc")
return
}
pi = new(model.PointInfo)
if err = conn.Scan(item, pi); err != nil {
err = errors.Wrapf(err, "conn.Scan(%d)", pi.Mid)
d.errProm.Incr("scan_mc")
}
return
}
// SetPointInfoCache .
func (d *Dao) SetPointInfoCache(c context.Context, pi *model.PointInfo) (err error) {
key := pointKey(pi.Mid)
conn := d.mc.Get(c)
defer conn.Close()
item := &gmc.Item{
Key: key,
Object: pi,
Expiration: d.mcExpire,
Flags: gmc.FlagJSON,
}
if err = conn.Set(item); err != nil {
err = errors.Wrapf(err, "d.SetPointInfoCache(%d)", pi.Mid)
d.errProm.Incr("set_mc")
return
}
return
}
// DelCache del cache.
func (d *Dao) delCache(c context.Context, key string) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == gmc.ErrNotFound {
err = nil
} else {
err = errors.Wrapf(err, "conn.Delete(%s)", key)
d.errProm.Incr("del_mc")
}
}
return
}

View File

@@ -0,0 +1,84 @@
package dao
import (
"context"
"testing"
"go-common/app/service/main/point/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaopointKey(t *testing.T) {
convey.Convey("pointKey", t, func(ctx convey.C) {
var (
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := pointKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDelPointInfoCache(t *testing.T) {
convey.Convey("DelPointInfoCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelPointInfoCache(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoPointInfoCache(t *testing.T) {
convey.Convey("PointInfoCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.PointInfoCache(c, mid)
ctx.Convey("Then err should be nil.pi should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetPointInfoCache(t *testing.T) {
convey.Convey("SetPointInfoCache", t, func(ctx convey.C) {
var (
c = context.Background()
pi = &model.PointInfo{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetPointInfoCache(c, pi)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaodelCache(t *testing.T) {
convey.Convey("delCache", t, func(ctx convey.C) {
var (
c = context.Background()
key = "1"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.delCache(c, key)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,211 @@
package dao
import (
"context"
xsql "database/sql"
"go-common/app/service/main/point/model"
"go-common/library/database/sql"
xtime "go-common/library/time"
"github.com/pkg/errors"
)
const (
// point sql
_pointInfoSQL = "SELECT mid,point_balance,ver FROM point_info WHERE mid=?"
_pointHistorySQL = "SELECT id,mid,point,order_id,change_type,change_time,relation_id,point_balance,remark,operator FROM point_change_history WHERE mid = ? AND id < ? ORDER BY id DESC LIMIT ?"
_pointHistoryCountSQL = "SELECT COUNT(1) FROM point_change_history WHERE mid = ?"
_updatePointSQL = "UPDATE point_info SET point_balance = ?,ver=? WHERE mid=? AND ver=?"
_insertPointSQL = "INSERT INTO point_info(mid,point_balance,ver) VALUES(?,?,?)"
_InsertPointHistorySQL = "INSERT INTO point_change_history(mid,point,order_id,change_type,change_time,relation_id,point_balance,remark,operator) VALUES(?,?,?,?,?,?,?,?,?)"
_pointHistoryCheckSQL = "SELECT id FROM point_change_history WHERE order_id=?"
_selPointHistorySQL = "SELECT id,mid,point,order_id,change_type,change_time,relation_id,point_balance,remark,operator FROM point_change_history WHERE mid = ? AND change_time>=? AND change_time <= ? "
_allPointConfig = "SELECT `id`,`app_id`,`point`,`operator`,`ctime`,`mtime` FROM `point_conf`;"
//TODO compatible vip point old gateway
_oldPointHistorySQL = "SELECT id,mid,point,order_id,change_type,change_time,relation_id,point_balance,remark,operator FROM point_change_history WHERE mid = ? ORDER BY id DESC LIMIT ?,?;"
)
// BeginTran begin transaction.
func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
return d.db.Begin(c)
}
// PointInfo .
func (d *Dao) PointInfo(c context.Context, mid int64) (pi *model.PointInfo, err error) {
row := d.db.QueryRow(c, _pointInfoSQL, mid)
pi = new(model.PointInfo)
if err = row.Scan(&pi.Mid, &pi.PointBalance, &pi.Ver); err != nil {
if err == sql.ErrNoRows {
err = nil
pi = nil
} else {
err = errors.WithStack(err)
}
}
return
}
//TxPointInfo .
func (d *Dao) TxPointInfo(c context.Context, tx *sql.Tx, mid int64) (pi *model.PointInfo, err error) {
row := tx.QueryRow(_pointInfoSQL, mid)
pi = new(model.PointInfo)
if err = row.Scan(&pi.Mid, &pi.PointBalance, &pi.Ver); err != nil {
if err == sql.ErrNoRows {
err = nil
pi = nil
} else {
err = errors.WithStack(err)
}
}
return
}
//PointHistory point history
func (d *Dao) PointHistory(c context.Context, mid int64, cursor int, ps int) (phs []*model.PointHistory, err error) {
var rows *sql.Rows
if rows, err = d.db.Query(c, _pointHistorySQL, mid, cursor, ps); err != nil {
err = errors.WithStack(err)
return
}
for rows.Next() {
ph := new(model.PointHistory)
if err = rows.Scan(&ph.ID, &ph.Mid, &ph.Point, &ph.OrderID, &ph.ChangeType, &ph.ChangeTime, &ph.RelationID, &ph.PointBalance, &ph.Remark, &ph.Operator); err != nil {
phs = nil
err = errors.WithStack(err)
return
}
phs = append(phs, ph)
}
return
}
//PointHistoryCount point history
func (d *Dao) PointHistoryCount(c context.Context, mid int64) (count int, err error) {
row := d.db.QueryRow(c, _pointHistoryCountSQL, mid)
if err = row.Scan(&count); err != nil {
err = errors.WithStack(err)
}
return
}
//UpdatePointInfo .
func (d *Dao) UpdatePointInfo(c context.Context, tx *sql.Tx, pi *model.PointInfo, ver int64) (a int64, err error) {
var res xsql.Result
if res, err = tx.Exec(_updatePointSQL, pi.PointBalance, pi.Ver, pi.Mid, ver); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
return
}
return
}
//InsertPoint .
func (d *Dao) InsertPoint(c context.Context, tx *sql.Tx, pi *model.PointInfo) (a int64, err error) {
var res xsql.Result
if res, err = tx.Exec(_insertPointSQL, pi.Mid, pi.PointBalance, pi.Ver); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
return
}
return
}
//InsertPointHistory .
func (d *Dao) InsertPointHistory(c context.Context, tx *sql.Tx, ph *model.PointHistory) (a int64, err error) {
var res xsql.Result
if res, err = tx.Exec(_InsertPointHistorySQL, ph.Mid, ph.Point, ph.OrderID, ph.ChangeType, ph.ChangeTime, ph.RelationID, ph.PointBalance, ph.Remark, ph.Operator); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
}
return
}
//SelPointHistory .
func (d *Dao) SelPointHistory(c context.Context, mid int64, startDate, endDate xtime.Time) (phs []*model.PointHistory, err error) {
var rows *sql.Rows
if rows, err = d.db.Query(c, _selPointHistorySQL, mid, startDate, endDate); err != nil {
err = errors.WithStack(err)
return
}
for rows.Next() {
ph := new(model.PointHistory)
if err = rows.Scan(&ph.ID, &ph.Mid, &ph.Point, &ph.OrderID, &ph.ChangeType, &ph.ChangeTime, &ph.RelationID, &ph.PointBalance, &ph.Remark, &ph.Operator); err != nil {
phs = nil
err = errors.WithStack(err)
}
phs = append(phs, ph)
}
return
}
// ExistPointOrder check orderID is uniq or not
func (d *Dao) ExistPointOrder(c context.Context, orID string) (id int, err error) {
row := d.db.QueryRow(c, _pointHistoryCheckSQL, orID)
if err = row.Scan(&id); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
err = errors.WithStack(err)
return
}
return
}
// AllPointConfig all point config.
func (d *Dao) AllPointConfig(c context.Context) (res []*model.VipPointConf, err error) {
var rows *sql.Rows
if rows, err = d.db.Query(c, _allPointConfig); err != nil {
err = errors.WithStack(err)
return
}
for rows.Next() {
vf := new(model.VipPointConf)
if err = rows.Scan(&vf.ID, &vf.AppID, &vf.Point, &vf.Operator, &vf.Ctime, &vf.Mtime); err != nil {
res = nil
err = errors.WithStack(err)
}
res = append(res, vf)
}
return
}
//OldPointHistory point history.
func (d *Dao) OldPointHistory(c context.Context, mid int64, start int, ps int) (phs []*model.OldPointHistory, err error) {
var rows *sql.Rows
if rows, err = d.db.Query(c, _oldPointHistorySQL, mid, start, ps); err != nil {
err = errors.WithStack(err)
return
}
for rows.Next() {
ph := new(model.PointHistory)
if err = rows.Scan(&ph.ID, &ph.Mid, &ph.Point, &ph.OrderID, &ph.ChangeType, &ph.ChangeTime, &ph.RelationID, &ph.PointBalance, &ph.Remark, &ph.Operator); err != nil {
phs = nil
err = errors.WithStack(err)
return
}
oph := new(model.OldPointHistory)
oph.ID = ph.ID
oph.Mid = ph.Mid
oph.Point = ph.Point
oph.OrderID = ph.OrderID
oph.ChangeType = ph.ChangeType
oph.ChangeTime = ph.ChangeTime.Time().Unix()
oph.RelationID = ph.RelationID
oph.PointBalance = ph.PointBalance
oph.Remark = ph.Remark
oph.Operator = ph.Operator
phs = append(phs, oph)
}
return
}

View File

@@ -0,0 +1,214 @@
package dao
import (
"context"
"testing"
"time"
"go-common/app/service/main/point/model"
xtime "go-common/library/time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoBeginTran(t *testing.T) {
convey.Convey("BeginTran", t, func(ctx convey.C) {
var c = context.Background()
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1, err := d.BeginTran(c)
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 TestDaoPointInfo(t *testing.T) {
convey.Convey("PointInfo", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.PointInfo(c, mid)
ctx.Convey("Then err should be nil.pi should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTxPointInfo(t *testing.T) {
convey.Convey("TxPointInfo", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
tx, err := d.BeginTran(c)
ctx.So(err, convey.ShouldBeNil)
_, err = d.TxPointInfo(c, tx, mid)
ctx.Convey("Then err should be nil.pi should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoPointHistory(t *testing.T) {
convey.Convey("PointHistory", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
cursor = int(1)
ps = int(2)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.PointHistory(c, mid, cursor, ps)
ctx.Convey("Then err should be nil.phs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoPointHistoryCount(t *testing.T) {
convey.Convey("PointHistoryCount", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.PointHistoryCount(c, mid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoUpdatePointInfo(t *testing.T) {
convey.Convey("UpdatePointInfo", t, func(ctx convey.C) {
var (
c = context.Background()
pi = &model.PointInfo{}
ver = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
tx, err := d.BeginTran(c)
ctx.So(err, convey.ShouldBeNil)
a, err := d.UpdatePointInfo(c, tx, pi, ver)
ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertPoint(t *testing.T) {
convey.Convey("InsertPoint", t, func(ctx convey.C) {
var (
c = context.Background()
pi = &model.PointInfo{
Mid: time.Now().Unix(),
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
tx, err := d.BeginTran(c)
ctx.So(err, convey.ShouldBeNil)
a, err := d.InsertPoint(c, tx, pi)
ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertPointHistory(t *testing.T) {
convey.Convey("InsertPointHistory", t, func(ctx convey.C) {
var (
c = context.Background()
ph = &model.PointHistory{
Mid: 1,
ChangeTime: xtime.Time(time.Now().Unix()),
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
tx, err := d.BeginTran(c)
ctx.So(err, convey.ShouldBeNil)
a, err := d.InsertPointHistory(c, tx, ph)
ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSelPointHistory(t *testing.T) {
convey.Convey("SelPointHistory", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
startDate xtime.Time
endDate xtime.Time
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.SelPointHistory(c, mid, startDate, endDate)
ctx.Convey("Then err should be nil.phs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoExistPointOrder(t *testing.T) {
convey.Convey("ExistPointOrder", t, func(ctx convey.C) {
var (
c = context.Background()
orID = "1"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
id, err := d.ExistPointOrder(c, orID)
ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAllPointConfig(t *testing.T) {
convey.Convey("AllPointConfig", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.AllPointConfig(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 TestDaoOldPointHistory(t *testing.T) {
convey.Convey("OldPointHistory", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
start = int(0)
ps = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.OldPointHistory(c, mid, start, ps)
ctx.Convey("Then err should be nil.phs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}