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,64 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"account.go",
"article_category.go",
"resource.go",
"video_category.go",
"vip.go",
],
importpath = "go-common/app/admin/main/growup/dao/resource",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/growup/conf:go_default_library",
"//app/admin/main/growup/model:go_default_library",
"//app/admin/main/growup/util:go_default_library",
"//app/service/main/account/api:go_default_library",
"//app/service/main/vip/model:go_default_library",
"//app/service/main/vip/rpc/client:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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 = [
"account_test.go",
"article_category_test.go",
"resource_test.go",
"video_category_test.go",
"vip_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/admin/main/growup/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,56 @@
package resource
import (
"context"
accgrpc "go-common/app/service/main/account/api"
"go-common/library/ecode"
"go-common/library/log"
)
// MidByNickname .
func MidByNickname(c context.Context, name string) (mid int64, err error) {
reply, err := accCli.InfosByName3(c, &accgrpc.NamesReq{Names: []string{name}})
if err != nil || reply == nil {
log.Error("accCli.InfosByName3 name(%s) err(%v)", name, err)
err = ecode.CreativeAccServiceErr
return
}
if reply.Infos != nil {
for k := range reply.Infos {
mid = k
}
}
return
}
// NamesByMIDs .
func NamesByMIDs(c context.Context, mids []int64) (res map[int64]string, err error) {
reply, err := accCli.Infos3(c, &accgrpc.MidsReq{Mids: mids})
if err != nil || reply == nil {
log.Error("accCli.NamesByMIDs mids(%v) err(%v)", mids, err)
err = ecode.CreativeAccServiceErr
return
}
if reply.Infos != nil {
res = make(map[int64]string, len(reply.Infos))
for mid, info := range reply.Infos {
res[mid] = info.Name
}
}
return
}
// NameByMID .
func NameByMID(c context.Context, mid int64) (nickname string, err error) {
reply, err := accCli.Info3(c, &accgrpc.MidReq{Mid: mid})
if err != nil || reply == nil {
log.Error("accCli.Info3 mid(%d) err(%v)", mid, err)
err = ecode.CreativeAccServiceErr
return
}
if reply.Info != nil {
nickname = reply.Info.Name
}
return
}

View File

