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,56 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"api.go",
"dao.go",
],
importpath = "go-common/app/interface/main/creative/dao/elec",
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/elec:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster:go_default_library",
"//library/xstr: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 = [
"api_test.go",
"dao_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,519 @@
package elec
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/elec"
"go-common/library/ecode"
"go-common/library/log"
)
const (
_userInfoURI = "/internal/member/info"
_userJoinURI = "/internal/member/elec/partin"
_userExitURI = "/internal/member/elec/exit"
_arcOpenURI = "/internal/archice/partin"
_arcCloseURI = "/internal/archice/exit"
_notifyURI = "/internal/notify/info"
_getStatusURI = "/api/user/queryset/v2"
_setStatusURI = "/api/user/modifyset/v2"
_recentRankURI = "/api/query.recent.do"
_currentRankURI = "/api/query.rank.do"
_totalRankURI = "/api/query.total.rank.do"
_dailyBillURI = "/api/query.daily.bill.do"
_balanceURI = "/api/query.wallet.balance.do"
_recentElecURI = "/api/recent/elec"
_remarkListURI = "/api/elec/remark/list"
_remarkDetailURI = "/api/elec/remake/detail"
_remarkURI = "/api/remake/reply"
)
// UserInfo get user elec info.
func (d *Dao) UserInfo(c context.Context, mid int64, ip string) (st *elec.UserInfo, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data *elec.UserInfo
}
if err = d.client.Get(c, d.userInfoURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.userInfoURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.userInfoURL, res)
err = ecode.CreativeElecErr
return
}
st = res.Data
return
}
// UserUpdate join or exit elec.
func (d *Dao) UserUpdate(c context.Context, mid int64, st int8, ip string) (u *elec.UserInfo, err error) {
params := url.Values{}
params.Set("appkey", conf.Conf.App.Key)
params.Set("appsecret", conf.Conf.App.Secret)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
// url
var (
query, _ = tool.Sign(params)
url string
)
if st == 1 {
url = d.userJoinURL + "?" + query
} else if st == 2 {
url = d.userExitURL + "?" + query
}
// new requests
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
var res struct {
Code int `json:"code"`
Data *elec.UserInfo
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("user elec update state url(%s) res(%v); mid(%d), ip(%s), code(%d)", url, res, mid, ip, res.Code)
err = ecode.CreativeElecErr
}
u = res.Data
return
}
// ArcUpdate arc open or close elec.
func (d *Dao) ArcUpdate(c context.Context, mid, aid int64, st int8, ip string) (err error) {
params := url.Values{}
params.Set("appkey", conf.Conf.App.Key)
params.Set("appsecret", conf.Conf.App.Secret)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
// url
var (
query, _ = tool.Sign(params)
url string
)
if st == 1 {
url = d.arcOpenURL + "?" + query
} else if st == 2 {
url = d.arcCloseURL + "?" + query
}
// new request
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Error("http.NewRequest(%s) error(%v); mid(%d), aid(%d), ip(%s)", url, err, mid, aid, ip)
err = ecode.CreativeElecErr
return
}
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
var res struct {
Code int `json:"code"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.client.Do(%s) error(%v); mid(%d), aid(%d), ip(%s)", url, err, mid, aid, ip)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("arc elec update state url(%s) res(%v); mid(%d), aid(%d), ip(%s)", url, res, mid, aid, ip)
err = ecode.CreativeElecErr
}
return
}
// Notify get up-to-date notice.
func (d *Dao) Notify(c context.Context, ip string) (nt *elec.Notify, err error) {
params := url.Values{}
var res struct {
Code int `json:"code"`
Data *elec.Notify
}
if err = d.client.Get(c, d.notifyURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.notifyURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec notify url(%s) res(%v)", d.notifyURL, res)
err = ecode.CreativeElecErr
return
}
nt = res.Data
return
}
// Status get elec setting status.
func (d *Dao) Status(c context.Context, mid int64, ip string) (st *elec.Status, err error) {
params := url.Values{}
params.Set("act", "appkey")
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data struct {
Info *elec.Status `json:"info"`
} `json:"data"`
}
if err = d.client.Get(c, d.getStatusURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.getStatusURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.getStatusURL, res)
err = ecode.CreativeElecErr
return
}
st = res.Data.Info
return
}
// UpStatus update elec setting status.
func (d *Dao) UpStatus(c context.Context, mid int64, spday int, ip string) (err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("appkey", conf.Conf.App.Key)
params.Set("appsecret", conf.Conf.App.Secret)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("display_specialday", strconv.Itoa(spday))
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
var (
query, _ = tool.Sign(params)
url string
)
url = d.setStatusURL + "?" + query
// new request
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
var res struct {
Code int `json:"code"`
Data struct {
Ret int `json:"ret"`
} `json:"data"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("user elec update setting url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
err = ecode.CreativeElecErr
}
return
}
// RecentRank get recent rank.
func (d *Dao) RecentRank(c context.Context, mid, size int64, ip string) (rec []*elec.Rank, err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("size", strconv.FormatInt(size, 10))
var res struct {
Code int `json:"code"`
Data struct {
List []*elec.Rank `json:"list"`
} `json:"data"`
}
if err = d.client.Get(c, d.recentRankURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.recentRankURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.recentRankURL, res)
err = ecode.CreativeElecErr
return
}
rec = res.Data.List
return
}
// CurrentRank get current rank.
func (d *Dao) CurrentRank(c context.Context, mid int64, ip string) (cur []*elec.Rank, err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data struct {
List []*elec.Rank `json:"list"`
} `json:"data"`
}
if err = d.client.Get(c, d.currentRankURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.currentRankURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.currentRankURL, res)
err = ecode.CreativeElecErr
return
}
cur = res.Data.List
return
}
// TotalRank get total rank.
func (d *Dao) TotalRank(c context.Context, mid int64, ip string) (tol []*elec.Rank, err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data struct {
List []*elec.Rank `json:"list"`
} `json:"data"`
}
if err = d.client.Get(c, d.totalRankURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.totalRankURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.totalRankURL, res)
err = ecode.CreativeElecErr
return
}
tol = res.Data.List
return
}
// DailyBill daily settlement.
func (d *Dao) DailyBill(c context.Context, mid int64, pn, ps int, begin, end, ip string) (bl *elec.BillList, err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("appkey", conf.Conf.App.Key)
params.Set("appsecret", conf.Conf.App.Secret)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("page_no", strconv.Itoa(pn))
params.Set("page_size", strconv.Itoa(ps))
params.Set("begin_time", begin)
params.Set("end_time", end)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
// url
var (
query, _ = tool.Sign(params)
url string
)
url = d.dailyBillURL + "?" + query
// new request
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
var res struct {
Code int `json:"code"`
Data *elec.BillList `json:"data"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("user elec daily bill url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
err = ecode.CreativeElecErr
}
bl = res.Data
return
}
// Balance get battery balance.
func (d *Dao) Balance(c context.Context, mid int64, ip string) (bal *elec.Balance, err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("appkey", conf.Conf.App.Key)
params.Set("appsecret", conf.Conf.App.Secret)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
var (
query, _ = tool.Sign(params)
url string
)
url = d.balanceURL + "?" + query
// new request
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
var res struct {
Code int `json:"code"`
Data *elec.Balance `json:"data"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("user elec balance url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
err = ecode.CreativeElecErr
}
bal = res.Data
return
}
// RecentElec get aid & elec_num.
func (d *Dao) RecentElec(c context.Context, mid int64, pn, ps int, ip string) (rec *elec.RecentElecList, err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("pn", strconv.Itoa(pn))
params.Set("ps", strconv.Itoa(ps))
var res struct {
Code int `json:"code"`
Data *elec.RecentElecList `json:"data"`
}
if err = d.client.Get(c, d.recentElecURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.recentElecURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.recentElecURL, res)
err = ecode.CreativeElecErr
return
}
rec = res.Data
return
}
// RemarkList get remark list.
func (d *Dao) RemarkList(c context.Context, mid int64, pn, ps int, begin, end, ip string) (rec *elec.RemarkList, err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("pn", strconv.Itoa(pn))
params.Set("ps", strconv.Itoa(ps))
params.Set("start_time", begin)
params.Set("end_time", end)
var res struct {
Code int `json:"code"`
Data *elec.RemarkList `json:"data"`
}
if err = d.client.Get(c, d.remarkListURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.remarkListURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.remarkListURL, res)
err = ecode.CreativeElecErr
return
}
rec = res.Data
return
}
// RemarkDetail get remark detail.
func (d *Dao) RemarkDetail(c context.Context, mid, id int64, ip string) (re *elec.Remark, err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("id", strconv.FormatInt(id, 10))
var res struct {
Code int `json:"code"`
Data struct {
Info *elec.Remark `json:"info"`
} `json:"data"`
}
if err = d.client.Get(c, d.remarkDetailURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.remarkDetailURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.remarkDetailURL, res)
err = ecode.CreativeElecErr
return
}
re = res.Data.Info
return
}
// Remark reply a msg.
func (d *Dao) Remark(c context.Context, mid, id int64, msg, ak, ck, ip string) (status int, err error) {
params := url.Values{}
params.Set("type", "json")
params.Set("act", "appkey")
params.Set("access_key", ak)
params.Set("appkey", conf.Conf.App.Key)
params.Set("appsecret", conf.Conf.App.Secret)
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("id", strconv.FormatInt(id, 10))
params.Set("message", msg)
params.Set("ts", strconv.FormatInt(time.Now().Unix(), 10))
var (
query, _ = tool.Sign(params)
url string
)
url = d.remarkURL + "?" + query
// new request
req, err := http.NewRequest("POST", url, nil)
if err != nil {
log.Error("http.NewRequest(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
req.Header.Set("X-BACKEND-BILI-REAL-IP", ip)
req.Header.Set("Cookie", ck)
var res struct {
Code int `json:"code"`
Data struct {
Status int `json:"status"`
} `json:"data"`
}
if err = d.client.Do(c, req, &res); err != nil {
log.Error("d.client.Do(%s) error(%v); mid(%d), ip(%s)", url, err, mid, ip)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("user elec daily bill url(%s) res(%v); mid(%d), ip(%s)", url, res, mid, ip)
if res.Code == 61001 || res.Code == 61002 {
err = ecode.Int(res.Code)
} else {
err = ecode.CreativeElecErr
}
}
status = res.Data.Status
return
}

View File

@@ -0,0 +1,250 @@
package elec
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestElecUserInfo(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("UserInfo", t, func(ctx convey.C) {
st, err := d.UserInfo(c, mid, ip)
ctx.Convey("Then err should be nil.st should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(st, convey.ShouldNotBeNil)
})
})
}
func TestElecUserUpdate(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
st = int8(1)
ip = "127.0.0.1"
)
convey.Convey("UserUpdate", t, func(ctx convey.C) {
u, err := d.UserUpdate(c, mid, st, ip)
ctx.Convey("Then err should be nil.u should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(u, convey.ShouldNotBeNil)
})
})
}
func TestElecArcUpdate(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
aid = int64(10106491)
st = int8(1)
ip = "127.0.0.1"
)
convey.Convey("ArcUpdate", t, func(ctx convey.C) {
err := d.ArcUpdate(c, mid, aid, st, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestElecNotify(t *testing.T) {
var (
c = context.TODO()
ip = ""
)
convey.Convey("Notify", t, func(ctx convey.C) {
nt, err := d.Notify(c, ip)
ctx.Convey("Then err should be nil.nt should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(nt, convey.ShouldNotBeNil)
})
})
}
func TestElecStatus(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("Status", t, func(ctx convey.C) {
st, err := d.Status(c, mid, ip)
ctx.Convey("Then err should be nil.st should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(st, convey.ShouldNotBeNil)
})
})
}
func TestElecUpStatus(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
spday = int(1)
ip = "127.0.0.1"
)
convey.Convey("UpStatus", t, func(ctx convey.C) {
err := d.UpStatus(c, mid, spday, ip)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
}
func TestElecRecentRank(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
size = int64(10)
ip = "127.0.0.1"
)
convey.Convey("RecentRank", t, func(ctx convey.C) {
rec, err := d.RecentRank(c, mid, size, ip)
ctx.Convey("Then err should be nil.rec should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rec, convey.ShouldNotBeNil)
})
})
}
func TestElecCurrentRank(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("CurrentRank", t, func(ctx convey.C) {
cur, err := d.CurrentRank(c, mid, ip)
ctx.Convey("Then err should be nil.cur should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cur, convey.ShouldNotBeNil)
})
})
}
func TestElecTotalRank(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("TotalRank", t, func(ctx convey.C) {
tol, err := d.TotalRank(c, mid, ip)
ctx.Convey("Then err should be nil.tol should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(tol, convey.ShouldNotBeNil)
})
})
}
func TestElecDailyBill(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
pn = int(1)
ps = int(10)
ip = "127.0.0.1"
begin = "2018-01-04"
end = "2019-01-04"
)
convey.Convey("DailyBill", t, func(ctx convey.C) {
bl, err := d.DailyBill(c, mid, pn, ps, begin, end, ip)
ctx.Convey("Then err should be nil.bl should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(bl, convey.ShouldNotBeNil)
})
})
}
func TestElecBalance(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
ip = "127.0.0.1"
)
convey.Convey("Balance", t, func(ctx convey.C) {
bal, err := d.Balance(c, mid, ip)
ctx.Convey("Then err should be nil.bal should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(bal, convey.ShouldNotBeNil)
})
})
}
func TestElecRecentElec(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
pn = int(1)
ps = int(10)
ip = "127.0.0.1"
)
convey.Convey("RecentElec", t, func(ctx convey.C) {
rec, err := d.RecentElec(c, mid, pn, ps, ip)
ctx.Convey("Then err should be nil.rec should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rec, convey.ShouldNotBeNil)
})
})
}
func TestElecRemarkList(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
pn = int(1)
ps = int(10)
begin = ""
end = ""
ip = "127.0.0.1"
)
convey.Convey("RemarkList", t, func(ctx convey.C) {
rec, err := d.RemarkList(c, mid, pn, ps, begin, end, ip)
ctx.Convey("Then err should be nil.rec should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rec, convey.ShouldNotBeNil)
})
})
}
func TestElecRemarkDetail(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
id = int64(0)
ip = "127.0.0.1"
)
convey.Convey("RemarkDetail", t, func(ctx convey.C) {
re, err := d.RemarkDetail(c, mid, id, ip)
ctx.Convey("Then err should be nil.re should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(re, convey.ShouldNotBeNil)
})
})
}
func TestElecRemark(t *testing.T) {
var (
c = context.TODO()
mid = int64(2089809)
id = int64(0)
msg = ""
ak = ""
ck = ""
ip = "127.0.0.1"
)
convey.Convey("Remark", t, func(ctx convey.C) {
status, err := d.Remark(c, mid, id, msg, ak, ck, ip)
ctx.Convey("Then err should be nil.status should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(status, convey.ShouldNotBeNil)
})
})
}

