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,75 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.cache.go",
"dao.go",
"grpc.go",
"http.go",
"mc.cache.go",
"mysql.go",
"redis.go",
],
importpath = "go-common/app/service/main/ugcpay/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/main/archive/api:go_default_library",
"//app/service/main/ugcpay/conf:go_default_library",
"//app/service/main/ugcpay/model:go_default_library",
"//library/cache:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/cache/redis: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/stat/prom:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/golang.org/x/sync/singleflight: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 = [
"dao.cache_test.go",
"dao_test.go",
"grpc_test.go",
"http_test.go",
"mc.cache_test.go",
"mysql_test.go",
"redis_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/service/main/ugcpay/conf:go_default_library",
"//app/service/main/ugcpay/model:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,177 @@
// Code generated by $GOPATH/src/go-common/app/tool/cache/gen. DO NOT EDIT.
/*
Package dao is a generated cache proxy package.
It is generated from:
type _cache interface {
// cache: -nullcache=&model.Order{ID:-1} -check_null_code=$!=nil&&$.ID==-1
OrderUser(c context.Context, id int64) (*model.Order, error)
// cache: -nullcache=&model.Asset{ID:-1} -check_null_code=$!=nil&&$.ID==-1 -singleflight=true
Asset(c context.Context, oid int64, otype string, currency string) (*model.Asset, error)
// cache: -nullcache=&model.AggrIncomeUser{Total:-1} -check_null_code=$!=nil&&$.Total==-1
AggrIncomeUser(c context.Context, mid int64) (*model.AggrIncomeUser, error)
// cache: -nullcache=&model.AggrIncomeUserMonthly{TotalPayTimes:-1} -check_null_code=$!=nil&&$.TotalPayTimes==-1
AggrIncomeUserMonthly(c context.Context, mid int64, ver string) (*model.AggrIncomeUserMonthly, error)
}
*/
package dao
import (
"context"
"go-common/app/service/main/ugcpay/model"
"go-common/library/net/metadata"
"go-common/library/stat/prom"
"golang.org/x/sync/singleflight"
)
var _ _cache
var cacheSingleFlights = [1]*singleflight.Group{{}}
// OrderUser get data from cache if miss will call source method, then add to cache.
func (d *Dao) OrderUser(c context.Context, id string) (res *model.Order, err error) {
addCache := true
res, err = d.CacheOrderUser(c, id)
if err != nil {
addCache = false
err = nil
}
defer func() {
if res != nil && res.ID == -1 {
res = nil
}
}()
if res != nil {
prom.CacheHit.Incr("OrderUser")
return
}
prom.CacheMiss.Incr("OrderUser")
res, err = d.RawOrderUser(c, id)
if err != nil {
return
}
miss := res
if miss == nil {
miss = &model.Order{ID: -1}
}
if !addCache {
return
}
d.cache.Save(func() {
d.AddCacheOrderUser(metadata.WithContext(c), id, miss)
})
return
}
// Asset get data from cache if miss will call source method, then add to cache.
func (d *Dao) Asset(c context.Context, id int64, otype string, currency string) (res *model.Asset, err error) {
addCache := true
res, err = d.CacheAsset(c, id, otype, currency)
if err != nil {
addCache = false
err = nil
}
defer func() {
if res != nil && res.ID == -1 {
res = nil
}
}()
if res != nil {
prom.CacheHit.Incr("Asset")
return
}
var rr interface{}
sf := d.cacheSFAsset(id, otype, currency)
rr, err, _ = cacheSingleFlights[0].Do(sf, func() (r interface{}, e error) {
prom.CacheMiss.Incr("Asset")
r, e = d.RawAsset(c, id, otype, currency)
return
})
res = rr.(*model.Asset)
if err != nil {
return
}
miss := res
if miss == nil {
miss = &model.Asset{ID: -1}
}
if !addCache {
return
}
d.cache.Save(func() {
d.AddCacheAsset(metadata.WithContext(c), id, otype, currency, miss)
})
return
}
// // AggrIncomeUser get data from cache if miss will call source method, then add to cache.
// func (d *Dao) AggrIncomeUser(c context.Context, mid int64, currency string) (res *model.AggrIncomeUser, err error) {
// addCache := true
// res, err = d.CacheAggrIncomeUser(c, mid, currency)
// if err != nil {
// addCache = false
// err = nil
// }
// defer func() {
// if res != nil && res.MID == -1 {
// res = nil
// }
// }()
// if res != nil {
// prom.CacheHit.Incr("AggrIncomeUser")
// return
// }
// prom.CacheMiss.Incr("AggrIncomeUser")
// res, err = d.RawAggrIncomeUser(c, mid, currency)
// if err != nil {
// return
// }
// miss := res
// if miss == nil {
// miss = &model.AggrIncomeUser{MID: -1}
// }
// if !addCache {
// return
// }
// d.cache.Save(func() {
// d.AddCacheAggrIncomeUser(metadata.WithContext(c), mid, currency, miss)
// })
// return
// }
// // AggrIncomeUseAssetList get data from cache if miss will call source method, then add to cache.
// func (d *Dao) AggrIncomeUseAssetList(c context.Context, mid int64, currency string, ver int64) (res []*model.AggrIncomeUserAsset, err error) {
// addCache := true
// res, err = d.CacheAggrIncomeUserAssetList(c, mid, currency, ver)
// if err != nil {
// addCache = false
// err = nil
// }
// defer func() {
// if res != nil && len(res) == 0 {
// res = nil
// }
// }()
// if res != nil {
// prom.CacheHit.Incr("AggrIncomeUseAssetList")
// return
// }
// prom.CacheMiss.Incr("AggrIncomeUseAssetList")
// res, err = d.RawAggrIncomeUserAssetList(c, mid, currency, ver)
// if err != nil {
// return
// }
// miss := res
// if miss == nil {
// miss = make([]*model.AggrIncomeUserAsset, 0)
// }
// if !addCache {
// return
// }
// d.cache.Save(func() {
// d.AddCacheAggrIncomeUserAssetList(metadata.WithContext(c), mid, currency, ver, miss)
// })
// return
// }

