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,52 @@
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.go",
"grpc.go",
],
importpath = "go-common/app/interface/main/ugcpay/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/ugcpay/conf:go_default_library",
"//app/interface/main/ugcpay/model:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/ugcpay/api/grpc/v1: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_test.go",
"grpc_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/ugcpay/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,40 @@
package dao
import (
"context"
"go-common/app/interface/main/ugcpay/conf"
archive "go-common/app/service/main/archive/api"
ugcpay "go-common/app/service/main/ugcpay/api/grpc/v1"
)
// Dao dao
type Dao struct {
c *conf.Config
ugcpayAPI ugcpay.UGCPayClient
archiveAPI archive.ArchiveClient
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
}
var err error
if dao.ugcpayAPI, err = ugcpay.NewClient(nil); err != nil {
panic(err)
}
if dao.archiveAPI, err = archive.NewClient(nil); err != nil {
panic(err)
}
return
}
// Close close the resource.
func (d *Dao) Close() {
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) error {
return nil
}

View File

@@ -0,0 +1,38 @@
package dao
import (
"context"
"flag"
"go-common/app/interface/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-interface")
flag.Set("conf_token", "e6b959f71772ec8207eb80b8a86137cc")
flag.Set("tree_id", "63917")
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)
d.Ping(context.Background())
i := m.Run()
d.Close()
os.Exit(i)
}

View File

@@ -0,0 +1,176 @@
package dao
import (
"context"
archive "go-common/app/service/main/archive/api"
ugcpay "go-common/app/service/main/ugcpay/api/grpc/v1"
"go-common/app/interface/main/ugcpay/model"
)
// TradeCreate create trade order for mid
func (d *Dao) TradeCreate(ctx context.Context, platform string, mid int64, oid int64, otype string, currency string) (orderID string, payData string, err error) {
var (
req = &ugcpay.TradeCreateReq{
Platform: platform,
Mid: mid,
Oid: oid,
Otype: otype,
Currency: currency,
}
reply *ugcpay.TradeCreateResp
)
if reply, err = d.ugcpayAPI.TradeCreate(ctx, req); err != nil {
return
}
orderID = reply.OrderId
payData = reply.PayData
return
}
// TradeQuery query trade order by orderID
func (d *Dao) TradeQuery(ctx context.Context, orderID string) (order *model.TradeOrder, err error) {
var (
req = &ugcpay.TradeOrderReq{
Id: orderID,
}
reply *ugcpay.TradeOrderResp
)
if reply, err = d.ugcpayAPI.TradeQuery(ctx, req); err != nil {
return
}
order = &model.TradeOrder{
OrderID: reply.OrderId,
MID: reply.Mid,
Biz: reply.Biz,
Platform: reply.Platform,
OID: reply.Oid,
OType: reply.Otype,
Fee: reply.Fee,
Currency: reply.Currency,
PayID: reply.PayId,
State: reply.State,
Reason: reply.Reason,
}
return
}
// TradeConfirm confirm trade order by orderID
func (d *Dao) TradeConfirm(ctx context.Context, orderID string) (order *model.TradeOrder, err error) {
var (
req = &ugcpay.TradeOrderReq{
Id: orderID,
}
reply *ugcpay.TradeOrderResp
)
if reply, err = d.ugcpayAPI.TradeConfirm(ctx, req); err != nil {
return
}
order = &model.TradeOrder{
OrderID: reply.OrderId,
MID: reply.Mid,
Biz: reply.Biz,
Platform: reply.Platform,
OID: reply.Oid,
OType: reply.Otype,
Fee: reply.Fee,
Currency: reply.Currency,
PayID: reply.PayId,
State: reply.State,
Reason: reply.Reason,
}
return
}
// TradeCancel cancel trade order by orderID
func (d *Dao) TradeCancel(ctx context.Context, orderID string) (err error) {
var (
req = &ugcpay.TradeOrderReq{
Id: orderID,
}
)
if _, err = d.ugcpayAPI.TradeCancel(ctx, req); err != nil {
return
}
return
}
// Income
// IncomeAssetOverview .
func (d *Dao) IncomeAssetOverview(ctx context.Context, mid int64) (inc *model.IncomeAssetOverview, err error) {
var (
req = &ugcpay.IncomeUserAssetOverviewReq{
Mid: mid,
}
reply *ugcpay.IncomeUserAssetOverviewResp
)
if reply, err = d.ugcpayAPI.IncomeUserAssetOverview(ctx, req); err != nil {
return
}
inc = &model.IncomeAssetOverview{
Total: reply.Total,
TotalBuyTimes: reply.TotalBuyTimes,
MonthNew: reply.MonthNew,
DayNew: reply.DayNew,
}
return
}
// IncomeUserAssetList .
func (d *Dao) IncomeUserAssetList(ctx context.Context, mid int64, ver int64, ps, pn int64) (inc *model.IncomeAssetMonthly, err error) {
var (
req = &ugcpay.IncomeUserAssetListReq{
Mid: mid,
Ver: ver,
Ps: ps,
Pn: pn,
}
reply *ugcpay.IncomeUserAssetListResp
)
if reply, err = d.ugcpayAPI.IncomeUserAssetList(ctx, req); err != nil {
return
}
inc = &model.IncomeAssetMonthly{
List: make([]*model.IncomeAssetMonthlyByContent, 0),
Page: &model.Page{
Num: reply.Page.Num,
Size: reply.Page.Size_,
Total: reply.Page.Total,
},
}
for _, c := range reply.List {
inc.List = append(inc.List, &model.IncomeAssetMonthlyByContent{
OID: c.Oid,
OType: c.Otype,
Currency: c.Currency,
Price: c.Price,
TotalBuyTimes: c.TotalBuyTimes,
NewBuyTimes: c.NewBuyTimes,
TotalErrTimes: c.TotalErrTimes,
NewErrTimes: c.NewErrTimes,
})
}
return
}
// archive
// ArchiveTitles 通过 aid list 获取稿件标题
func (d *Dao) ArchiveTitles(ctx context.Context, aids []int64) (arcTitles map[int64]string, err error) {
var (
req = &archive.ArcsRequest{
Aids: aids,
}
reply *archive.ArcsReply
)
if reply, err = d.archiveAPI.Arcs(ctx, req); err != nil {
return
}
arcTitles = make(map[int64]string)
for aid, a := range reply.Arcs {
arcTitles[aid] = a.Title
}
return
}