View File

@@ -0,0 +1,178 @@
package elec
import (
"context"
"net/url"
"strconv"
"go-common/app/interface/main/creative/conf"
"go-common/app/interface/main/creative/model/elec"
"go-common/library/ecode"
"go-common/library/log"
httpx "go-common/library/net/http/blademaster"
"go-common/library/xstr"
)
const (
_userStatURL = "/internal/member/info"
_arcStatURL = "/internal/member/show"
_elecRelationURI = "/api/query.elec.relation.do"
)
// Dao define
type Dao struct {
c *conf.Config
// http
client *httpx.Client
// uri
userStatURI string
arcStatURI string
elecRelationURI string
// user
userInfoURL string
userJoinURL string
userExitURL string
// arc
arcOpenURL string
arcCloseURL string
notifyURL string
// status
getStatusURL string
setStatusURL string
// rank
recentRankURL string
currentRankURL string
totalRankURL string
// money
dailyBillURL string
balanceURL string
// recent elec for app
recentElecURL string
// elec remark
remarkListURL string
remarkDetailURL string
remarkURL string
}
// New init dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
client: httpx.NewClient(c.HTTPClient.Slow),
userStatURI: c.Host.Elec + _userStatURL,
arcStatURI: c.Host.Elec + _arcStatURL,
elecRelationURI: c.Host.Elec + _elecRelationURI,
// api elec.
userInfoURL: c.Host.Elec + _userInfoURI,
userJoinURL: c.Host.Elec + _userJoinURI,
userExitURL: c.Host.Elec + _userExitURI,
arcOpenURL: c.Host.Elec + _arcOpenURI,
arcCloseURL: c.Host.Elec + _arcCloseURI,
notifyURL: c.Host.Elec + _notifyURI,
getStatusURL: c.Host.Elec + _getStatusURI,
setStatusURL: c.Host.Elec + _setStatusURI,
recentRankURL: c.Host.Elec + _recentRankURI,
currentRankURL: c.Host.Elec + _currentRankURI,
totalRankURL: c.Host.Elec + _totalRankURI,
dailyBillURL: c.Host.Elec + _dailyBillURI,
balanceURL: c.Host.Elec + _balanceURI,
recentElecURL: c.Host.Elec + _recentElecURI,
remarkListURL: c.Host.Elec + _remarkListURI,
remarkDetailURL: c.Host.Elec + _remarkDetailURI,
remarkURL: c.Host.Elec + _remarkURI,
}
return
}
// UserState get user elec state.
func (d *Dao) UserState(c context.Context, mid int64, ip string) (data *elec.UserState, err error) {
params := url.Values{}
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
Data struct {
Mid int `json:"mid"`
State int `json:"state"`
Reason string `json:"reason"`
Count int `json:"count"`
CTime string `json:"ctime"`
} `json:"data"`
}
if err = d.client.Get(c, d.userInfoURL, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.userInfoURL+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
log.Info("UserState d.userInfoURL url(%s), code(%d)", d.userInfoURL+"?"+params.Encode(), res.Code)
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.userInfoURL, res)
err = ecode.CreativeElecErr
return
}
data = &elec.UserState{
Mid: strconv.Itoa(res.Data.Mid),
State: strconv.Itoa(res.Data.State),
Count: strconv.Itoa(res.Data.Count),
CTime: res.Data.CTime,
Reason: res.Data.Reason,
}
return
}
// ArchiveState get arc elec state.
func (d *Dao) ArchiveState(c context.Context, aid, mid int64, ip string) (data *elec.ArcState, err error) {
params := url.Values{}
params.Set("upmid", strconv.FormatInt(mid, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("aid", strconv.FormatInt(aid, 10))
params.Set("nolist", "1")
var res struct {
Code int `json:"code"`
Data *elec.ArcState `json:"data"`
}
if err = d.client.Get(c, d.arcStatURI, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.arcStatURI+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
log.Info("ArchiveState d.arcStatURI url(%s), code(%d)", d.arcStatURI+"?"+params.Encode(), res.Code)
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.arcStatURI, res)
err = ecode.CreativeElecErr
return
}
data = res.Data
return
}
// ElecRelation check if multi user charged.
func (d *Dao) ElecRelation(c context.Context, mid int64, mids []int64, ip string) (chargeMap map[int64]int, err error) {
params := url.Values{}
params.Set("act", "appkey")
params.Set("type", "json")
params.Set("up_mid", strconv.Itoa(int(mid)))
params.Set("mids", xstr.JoinInts(mids))
var res struct {
Code int `json:"code"`
Data *elec.EleRelation `json:"data"`
}
if err = d.client.Get(c, d.elecRelationURI, ip, params, &res); err != nil {
log.Error("elec url(%s) response(%v) error(%v)", d.elecRelationURI+"?"+params.Encode(), res, err)
err = ecode.CreativeElecErr
return
}
if res.Code != 0 {
log.Error("elec url(%s) res(%v)", d.elecRelationURI, res)
err = ecode.CreativeElecErr
return
}
chargeMap = map[int64]int{}
for _, v := range res.Data.RetList {
if !v.IsElec {
chargeMap[v.Mid] = 0
} else {
chargeMap[v.Mid] = 1
}
}
return
}

View File

@@ -0,0 +1,35 @@
package elec
import (
"flag"
"go-common/app/interface/main/creative/conf"
"os"
"testing"
)
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)
m.Run()
os.Exit(0)
}