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,57 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"api_test.go",
"dao_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"chaodian.go",
"dao.go",
],
importpath = "go-common/app/interface/main/creative/dao/order",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/dao/tool:go_default_library",
"//app/interface/main/creative/model/order:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/time: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,78 @@
package order
import (
"context"
"net/http"
"net/url"
"strconv"
"time"
"go-common/app/interface/main/creative/conf"
"go-common/app/interface/main/creative/dao/tool"
"go-common/app/interface/main/creative/model/order"
"go-common/library/ecode"
"go-common/library/log"
)
// UpValidate fn
func (d *Dao) UpValidate(c context.Context, mid int64, ip string) (uv *order.UpValidate, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("appkey", conf.Conf.HTTPClient.UpMng.Key)
params.Set("appsecret", conf.Conf.HTTPClient.UpMng.Secret)
params.Set("ts", strconv.FormatInt(time.Now().UnixNano()/1000000, 10))
var (
sign, _ = tool.Sign(params)
res struct {
Status string `json:"status"`
CurrentTime int64 `json:"current_time"`
Result *order.UpValidate `json:"result"`
}
_upValidateURL = d.upValidateURI + "?" + sign
)
log.Info("upValidate url(%s)", _upValidateURL)
req, err := http.NewRequest("GET", _upValidateURL, nil)
if err != nil {
log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", _upValidateURL, err, mid, ip)
err = ecode.CreativeOrderAPIErr
return
}
req.Header.Set("Accept", "application/json")
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.client.Do upValidate url(%s)|mid(%d)|ip(%s)|error(%v)", _upValidateURL, mid, ip, err)
err = ecode.CreativeOrderAPIErr
return
}
if res.Status != "success" {
log.Error("upValidate url(%s)|mid(%d) res(%v)", _upValidateURL, mid, res)
return
}
uv = res.Result
return
}
// GrowAccountState 获取up主状态 type 类型 0 视频 2 专栏 3 素材.
func (d *Dao) GrowAccountState(c context.Context, mid int64, ty int) (result *order.UpValidate, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("type", strconv.Itoa(ty))
var res struct {
Code int `json:"code"`
Message string `json:"message"`
Data *order.UpValidate `json:"data"`
}
if err = d.client.Get(c, d.accountStateURI, "", params, &res); err != nil {
log.Error("GrowAccountState url(%s) response(%v) error(%v)", d.accountStateURI+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
log.Info("GrowAccountState url(%s)", d.accountStateURI+"?"+params.Encode())
if res.Code != 0 {
log.Error("GrowAccountState url(%s),code(%d) msg(%s) res(%v)", d.accountStateURI, res.Code, res.Message, res)
err = ecode.Int(res.Code)
return
}
result = res.Data
return
}

View File

@@ -0,0 +1,202 @@
package order
import (
"context"
"strings"
"testing"
"github.com/smartystreets/goconvey/convey"
gock "gopkg.in/h2non/gock.v1"
)
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}
func TestOrderExecuteOrders(t *testing.T) {
convey.Convey("ExecuteOrders", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2089809)
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.executeOrdersURI).Reply(200).JSON(`{"code": 0,"message": "0", "ttl": 1,"data":"{}"}`)
_, err := d.ExecuteOrders(c, mid, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestOrderUps(t *testing.T) {
convey.Convey("Ups", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.upsURI).Reply(200).JSON(`
{
"code": 0,
"message": "ok",
"requestId": "46aab0b0157111e9bca70a36f8bfcc85",
"ts": 1547191187259,
"data": [
2089809
]
}`)
_, err := d.Ups(c)
ctx.Convey("Then err should be nil.ups should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestOrderOrderByAid(t *testing.T) {
convey.Convey("OrderByAid", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(10111835)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.getOrderByAidURI).Reply(200).JSON(`
{
"code": 0,
"message": "ok",
"requestId": "cbf35e10157211e9bca70a36f8bfcc85",
"ts": 1547191840369,
"data": {
"execute_order_id": 13,
"business_order_id": 6,
"business_order_name": "这是一个测试项目",
"id_code": 10002824,
"game_base_id": 0,
"game_name": ""
}
}`)
_, _, _, err := d.OrderByAid(c, aid)
ctx.Convey("Then err should be nil.orderID,orderName,gameBaseID should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestOrderUnbind(t *testing.T) {
convey.Convey("Unbind", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2089809)
aid = int64(10111835)
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("POST", d.archiveStatusURI).Reply(200).JSON(`{"code": 0,"message": "0", "ttl": 1,"data":"{}"}`)
err := d.Unbind(c, mid, aid, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestOrderOasis(t *testing.T) {
convey.Convey("Oasis", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2089809)
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.oasisURI).Reply(200).JSON(`
{
"code": 0,
"message": "ok",
"requestId": "14909c20157111e9bca70a36f8bfcc85",
"ts": 1547191103202,
"data": {
"state": 1,
"running_execute_order_count": 0,
"total_execute_order_count": 0
}
}`)
_, err := d.Oasis(c, mid, ip)
ctx.Convey("Then err should be nil.oa should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestOrderLaunchTime(t *testing.T) {
convey.Convey("LaunchTime", t, func(ctx convey.C) {
var (
c = context.Background()
orderID = int64(15)
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.launchTimeURI).Reply(200).JSON(`
{
"code": 0,
"message": "ok",
"requestId": "75839970157011e9bca70a36f8bfcc85",
"ts": 1547190836359,
"data": {
"begin_date": 1547127000
}
}
`)
_, err := d.LaunchTime(c, orderID, ip)
ctx.Convey("Then err should be nil.beginDate,endDate should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestOrderUpValidate(t *testing.T) {
convey.Convey("UpValidate", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2089809)
ip = "127.0.0.1"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.upValidateURI).Reply(200).JSON(`{"code": 0,"message": "0", "ttl": 1,"data":"{}"}`)
_, err := d.UpValidate(c, mid, ip)
ctx.Convey("Then err should be nil.uv should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestOrderGrowAccountState(t *testing.T) {
convey.Convey("GrowAccountState", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(2089809)
ty = int(3)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.accountStateURI).Reply(200).JSON(`{"code": 0,"message": "0", "ttl": 1,"data":"{}"}`)
_, err := d.GrowAccountState(c, mid, ty)
ctx.Convey("Then err should be nil.result should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldEqual, err)
})
})
})
}

View File

@@ -0,0 +1,154 @@
package order
import (
"context"
"go-common/app/interface/main/creative/model/order"
"go-common/library/ecode"
"go-common/library/log"
xtime "go-common/library/time"
"net/url"
"strconv"
)
// ExecuteOrders orders.
func (d *Dao) ExecuteOrders(c context.Context, mid int64, ip string) (orders []*order.Order, err error) {
params := url.Values{}
params.Set("up_mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Orders []*order.Order `json:"data"`
}
if err = d.chaodian.Get(c, d.executeOrdersURI, ip, params, &res); err != nil {
log.Error("chaodian url(%s) response(%v) error(%v)", d.executeOrdersURI+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
log.Info("chaodian url(%s)", d.executeOrdersURI+"?"+params.Encode())
if res.Code != 0 {
log.Error("chaodian url(%s) res(%v)", d.executeOrdersURI, res)
orders = nil
return
}
orders = res.Orders
return
}
// Ups ups
func (d *Dao) Ups(c context.Context) (ups map[int64]int64, err error) {
params := url.Values{}
var res struct {
Code int `json:"code"`
Ups []int64 `json:"data"`
}
if err = d.chaodian.Get(c, d.upsURI, "", params, &res); err != nil {
log.Error("chaodian url(%s) response(%v) error(%v)", d.upsURI+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
log.Info("chaodian url(%s)", d.upsURI+"?"+params.Encode())
if res.Code != 0 {
log.Error("chaodian url(%s) res(%v)", d.upsURI, res)
err = ecode.CreativeOrderAPIErr
return
}
ups = make(map[int64]int64)
for _, v := range res.Ups {
ups[v] = v
}
return
}
// OrderByAid order by aid.
func (d *Dao) OrderByAid(c context.Context, aid int64) (orderID int64, orderName string, gameBaseID int64, err error) {
params := url.Values{}
params.Set("av_id", strconv.FormatInt(aid, 10))
var res struct {
Code int `json:"code"`
Order order.Order `json:"data"`
}
if err = d.chaodian.Get(c, d.getOrderByAidURI, "", params, &res); err != nil {
log.Error("chaodian url(%s) response(%v) error(%v)", d.getOrderByAidURI+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
log.Info("chaodian (%s)", d.getOrderByAidURI+"?"+params.Encode())
if res.Code != 0 {
log.Error("chaodian url(%s) res(%v)", d.getOrderByAidURI, res)
orderID = 0
return
}
orderID = res.Order.ExeOdID
orderName = res.Order.BzOdName
gameBaseID = res.Order.GameBaseID
log.Info("chaodian GetOrderByAid Res OrderInfo (%+v)", res.Order)
return
}
// Unbind unbind order id.
func (d *Dao) Unbind(c context.Context, mid, aid int64, ip string) (err error) {
params := url.Values{}
params.Set("status", "-1")
params.Set("av_id", strconv.FormatInt(aid, 10))
var res struct {
Code int `json:"code"`
Message string `json:"message"`
}
if err = d.chaodian.Post(c, d.archiveStatusURI, ip, params, &res); err != nil {
log.Error("chaodian d.chaodian.POST uri(%s) aid(%d) mid(%d) error(%v)", d.archiveStatusURI+"?"+params.Encode(), mid, aid, err)
return
}
log.Info("chaodian Unbind url with params: (%s)", d.archiveStatusURI+"?"+params.Encode())
if res.Code != 0 {
err = ecode.Int(res.Code)
log.Error("chaodian unbind uri(%s) aid(%d) mid(%d) res.code(%d) error(%v)", d.archiveStatusURI+"?"+params.Encode(), mid, aid, res.Code, err)
}
return
}
// Oasis for orders.
func (d *Dao) Oasis(c context.Context, mid int64, ip string) (oa *order.Oasis, err error) {
params := url.Values{}
params.Set("up_id", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data *order.Oasis `json:"data"`
}
if err = d.chaodian.Get(c, d.oasisURI, ip, params, &res); err != nil {
log.Error("chaodian up_execute_order_statics url(%s) response(%v) error(%v)", d.oasisURI+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
log.Info("chaodian up_execute_order_statics mid(%d) url(%s) res(%+v)", mid, d.oasisURI+"?"+params.Encode(), res)
if res.Code != 0 {
log.Error("chaodian up_execute_order_statics url(%s) res(%v)", d.oasisURI, res)
oa = nil
return
}
oa = res.Data
return
}
// LaunchTime publish time from order id.
func (d *Dao) LaunchTime(c context.Context, orderID int64, ip string) (beginDate xtime.Time, err error) {
params := url.Values{}
params.Set("execute_order_id", strconv.FormatInt(orderID, 10))
var res struct {
Code int `json:"code"`
Data struct {
BeginDate xtime.Time `json:"begin_date"`
} `json:"data"`
}
if err = d.chaodian.Get(c, d.launchTimeURI, "", params, &res); err != nil {
log.Error("chaodian LaunchTime url(%s) response(%v) error(%v)", d.launchTimeURI+"?"+params.Encode(), res, err)
err = ecode.CreativeOrderAPIErr
return
}
log.Info("chaodian LaunchTime url(%s)", d.launchTimeURI+"?"+params.Encode())
if res.Code != 0 {
log.Error("chaodian LaunchTime url(%s) res(%v)", d.launchTimeURI, res)
err = ecode.CreativeOrderAPIErr
return
}
beginDate = res.Data.BeginDate
return
}

View File

@@ -0,0 +1,54 @@
package order
import (
"go-common/app/interface/main/creative/conf"
bm "go-common/library/net/http/blademaster"
)
const (
// --- from chaodian v2
_executeOrders = "/api/open_api/v2/execute_orders"
_ups = "/api/open_api/v2/ups"
_getOrderByAid = "/api/open_api/v2/execute_orders/by_av_id"
_archiveStatus = "/api/open_api/v2/execute_orders/video/status"
_oasis = "/api/open_api/v2/ups/up_execute_order_statistics" //绿洲计划
_launchtime = "/api/open_api/v2/execute_orders/launch_time"
// ----
_upValidate = "/meet/api/openApi/v1/up/validate"
_accountState = "/allowance/api/x/admin/growup/up/account/state"
)
// Dao define
type Dao struct {
c *conf.Config
// http
client *bm.Client
chaodian *bm.Client
// uri
executeOrdersURI string
upsURI string
getOrderByAidURI string
archiveStatusURI string
oasisURI string
launchTimeURI string
upValidateURI string
accountStateURI string
}
// New init dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
client: bm.NewClient(c.HTTPClient.UpMng),
chaodian: bm.NewClient(c.HTTPClient.Chaodian),
executeOrdersURI: c.Host.Chaodian + _executeOrders,
upsURI: c.Host.Chaodian + _ups,
getOrderByAidURI: c.Host.Chaodian + _getOrderByAid,
archiveStatusURI: c.Host.Chaodian + _archiveStatus,
oasisURI: c.Host.Chaodian + _oasis,
launchTimeURI: c.Host.Chaodian + _launchtime,
upValidateURI: c.Host.UpMng + _upValidate,
accountStateURI: c.Host.Profit + _accountState,
}
return
}

View File

@@ -0,0 +1,35 @@
package order
import (
"flag"
"os"
"testing"
"go-common/app/interface/main/creative/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative")
flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
flag.Set("tree_id", "2305")
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/creative.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}