View File

@@ -0,0 +1,79 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoOrderUser(t *testing.T) {
convey.Convey("OrderUser", t, func(ctx convey.C) {
var (
c = context.Background()
id = "56272730181109202542"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.OrderUser(c, id)
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 TestDaoAsset(t *testing.T) {
convey.Convey("Asset", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(10110745)
otype = "archive"
currency = "bp"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelCacheAsset(c, id, otype, currency)
convey.So(err, convey.ShouldBeNil)
res, err := d.Asset(c, id, otype, currency)
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 TestDaoAggrIncomeUser(t *testing.T) {
// convey.Convey("AggrIncomeUser", t, func(ctx convey.C) {
// var (
// c = context.Background()
// mid = int64(46333)
// cur = "bp"
// )
// ctx.Convey("When everything goes positive", func(ctx convey.C) {
// res, err := d.AggrIncomeUser(c, mid, cur)
// 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 TestDaoAggrIncomeUserMonthly(t *testing.T) {
// convey.Convey("AggrIncomeUserMonthly", t, func(ctx convey.C) {
// var (
// c = context.Background()
// id = int64(46333)
// ver = int64(201810)
// cur = "bp"
// )
// ctx.Convey("When everything goes positive", func(ctx convey.C) {
// res, err := d.AggrIncomeUseAssetList(c, id, cur, ver)
// 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,89 @@
package dao
import (
"context"
"fmt"
arcGRPC "go-common/app/service/main/archive/api"
"go-common/app/service/main/ugcpay/conf"
"go-common/app/service/main/ugcpay/model"
"go-common/library/cache"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
xsql "go-common/library/database/sql"
)
// Dao dao
type Dao struct {
c *conf.Config
mc *memcache.Pool
redis *redis.Pool
db *xsql.DB
cache *cache.Cache
archiveAPI arcGRPC.ArchiveClient
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
mc: memcache.NewPool(c.Memcache),
redis: redis.NewPool(c.Redis),
db: xsql.NewMySQL(c.MySQL),
cache: cache.New(10, 10240),
}
var err error
if dao.archiveAPI, err = arcGRPC.NewClient(nil); err != nil {
panic(err)
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.mc.Close()
d.redis.Close()
d.db.Close()
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) error {
return d.db.Ping(c)
}
func orderKey(id string) string {
return fmt.Sprintf("up_o_%s", id)
}
func assetKey(oid int64, otype string, currency string) string {
return fmt.Sprintf("up_a_%d_%s_%s", oid, otype, currency)
}
func (d *Dao) cacheSFAsset(oid int64, otype string, currency string) string {
return fmt.Sprintf("up_a_sf_%d_%s_%s", oid, otype, currency)
}
//go:generate $GOPATH/src/go-common/app/tool/cache/mc
type _mc interface {
//mc: -key=orderKey -type=get
CacheOrderUser(c context.Context, id int64) (*model.Order, error)
//mc: -key=orderKey -expire=d.cacheTTL.OrderTTL
AddCacheOrderUser(c context.Context, id int64, value *model.Order) error
//mc: -key=orderKey
DelCacheOrderUser(c context.Context, id int64) error
//mc: -key=assetKey -type=get
CacheAsset(c context.Context, oid int64, otype string, currency string) (*model.Asset, error)
//mc: -key=assetKey -expire=d.cacheTTL.AssetTTL
AddCacheAsset(c context.Context, oid int64, otype string, currency string, value *model.Asset) error
//mc: -key=assetKey
DelCacheAsset(c context.Context, oid int64, otype string, currency string) error
}
//go:generate $GOPATH/src/go-common/app/tool/cache/gen
type _cache interface {
// cache: -nullcache=&model.Order{ID:-1} -check_null_code=$!=nil&&$.ID==-1
OrderUser(c context.Context, id int64) (*model.Order, error)
// cache: -nullcache=&model.Asset{ID:-1} -check_null_code=$!=nil&&$.ID==-1 -singleflight=true
Asset(c context.Context, oid int64, otype string, currency string) (*model.Asset, error)
}

View File

@@ -0,0 +1,43 @@
package dao
import (
"context"
"flag"
"go-common/app/service/main/ugcpay/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.ugcpay-service")
flag.Set("conf_token", "a3e98fbfb0f63520a8cdc699365460c2")
flag.Set("tree_id", "62221")
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/test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
conf.Conf.CacheTTL.OrderTTL = 1
conf.Conf.CacheTTL.AssetTTL = 1
conf.Conf.CacheTTL.AssetRelationTTL = 1
conf.Conf.CacheTTL.AggrIncomeUserTTL = 1
conf.Conf.CacheTTL.AggrIncomeUserMonthlyTTL = 1
if err := d.Ping(context.Background()); err != nil {
panic(err)
}
os.Exit(m.Run())
}

View File

@@ -0,0 +1,32 @@
package dao
import (
"context"
arc "go-common/app/service/main/archive/api"
"go-common/library/ecode"
)
// ArchiveUGCPay get archive ugcpay flag.
func (d *Dao) ArchiveUGCPay(ctx context.Context, aid int64) (pay bool, err error) {
var (
req = &arc.ArcRequest{
Aid: aid,
}
reply *arc.ArcReply
)
if reply, err = d.archiveAPI.Arc(ctx, req); err != nil {
if err == ecode.NothingFound {
err = nil
pay = false
return
}
return
}
if reply != nil && reply.Arc != nil && reply.Arc.Rights.UGCPay == 1 {
pay = true
} else {
pay = false
}
return
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoArchiveUGCPay(t *testing.T) {
convey.Convey("ArchiveUGCPay", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(2333)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.ArchiveUGCPay(c, aid)
ctx.Convey("Then err should be nil.pay should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
// ctx.So(pay, convey.ShouldEqual, false)
})
})
})
}

View File

@@ -0,0 +1,111 @@
package dao
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
"go-common/app/service/main/ugcpay/conf"
"go-common/app/service/main/ugcpay/model"
"go-common/library/ecode"
"go-common/library/log"
"github.com/pkg/errors"
)
// PayRefund 调用支付平台退款接口
func (d *Dao) PayRefund(c context.Context, dataJSON string) (err error) {
resp := new(struct {
Code int `json:"errno"`
Msg string `json:"msg"`
})
if err = d.paySend(c, conf.Conf.Biz.Pay.URLRefund, dataJSON, resp); err != nil {
err = errors.WithStack(err)
return
}
if resp.Code != ecode.OK.Code() {
err = ecode.Int(resp.Code)
}
return
}
// PayCancel 调用支付平台订单取消接口
func (d *Dao) PayCancel(c context.Context, dataJSON string) (err error) {
resp := new(struct {
Code int `json:"errno"`
Msg string `json:"msg"`
})
if err = d.paySend(c, conf.Conf.Biz.Pay.URLCancel, dataJSON, resp); err != nil {
err = errors.WithStack(err)
return
}
if resp.Code != ecode.OK.Code() {
err = ecode.Int(resp.Code)
}
return
}
// PayQuery 调用支付平台订单查询接口 return map[orderID]*model.PayOrder
func (d *Dao) PayQuery(c context.Context, dataJSON string) (orders map[string][]*model.PayOrder, err error) {
resp := new(struct {
Code int `json:"errno"`
Msg string `json:"msg"`
Data *model.PayQuery `json:"data"`
})
if err = d.paySend(c, conf.Conf.Biz.Pay.URLQuery, dataJSON, resp); err != nil {
err = errors.WithStack(err)
return
}
if resp.Code != ecode.OK.Code() {
err = ecode.Int(resp.Code)
return
}
if resp.Data == nil {
err = errors.Errorf("PayQuery got nil data, resp: %+v", resp)
return
}
orders = make(map[string][]*model.PayOrder)
for _, o := range resp.Data.Orders {
orders[o.OrderID] = append(orders[o.OrderID], o)
}
return
}
func (d *Dao) paySend(c context.Context, url string, jsonData string, respData interface{}) (err error) {
var (
req *http.Request
client = new(http.Client)
resp *http.Response
bs []byte
)
if req, err = http.NewRequest(http.MethodPost, url, strings.NewReader(jsonData)); err != nil {
err = errors.WithStack(err)
return
}
req.Header.Add("Content-Type", "application/json")
if resp, err = client.Do(req); err != nil {
err = errors.Wrapf(err, "call url: %s, body: %s", url, jsonData)
return
}
defer resp.Body.Close()
if resp.StatusCode >= http.StatusBadRequest {
err = errors.Errorf("d.paySend incorrect http status: %d, host: %s, url: %s", resp.StatusCode, req.URL.Host, req.URL.String())
return
}
if bs, err = ioutil.ReadAll(resp.Body); err != nil {
err = errors.Wrapf(err, "d.paySend ioutil.ReadAll")
return
}
log.Info("paySend call url: %s, body: %s, resp: %s", url, jsonData, bs)
if err = json.Unmarshal(bs, respData); err != nil {
err = errors.WithStack(err)
return
}
log.Info("paySend call url: %v, body: %s, resp: %+v", url, jsonData, respData)
return
}

View File

@@ -0,0 +1,55 @@
package dao
import (
"context"
"testing"
"go-common/library/ecode"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoPayRefund(t *testing.T) {
convey.Convey("PayRefund", t, func(ctx convey.C) {
var (
c = context.Background()
dataJSON = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.PayRefund(c, dataJSON)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.Int(8004020001))
})
})
})
}
func TestDaoPayCancel(t *testing.T) {
convey.Convey("PayCancel", t, func(ctx convey.C) {
var (
c = context.Background()
dataJSON = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.PayCancel(c, dataJSON)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, ecode.Int(8004020001))
})
})
})
}
func TestDaoPayQuery(t *testing.T) {
convey.Convey("PayQuery", t, func(ctx convey.C) {
var (
c = context.Background()
dataJSON = `{"customerId":"10017","orderIds":"77546846181122123422","sign":"860d970710eac87650f221d0e0db6940","signType":"MD5","timestamp":"1542882768000","traceId":"1542882768117855000","version":"1.0"}`
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.PayQuery(c, dataJSON)
ctx.Convey("Then err should be nil.orders should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,132 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/main/ugcpay/conf"
"go-common/app/service/main/ugcpay/model"
"go-common/library/cache/memcache"
"go-common/library/log"
"go-common/library/stat/prom"
)
var _ _mc
// CacheOrderUser get data from mc
func (d *Dao) CacheOrderUser(c context.Context, id string) (res *model.Order, err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := orderKey(id)
reply, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:CacheOrderUser")
log.Errorv(c, log.KV("CacheOrderUser", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
res = &model.Order{}
err = conn.Scan(reply, res)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheOrderUser")
log.Errorv(c, log.KV("CacheOrderUser", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// AddCacheOrderUser Set data to mc
func (d *Dao) AddCacheOrderUser(c context.Context, id string, val *model.Order) (err error) {
if val == nil {
return
}
conn := d.mc.Get(c)
defer conn.Close()
key := orderKey(id)
item := &memcache.Item{Key: key, Object: val, Expiration: conf.Conf.CacheTTL.OrderTTL, Flags: memcache.FlagJSON}
if err = conn.Set(item); err != nil {
prom.BusinessErrCount.Incr("mc:AddCacheOrderUser")
log.Errorv(c, log.KV("AddCacheOrderUser", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// DelCacheOrderUser delete data from mc
func (d *Dao) DelCacheOrderUser(c context.Context, id string) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := orderKey(id)
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:DelCacheOrderUser")
log.Errorv(c, log.KV("DelCacheOrderUser", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// CacheAsset get data from mc
func (d *Dao) CacheAsset(c context.Context, id int64, otype string, currency string) (res *model.Asset, err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := assetKey(id, otype, currency)
reply, err := conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:CacheAsset")
log.Errorv(c, log.KV("CacheAsset", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
res = &model.Asset{}
err = conn.Scan(reply, res)
if err != nil {
prom.BusinessErrCount.Incr("mc:CacheAsset")
log.Errorv(c, log.KV("CacheAsset", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// AddCacheAsset Set data to mc
func (d *Dao) AddCacheAsset(c context.Context, id int64, otype string, currency string, value *model.Asset) (err error) {
if value == nil {
return
}
conn := d.mc.Get(c)
defer conn.Close()
key := assetKey(id, otype, currency)
item := &memcache.Item{Key: key, Object: value, Expiration: conf.Conf.CacheTTL.AssetTTL, Flags: memcache.FlagJSON}
if err = conn.Set(item); err != nil {
prom.BusinessErrCount.Incr("mc:AddCacheAsset")
log.Errorv(c, log.KV("AddCacheAsset", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}
// DelCacheAsset delete data from mc
func (d *Dao) DelCacheAsset(c context.Context, id int64, otype string, currency string) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
key := assetKey(id, otype, currency)
if err = conn.Delete(key); err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
prom.BusinessErrCount.Incr("mc:DelCacheAsset")
log.Errorv(c, log.KV("DelCacheAsset", fmt.Sprintf("%+v", err)), log.KV("key", key))
return
}
return
}

View File

@@ -0,0 +1,212 @@
package dao
import (
"context"
"go-common/app/service/main/ugcpay/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddCacheOrderUser(t *testing.T) {
convey.Convey("AddCacheOrderUser", t, func(ctx convey.C) {
var (
c = context.Background()
id = "test"
val = &model.Order{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.AddCacheOrderUser(c, id, val)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoCacheOrderUser(t *testing.T) {
convey.Convey("CacheOrderUser", t, func(ctx convey.C) {
var (
c = context.Background()
id = "test"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.CacheOrderUser(c, id)
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 TestDaoDelCacheOrderUser(t *testing.T) {
convey.Convey("DelCacheOrderUser", t, func(ctx convey.C) {
var (
c = context.Background()
id = "test"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelCacheOrderUser(c, id)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoAddCacheAsset(t *testing.T) {
convey.Convey("AddCacheAsset", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(233333)
otype = "archive"
currency = "bp"
value = &model.Asset{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.AddCacheAsset(c, id, otype, currency, value)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoCacheAsset(t *testing.T) {
convey.Convey("CacheAsset", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(233333)
otype = "archive"
currency = "bp"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.CacheAsset(c, id, otype, currency)
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 TestDaoDelCacheAsset(t *testing.T) {
convey.Convey("DelCacheAsset", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(233333)
otype = "archive"
currency = "bp"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelCacheAsset(c, id, otype, currency)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
// func TestDaoCacheAggrIncomeUser(t *testing.T) {
// convey.Convey("CacheAggrIncomeUser", t, func(ctx convey.C) {
// var (
// c = context.Background()
// id = int64(46333)
// cur = "bp"
// )
// ctx.Convey("When everything goes positive", func(ctx convey.C) {
// res, err := d.CacheAggrIncomeUser(c, id, cur)
// 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 TestDaoAddCacheAggrIncomeUser(t *testing.T) {
// convey.Convey("AddCacheAggrIncomeUser", t, func(ctx convey.C) {
// var (
// c = context.Background()
// id = int64(46333)
// val = &model.AggrIncomeUser{}
// cur = "bp"
// )
// ctx.Convey("When everything goes positive", func(ctx convey.C) {
// err := d.AddCacheAggrIncomeUser(c, id, cur, val)
// ctx.Convey("Then err should be nil.", func(ctx convey.C) {
// ctx.So(err, convey.ShouldBeNil)
// })
// })
// })
// }
// func TestDaoDelCacheAggrIncomeUser(t *testing.T) {
// convey.Convey("DelCacheAggrIncomeUser", t, func(ctx convey.C) {
// var (
// c = context.Background()
// id = int64(46333)
// cur = "bp"
// )
// ctx.Convey("When everything goes positive", func(ctx convey.C) {
// err := d.DelCacheAggrIncomeUser(c, id, cur)
// ctx.Convey("Then err should be nil.", func(ctx convey.C) {
// ctx.So(err, convey.ShouldBeNil)
// })
// })
// })
// }
// func TestDaoCacheAggrIncomeUserMonthly(t *testing.T) {
// convey.Convey("CacheAggrIncomeUserMonthly", t, func(ctx convey.C) {
// var (
// c = context.Background()
// id = int64(46333)
// ver = int64(123)
// cur = "bp"
// )
// ctx.Convey("When everything goes positive", func(ctx convey.C) {
// res, err := d.CacheAggrIncomeUserAssetList(c, id, cur, ver)
// 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 TestDaoAddCacheAggrIncomeUserMonthly(t *testing.T) {
// convey.Convey("AddCacheAggrIncomeUserMonthly", t, func(ctx convey.C) {
// var (
// c = context.Background()
// id = int64(46333)
// ver = int64(123)
// cur = "bp"
// value = []*model.AggrIncomeUserAsset{}
// )
// ctx.Convey("When everything goes positive", func(ctx convey.C) {
// err := d.AddCacheAggrIncomeUserAssetList(c, id, cur, ver, value)
// ctx.Convey("Then err should be nil.", func(ctx convey.C) {
// ctx.So(err, convey.ShouldBeNil)
// })
// })
// })
// }
// func TestDaoDelCacheAggrIncomeUserMonthly(t *testing.T) {
// convey.Convey("DelCacheAggrIncomeUserMonthly", t, func(ctx convey.C) {
// var (
// c = context.Background()
// id = int64(46333)
// ver = int64(123)
// cur = "bp"
// )
// ctx.Convey("When everything goes positive", func(ctx convey.C) {
// err := d.DelCacheAggrIncomeUserAssetList(c, id, cur, ver)
// ctx.Convey("Then err should be nil.", func(ctx convey.C) {
// ctx.So(err, convey.ShouldBeNil)
// })
// })
// })
// }

View File

@@ -0,0 +1,293 @@
package dao
import (
"context"
"database/sql"
"go-common/app/service/main/ugcpay/model"
xsql "go-common/library/database/sql"
)
var (
_selectOrderUser = `SELECT id,order_id,mid,biz,platform,oid,otype,fee,real_fee,currency,pay_id,pay_reason,pay_time,state,ctime,mtime,refund_time,version FROM order_user WHERE order_id=? LIMIT 1`
_insertOrderUser = "INSERT INTO order_user (order_id,mid,biz,platform,oid,otype,fee,real_fee,currency,pay_id,pay_reason,pay_time,state,refund_time,version) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
_updateOrderUser = "UPDATE order_user SET order_id=?,mid=?,biz=?,platform=?,oid=?,otype=?,fee=?,real_fee=?,currency=?,pay_id=?,pay_reason=?,pay_time=?,state=?,refund_time=?,version=version+1 WHERE order_id=? AND version=?"
_insertLogOrderUser = "INSERT INTO log_order_user (order_id,from_state,to_state,`desc`) VALUES (?,?,?,?)"
_selectAsset = "SELECT id,mid,oid,otype,currency,price,state,ctime,mtime FROM asset WHERE oid=? AND otype=? AND currency=? LIMIT 1"
_upsertAsset = "INSERT INTO asset (mid,oid,otype,currency,price,state) VALUES (?,?,?,?,?,?) ON DUPLICATE KEY UPDATE mid=?,price=?,state=?"
_selectAssetRelation = "SELECT id,oid,otype,mid,state,ctime,mtime FROM asset_relation WHERE oid=? AND otype=? AND mid=? LIMIT 1"
_upsertAssetRelation = "INSERT INTO asset_relation (oid,otype,mid,state) VALUES (?,?,?,?) ON DUPLICATE KEY UPDATE state=?"
_selectBillUserDaily = "SELECT id,mid,biz,currency,`in`,`out`,ver,ctime,mtime,version FROM bill_user_daily WHERE mid=? AND biz=? AND currency=? AND ver=? LIMIT 1"
_selectBillUserDailyListByMonthVer = "SELECT id,mid,biz,currency,`in`,`out`,ver,ctime,mtime,version FROM bill_user_daily WHERE mid=? AND biz=? AND currency=? AND month_ver=?"
_selectAggrIncomeUser = "SELECT id,mid,currency,pay_success,pay_error,total_in,total_out,ctime,mtime FROM aggr_income_user WHERE mid=? AND currency=? LIMIT 1"
_selectAggrIncomeAssetList = "SELECT id,mid,currency,ver,oid,otype,pay_success,pay_error,total_in,total_out,ctime,mtime FROM aggr_income_user_asset WHERE mid=? AND currency=? AND ver=? ORDER BY oid DESC LIMIT ?"
_selectAggrIncomeAsset = "SELECT id,mid,currency,ver,oid,otype,pay_success,pay_error,total_in,total_out,ctime,mtime FROM aggr_income_user_asset WHERE mid=? AND currency=? AND ver=? AND oid=? AND otype=? LIMIT 1"
_orderRechargeShell = "SELECT id,mid,order_id,biz,amount,pay_msg,state,`ver`,ctime,mtime FROM order_recharge_shell WHERE order_id=?"
_updateOrderRechargeShell = "UPDATE order_recharge_shell SET mid=?,order_id=?,biz=?,amount=?,pay_msg=?,state=?,`ver`=? WHERE order_id=?"
_insertOrderRechargeShellLog = "INSERT INTO log_order_recharge_shell (order_id,from_state,to_state,`desc`,bill_user_monthly_id) VALUES (?,?,?,?,?)"
)
// BeginTran begin transcation.
func (d *Dao) BeginTran(c context.Context) (tx *xsql.Tx, err error) {
return d.db.Begin(c)
}
// RawOrderUser get user order
func (d *Dao) RawOrderUser(ctx context.Context, id string) (data *model.Order, err error) {
data = &model.Order{}
row := d.db.Master().QueryRow(ctx, _selectOrderUser, id)
if err = row.Scan(&data.ID, &data.OrderID, &data.MID, &data.Biz, &data.Platform, &data.OID, &data.OType, &data.Fee, &data.RealFee, &data.Currency, &data.PayID, &data.PayReason, &data.PayTime, &data.State, &data.CTime, &data.MTime, &data.RefundTime, &data.Version); err != nil {
if err == xsql.ErrNoRows {
err = nil
data = nil
return
}
return
}
return
}
// InsertOrderUser is.
func (d *Dao) InsertOrderUser(ctx context.Context, data *model.Order) (id int64, err error) {
var (
res sql.Result
)
if res, err = d.db.Exec(ctx, _insertOrderUser, data.OrderID, data.MID, data.Biz, data.Platform, data.OID, data.OType, data.Fee, data.RealFee, data.Currency, data.PayID, data.PayReason, data.PayTime, data.State, data.RefundTime, data.Version); err != nil {
return
}
if id, err = res.LastInsertId(); err != nil {
return
}
return
}
// TXUpdateOrderUser .
func (d *Dao) TXUpdateOrderUser(ctx context.Context, tx *xsql.Tx, data *model.Order) (affected int64, err error) {
res, err := tx.Exec(_updateOrderUser, data.OrderID, data.MID, data.Biz, data.Platform, data.OID, data.OType, data.Fee, data.RealFee, data.Currency, data.PayID, data.PayReason, data.PayTime, data.State, data.RefundTime, data.OrderID, data.Version)
if err != nil {
return
}
affected, err = res.RowsAffected()
return
}
// TXInsertOrderUserLog .
func (d *Dao) TXInsertOrderUserLog(ctx context.Context, tx *xsql.Tx, data *model.LogOrder) (id int64, err error) {
var (
res sql.Result
)
if res, err = tx.Exec(_insertLogOrderUser, data.OrderID, data.FromState, data.ToState, data.Desc); err != nil {
return
}
if id, err = res.LastInsertId(); err != nil {
return
}
return
}
// RawAsset is
func (d *Dao) RawAsset(ctx context.Context, oid int64, otype string, currency string) (data *model.Asset, err error) {
data = &model.Asset{}
row := d.db.Master().QueryRow(ctx, _selectAsset, oid, otype, currency)
if err = row.Scan(&data.ID, &data.MID, &data.OID, &data.OType, &data.Currency, &data.Price, &data.State, &data.CTime, &data.MTime); err != nil {
if err == xsql.ErrNoRows {
err = nil
data = nil
return
}
return
}
return
}
// UpsertAsset is
func (d *Dao) UpsertAsset(ctx context.Context, data *model.Asset) (err error) {
if _, err = d.db.Exec(ctx, _upsertAsset, data.MID, data.OID, data.OType, data.Currency, data.Price, data.State, data.MID, data.Price, data.State); err != nil {
return
}
return
}
// RawAssetRelation is
func (d *Dao) RawAssetRelation(ctx context.Context, mid int64, oid int64, otype string) (data *model.AssetRelation, err error) {
data = &model.AssetRelation{}
row := d.db.Master().QueryRow(ctx, _selectAssetRelation, oid, otype, mid)
if err = row.Scan(&data.ID, &data.OID, &data.OType, &data.MID, &data.State, &data.CTime, &data.MTime); err != nil {
if err == xsql.ErrNoRows {
err = nil
data = nil
return
}
return
}
return
}
// UpsertAssetRelation is
func (d *Dao) UpsertAssetRelation(ctx context.Context, data *model.AssetRelation) (rows int64, err error) {
var (
result sql.Result
)
if result, err = d.db.Exec(ctx, _upsertAssetRelation, data.OID, data.OType, data.MID, data.State, data.State); err != nil {
return
}
if rows, err = result.RowsAffected(); err != nil {
return
}
return
}
// TXUpsertAssetRelation is
func (d *Dao) TXUpsertAssetRelation(ctx context.Context, tx *xsql.Tx, data *model.AssetRelation) (rows int64, err error) {
var (
result sql.Result
)
if result, err = tx.Exec(_upsertAssetRelation, data.OID, data.OType, data.MID, data.State, data.State); err != nil {
return
}
if rows, err = result.RowsAffected(); err != nil {
return
}
return
}
// RawAggrIncomeUser is.
func (d *Dao) RawAggrIncomeUser(ctx context.Context, mid int64, currency string) (data *model.AggrIncomeUser, err error) {
data = &model.AggrIncomeUser{}
row := d.db.QueryRow(ctx, _selectAggrIncomeUser, mid, currency)
if err = row.Scan(&data.ID, &data.MID, &data.Currency, &data.PaySuccess, &data.PayError, &data.TotalIn, &data.TotalOut, &data.CTime, &data.MTime); err != nil {
if err == xsql.ErrNoRows {
err = nil
data = nil
return
}
return
}
return
}
// RawAggrIncomeUserAssetList is.
func (d *Dao) RawAggrIncomeUserAssetList(ctx context.Context, mid int64, currency string, ver int64, limit int) (data []*model.AggrIncomeUserAsset, err error) {
var (
rows *xsql.Rows
)
if rows, err = d.db.Query(ctx, _selectAggrIncomeAssetList, mid, currency, ver, limit); err != nil {
return
}
defer rows.Close()
for rows.Next() {
var (
d = &model.AggrIncomeUserAsset{}
)
if err = rows.Scan(&d.ID, &d.MID, &d.Currency, &d.Ver, &d.OID, &d.OType, &d.PaySuccess, &d.PayError, &d.TotalIn, &d.TotalOut, &d.CTime, &d.MTime); err != nil {
return
}
data = append(data, d)
}
if err = rows.Err(); err != nil {
return
}
return
}
// RawAggrIncomeUserAsset .
func (d *Dao) RawAggrIncomeUserAsset(ctx context.Context, mid int64, currency string, oid int64, otype string, ver int64) (data *model.AggrIncomeUserAsset, err error) {
data = &model.AggrIncomeUserAsset{}
row := d.db.QueryRow(ctx, _selectAggrIncomeAsset, mid, currency, ver, oid, otype)
if err = row.Scan(&data.ID, &data.MID, &data.Currency, &data.Ver, &data.OID, &data.OType, &data.PaySuccess, &data.PayError, &data.TotalIn, &data.TotalOut, &data.CTime, &data.MTime); err != nil {
if err == xsql.ErrNoRows {
err = nil
data = nil
return
}
return
}
return
}
// RawBillUserDaily is.
func (d *Dao) RawBillUserDaily(ctx context.Context, mid int64, biz string, currency string, ver int64) (data *model.Bill, err error) {
data = &model.Bill{}
row := d.db.QueryRow(ctx, _selectBillUserDaily, mid, biz, currency, ver)
if err = row.Scan(&data.ID, &data.MID, &data.Biz, &data.Currency, &data.In, &data.Out, &data.Ver, &data.CTime, &data.MTime, &data.Version); err != nil {
if err == xsql.ErrNoRows {
err = nil
data = nil
return
}
return
}
return
}
// RawBillUserDailyByMonthVer .
func (d *Dao) RawBillUserDailyByMonthVer(ctx context.Context, mid int64, biz string, currency string, monthVer int64) (datas []*model.Bill, err error) {
rows, err := d.db.Query(ctx, _selectBillUserDailyListByMonthVer, mid, biz, currency, monthVer)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
data := &model.Bill{}
if err = rows.Scan(&data.ID, &data.MID, &data.Biz, &data.Currency, &data.In, &data.Out, &data.Ver, &data.CTime, &data.MTime, &data.Version); err != nil {
return
}
datas = append(datas, data)
}
err = rows.Err()
return
}
// RawAccountUser is.
// func (d *Dao) RawAccountUser(ctx context.Context, mid int64, biz string, currency string) (data *model.AccountUser, err error) {
// data = &model.AccountUser{}
// row := d.db.QueryRow(ctx, _selectAccountUser, mid, biz, currency)
// if err = row.Scan(&data.ID, &data.Biz, &data.MID, &data.Currency, &data.Balance, &data.Ver, &data.State, &data.CTime, &data.MTime); err != nil {
// if err == xsql.ErrNoRows {
// err = nil
// data = nil
// return
// }
// return
// }
// return
// }
// RawOrderRechargeShell .
func (d *Dao) RawOrderRechargeShell(ctx context.Context, orderID string) (data *model.OrderRechargeShell, err error) {
data = &model.OrderRechargeShell{}
row := d.db.QueryRow(ctx, _orderRechargeShell, orderID)
if err = row.Scan(&data.ID, &data.MID, &data.OrderID, &data.Biz, &data.Amount, &data.PayMSG, &data.State, &data.Ver, &data.CTime, &data.MTime); err != nil {
if err == xsql.ErrNoRows {
err = nil
data = nil
return
}
return
}
return
}
// TXUpdateOrderRechargeShell .
func (d *Dao) TXUpdateOrderRechargeShell(ctx context.Context, tx *xsql.Tx, data *model.OrderRechargeShell) (err error) {
if _, err = tx.Exec(_updateOrderRechargeShell, data.MID, data.OrderID, data.Biz, data.Amount, data.PayMSG, data.State, data.Ver, data.OrderID); err != nil {
return
}
return
}
// TXInsertOrderRechargeShellLog .
func (d *Dao) TXInsertOrderRechargeShellLog(ctx context.Context, tx *xsql.Tx, order *model.OrderRechargeShellLog) (id int64, err error) {
result, err := tx.Exec(_insertOrderRechargeShellLog, order.OrderID, order.FromState, order.ToState, order.Desc, order.BillUserMonthlyID)
if err != nil {
return
}
id, err = result.LastInsertId()
return
}

View File

@@ -0,0 +1,354 @@
package dao
import (
"context"
"fmt"
"strconv"
"time"
"testing"
"go-common/app/service/main/ugcpay/model"
xsql "go-common/library/database/sql"
"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) {
tx, err := d.BeginTran(c)
ctx.Convey("Then err should be nil.tx should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(tx, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRawOrderUser(t *testing.T) {
convey.Convey("RawOrderUser", t, func(ctx convey.C) {
var (
c = context.Background()
id = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RawOrderUser(c, id)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertOrderUser(t *testing.T) {
convey.Convey("InsertOrderUser", t, func(ctx convey.C) {
var (
c = context.Background()
data = &model.Order{
OrderID: strconv.FormatInt(time.Now().Unix(), 10),
MID: 35858,
Biz: "asset",
Platform: "ios",
OID: 2333,
OType: "archive",
Fee: 1300,
Currency: "bp",
State: "created",
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
id, err := d.InsertOrderUser(c, data)
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 TestDaoRawAsset(t *testing.T) {
convey.Convey("RawAsset", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(2333)
otype = "archive"
currency = "bp"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RawAsset(c, oid, otype, currency)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpsertAsset(t *testing.T) {
convey.Convey("UpsertAsset", t, func(ctx convey.C) {
var (
c = context.Background()
data = &model.Asset{
MID: 46333,
OID: 2333,
OType: "archive",
Currency: "bp",
Price: 1000,
State: "valid",
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.UpsertAsset(c, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoRawAssetRelation(t *testing.T) {
convey.Convey("RawAssetRelation", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(35858)
oid = int64(2333)
otype = "archive"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RawAssetRelation(c, mid, oid, otype)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpsertAssetRelation(t *testing.T) {
convey.Convey("UpsertAssetRelation", t, func(ctx convey.C) {
var (
c = context.Background()
data = &model.AssetRelation{
OID: 2333,
OType: "archive",
MID: 35858,
State: "paid",
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.UpsertAssetRelation(c, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoRawAggrIncomeUser(t *testing.T) {
convey.Convey("RawAggrIncomeUser", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(46333)
cur = "bp"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RawAggrIncomeUser(c, mid, cur)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRawAggrIncomeUserMonthly(t *testing.T) {
convey.Convey("RawAggrIncomeUserMonthly", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(46333)
cur = "bp"
ver = int64(201809)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RawAggrIncomeUserAssetList(c, mid, cur, ver, 1000)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRawAggrIncomeUserAsset(t *testing.T) {
convey.Convey("RawAggrIncomeUserAsset", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(46333)
cur = "bp"
ver = int64(201809)
oid = int64(10110842)
otype = "archive"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RawAggrIncomeUserAsset(c, mid, cur, oid, otype, ver)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRawBillUserDaily(t *testing.T) {
convey.Convey("RawBillUserDaily", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(46333)
biz = "asset"
cur = "bp"
ver = int64(20181030)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RawBillUserDaily(c, mid, biz, cur, ver)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
t.Log(data)
})
})
})
}
func TestDaoRawBillUserDailyListByMonthVer(t *testing.T) {
convey.Convey("RawBillUserDailyListByMonthVer", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(46333)
biz = "asset"
cur = "bp"
ver = int64(201811)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
datas, err := d.RawBillUserDailyByMonthVer(c, mid, biz, cur, ver)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(datas, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTXInsertOrderUserLog(t *testing.T) {
convey.Convey("TestDaoTXInsertOrderUserLog", t, func(ctx convey.C) {
var (
c = context.Background()
log = &model.LogOrder{
OrderID: "test",
FromState: "ut1",
ToState: "ut2",
Desc: "hahah",
}
tx *xsql.Tx
)
tx, err := d.BeginTran(c)
convey.So(err, convey.ShouldBeNil)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
id, err := d.TXInsertOrderUserLog(c, tx, log)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldBeGreaterThan, 0)
})
})
})
}
func TestDaoOrderRechargeShell(t *testing.T) {
convey.Convey("TestDaoOrderRechargeShell", t, func(ctx convey.C) {
var (
c = context.Background()
orderID = "23656019181109210220"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
data, err := d.RawOrderRechargeShell(c, orderID)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTXUpdateOrderRechargeShell(t *testing.T) {
convey.Convey("TestDaoTXOrderRechargeShell", t, func(ctx convey.C) {
var (
c = context.Background()
data = &model.OrderRechargeShell{
MID: 46333,
OrderID: "123",
Biz: "asset",
Amount: 1,
PayMSG: "ut test",
State: "test",
Ver: 201811,
}
)
tx, err := d.BeginTran(c)
convey.So(err, convey.ShouldBeNil)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.TXUpdateOrderRechargeShell(c, tx, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTXInsertOrderRechargeShellLog(t *testing.T) {
convey.Convey("TestDaoTXInsertOrderRechargeShellLog", t, func(ctx convey.C) {
var (
c = context.Background()
data = &model.OrderRechargeShellLog{
OrderID: "233",
FromState: "ut1",
ToState: "ut2",
Desc: "ut test",
BillUserMonthlyID: fmt.Sprintf("ut_%d", time.Now().Unix()),
}
)
tx, err := d.BeginTran(c)
convey.So(err, convey.ShouldBeNil)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
id, err := d.TXInsertOrderRechargeShellLog(c, tx, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldBeGreaterThan, 0)
})
})
})
}
func TestDaoTXUpdateOrderUser(t *testing.T) {
convey.Convey("TXUpdateOrderUser", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
data = &model.Order{
OrderID: "96841175181102170810",
State: "ut test",
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.TXUpdateOrderUser(c, tx, data)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Reset(func() {
tx.Commit()
})
})
}

View File

@@ -0,0 +1,96 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/main/ugcpay/conf"
"go-common/library/cache/redis"
"github.com/pkg/errors"
)
func assetRelationKey(mid int64) string {
return fmt.Sprintf("up_ar_%d", mid)
}
func assetRelationField(oid int64, otype string) string {
return fmt.Sprintf("%s_%d", otype, oid)
}
func (d *Dao) pingRedis(c context.Context) (err error) {
conn := d.redis.Get(c)
defer conn.Close()
if _, err = conn.Do("SET", "PING", "PONG"); err != nil {
err = errors.WithStack(err)
}
return
}
// CacheAssetRelationState get asset relation state.
func (d *Dao) CacheAssetRelationState(c context.Context, oid int64, otype string, mid int64) (state string, err error) {
var (
key = assetRelationKey(mid)
field = assetRelationField(oid, otype)
conn = d.redis.Get(c)
)
defer conn.Close()
if state, err = redis.String(conn.Do("HGET", key, field)); err != nil {
if err == redis.ErrNil {
err = nil
state = "miss"
return
}
err = errors.Wrapf(err, "conn.Do(HGET, %s ,%s)", key, field)
return
}
return
}
// AddCacheAssetRelationState set asset relation state.
func (d *Dao) AddCacheAssetRelationState(c context.Context, oid int64, otype string, mid int64, state string) (err error) {
var (
key = assetRelationKey(mid)
field = assetRelationField(oid, otype)
conn = d.redis.Get(c)
)
defer conn.Close()
if _, err = conn.Do("HSET", key, field, state); err != nil {
err = errors.Wrapf(err, "conn.Do(HSET, %s, %s, %s)", key, field, state)
return
}
if _, err = conn.Do("EXPIRE", key, conf.Conf.CacheTTL.AssetRelationTTL); err != nil {
err = errors.Wrapf(err, "conn.Do(EXPIRE, %s, %d)", key, conf.Conf.CacheTTL.AssetRelationTTL)
return
}
return
}
// DelCacheAssetRelationState delete asset relation state.
func (d *Dao) DelCacheAssetRelationState(c context.Context, oid int64, otype string, mid int64) (err error) {
var (
key = assetRelationKey(mid)
field = assetRelationField(oid, otype)
conn = d.redis.Get(c)
)
defer conn.Close()
if _, err = conn.Do("HDEL", key, field); err != nil {
err = errors.Wrapf(err, "conn.Do(HDEL, %s, %s)", key, field)
return
}
return
}
// DelCacheAssetRelation delete assetrelation.
func (d *Dao) DelCacheAssetRelation(c context.Context, mid int64) (err error) {
var (
key = assetRelationKey(mid)
conn = d.redis.Get(c)
)
defer conn.Close()
if _, err = conn.Do("DEL", key); err != nil {
err = errors.Wrapf(err, "conn.Do(DEL, %s)", key)
return
}
return
}

View File

@@ -0,0 +1,119 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAddCacheAssetRelationState(t *testing.T) {
convey.Convey("AddCacheAssetRelationState", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(2233)
otype = "archive"
mid = int64(46333)
state = "paid"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.AddCacheAssetRelationState(c, oid, otype, mid, state)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoassetRelationKey(t *testing.T) {
convey.Convey("assetRelationKey", t, func(ctx convey.C) {
var (
mid = int64(46333)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := assetRelationKey(mid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoassetRelationField(t *testing.T) {
convey.Convey("assetRelationField", t, func(ctx convey.C) {
var (
oid = int64(2233)
otype = "archive"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := assetRelationField(oid, otype)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaopingRedis(t *testing.T) {
convey.Convey("pingRedis", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.pingRedis(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoCacheAssetRelationState(t *testing.T) {
convey.Convey("CacheAssetRelationState", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(2233)
otype = "archive"
mid = int64(46333)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
state, err := d.CacheAssetRelationState(c, oid, otype, mid)
ctx.Convey("Then err should be nil.state should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(state, convey.ShouldEqual, "paid")
})
})
})
}
func TestDaoDelCacheAssetRelationState(t *testing.T) {
convey.Convey("DelCacheAssetRelationState", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(2233)
otype = "archive"
mid = int64(46333)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelCacheAssetRelationState(c, oid, otype, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelCacheAssetRelation(t *testing.T) {
convey.Convey("DelCacheAssetRelation", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(46333)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DelCacheAssetRelation(c, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}