View File

@@ -0,0 +1,127 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoTradeCreate(t *testing.T) {
convey.Convey("TradeCreate", t, func(ctx convey.C) {
var (
c = context.Background()
platform = "ios"
mid = int64(46333)
oid = int64(10110745)
otype = "archive"
currency = "bp"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
orderID, payData, err := d.TradeCreate(c, platform, mid, oid, otype, currency)
ctx.Convey("Then err should be nil.orderID,payData should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(payData, convey.ShouldNotBeNil)
ctx.So(orderID, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTradeQuery(t *testing.T) {
convey.Convey("TradeQuery", t, func(ctx convey.C) {
var (
c = context.Background()
orderID = "32172647181109193324"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
order, err := d.TradeQuery(c, orderID)
ctx.Convey("Then err should be nil.order should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(order, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTradeConfirm(t *testing.T) {
convey.Convey("TradeConfirm", t, func(ctx convey.C) {
var (
c = context.Background()
orderID = "32172647181109193324"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
order, err := d.TradeConfirm(c, orderID)
ctx.Convey("Then err should be nil.order should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(order, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTradeCancel(t *testing.T) {
convey.Convey("TradeCancel", t, func(ctx convey.C) {
var (
c = context.Background()
orderID = "32172647181109193324"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.TradeCancel(c, orderID)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoIncomeAssetOverview(t *testing.T) {
convey.Convey("IncomeAssetOverview", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(46333)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
inc, err := d.IncomeAssetOverview(c, mid)
ctx.Convey("Then err should be nil.inc should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(inc, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoIncomeUserAssetList(t *testing.T) {
convey.Convey("IncomeUserAssetList", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(46333)
ver = int64(201811)
ps = int64(10)
pn = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
inc, err := d.IncomeUserAssetList(c, mid, ver, ps, pn)
ctx.Convey("Then err should be nil.inc should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(inc, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoArchiveTitles(t *testing.T) {
convey.Convey("ArchiveTitles", t, func(ctx convey.C) {
var (
c = context.Background()
aids = []int64{10110745}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
arcTitles, err := d.ArchiveTitles(c, aids)
ctx.Convey("Then err should be nil.arcTitles should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(arcTitles, convey.ShouldNotBeNil)
})
})
})
}