@@ -0,0 +1,56 @@
package resource
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestResourceMidByNickname(t *testing.T) {
convey.Convey("MidByNickname", t, func(ctx convey.C) {
var (
c = context.Background()
name = "name"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
mid, err := MidByNickname(c, name)
ctx.Convey("Then err should be nil.mid should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(mid, convey.ShouldNotBeNil)
})
})
})
}
func TestResourceNamesByMIDs(t *testing.T) {
convey.Convey("NamesByMIDs", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{1, 2}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := NamesByMIDs(c, mids)
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 TestResourceNameByMID(t *testing.T) {
convey.Convey("NameByMID", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
nickname, err := NameByMID(c, mid)
ctx.Convey("Then err should be nil.nickname should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(nickname, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,51 @@
package resource
import (
"context"
"go-common/library/ecode"
"go-common/library/log"
)
// ColumnInfo column category
type columnInfo struct {
ID int64 `json:"id"`
PID int64 `json:"parent_id"`
Name string `json:"name"`
}
// ColumnCategory get column types.
func columnCategory(c context.Context) (data []*columnInfo, err error) {
var res struct {
Code int `json:"code"`
Message string `json:"message"`
Data []*columnInfo `json:"data"`
}
url := articleCategoryURL
if err = client.Get(c, url, "", nil, &res); err != nil {
log.Error("resource.columnCategory GET error(%v) | uri(%s)", err, url)
return
}
if res.Code != 0 {
log.Error("resource.columnCategory code != 0. res.Code(%d) | uri(%s) res(%v)", res.Code, url, res)
err = ecode.GrowupGetTypeError
return
}
data = res.Data
return
}
// ColumnCategoryNameToID .
func ColumnCategoryNameToID(c context.Context) (categories map[string]int64, err error) {
data, err := columnCategory(c)
if err != nil {
return
}
categories = make(map[string]int64, len(data))
for _, v := range data {
if v.PID == 0 {
categories[v.Name] = v.ID
}
}
return
}

View File

@@ -0,0 +1,38 @@
package resource
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestResourcecolumnCategory(t *testing.T) {
convey.Convey("columnCategory", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
data, err := columnCategory(c)
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 TestResourceColumnCategoryNameToID(t *testing.T) {
convey.Convey("ColumnCategoryNameToID", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
categories, err := ColumnCategoryNameToID(c)
ctx.Convey("Then err should be nil.categories should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(categories, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,30 @@
package resource
import (
"go-common/app/admin/main/growup/conf"
accgrpc "go-common/app/service/main/account/api"
vip "go-common/app/service/main/vip/rpc/client"
httpx "go-common/library/net/http/blademaster"
"github.com/pkg/errors"
)
var (
vipRPC *vip.Service
client *httpx.Client
accCli accgrpc.AccountClient
videoCategoryURL string
articleCategoryURL string
)
// Init .
func Init(c *conf.Config) {
var err error
vipRPC = vip.New(c.VipRPC)
client = httpx.NewClient(c.HTTPClient)
videoCategoryURL = c.Host.VideoType + "/videoup/types"
articleCategoryURL = c.Host.ColumnType + "/x/article/categories"
if accCli, err = accgrpc.NewClient(c.Account); err != nil {
panic(errors.WithMessage(err, "Failed to dial account service"))
}
}

View File

@@ -0,0 +1,31 @@
package resource
import (
"flag"
"os"
"testing"
"go-common/app/admin/main/growup/conf"
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "mobile.studio.growup-admin")
flag.Set("conf_token", "ac1fd397cbc33eb60541e8734844bdd5")
flag.Set("tree_id", "13583")
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/growup-admin.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
Init(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,65 @@
package resource
import (
"context"
"go-common/library/ecode"
"go-common/library/log"
)
// videoInfo video category
type videoInfo struct {
ID int64 `json:"id"`
PID int64 `json:"pid"`
Name string `json:"name"`
}
func videoCategory(c context.Context) (data map[int16]videoInfo, err error) {
var res struct {
Code int `json:"code"`
Message string `json:"message"`
Data map[int16]videoInfo `json:"data"`
}
url := videoCategoryURL
if err = client.Get(c, url, "", nil, &res); err != nil {
log.Error("resource.videoCategory GET error(%v) | uri(%s)", err, url)
return
}
if res.Code != 0 {
log.Error("resource.videoCategory code != 0. res.Code(%d) | uri(%s) res(%v)", res.Code, url, res)
err = ecode.GrowupGetTypeError
return
}
data = res.Data
return
}
// VideoCategoryIDToName .
func VideoCategoryIDToName(c context.Context) (res map[int64]string, err error) {
data, err := videoCategory(c)
if err != nil {
return
}
res = make(map[int64]string, len(data))
for _, v := range data {
if v.PID == 0 {
res[v.ID] = v.Name
}
}
return
}
// VideoCategoryNameToID .
func VideoCategoryNameToID(c context.Context) (res map[string]int64, err error) {
data, err := videoCategory(c)
if err != nil {
return
}
res = make(map[string]int64, len(data))
for _, v := range data {
if v.PID == 0 {
res[v.Name] = v.ID
}
}
return
}

View File

@@ -0,0 +1,53 @@
package resource
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestResourcevideoCategory(t *testing.T) {
convey.Convey("videoCategory", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
data, err := videoCategory(c)
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 TestResourceVideoCategoryIDToName(t *testing.T) {
convey.Convey("VideoCategoryIDToName", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := VideoCategoryIDToName(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 TestResourceVideoCategoryNameToID(t *testing.T) {
convey.Convey("VideoCategoryNameToID", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := VideoCategoryNameToID(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)
})
})
})
}

View File

@@ -0,0 +1,36 @@
package resource
import (
"context"
"go-common/app/admin/main/growup/model"
"go-common/app/admin/main/growup/util"
vip "go-common/app/service/main/vip/model"
"go-common/library/log"
)
const _panelType = "incentive"
// VipProducts returns <vipProductID, goodsInfo> pairs
func VipProducts(c context.Context) (r map[string]*model.GoodsInfo, err error) {
res, err := vipRPC.VipPanelInfo5(c, &vip.ArgPanel{PanelType: _panelType})
if err != nil {
log.Error("VipPanelInfo5 err(%v)", err)
return
}
r = make(map[string]*model.GoodsInfo, len(res.Vps))
for _, v := range res.Vps {
r[v.PdID] = &model.GoodsInfo{
// vip商品唯一标识
ProductID: v.PdID,
// vip商品名称
ProductName: v.PdName,
// 大会员实时价格 = 激励兑换商品的实时成本价; 单位元转换为单位分
OriginPrice: int64(util.MulWithRound(v.DPrice, float64(100), 0)),
// vip会员时长
Month: v.Month,
}
}
return
}

View File

@@ -0,0 +1,23 @@
package resource
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestResourceVipProducts(t *testing.T) {
convey.Convey("VipProducts", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
r, err := VipProducts(c)
ctx.Convey("Then err should be nil.r should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(r, convey.ShouldNotBeNil)
})
})
})
}