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,102 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"activity_test.go",
"auto_test.go",
"av_breach_test.go",
"bgm_white_list_test.go",
"blacklist_test.go",
"budget_test.go",
"charge_ratio_test.go",
"cheat_test.go",
"dao_test.go",
"data_test.go",
"email_test.go",
"hbase_test.go",
"income_test.go",
"lottery_test.go",
"tag_test.go",
"task_status_test.go",
"up_bill_test.go",
"up_category_info_test.go",
"up_info_video_test.go",
"up_quality_info_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//app/job/main/growup/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"activity.go",
"auto.go",
"av_breach.go",
"bgm_white_list.go",
"blacklist.go",
"budget.go",
"charge_ratio.go",
"cheat.go",
"dao.go",
"data.go",
"email.go",
"hbase.go",
"income.go",
"lottery.go",
"tag.go",
"task_status.go",
"up_bill.go",
"up_category_info.go",
"up_info_video.go",
"up_quality_info.go",
],
importpath = "go-common/app/job/main/growup/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//app/job/main/growup/model:go_default_library",
"//library/database/hbase.v2:go_default_library",
"//library/database/sql: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",
"//library/xstr:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/job/main/growup/dao/charge:all-srcs",
"//app/job/main/growup/dao/dataplatform:all-srcs",
"//app/job/main/growup/dao/email:all-srcs",
"//app/job/main/growup/dao/income:all-srcs",
"//app/job/main/growup/dao/tag:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,151 @@
package dao
import (
"context"
"fmt"
"go-common/app/job/main/growup/model"
"go-common/library/log"
)
const (
_activitisSQL = "SELECT id,name,signed_start,signed_end,sign_up,sign_up_start,sign_up_end,object,upload_start,upload_end,win_type,require_items,require_value,statistics_start,statistics_end,bonus_type,bonus_time,progress_frequency,update_page,progress_start,progress_end,progress_sync,bonus_query,bonus_query_start,bonus_query_end FROM creative_activity"
_upActivitySQL = "SELECT mid,activity_id,state,success_time FROM up_activity WHERE activity_id = ? AND state > 0 AND is_deleted = 0"
_activityBonuseSQL = "SELECT bonus_money,ranking FROM activity_bonus WHERE activity_id = ?"
_upAvInfoSQL = "SELECT id,av_id,mid,upload_time FROM up_av_info WHERE id > ? ORDER BY id LIMIT ?"
_archiveInfoSQL = "SELECT id,av_id,state,likes,share,play,reply,danmu FROM activity_archive_info WHERE id > ? AND activity_id = ? ORDER BY id LIMIT ?"
_inUpActivitySQL = "INSERT INTO up_activity(mid,nickname,activity_id,aids,aid_num,ranking,bonus,state,success_time) VALUES %s ON DUPLICATE KEY UPDATE aids=VALUES(aids),aid_num=VALUES(aid_num),ranking=VALUES(ranking),bonus=VALUES(bonus),state=VALUES(state)"
_updateUpStateSQL = "UPDATE up_activity SET state = ?, bonus = 0, ranking = 0, aids = '' WHERE activity_id = ? AND state = ?"
)
// GetCActivities get activity by query
func (d *Dao) GetCActivities(c context.Context) (acs []*model.CActivity, err error) {
acs = make([]*model.CActivity, 0)
rows, err := d.db.Query(c, _activitisSQL)
if err != nil {
log.Error("GetCActivities d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
ac := &model.CActivity{}
err = rows.Scan(&ac.ID, &ac.Name, &ac.SignedStart, &ac.SignedEnd, &ac.SignUp, &ac.SignUpStart, &ac.SignUpEnd, &ac.Object, &ac.UploadStart, &ac.UploadEnd, &ac.WinType, &ac.RequireItems, &ac.RequireValue, &ac.StatisticsStart, &ac.StatisticsEnd, &ac.BonusType, &ac.BonusTime, &ac.ProgressFrequency, &ac.UpdatePage, &ac.ProgressStart, &ac.ProgressEnd, &ac.ProgressSync, &ac.BonusQuery, &ac.BonusQuerStart, &ac.BonusQueryEnd)
if err != nil {
log.Error("GetCActivities row.Scan error(%v)", err)
return
}
acs = append(acs, ac)
}
err = rows.Err()
return
}
// ListUpActivity get up from up_activity
func (d *Dao) ListUpActivity(c context.Context, id int64) (ups []*model.UpActivity, err error) {
ups = make([]*model.UpActivity, 0)
rows, err := d.db.Query(c, _upActivitySQL, id)
if err != nil {
log.Error("ListUpActivity d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
up := &model.UpActivity{}
err = rows.Scan(&up.MID, &up.ActivityID, &up.State, &up.SuccessTime)
if err != nil {
log.Error("ListUpActivity rows.Scan error(%v)", err)
return
}
ups = append(ups, up)
}
err = rows.Err()
return
}
// GetActivityBonus get activity_bonus by activity_id
func (d *Dao) GetActivityBonus(c context.Context, id int64) (actBonus map[int64]int64, err error) {
actBonus = make(map[int64]int64)
rows, err := d.db.Query(c, _activityBonuseSQL, id)
if err != nil {
log.Error("GetActivityBonus d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var rank, money int64
err = rows.Scan(&money, &rank)
if err != nil {
log.Error("GetActivityBonus rows.Scan error(%v)", err)
return
}
actBonus[rank] = money
}
err = rows.Err()
return
}
// GetAvUploadByMID get up_av_info by mid
func (d *Dao) GetAvUploadByMID(c context.Context, id int64, limit int) (avs []*model.AvUpload, err error) {
avs = make([]*model.AvUpload, 0)
rows, err := d.db.Query(c, _upAvInfoSQL, id, limit)
if err != nil {
log.Error("GetAvUploadByMID d.dbQuery error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
av := &model.AvUpload{}
err = rows.Scan(&av.ID, &av.AvID, &av.MID, &av.UploadTime)
if err != nil {
log.Error("GetAvUploadByMID rows.Scan error(%v)", err)
return
}
avs = append(avs, av)
}
return
}
// GetArchiveInfo get activity archive info
func (d *Dao) GetArchiveInfo(c context.Context, activityID, id int64, limit int) (avs []*model.ArchiveStat, err error) {
avs = make([]*model.ArchiveStat, 0)
rows, err := d.db.Query(c, _archiveInfoSQL, id, activityID, limit)
if err != nil {
log.Error("GetArchiveInfo d.dbQuery error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
av := &model.ArchiveStat{}
err = rows.Scan(&av.ID, &av.AvID, &av.State, &av.Like, &av.Share, &av.Play, &av.Reply, &av.Dm)
if err != nil {
log.Error("GetArchiveInfo rows.Scan error(%v)", err)
return
}
avs = append(avs, av)
}
return
}
// UpdateUpActivityState update up_activity state
func (d *Dao) UpdateUpActivityState(c context.Context, id int64, oldState, newState int) (count int64, err error) {
res, err := d.db.Exec(c, _updateUpStateSQL, newState, id, oldState)
if err != nil {
log.Error("UpdateUpActivityState tx.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// InsertUpActivityBatch insert up_activity
func (d *Dao) InsertUpActivityBatch(c context.Context, vals string) (count int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inUpActivitySQL, vals))
if err != nil {
log.Error("InsertUpActivityBatch d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,124 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetCActivities(t *testing.T) {
convey.Convey("GetCActivities", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
acs, err := d.GetCActivities(c)
ctx.Convey("Then err should be nil.acs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(acs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListUpActivity(t *testing.T) {
convey.Convey("ListUpActivity", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ups, err := d.ListUpActivity(c, id)
ctx.Convey("Then err should be nil.ups should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ups, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetActivityBonus(t *testing.T) {
convey.Convey("GetActivityBonus", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
actBonus, err := d.GetActivityBonus(c, id)
ctx.Convey("Then err should be nil.actBonus should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(actBonus, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvUploadByMID(t *testing.T) {
convey.Convey("GetAvUploadByMID", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(10)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
avs, err := d.GetAvUploadByMID(c, id, limit)
ctx.Convey("Then err should be nil.avs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(avs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetArchiveInfo(t *testing.T) {
convey.Convey("GetArchiveInfo", t, func(ctx convey.C) {
var (
c = context.Background()
activityID = int64(10)
id = int64(100)
limit = int(200)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
avs, err := d.GetArchiveInfo(c, activityID, id, limit)
ctx.Convey("Then err should be nil.avs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(avs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateUpActivityState(t *testing.T) {
convey.Convey("UpdateUpActivityState", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(100)
oldState = int(10)
newState = int(200)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
count, err := d.UpdateUpActivityState(c, id, oldState, newState)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertUpActivityBatch(t *testing.T) {
convey.Convey("InsertUpActivityBatch", t, func(ctx convey.C) {
var (
c = context.Background()
vals = "(10, 'test', 100, 90, 100, 1, 1000, 3, '2018-06-23')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
count, err := d.InsertUpActivityBatch(c, vals)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,108 @@
package dao
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/library/log"
"go-common/library/xstr"
)
// DoAvBreach av breach by api.
func (d *Dao) DoAvBreach(c context.Context, mid int64, aid int64, ctype int, reason string) (err error) {
params := url.Values{}
params.Set("type", strconv.Itoa(ctype))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("aids", strconv.FormatInt(aid, 10))
params.Set("reason", reason)
var res struct {
Code int `json:"code"`
Message string `json:"message"`
}
url := d.breachURL
if err = d.client.Post(c, url, "", params, &res); err != nil {
log.Error("d.client.Post url(%s) error(%v)", url+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("DoAvBreach code != 0. res.Code(%d) | url(%s) error(%v)", res.Code, url+"?"+params.Encode(), res.Message)
err = fmt.Errorf("DoAvBreach error(%s)", res.Message)
}
return
}
// DoUpForbid up forbid by api
func (d *Dao) DoUpForbid(c context.Context, mid int64, days int, ctype int, reason string) (err error) {
params := url.Values{}
params.Set("type", strconv.Itoa(ctype))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("days", strconv.Itoa(days))
params.Set("reason", reason)
var res struct {
Code int `json:"code"`
Message string `json:"message"`
}
url := d.forbidURL
if err = d.client.Post(c, url, "", params, &res); err != nil {
log.Error("d.client.Post url(%s) error(%v)", url+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("DoUpForbid code != 0. res.Code(%d) | url(%s) error(%v)", res.Code, url+"?"+params.Encode(), res.Message)
err = fmt.Errorf("DoUpForbid error(%s)", res.Message)
}
return
}
// DoUpDismiss up dismiss by api
func (d *Dao) DoUpDismiss(c context.Context, mid int64, ctype int, reason string) (err error) {
params := url.Values{}
params.Set("type", strconv.Itoa(ctype))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("reason", reason)
var res struct {
Code int `json:"code"`
Message string `json:"message"`
}
url := d.dismissURL
if err = d.client.Post(c, url, "", params, &res); err != nil {
log.Error("d.client.Post url(%s) error(%v)", url+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("DoUpDismiss code != 0. res.Code(%d) | url(%s) error(%v)", res.Code, url+"?"+params.Encode(), res.Message)
err = fmt.Errorf("DoUpDismiss error(%s)", res.Message)
}
return
}
// DoUpPass up pass by api
func (d *Dao) DoUpPass(c context.Context, mids []int64, ctype int) (err error) {
params := url.Values{}
params.Set("type", strconv.Itoa(ctype))
params.Set("mids", xstr.JoinInts(mids))
var res struct {
Code int `json:"code"`
Message string `json:"message"`
}
url := d.passURL
if err = d.client.Post(c, url, "", params, &res); err != nil {
log.Error("d.client.Post url(%s) error(%v)", url+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("DoUpPass code != 0. res.Code(%d) | url(%s) error(%v)", res.Code, url+"?"+params.Encode(), res.Message)
err = fmt.Errorf("DoUpPass error(%s)", res.Message)
}
return
}

View File

@@ -0,0 +1,77 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoDoAvBreach(t *testing.T) {
convey.Convey("DoAvBreach", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(10)
aid = int64(100)
ctype = int(1)
reason = "test"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DoAvBreach(c, mid, aid, ctype, reason)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDoUpForbid(t *testing.T) {
convey.Convey("DoUpForbid", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(10)
days = int(100)
ctype = int(1)
reason = "test"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DoUpForbid(c, mid, days, ctype, reason)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDoUpDismiss(t *testing.T) {
convey.Convey("DoUpDismiss", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(100)
ctype = int(1)
reason = "test"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DoUpDismiss(c, mid, ctype, reason)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDoUpPass(t *testing.T) {
convey.Convey("DoUpPass", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{1, 2, 3}
ctype = int(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.DoUpPass(c, mids, ctype)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,89 @@
package dao
import (
"context"
"fmt"
"go-common/app/job/main/growup/model"
"go-common/library/log"
)
const (
// list av_breach_record
_avBreachRecordSQL = "SELECT id,av_id,mid,money,cdate,reason FROM av_breach_record WHERE cdate >= '%s' AND cdate <= '%s'"
_avBreachPreSQL = "SELECT aid,mid FROM av_breach_pre WHERE cdate = '%s' AND ctype = ? AND state = ?"
// insert
_inAvBreachPreSQL = "INSERT INTO av_breach_pre(aid, mid, cdate, ctype, state) VALUES(%s) ON DUPLICATE KEY UPDATE state=VALUES(state)"
// update
_upAvBreachPresSQL = "UPDATE av_breach_pre SET state = ? WHERE aid = ? AND ctype = ? AND cdate >= '%s' AND state = 1"
)
// GetAvBreach get av_breach by date
func (d *Dao) GetAvBreach(c context.Context, start, end string) (avs []*model.AvBreach, err error) {
avs = make([]*model.AvBreach, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_avBreachRecordSQL, start, end))
if err != nil {
log.Error("GetAvBreach d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.AvBreach{}
err = rows.Scan(&list.ID, &list.AvID, &list.MID, &list.Money, &list.Date, &list.Reason)
if err != nil {
log.Error("GetAvBreach rows scan error(%v)", err)
return
}
avs = append(avs, list)
}
err = rows.Err()
return
}
// GetAvBreachPre get av breach pre by date and state
func (d *Dao) GetAvBreachPre(c context.Context, ctype, state int, cdate string) (avs []*model.AvBreach, err error) {
avs = make([]*model.AvBreach, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_avBreachPreSQL, cdate), ctype, state)
if err != nil {
log.Error("GetAvBreachPre d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.AvBreach{}
err = rows.Scan(&list.AvID, &list.MID)
if err != nil {
log.Error("GetAvBreachPre rows scan error(%v)", err)
return
}
avs = append(avs, list)
}
err = rows.Err()
return
}
// InsertAvBreachPre insert into av_breach_pre
func (d *Dao) InsertAvBreachPre(c context.Context, val string) (rows int64, err error) {
if val == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inAvBreachPreSQL, val))
if err != nil {
log.Error("InsertAvBreachPre(%s) tx.Exec error(%v)", val, err)
return
}
return res.RowsAffected()
}
// UpdateAvBreachPre update av breach pre
func (d *Dao) UpdateAvBreachPre(c context.Context, aid, ctype int64, date string, state int) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_upAvBreachPresSQL, date), state, aid, ctype)
if err != nil {
log.Error("UpdateAvBreachPre tx.Exec error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,60 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetAvBreach(t *testing.T) {
convey.Convey("GetAvBreach", t, func(ctx convey.C) {
var (
c = context.Background()
start = "2018-06-20"
end = "2018-10-20"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
avs, err := d.GetAvBreach(c, start, end)
ctx.Convey("Then err should be nil.avs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(avs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvBreachPre(t *testing.T) {
convey.Convey("GetAvBreachPre", t, func(ctx convey.C) {
var (
c = context.Background()
ctype = int(1)
state = int(3)
cdate = "2018-10-23"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
avs, err := d.GetAvBreachPre(c, ctype, state, cdate)
ctx.Convey("Then err should be nil.avs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(avs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertAvBreachPre(t *testing.T) {
convey.Convey("InsertAvBreachPre", t, func(ctx convey.C) {
var (
c = context.Background()
val = "100, 29, '2018-06-23', 1, 3"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "DELETE FROM av_breach_pre WHERE aid=100")
rows, err := d.InsertAvBreachPre(c, val)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,19 @@
package dao
import (
"context"
)
const (
// insert
_inBgmWhiteSQL = "INSERT INTO bgm_white_list(mid) VALUES (?) ON DUPLICATE KEY UPDATE mid = VALUES(mid)"
)
// InsertBgmWhiteList insert into bgm_white_list
func (d *Dao) InsertBgmWhiteList(c context.Context, mid int64) (rows int64, err error) {
res, err := d.db.Exec(c, _inBgmWhiteSQL, mid)
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoInsertBgmWhiteList(t *testing.T) {
convey.Convey("InsertBgmWhiteList", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(111)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
rows, err := d.InsertBgmWhiteList(c, mid)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,135 @@
package dao
import (
"context"
"database/sql"
"fmt"
"time"
"go-common/app/job/main/growup/model"
"go-common/library/log"
)
const (
// list all blacklist
_listBlacklistSQL = "SELECT av_id,mid,reason,ctype,has_signed,nickname FROM av_black_list WHERE %s is_delete = 0 LIMIT ?,?"
// query last record ctime by reason and id
_queryOneByReasonSQL = "SELECT ctime FROM av_black_list WHERE reason = ? AND is_delete = 0 ORDER BY id DESC LIMIT 1"
// query record from bilibili_business_up_cooperate.business_order_sheet
_queryExecuteOrderSQL = "SELECT av_id, making_order_up_mid, ctime FROM business_order_sheet WHERE ctime >= ? AND ctime <= ? AND is_deleted = 0 ORDER BY ctime DESC"
// add to blacklist batch
_addBlacklistBatchSQL = "INSERT INTO av_black_list(av_id, mid, reason, ctype, has_signed, nickname) VALUES %s ON DUPLICATE KEY UPDATE mid=VALUES(mid),reason=VALUES(reason),has_signed=VALUES(has_signed),nickname=VALUES(nickname)"
// query mid, nickname from up_info_video
_queryHasSignUpInfoSQL = "SELECT mid, nickname FROM up_info_video where account_state = 3 AND is_deleted = 0 limit ?, ?"
)
// ListBlacklist list all blacklist
func (d *Dao) ListBlacklist(c context.Context, query string, from, limit int) (backlists []*model.Blacklist, err error) {
if query != "" {
query += " AND"
}
rows, err := d.db.Query(c, fmt.Sprintf(_listBlacklistSQL, query), from, limit)
if err != nil {
log.Error("ListBlacklist d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.Blacklist{}
err = rows.Scan(&list.AvID, &list.MID, &list.Reason, &list.CType, &list.HasSigned, &list.Nickname)
if err != nil {
log.Error("ListBlacklist rows scan error(%v)", err)
return
}
backlists = append(backlists, list)
}
err = rows.Err()
return
}
// GetExecuteOrder get execute order by date
func (d *Dao) GetExecuteOrder(c context.Context, startTime, endTime time.Time) (executeOrders []*model.ExecuteOrder, err error) {
rows, err := d.rddb.Query(c, _queryExecuteOrderSQL, startTime, endTime)
if err != nil {
log.Error("GetExecuteOrder d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
order := &model.ExecuteOrder{}
err = rows.Scan(&order.AvID, &order.MID, &order.CTime)
if err != nil {
log.Error("GetExecuteOrder rows.Scan error(%v)", err)
return
}
if order.AvID > 0 {
executeOrders = append(executeOrders, order)
}
}
err = rows.Err()
return
}
// GetLastCtime get last ctime by query
func (d *Dao) GetLastCtime(c context.Context, reason int) (ctime int64, err error) {
row := d.db.QueryRow(c, _queryOneByReasonSQL, reason)
var t time.Time
err = row.Scan(&t)
if err != nil {
if err == sql.ErrNoRows {
err = nil
ctime = 0
} else {
log.Error("GetLastCtime row Scan error(%v)", err)
}
} else {
ctime = t.Unix()
}
return
}
// AddBlacklistBatch add batch to blacklist
func (d *Dao) AddBlacklistBatch(c context.Context, blacklist []*model.Blacklist) (count int64, err error) {
if len(blacklist) == 0 {
return
}
var vals string
for _, row := range blacklist {
vals += fmt.Sprintf("(%d, %d, %d, %d, %d, '%s'),", row.AvID, row.MID, row.Reason, row.CType, row.HasSigned, row.Nickname)
}
if len(vals) > 0 {
vals = vals[0 : len(vals)-1]
}
res, err := d.db.Exec(c, fmt.Sprintf(_addBlacklistBatchSQL, vals))
if err != nil {
log.Error("AddBlacklistBatch d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// GetHasSignUpInfo get all has signed up info
func (d *Dao) GetHasSignUpInfo(c context.Context, offset, limit int, m map[int64]string) (err error) {
rows, err := d.db.Query(c, _queryHasSignUpInfoSQL, offset, limit)
if err != nil {
log.Error("GetHasSignUpInfo d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var mid int64
var nickname string
err = rows.Scan(&mid, &nickname)
if err != nil {
log.Error("GetHasSignUpInfo rows.Scan error(%v)", err)
return
}
m[mid] = nickname
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,106 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
"go-common/app/job/main/growup/model"
)
func TestDaoListBlacklist(t *testing.T) {
convey.Convey("ListBlacklist", t, func(ctx convey.C) {
var (
c = context.Background()
query = "av_id=1"
from = int(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_black_list(av_id, mid, reason, ctype, has_signed, nickname) VALUES (1,2,1,3,1,'test') ON DUPLICATE KEY UPDATE mid=VALUES(mid),reason=VALUES(reason),has_signed=VALUES(has_signed),nickname=VALUES(nickname)")
backlists, err := d.ListBlacklist(c, query, from, limit)
ctx.Convey("Then err should be nil.backlists should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(backlists, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetExecuteOrder(t *testing.T) {
convey.Convey("GetExecuteOrder", t, func(ctx convey.C) {
var (
c = context.Background()
startTime = time.Now()
endTime = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
executeOrders, err := d.GetExecuteOrder(c, startTime, endTime)
ctx.Convey("Then err should not be nil.executeOrders should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(executeOrders, convey.ShouldBeNil)
})
})
})
}
func TestDaoGetLastCtime(t *testing.T) {
convey.Convey("GetLastCtime", t, func(ctx convey.C) {
var (
c = context.Background()
reason = int(3)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ctime, err := d.GetLastCtime(c, reason)
ctx.Convey("Then err should be nil.ctime should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ctime, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAddBlacklistBatch(t *testing.T) {
convey.Convey("AddBlacklistBatch", t, func(ctx convey.C) {
var (
c = context.Background()
blacklist = []*model.Blacklist{
&model.Blacklist{
ID: int64(10),
AvID: int64(100),
MID: int64(3),
Reason: 2,
CType: 1,
HasSigned: 1,
Nickname: "test",
},
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
count, err := d.AddBlacklistBatch(c, blacklist)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetHasSignUpInfo(t *testing.T) {
convey.Convey("GetHasSignUpInfo", t, func(ctx convey.C) {
var (
c = context.Background()
offset = int(0)
limit = int(100)
m = make(map[int64]string)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.GetHasSignUpInfo(c, offset, limit, m)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,58 @@
package dao
import (
"context"
"fmt"
"go-common/app/job/main/growup/model"
"go-common/library/log"
)
const (
// get expense_daily_info total_expense
_expenseDailyTotalSQL = "SELECT total_expense,ctype FROM expense_daily_info WHERE date = '%s'"
// insert
_insertDailyExpenseSQL = "INSERT INTO expense_daily_info(day_expense, up_count, av_count, up_avg_expense, av_avg_expense, total_expense, date, ctype) VALUES (%d,%d,%d,%d,%d,%d,'%s',%d) ON DUPLICATE KEY UPDATE day_expense = VALUES(day_expense), up_count = VALUES(up_count), av_count = VALUES(av_count), up_avg_expense = values(up_avg_expense), av_avg_expense = values(av_avg_expense), total_expense = values(total_expense), date = values(date)"
_insertMonthlyExpenseSQL = "INSERT INTO expense_monthly_info(month_expense, up_count, av_count, up_avg_expense, av_avg_expense, total_expense, date, month, ctype) VALUES (%d,%d,%d,%d,%d,%d,'%s','%s',%d) ON DUPLICATE KEY UPDATE month_expense = VALUES(month_expense), up_count = VALUES(up_count), av_count = VALUES(av_count), up_avg_expense = values(up_avg_expense), av_avg_expense = values(av_avg_expense), total_expense = values(total_expense), date = values(date)"
)
// GetTotalExpenseByDate get expense_daily_info by date
func (d *Dao) GetTotalExpenseByDate(c context.Context, date string) (expense map[int64]int64, err error) {
expense = make(map[int64]int64)
rows, err := d.db.Query(c, fmt.Sprintf(_expenseDailyTotalSQL, date))
if err != nil {
log.Error("GetTotalExpenseByDate d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var ctype, totalExpense int64
err = rows.Scan(&totalExpense, &ctype)
if err != nil {
log.Error("GetTotalExpenseByDate rows.Scan error(%v)", err)
return
}
expense[ctype] = totalExpense
}
err = rows.Err()
return
}
// InsertDailyExpense insert expense_daily_info
func (d *Dao) InsertDailyExpense(c context.Context, e *model.BudgetExpense) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_insertDailyExpenseSQL, e.Expense, e.UpCount, e.AvCount, e.UpAvgExpense, e.AvAvgExpense, e.TotalExpense, e.Date, e.CType))
if err != nil {
return
}
return res.RowsAffected()
}
// InsertMonthlyExpense insert expense_monthly_info
func (d *Dao) InsertMonthlyExpense(c context.Context, e *model.BudgetExpense) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_insertMonthlyExpenseSQL, e.Expense, e.UpCount, e.AvCount, e.UpAvgExpense, e.AvAvgExpense, e.TotalExpense, e.Date, e.Date.Format("2006-01"), e.CType))
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,76 @@
package dao
import (
"context"
"go-common/app/job/main/growup/model"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetTotalExpenseByDate(t *testing.T) {
convey.Convey("GetTotalExpenseByDate", t, func(ctx convey.C) {
var (
c = context.Background()
date = "2018-06-23"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
expense, err := d.GetTotalExpenseByDate(c, date)
ctx.Convey("Then err should be nil.expense should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(expense, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertDailyExpense(t *testing.T) {
convey.Convey("InsertDailyExpense", t, func(ctx convey.C) {
var (
c = context.Background()
e = &model.BudgetExpense{
Expense: 100,
UpCount: 100,
AvCount: 100,
UpAvgExpense: 100,
AvAvgExpense: 100,
Date: time.Now(),
TotalExpense: 100,
CType: 1,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertDailyExpense(c, e)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertMonthlyExpense(t *testing.T) {
convey.Convey("InsertMonthlyExpense", t, func(ctx convey.C) {
var (
c = context.Background()
e = &model.BudgetExpense{
Expense: 100,
UpCount: 100,
AvCount: 100,
UpAvgExpense: 100,
AvAvgExpense: 100,
Date: time.Now(),
TotalExpense: 100,
CType: 1,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertMonthlyExpense(c, e)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,65 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"av_charge.go",
"av_charge_statis.go",
"bgm.go",
"column_charge.go",
"dao.go",
"date_statia.go",
"up_charge.go",
],
importpath = "go-common/app/job/main/growup/dao/charge",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//app/job/main/growup/model/charge:go_default_library",
"//library/database/sql:go_default_library",
"//library/log: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 = [
"av_charge_statis_test.go",
"av_charge_test.go",
"bgm_test.go",
"column_charge_test.go",
"dao_test.go",
"date_statia_test.go",
"up_charge_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//app/job/main/growup/model/charge:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,79 @@
package charge
import (
"context"
"fmt"
"strconv"
"time"
model "go-common/app/job/main/growup/model/charge"
"go-common/library/log"
)
const (
_layout = "2006-01-02"
_avDailyChargeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_daily_charge_%s WHERE id > ? AND date = ? AND inc_charge > 0 ORDER BY id LIMIT ?"
_avWeeklyChargeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_weekly_charge WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
_avMonthlyChargeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_monthly_charge WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
_inAvChargeTableSQL = "INSERT INTO %s(av_id,mid,tag_id,is_original,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,upload_time) VALUES %s ON DUPLICATE KEY UPDATE danmaku_count=VALUES(danmaku_count),comment_count=VALUES(comment_count),collect_count=VALUES(collect_count),coin_count=VALUES(coin_count),share_count=VALUES(share_count),elec_pay_count=VALUES(elec_pay_count),total_play_count=VALUES(total_play_count),web_play_count=VALUES(web_play_count),app_play_count=VALUES(app_play_count),h5_play_count=VALUES(h5_play_count),lv_unknown=VALUES(lv_unknown),lv_0=VALUES(lv_0),lv_1=VALUES(lv_1),lv_2=VALUES(lv_2),lv_3=VALUES(lv_3),lv_4=VALUES(lv_4),lv_5=VALUES(lv_5),lv_6=VALUES(lv_6),v_score=VALUES(v_score),inc_charge=VALUES(inc_charge),total_charge=VALUES(total_charge)"
)
// IAvCharge av_charge_weekly monthly
type IAvCharge func(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error)
// AvDailyCharge av_daily_charge
func (d *Dao) AvDailyCharge(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error) {
month := date.Month()
monthStr := strconv.Itoa(int(month))
if month < 10 {
monthStr = "0" + monthStr
}
return d.GetAvCharge(c, fmt.Sprintf(_avDailyChargeSQL, monthStr), date.Format(_layout), id, limit)
}
// AvWeeklyCharge av_weekly_charge
func (d *Dao) AvWeeklyCharge(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error) {
return d.GetAvCharge(c, _avWeeklyChargeSQL, date.Format(_layout), id, limit)
}
// AvMonthlyCharge av_monthly_charge
func (d *Dao) AvMonthlyCharge(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error) {
return d.GetAvCharge(c, _avMonthlyChargeSQL, date.Format(_layout), id, limit)
}
// GetAvCharge get av_charge
func (d *Dao) GetAvCharge(c context.Context, sql string, date string, id int64, limit int) (data []*model.AvCharge, err error) {
rows, err := d.db.Query(c, sql, id, date, limit)
if err != nil {
log.Error("d.Query av_charge(%s) error(%v)", sql, err)
return
}
defer rows.Close()
for rows.Next() {
adc := &model.AvCharge{}
err = rows.Scan(&adc.ID, &adc.AvID, &adc.MID, &adc.TagID, &adc.IsOriginal, &adc.UploadTime, &adc.DanmakuCount, &adc.CommentCount, &adc.CollectCount, &adc.CoinCount, &adc.ShareCount, &adc.ElecPayCount, &adc.TotalPlayCount, &adc.WebPlayCount, &adc.AppPlayCount, &adc.H5PlayCount, &adc.LvUnknown, &adc.Lv0, &adc.Lv1, &adc.Lv2, &adc.Lv3, &adc.Lv4, &adc.Lv5, &adc.Lv6, &adc.VScore, &adc.IncCharge, &adc.TotalCharge, &adc.Date, &adc.IsDeleted, &adc.CTime, &adc.MTime)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
data = append(data, adc)
}
return
}
// InsertAvChargeTable add av charge batch
func (d *Dao) InsertAvChargeTable(c context.Context, vals, table string) (rows int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inAvChargeTableSQL, table, vals))
if err != nil {
log.Error("InsertAvChargeTable(%s) tx.Exec error(%v)", table, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,49 @@
package charge
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/charge"
"go-common/library/log"
)
const (
_avChargeStatisSQL = "SELECT id,av_id,mid,total_charge,upload_time FROM av_charge_statis WHERE id > ? ORDER BY id LIMIT ?"
_inAvChargeStatisSQL = "INSERT INTO av_charge_statis(av_id,mid,tag_id,is_original,total_charge,upload_time) VALUES %s ON DUPLICATE KEY UPDATE total_charge=VALUES(total_charge)"
)
// AvChargeStatis get av_charge_statis
func (d *Dao) AvChargeStatis(c context.Context, id int64, limit int) (data []*model.AvChargeStatis, err error) {
rows, err := d.db.Query(c, _avChargeStatisSQL, id, limit)
if err != nil {
log.Error("d.Query av_charge_statis error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
ads := &model.AvChargeStatis{}
err = rows.Scan(&ads.ID, &ads.AvID, &ads.MID, &ads.TotalCharge, &ads.UploadTime)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
data = append(data, ads)
}
return
}
// InsertAvChargeStatisBatch add av charge statis batch
func (d *Dao) InsertAvChargeStatisBatch(c context.Context, vals string) (count int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inAvChargeStatisSQL, vals))
if err != nil {
log.Error("InsertAvChargeStatisBatch d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,96 @@
package charge
import (
"bytes"
"context"
"strconv"
"testing"
"time"
model "go-common/app/job/main/growup/model/charge"
xtime "go-common/library/time"
. "github.com/smartystreets/goconvey/convey"
)
func Test_AvChargeStatis(t *testing.T) {
Convey("AvChargeStatis", t, func() {
_, err := d.AvChargeStatis(context.Background(), 0, 2000)
So(err, ShouldBeNil)
})
}
func Test_InsertAvChargeStatisBatch(t *testing.T) {
Convey("InsertAvChargeStatisBatch", t, func() {
c := context.Background()
d.db.Exec(c, "DELETE FROM av_charge_statis where av_id = 11")
avChargeStatis := []*model.AvChargeStatis{}
value := &model.AvChargeStatis{
AvID: 11,
MID: 11,
TagID: 11,
}
avChargeStatis = append(avChargeStatis, value)
vals := assembleAvChargeStatis(avChargeStatis)
count, err := d.InsertAvChargeStatisBatch(c, vals)
So(err, ShouldBeNil)
So(count, ShouldEqual, 1)
d.db.Exec(c, "DELETE FROM av_charge_statis where av_id = 11")
})
}
func benchmarkInsertAvChargeStatisBatch(size int64, b *testing.B) {
avChargeStatis := make([]*model.AvChargeStatis, size)
var i int64
for i = 0; i < size; i++ {
avChargeStatis[i] = &model.AvChargeStatis{
AvID: i,
MID: i,
TagID: i,
IsOriginal: int(i),
UploadTime: xtime.Time(time.Now().Unix()),
TotalCharge: i,
}
}
vals := assembleAvChargeStatis(avChargeStatis)
for n := 0; n < b.N; n++ {
d.InsertAvChargeStatisBatch(context.Background(), vals)
}
}
func BenchmarkInsertAvChargeStatisBatch100(b *testing.B) { benchmarkInsertAvChargeStatisBatch(100, b) }
func BenchmarkInsertAvChargeStatisBatch1000(b *testing.B) { benchmarkInsertAvChargeStatisBatch(1000, b) }
func BenchmarkInsertAvChargeStatisBatch10000(b *testing.B) {
benchmarkInsertAvChargeStatisBatch(10000, b)
}
func assembleAvChargeStatis(avChargeStatis []*model.AvChargeStatis) (vals string) {
var buf bytes.Buffer
for _, row := range avChargeStatis {
buf.WriteString("(")
buf.WriteString(strconv.FormatInt(row.AvID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.MID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.TagID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.Itoa(row.IsOriginal))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.TotalCharge, 10))
buf.WriteByte(',')
buf.WriteString(row.UploadTime.Time().Format(_layout))
buf.WriteString(")")
buf.WriteByte(',')
}
if buf.Len() > 0 {
buf.Truncate(buf.Len() - 1)
}
vals = buf.String()
buf.Reset()
return
}

View File

@@ -0,0 +1,103 @@
package charge
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestChargeAvDailyCharge(t *testing.T) {
convey.Convey("AvDailyCharge", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, 6, 24, 0, 0, 0, 0, time.Local)
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO av_daily_charge_06(av_id,mid,date,inc_charge) VALUES(1,2,'2018-06-24',100) ON DUPLICATE KEY UPDATE inc_charge=VALUES(inc_charge)")
data, err := d.AvDailyCharge(c, date, id, limit)
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 TestChargeAvWeeklyCharge(t *testing.T) {
convey.Convey("AvWeeklyCharge", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, 6, 24, 0, 0, 0, 0, time.Local)
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO av_weekly_charge(av_id,mid,date) VALUES(1,2,'2018-06-24') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
data, err := d.AvWeeklyCharge(c, date, id, limit)
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 TestChargeAvMonthlyCharge(t *testing.T) {
convey.Convey("AvMonthlyCharge", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, 6, 24, 0, 0, 0, 0, time.Local)
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO av_monthly_charge(av_id,mid,date) VALUES(1,2,'2018-06-24') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
data, err := d.AvMonthlyCharge(c, date, id, limit)
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 TestChargeGetAvCharge(t *testing.T) {
convey.Convey("GetAvCharge", t, func(ctx convey.C) {
var (
c = context.Background()
sql = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_monthly_charge WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
date = "2018-06-24"
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO av_daily_charge_06(av_id,mid,date) VALUES(1,2,'2018-06-24') ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
data, err := d.GetAvCharge(c, sql, date, id, limit)
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 TestChargeInsertAvChargeTable(t *testing.T) {
convey.Convey("InsertAvChargeTable", t, func(ctx convey.C) {
var (
c = context.Background()
vals = "(1,2,3,1,5,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,'2018-06-24', '2018-06-24')"
table = "av_weekly_charge"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertAvChargeTable(c, vals, table)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,106 @@
package charge
import (
"context"
"fmt"
"time"
model "go-common/app/job/main/growup/model/charge"
"go-common/library/log"
)
const (
_getBGMSQL = "SELECT id,mid,sid,aid,cid,join_at,title FROM background_music WHERE id > ? ORDER BY id LIMIT ?"
_bgmChargeSQL = "SELECT id,mid,sid,aid,cid,join_at,inc_charge,date FROM %s WHERE id > ? AND date = ? AND inc_charge > 0 ORDER BY id LIMIT ?"
_bgmStatisSQL = "SELECT id,mid,sid,aid,cid,total_charge FROM bgm_charge_statis WHERE id > ? ORDER BY id LIMIT ?"
_inBgmChargeTableSQL = "INSERT INTO %s(sid,aid,mid,cid,title,inc_charge,date,join_at) VALUES %s ON DUPLICATE KEY UPDATE inc_charge=VALUES(inc_charge)"
_inBgmStatisSQL = "INSERT INTO bgm_charge_statis(sid,aid,mid,cid,title,total_charge,join_at) VALUES %s ON DUPLICATE KEY UPDATE total_charge=VALUES(total_charge)"
)
// GetBgm get bgms
func (d *Dao) GetBgm(c context.Context, id int64, limit int64) (bs []*model.Bgm, last int64, err error) {
rows, err := d.db.Query(c, _getBGMSQL, id, limit)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
b := &model.Bgm{}
err = rows.Scan(&last, &b.MID, &b.SID, &b.AID, &b.CID, &b.JoinAt, &b.Title)
if err != nil {
return
}
bs = append(bs, b)
}
return
}
// BgmCharge get bgm charge by date
func (d *Dao) BgmCharge(c context.Context, date time.Time, id int64, limit int, table string) (bgms []*model.BgmCharge, err error) {
bgms = make([]*model.BgmCharge, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_bgmChargeSQL, table), id, date, limit)
if err != nil {
log.Error("BgmCharge d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
bgm := &model.BgmCharge{}
err = rows.Scan(&bgm.ID, &bgm.MID, &bgm.SID, &bgm.AID, &bgm.CID, &bgm.JoinAt, &bgm.IncCharge, &bgm.Date)
if err != nil {
log.Error("BgmCharge rows.Scan error(%v)", err)
return
}
bgms = append(bgms, bgm)
}
return
}
// BgmStatis bgm statis
func (d *Dao) BgmStatis(c context.Context, id int64, limit int) (bgms []*model.BgmStatis, err error) {
bgms = make([]*model.BgmStatis, 0)
rows, err := d.db.Query(c, _bgmStatisSQL, id, limit)
if err != nil {
log.Error("BgmStatis d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
bgm := &model.BgmStatis{}
err = rows.Scan(&bgm.ID, &bgm.MID, &bgm.SID, &bgm.AID, &bgm.CID, &bgm.TotalCharge)
if err != nil {
log.Error("BgmStatis rows.Scan error(%v)", err)
return
}
bgms = append(bgms, bgm)
}
return
}
// InsertBgmChargeTable insert bgm charge
func (d *Dao) InsertBgmChargeTable(c context.Context, vals, table string) (rows int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inBgmChargeTableSQL, table, vals))
if err != nil {
log.Error("InsertBgmChargeTable(%s) tx.Exec error(%v)", table, err)
return
}
return res.RowsAffected()
}
// InsertBgmStatisBatch insert bgm statis
func (d *Dao) InsertBgmStatisBatch(c context.Context, vals string) (rows int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inBgmStatisSQL, vals))
if err != nil {
log.Error("InsertBgmStatisBatch tx.Exec error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,99 @@
package charge
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestChargeGetBgm(t *testing.T) {
convey.Convey("GetBgm", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO backgroud_music(aid,sid) VALUES(1,2)")
bs, last, err := d.GetBgm(c, id, limit)
ctx.Convey("Then err should be nil.bs,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(bs, convey.ShouldNotBeNil)
})
})
})
}
func TestChargeBgmCharge(t *testing.T) {
convey.Convey("BgmCharge", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, 6, 24, 0, 0, 0, 0, time.Local)
id = int64(0)
limit = int(100)
table = "bgm_daily_charge"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO bgm_daily_charge(aid,sid,inc_charge) VALUES(1,2,3)")
bgms, err := d.BgmCharge(c, date, id, limit, table)
ctx.Convey("Then err should be nil.bgms should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(bgms, convey.ShouldNotBeNil)
})
})
})
}
func TestChargeBgmStatis(t *testing.T) {
convey.Convey("BgmStatis", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO bgm_charge_statis(aid,sid,total_charge) VALUES(1,2,3)")
bgms, err := d.BgmStatis(c, id, limit)
ctx.Convey("Then err should be nil.bgms should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(bgms, convey.ShouldNotBeNil)
})
})
})
}
func TestChargeInsertBgmChargeTable(t *testing.T) {
convey.Convey("InsertBgmChargeTable", t, func(ctx convey.C) {
var (
c = context.Background()
vals = "(1,2,3,4,'test',100, '2018-06-24','2018-06-24')"
table = "bgm_weekly_charge"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertBgmChargeTable(c, vals, table)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestChargeInsertBgmStatisBatch(t *testing.T) {
convey.Convey("InsertBgmStatisBatch", t, func(ctx convey.C) {
var (
c = context.Background()
vals = "(1,2,3,4,'test',100,'2018-06-24')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertBgmStatisBatch(c, vals)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,94 @@
package charge
import (
"context"
"fmt"
"time"
model "go-common/app/job/main/growup/model/charge"
"go-common/library/log"
)
const (
_columnChargeSQL = "SELECT id,aid,title,mid,tag_id,words,upload_time,inc_charge,view_c,date FROM %s WHERE id > ? AND date = ? AND inc_charge > 0 ORDER BY id LIMIT ?"
_columnStatisSQL = "SELECT id,aid,total_charge FROM column_charge_statis WHERE id > ? ORDER BY id LIMIT ?"
_countCmDailySQL = "SELECT COUNT(*) FROM column_daily_charge WHERE date = '%s'"
_inCmChargeTableSQL = "INSERT INTO %s(aid,mid,tag_id,inc_charge,date,upload_time) VALUES %s ON DUPLICATE KEY UPDATE inc_charge=VALUES(inc_charge)"
_inCmStatisSQL = "INSERT INTO column_charge_statis(aid,mid,tag_id,total_charge,upload_time) VALUES %s ON DUPLICATE KEY UPDATE total_charge=VALUES(total_charge)"
)
// ColumnCharge get column charge by date
func (d *Dao) ColumnCharge(c context.Context, date time.Time, id int64, limit int, table string) (columns []*model.Column, err error) {
columns = make([]*model.Column, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_columnChargeSQL, table), id, date, limit)
if err != nil {
log.Error("ColumnCharge d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
column := &model.Column{}
err = rows.Scan(&column.ID, &column.AID, &column.Title, &column.MID, &column.TagID, &column.Words, &column.UploadTime, &column.IncCharge, &column.IncViewCount, &column.Date)
if err != nil {
log.Error("ColumnCharge rows.Scan error(%v)", err)
return
}
columns = append(columns, column)
}
return
}
// CmStatis column statis
func (d *Dao) CmStatis(c context.Context, id int64, limit int) (columns []*model.ColumnStatis, err error) {
columns = make([]*model.ColumnStatis, 0)
rows, err := d.db.Query(c, _columnStatisSQL, id, limit)
if err != nil {
log.Error("CmStatis d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
column := &model.ColumnStatis{}
err = rows.Scan(&column.ID, &column.AID, &column.TotalCharge)
if err != nil {
log.Error("CmStatis rows.Scan error(%v)", err)
return
}
columns = append(columns, column)
}
return
}
// InsertCmChargeTable insert column charge
func (d *Dao) InsertCmChargeTable(c context.Context, vals, table string) (rows int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inCmChargeTableSQL, table, vals))
if err != nil {
log.Error("InsertCmChargeTable(%s) tx.Exec error(%v)", table, err)
return
}
return res.RowsAffected()
}
// InsertCmStatisBatch insert column statis
func (d *Dao) InsertCmStatisBatch(c context.Context, vals string) (rows int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inCmStatisSQL, vals))
if err != nil {
log.Error("InsertCmStatisBatch tx.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// CountCmDailyCharge get column_daily_charge count
func (d *Dao) CountCmDailyCharge(c context.Context, date string) (count int64, err error) {
err = d.db.QueryRow(c, fmt.Sprintf(_countCmDailySQL, date)).Scan(&count)
return
}

View File

@@ -0,0 +1,80 @@
package charge
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestChargeColumnCharge(t *testing.T) {
convey.Convey("ColumnCharge", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, 6, 24, 0, 0, 0, 0, time.Local)
id = int64(0)
limit = int(100)
table = "column_weekly_charge"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO column_weekly_charge(aid,mid,date) VALUES(1,2,'2018-06-24')")
columns, err := d.ColumnCharge(c, date, id, limit, table)
ctx.Convey("Then err should be nil.columns should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(columns, convey.ShouldNotBeNil)
})
})
})
}
func TestChargeCmStatis(t *testing.T) {
convey.Convey("CmStatis", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO column_charge_statis(aid,mid) VALUES(1,2)")
columns, err := d.CmStatis(c, id, limit)
ctx.Convey("Then err should be nil.columns should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(columns, convey.ShouldNotBeNil)
})
})
})
}
func TestChargeInsertCmChargeTable(t *testing.T) {
convey.Convey("InsertCmChargeTable", t, func(ctx convey.C) {
var (
c = context.Background()
vals = "(1,2,3,100,'2018-06-24','2018-06-24')"
table = "column_monthly_charge"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertCmChargeTable(c, vals, table)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestChargeInsertCmStatisBatch(t *testing.T) {
convey.Convey("InsertCmStatisBatch", t, func(ctx convey.C) {
var (
c = context.Background()
vals = "(1,2,3,4,'2018-06-24')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertCmStatisBatch(c, vals)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,51 @@
package charge
import (
"context"
"go-common/app/job/main/growup/conf"
"go-common/library/database/sql"
)
// Dao dao
type Dao struct {
c *conf.Config
db *sql.DB
}
// New fn
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.Mysql.Growup),
}
return d
}
// Ping ping health.
func (d *Dao) Ping(c context.Context) (err error) {
return d.db.Ping(c)
}
// Close close connections of mc, redis, db.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
}
// Exec exec
func (d *Dao) Exec(c context.Context, sql string) (err error) {
_, err = d.db.Exec(c, sql)
return
}
// QueryRow QueryRow
func (d *Dao) QueryRow(c context.Context, sql string) (rows *sql.Row) {
return d.db.QueryRow(c, sql)
}
// Query query
func (d *Dao) Query(c context.Context, sql string) (rows *sql.Rows, err error) {
return d.db.Query(c, sql)
}

View File

@@ -0,0 +1,34 @@
package charge
import (
"flag"
"go-common/app/job/main/growup/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "mobile.studio.growup-job")
flag.Set("conf_token", "8781e02680f40996bc01eb1248ac2ac9")
flag.Set("tree_id", "14716")
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-job.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,45 @@
package charge
import (
"context"
"fmt"
"go-common/library/log"
)
const (
_inStatisTableSQL = "INSERT INTO %s(avs, money_section, money_tips, charge, category_id, cdate) VALUES %s ON DUPLICATE KEY UPDATE avs=VALUES(avs),charge=VALUES(charge),cdate=VALUES(cdate)"
_delStatisTableSQL = "DELETE FROM %s WHERE cdate = ?"
)
// InsertStatisTable add archive_charge_date_statis batch
func (d *Dao) InsertStatisTable(c context.Context, table, vals string) (rows int64, err error) {
if table == "" {
err = fmt.Errorf("InsertStatisTable table(%s) val(%s) error", table, vals)
return
}
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inStatisTableSQL, table, vals))
if err != nil {
log.Error("InsertStatisTable d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// DelStatisTable delete av_charge_statis
func (d *Dao) DelStatisTable(c context.Context, table, date string) (rows int64, err error) {
if table == "" || date == "" {
err = fmt.Errorf("DelStatisTable table(%s) date(%s) error", table, date)
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_delStatisTableSQL, table), date)
if err != nil {
log.Error("DelStatisTable d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,42 @@
package charge
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestChargeInsertStatisTable(t *testing.T) {
convey.Convey("InsertStatisTable", t, func(ctx convey.C) {
var (
c = context.Background()
table = "av_charge_daily_statis"
vals = "(100,1,'100-200',100,1,'2018-06-24')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertStatisTable(c, table, vals)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestChargeDelStatisTable(t *testing.T) {
convey.Convey("DelStatisTable", t, func(ctx convey.C) {
var (
c = context.Background()
table = "av_charge_daily_statis"
date = "2018-06-24"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.DelStatisTable(c, table, date)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,46 @@
package charge
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/charge"
"go-common/library/log"
)
const (
_insertUpChargeSQL = "INSERT INTO %s (mid,inc_charge,total_charge,date) VALUES %s ON DUPLICATE KEY UPDATE inc_charge=VALUES(inc_charge),total_charge=VALUES(total_charge)"
_upChargeSQL = "SELECT id,mid,inc_charge,total_charge,date FROM %s WHERE date=? AND id > ? ORDER BY id LIMIT ?"
)
// GetUpCharges get up_charge
func (d *Dao) GetUpCharges(c context.Context, table string, date string, offset, limit int64) (last int64, charges map[int64]*model.UpCharge, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_upChargeSQL, table), date, offset, limit)
if err != nil {
log.Error("d.db.Query GetUpCharges error(%v)", err)
return
}
charges = make(map[int64]*model.UpCharge)
defer rows.Close()
for rows.Next() {
c := &model.UpCharge{}
err = rows.Scan(&last, &c.MID, &c.IncCharge, &c.TotalCharge, &c.Date)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
charges[c.MID] = c
}
return
}
// InsertUpCharge insert up_charge
func (d *Dao) InsertUpCharge(c context.Context, table string, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_insertUpChargeSQL, table, values))
if err != nil {
log.Error("d.db.Exec InsertUpCharge error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,46 @@
package charge
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestChargeGetUpCharges(t *testing.T) {
convey.Convey("GetUpCharges", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_daily_charge"
date = "2018-06-24"
offset = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_daily_charge(mid,date) VALUES(1, '2018-06-24')")
last, charges, err := d.GetUpCharges(c, table, date, offset, limit)
ctx.Convey("Then err should be nil.last,charges should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(charges, convey.ShouldNotBeNil)
ctx.So(last, convey.ShouldNotBeNil)
})
})
})
}
func TestChargeInsertUpCharge(t *testing.T) {
convey.Convey("InsertUpCharge", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_daily_charge"
values = "(1,2,3,'2018-06-24')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertUpCharge(c, table, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,41 @@
package dao
import (
"context"
"go-common/app/job/main/growup/model"
"go-common/library/log"
)
const (
_avDailyIncChargeSQL = "SELECT inc_charge, tag_id FROM av_daily_charge_06 WHERE av_id = ? AND date = '2018-06-24' AND upload_time >= '2018-06-24'"
_avChargeRatioSQL = "SELECT id,av_id,ratio,adjust_type FROM av_charge_ratio WHERE id > ? ORDER BY id LIMIT ?"
)
// AvDailyIncCharge get av_daily_charge inc_charge
func (d *Dao) AvDailyIncCharge(c context.Context, avID int64) (incCharge, tagID int64, err error) {
err = d.db.QueryRow(c, _avDailyIncChargeSQL, avID).Scan(&incCharge, &tagID)
return
}
// AvChargeRatio get av_charge_ratio
func (d *Dao) AvChargeRatio(c context.Context, id int64, limit int64) (m map[int64]*model.AvChargeRatio, last int64, err error) {
rows, err := d.db.Query(c, _avChargeRatioSQL, id, limit)
if err != nil {
log.Error("d.db.Query AvChargeRatio error(%v)", err)
return
}
m = make(map[int64]*model.AvChargeRatio)
defer rows.Close()
for rows.Next() {
ratio := &model.AvChargeRatio{}
err = rows.Scan(&last, &ratio.AvID, &ratio.Ratio, &ratio.AdjustType)
if err != nil {
log.Error("AvChargeRatio scan error(%v)", err)
return
}
m[ratio.AvID] = ratio
}
return
}

View File

@@ -0,0 +1,44 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAvDailyIncCharge(t *testing.T) {
convey.Convey("AvDailyIncCharge", t, func(ctx convey.C) {
var (
c = context.Background()
avID = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_daily_charge_06(av_id,inc_charge,date,upload_time) VALUES(10, 100, '2018-06-24', '2018-06-24') ON DUPLICATE KEY UPDATE av_id=VALUES(av_id)")
incCharge, tagID, err := d.AvDailyIncCharge(c, avID)
ctx.Convey("Then err should be nil.incCharge,tagID should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(tagID, convey.ShouldNotBeNil)
ctx.So(incCharge, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAvChargeRatio(t *testing.T) {
convey.Convey("AvChargeRatio", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(10)
limit = int64(1000)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
m, last, err := d.AvChargeRatio(c, id, limit)
ctx.Convey("Then err should be nil.m,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(m, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,141 @@
package dao
import (
"context"
"fmt"
"time"
"go-common/library/log"
"go-common/library/xstr"
"go-common/app/job/main/growup/model"
)
const (
// select
_upsSQL = "SELECT mid,nickname,fans,signed_at,is_deleted FROM up_info_video WHERE account_state = 3 AND mid IN (%s)"
_avIncomeSQL = "SELECT mid,av_id,upload_time,total_income FROM av_income_statis WHERE av_id IN (%s) AND mtime > ?"
_playCountSQL = "SELECT mid,play FROM up_base_statistics WHERE mid IN (%s)"
_avBreachSQL = "SELECT id,av_id FROM av_breach_record WHERE id > ? ORDER BY id LIMIT ?"
_deleteArchiveSpySQL = "DELETE FROM archive_spy_statistics LIMIT ?"
_deleteUpSpySQL = "DELETE FROM up_spy_statistics LIMIT ?"
// insert
_inCheatUpsSQL = "INSERT INTO up_spy_statistics(mid,signed_at,nickname,fans,cheat_fans,play_count,cheat_play_count,account_state) VALUES %s ON DUPLICATE KEY UPDATE signed_at=VALUES(signed_at),nickname=VALUES(nickname),fans=VALUES(fans),cheat_fans=VALUES(cheat_fans),play_count=VALUES(play_count),cheat_play_count=VALUES(cheat_play_count),account_state=VALUES(account_state)"
_inCheatArchiveSQL = "INSERT INTO archive_spy_statistics(archive_id,mid,nickname,upload_time,total_income,cheat_play_count,cheat_favorite,cheat_coin,deducted) VALUES %s ON DUPLICATE KEY UPDATE nickname=VALUES(nickname),total_income=VALUES(total_income),cheat_play_count=VALUES(cheat_play_count),cheat_favorite=VALUES(cheat_favorite),cheat_coin=VALUES(cheat_coin),deducted=VALUES(deducted)"
)
// DelArchiveSpy del archive spy
func (d *Dao) DelArchiveSpy(c context.Context, limit int64) (rows int64, err error) {
res, err := d.db.Exec(c, _deleteArchiveSpySQL, limit)
if err != nil {
return
}
return res.RowsAffected()
}
// DelUpSpy del up spy
func (d *Dao) DelUpSpy(c context.Context, limit int64) (rows int64, err error) {
res, err := d.db.Exec(c, _deleteUpSpySQL, limit)
if err != nil {
return
}
return res.RowsAffected()
}
// AvBreachRecord get av_ids deducted
func (d *Dao) AvBreachRecord(c context.Context, id int64, limit int64) (last int64, ds map[int64]bool, err error) {
rows, err := d.db.Query(c, _avBreachSQL, id, limit)
if err != nil {
return
}
ds = make(map[int64]bool)
defer rows.Close()
for rows.Next() {
var avID int64
err = rows.Scan(&last, &avID)
if err != nil {
return
}
ds[avID] = true
}
return
}
// Ups get ups in up_info_video
func (d *Dao) Ups(c context.Context, mids []int64) (cs map[int64]*model.Cheating, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_upsSQL, xstr.JoinInts(mids)))
if err != nil {
return
}
defer rows.Close()
cs = make(map[int64]*model.Cheating)
for rows.Next() {
c := &model.Cheating{}
err = rows.Scan(&c.MID, &c.Nickname, &c.Fans, &c.SignedAt, &c.IsDeleted)
if err != nil {
return
}
cs[c.MID] = c
}
return
}
// Avs get avs in av_income_statis
func (d *Dao) Avs(c context.Context, date time.Time, aids []int64) (cs map[int64]*model.Cheating, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_avIncomeSQL, xstr.JoinInts(aids)), date)
if err != nil {
return
}
defer rows.Close()
cs = make(map[int64]*model.Cheating)
for rows.Next() {
c := &model.Cheating{}
err = rows.Scan(&c.MID, &c.AvID, &c.UploadTime, &c.TotalIncome)
if err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
cs[c.AvID] = c
}
return
}
// PlayCount get play count in up_base_statistics
func (d *Dao) PlayCount(c context.Context, mids []int64) (cs map[int64]int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_playCountSQL, xstr.JoinInts(mids)))
if err != nil {
return
}
cs = make(map[int64]int64)
defer rows.Close()
for rows.Next() {
var mid, count int64
err = rows.Scan(&mid, &count)
if err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
cs[mid] = count
}
return
}
// InsertCheatUps insert cheat ups
func (d *Dao) InsertCheatUps(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inCheatUpsSQL, values))
if err != nil {
return
}
return res.RowsAffected()
}
// InsertCheatArchives insert cheat archives
func (d *Dao) InsertCheatArchives(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inCheatArchiveSQL, values))
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,140 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoDelArchiveSpy(t *testing.T) {
convey.Convey("DelArchiveSpy", t, func(ctx convey.C) {
var (
c = context.Background()
limit = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.DelArchiveSpy(c, limit)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDelUpSpy(t *testing.T) {
convey.Convey("DelUpSpy", t, func(ctx convey.C) {
var (
c = context.Background()
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.DelUpSpy(c, limit)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAvBreachRecord(t *testing.T) {
convey.Convey("AvBreachRecord", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
last, ds, err := d.AvBreachRecord(c, id, limit)
ctx.Convey("Then err should be nil.last,ds should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ds, convey.ShouldNotBeNil)
ctx.So(last, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUps(t *testing.T) {
convey.Convey("Ups", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{1, 2, 3, 4, 5, 6}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
cs, err := d.Ups(c, mids)
ctx.Convey("Then err should be nil.cs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAvs(t *testing.T) {
convey.Convey("Avs", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Now()
aids = []int64{1, 2, 3, 4, 5, 6}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
cs, err := d.Avs(c, date, aids)
ctx.Convey("Then err should be nil.cs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoPlayCount(t *testing.T) {
convey.Convey("PlayCount", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{1, 2, 3, 4, 5, 6}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
cs, err := d.PlayCount(c, mids)
ctx.Convey("Then err should be nil.cs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertCheatUps(t *testing.T) {
convey.Convey("InsertCheatUps", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(2, '2018-06-23', 'test', 100, 100, 100, 100, 3)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertCheatUps(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertCheatArchives(t *testing.T) {
convey.Convey("InsertCheatArchives", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1, 2, 'test', '2018-06-23', 100, 100, 100, 100, 100)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertCheatArchives(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,71 @@
package dao
import (
"context"
"go-common/app/job/main/growup/conf"
"go-common/library/database/hbase.v2"
"go-common/library/database/sql"
"go-common/library/log"
xhttp "go-common/library/net/http/blademaster"
)
// Dao dao
type Dao struct {
c *conf.Config
db *sql.DB
rddb *sql.DB
archiveURL string
typeURL string
breachURL string
forbidURL string
dismissURL string
passURL string
avLotteryURL string
avVoteURL string
client *xhttp.Client
// hbase
hbase *hbase.Client
}
// New fn
func New(c *conf.Config) (d *Dao) {
log.Info("dao start")
d = &Dao{
c: c,
db: sql.NewMySQL(c.Mysql.Growup),
rddb: sql.NewMySQL(c.Mysql.Allowance),
client: xhttp.NewClient(c.HTTPClient),
archiveURL: c.Host.Archive + "/manager/search",
typeURL: c.Host.VideoType + "/videoup/types",
breachURL: c.Host.Profit + "/allowance/api/x/admin/growup/auto/archive/breach",
forbidURL: c.Host.Profit + "/allowance/api/x/admin/growup/auto/up/forbid",
dismissURL: c.Host.Profit + "/allowance/api/x/admin/growup/auto/up/dismiss",
passURL: c.Host.Profit + "/allowance/api/x/admin/growup/up/pass",
avLotteryURL: c.Host.VC + "/lottery_svr/v0/lottery_svr/export_rids",
avVoteURL: c.Host.API + "/x/internal/creative/archive/vote",
// hbase
hbase: hbase.NewClient(c.HBase.Config),
}
log.Info("data init end")
//d.db.State = prom.LibClient
return
}
// Ping ping health.
func (d *Dao) Ping(c context.Context) (err error) {
return d.db.Ping(c)
}
// Close close connections of mc, redis, db.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
}
// BeginTran begin transcation
func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
return d.db.Begin(c)
}

View File

@@ -0,0 +1,43 @@
package dao
import (
"context"
"flag"
"go-common/app/job/main/growup/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "mobile.studio.growup-job")
flag.Set("conf_token", "8781e02680f40996bc01eb1248ac2ac9")
flag.Set("tree_id", "14716")
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-job.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func Exec(c context.Context, sql string) (rows int64, err error) {
res, err := d.db.Exec(c, sql)
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,217 @@
package dao
import (
"context"
"fmt"
"go-common/library/database/sql"
"go-common/library/xstr"
"go-common/app/job/main/growup/model"
)
const (
_avIDs = "SELECT av_id,mid,tag_id,income,tax_money FROM av_income WHERE date=? AND mid=?"
_avCharges = "SELECT av_id,inc_charge FROM av_daily_charge_04 WHERE av_id IN (%s) AND date=?"
_upChargeRatio = "SELECT mid,ratio FROM up_charge_ratio WHERE tag_id = ?"
_upIncomeStatis = "SELECT mid,total_income FROM up_income_statis WHERE mid in (%s)"
_upIncomeDate = "SELECT mid, income FROM %s WHERE date = ? AND mid in (%s)"
_upIncome = "SELECT id,mid,date,base_income FROM %s WHERE id > ? ORDER BY id LIMIT ?"
_inUpIncome = "INSERT INTO %s(mid,date,av_base_income) VALUES %s ON DUPLICATE KEY UPDATE av_base_income=VALUES(av_base_income)"
_creditScoreSQL = "SELECT id,mid,credit_score FROM up_info_%s WHERE id > ? ORDER BY id LIMIT ?"
_inCreditScoreSQL = "INSERT INTO credit_score(mid,score) VALUES %s ON DUPLICATE KEY UPDATE score=VALUES(score)"
_bgmIncome = "SELECT sid,income FROM bgm_income"
_bgmIncomeStatis = "INSERT INTO bgm_income_statis(sid,total_income) VALUES(?,?)"
)
// InsertBGMIncomeStatis fix bgm income statis
func (d *Dao) InsertBGMIncomeStatis(c context.Context, sid int64, income int64) (rows int64, err error) {
res, err := d.db.Exec(c, _bgmIncomeStatis, sid, income)
if err != nil {
return
}
return res.RowsAffected()
}
// GetBGMIncome map[sid]totalIncome
func (d *Dao) GetBGMIncome(c context.Context) (statis map[int64]int64, err error) {
rows, err := d.db.Query(c, _bgmIncome)
if err != nil {
return
}
statis = make(map[int64]int64)
defer rows.Close()
for rows.Next() {
var sid, income int64
err = rows.Scan(&sid, &income)
if err != nil {
return
}
if _, ok := statis[sid]; ok {
statis[sid] += income
} else {
statis[sid] = income
}
}
return
}
// GetCreditScore get credit scores
func (d *Dao) GetCreditScore(c context.Context, table string, id int64, limit int64) (scores map[int64]int, last int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_creditScoreSQL, table), id, limit)
if err != nil {
return
}
scores = make(map[int64]int)
defer rows.Close()
for rows.Next() {
var mid int64
var score int
err = rows.Scan(&last, &mid, &score)
if err != nil {
return
}
scores[mid] = score
}
return
}
// SyncCreditScore sync credit score
func (d *Dao) SyncCreditScore(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inCreditScoreSQL, values))
if err != nil {
return
}
return res.RowsAffected()
}
// GetAvBaseIncome get up av baseincome
func (d *Dao) GetAvBaseIncome(c context.Context, table string, id, limit int64) (abs []*model.AvBaseIncome, last int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_upIncome, table), id, limit)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
ab := &model.AvBaseIncome{}
err = rows.Scan(&last, &ab.MID, &ab.Date, &ab.AvBaseIncome)
if err != nil {
return
}
abs = append(abs, ab)
}
return
}
// BatchUpdateUpIncome batch update av_base_income
func (d *Dao) BatchUpdateUpIncome(c context.Context, table, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inUpIncome, table, values))
if err != nil {
return
}
return res.RowsAffected()
}
// GetAvs avs map[av_id]*model.Av
func (d *Dao) GetAvs(c context.Context, date string, mid int64) (avs map[int64]*model.Av, err error) {
rows, err := d.db.Query(c, _avIDs, date, mid)
if err != nil {
return
}
avs = make(map[int64]*model.Av)
defer rows.Close()
for rows.Next() {
av := &model.Av{}
err = rows.Scan(&av.AvID, &av.MID, &av.TagID, &av.Income, &av.TaxMoney)
if err != nil {
return
}
avs[av.AvID] = av
}
return
}
// GetAvCharges get av charges
func (d *Dao) GetAvCharges(c context.Context, avIds []int64, date string) (charges map[int64]int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_avCharges, xstr.JoinInts(avIds)), date)
if err != nil {
return
}
charges = make(map[int64]int64)
defer rows.Close()
for rows.Next() {
var avID, charge int64
err = rows.Scan(&avID, &charge)
if err != nil {
return
}
charges[avID] = charge
}
return
}
// GetUpChargeRatio get up_charge_ratio
func (d *Dao) GetUpChargeRatio(c context.Context, tagID int64) (ups map[int64]int64, err error) {
rows, err := d.db.Query(c, _upChargeRatio, tagID)
if err != nil {
return
}
defer rows.Close()
ups = make(map[int64]int64)
for rows.Next() {
var mid, ratio int64
err = rows.Scan(&mid, &ratio)
if err != nil {
return
}
ups[mid] = ratio
}
return
}
// GetUpIncomeStatis get up_income_statis
func (d *Dao) GetUpIncomeStatis(c context.Context, mids []int64) (ups map[int64]int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeStatis, xstr.JoinInts(mids)))
if err != nil {
return
}
defer rows.Close()
ups = make(map[int64]int64)
for rows.Next() {
var mid, income int64
err = rows.Scan(&mid, &income)
if err != nil {
return
}
ups[mid] = income
}
return
}
// GetUpIncomeDate get up_income by date
func (d *Dao) GetUpIncomeDate(c context.Context, mids []int64, table, date string) (ups map[int64]int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeDate, table, xstr.JoinInts(mids)), date)
if err != nil {
return
}
defer rows.Close()
ups = make(map[int64]int64)
for rows.Next() {
var mid, income int64
err = rows.Scan(&mid, &income)
if err != nil {
return
}
ups[mid] = income
}
return
}
// UpdateDate update date
func (d *Dao) UpdateDate(tx *sql.Tx, stmt string) (count int64, err error) {
res, err := tx.Exec(stmt)
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,214 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoInsertBGMIncomeStatis(t *testing.T) {
convey.Convey("InsertBGMIncomeStatis", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(100)
income = int64(1090)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "DELETE FROM bgm_income_statis WHERE sid=100")
rows, err := d.InsertBGMIncomeStatis(c, sid, income)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetBGMIncome(t *testing.T) {
convey.Convey("GetBGMIncome", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
statis, err := d.GetBGMIncome(c)
ctx.Convey("Then err should be nil.statis should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(statis, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetCreditScore(t *testing.T) {
convey.Convey("GetCreditScore", t, func(ctx convey.C) {
var (
c = context.Background()
table = "video"
id = int64(100)
limit = int64(200)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
scores, last, err := d.GetCreditScore(c, table, id, limit)
ctx.Convey("Then err should be nil.scores,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(scores, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSyncCreditScore(t *testing.T) {
convey.Convey("SyncCreditScore", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(100, 100)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.SyncCreditScore(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvBaseIncome(t *testing.T) {
convey.Convey("GetAvBaseIncome", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_income"
id = int64(0)
limit = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_income(mid,date,income) VALUES(1,'2018-06-24',2) ON DUPLICATE KEY UPDATE income=VALUES(income)")
abs, last, err := d.GetAvBaseIncome(c, table, id, limit)
ctx.Convey("Then err should be nil.abs,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(abs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoBatchUpdateUpIncome(t *testing.T) {
convey.Convey("BatchUpdateUpIncome", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_income"
values = "(100, '2018-06-23', 100)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.BatchUpdateUpIncome(c, table, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvs(t *testing.T) {
convey.Convey("GetAvs", t, func(ctx convey.C) {
var (
c = context.Background()
date = "2018-06-23"
mid = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
avs, err := d.GetAvs(c, date, mid)
ctx.Convey("Then err should be nil.avs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(avs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvCharges(t *testing.T) {
convey.Convey("GetAvCharges", t, func(ctx convey.C) {
var (
c = context.Background()
avIds = []int64{100, 200}
date = "2018-06-23"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
charges, err := d.GetAvCharges(c, avIds, date)
ctx.Convey("Then err should be nil.charges should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(charges, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUpChargeRatio(t *testing.T) {
convey.Convey("GetUpChargeRatio", t, func(ctx convey.C) {
var (
c = context.Background()
tagID = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ups, err := d.GetUpChargeRatio(c, tagID)
ctx.Convey("Then err should be nil.ups should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ups, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUpIncomeStatis(t *testing.T) {
convey.Convey("GetUpIncomeStatis", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{100, 200}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ups, err := d.GetUpIncomeStatis(c, mids)
ctx.Convey("Then err should be nil.ups should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ups, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUpIncomeDate(t *testing.T) {
convey.Convey("GetUpIncomeDate", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{100, 200}
table = "up_income"
date = "2018-06-23"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ups, err := d.GetUpIncomeDate(c, mids, table, date)
ctx.Convey("Then err should be nil.ups should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ups, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateDate(t *testing.T) {
convey.Convey("UpdateDate", t, func(ctx convey.C) {
var (
tx, _ = d.BeginTran(context.Background())
stmt = "UPDATE up_info_video SET account_state=1 WHERE mid=10"
)
defer tx.Commit()
ctx.Convey("When everything goes positive", func(ctx convey.C) {
count, err := d.UpdateDate(tx, stmt)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,56 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"dataplatform_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"dataplatform.go",
],
importpath = "go-common/app/job/main/growup/dao/dataplatform",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//app/job/main/growup/model:go_default_library",
"//app/job/main/growup/model/income:go_default_library",
"//library/conf/env:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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,34 @@
package dataplatform
import (
"go-common/app/job/main/growup/conf"
"go-common/library/database/sql"
xhttp "go-common/library/net/http/blademaster"
)
// Dao is redis dao.
type Dao struct {
c *conf.Config
db *sql.DB
url string
spyURL string
bgmURL string
basicURL string
client *xhttp.Client
}
// New is new redis dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.Mysql.Growup),
// client
client: xhttp.NewClient(c.DPClient),
url: c.Host.DataPlatform + "/avenger/api/38/query",
spyURL: c.Host.DataPlatform + "/avenger/api/51/query",
// bgmURL: c.Host.DataPlatform + "/avenger/api/81/query",
bgmURL: c.Host.DataPlatform + "/avenger/api/95/query",
basicURL: c.Host.DataPlatform + "/avenger/api/200/query",
}
return
}

View File

@@ -0,0 +1,34 @@
package dataplatform
import (
"flag"
"go-common/app/job/main/growup/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "mobile.studio.growup-job")
flag.Set("conf_token", "8781e02680f40996bc01eb1248ac2ac9")
flag.Set("tree_id", "14716")
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-job.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,245 @@
package dataplatform
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
xhttp "net/http"
"net/url"
"sort"
"strings"
"time"
"go-common/app/job/main/growup/model"
income "go-common/app/job/main/growup/model/income"
"go-common/library/conf/env"
"go-common/library/log"
)
var (
signParams = []string{"appKey", "timestamp", "version"}
_userAgent = "User-Agent"
)
func (d *Dao) setParams() url.Values {
params := url.Values{}
params.Set("appKey", d.c.DPClient.Key)
params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
params.Set("version", "1.0")
params.Set("signMethod", "md5")
return params
}
// GetArchiveByMID get archive id by mid
func (d *Dao) GetArchiveByMID(c context.Context, query string) (ids []int64, err error) {
ids = make([]int64, 0)
params := d.setParams()
params.Set("query", query)
var res struct {
Code int `json:"code"`
Result []*model.ArchiveID `json:"result"`
}
if err = d.NewRequest(c, d.url, "", params, &res); err != nil {
log.Error("dataplatform.send NewRequest url(%s) error(%v)", d.url+"?"+params.Encode(), err)
return
}
if res.Code != 200 {
log.Error("dateplatform.send NewRequest error code:%d ; url(%s) ", res.Code, d.url+"?"+params.Encode())
return
}
if res.Code == 200 && len(res.Result) > 0 {
for _, archive := range res.Result {
ids = append(ids, archive.ID)
}
}
return
}
// Send ...
func (d *Dao) Send(c context.Context, query string) (infos []*model.ArchiveInfo, err error) {
log.Info("dateplatform Send start")
params := url.Values{}
params.Set("appKey", d.c.DPClient.Key)
params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
params.Set("version", "1.0")
params.Set("signMethod", "md5")
params.Set("query", query)
var res struct {
Code int `json:"code"`
Result []*model.ArchiveInfo `json:"result"`
}
if err = d.NewRequest(c, d.url, "", params, &res); err != nil {
log.Error("dataplatform.send NewRequest url(%s) error(%v)", d.url+"?"+params.Encode(), err)
return
}
if res.Code != 200 {
log.Error("dateplatform.send NewRequest error code:%d ; url(%s) ", res.Code, d.url+"?"+params.Encode())
return
}
if res.Code == 200 && len(res.Result) > 0 {
infos = res.Result
}
return
}
// SendSpyRequest send.
func (d *Dao) SendSpyRequest(c context.Context, query string) (infos []*model.Spy, err error) {
log.Info("dateplatform Send start")
params := url.Values{}
params.Set("appKey", d.c.DPClient.Key)
params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
params.Set("version", "1.0")
params.Set("signMethod", "md5")
params.Set("query", query)
var res struct {
Code int `json:"code"`
Result []*model.Spy `json:"result"`
}
if err = d.NewRequest(c, d.spyURL, "", params, &res); err != nil {
log.Error("dataplatform.SendSpyRequest NewRequest url(%s) error(%v)", d.spyURL+"?"+params.Encode(), err)
return
}
if res.Code != 200 {
log.Error("dateplatform.SendSpyRequest NewRequest error code:%d ; url(%s) ", res.Code, d.spyURL+"?"+params.Encode())
return
}
if res.Code == 200 && len(res.Result) > 0 {
infos = res.Result
}
return
}
// SendBGMRequest get bgm infos
func (d *Dao) SendBGMRequest(c context.Context, query string) (infos []*income.BGM, err error) {
params := url.Values{}
params.Set("appKey", d.c.DPClient.Key)
params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
params.Set("version", "1.0")
params.Set("signMethod", "md5")
params.Set("query", query)
var res struct {
Code int `json:"code"`
Result []*income.BGM `json:"result"`
}
if err = d.NewRequest(c, d.bgmURL, "", params, &res); err != nil {
log.Error("dataplatform.SendBGMRequest NewRequest url(%s) error(%v)", d.bgmURL+"?"+params.Encode(), err)
return
}
if res.Code != 200 {
log.Error("dateplatform.SendBGMRequest NewRequest error code:%d ; url(%s) ", res.Code, d.bgmURL+"?"+params.Encode())
return
}
if res.Code == 200 && len(res.Result) > 0 {
infos = res.Result
}
return
}
// SendBasicDataRequest get basic data status
func (d *Dao) SendBasicDataRequest(c context.Context, query string) (ok bool, err error) {
params := url.Values{}
params.Set("appKey", d.c.DPClient.Key)
params.Set("timestamp", time.Now().Format("2006-01-02 15:04:05"))
params.Set("version", "1.0")
params.Set("signMethod", "md5")
params.Set("query", query)
var res struct {
Code int `json:"code"`
Result []*struct {
Stat int `json:"stat"`
} `json:"result"`
}
url := d.basicURL
if err = d.NewRequest(c, url, "", params, &res); err != nil {
log.Error("SendBasicDataRequest NewRequest url(%s) error(%v)", url+"?"+params.Encode(), err)
return
}
if res.Code != 200 {
log.Error("SendBasicDataRequest NewRequest error code:%d ; url(%s) ", res.Code, url+"?"+params.Encode())
return
}
if res.Code == 200 && len(res.Result) > 0 {
info := res.Result[0]
if info.Stat == 1 {
ok = true
}
}
return
}
// NewRequest new http request with method, url, ip, values and headers.
func (d *Dao) NewRequest(c context.Context, url, realIP string, params url.Values, res interface{}) (err error) {
enc, err := d.sign(params)
if err != nil {
log.Error("url:%s,params:%v", url, params)
return
}
if enc != "" {
url = url + "?" + enc
}
req, err := xhttp.NewRequest(xhttp.MethodGet, url, nil)
if err != nil {
log.Error("method:%s,url:%s", xhttp.MethodGet, url)
return
}
req.Header.Set(_userAgent, "haoguanwei@bilibili.com "+env.AppID)
if err != nil {
return
}
return d.client.Do(c, req, res)
}
// sign calc appkey and appsecret sign.
func (d *Dao) sign(params url.Values) (query string, err error) {
tmp := params.Encode()
signTmp := d.encode(params)
if strings.IndexByte(tmp, '+') > -1 {
tmp = strings.Replace(tmp, "+", "%20", -1)
}
var b bytes.Buffer
b.WriteString(d.c.DPClient.Secret)
b.WriteString(signTmp)
b.WriteString(d.c.DPClient.Secret)
mh := md5.Sum(b.Bytes())
// query
var qb bytes.Buffer
qb.WriteString(tmp)
qb.WriteString("&sign=")
qb.WriteString(strings.ToUpper(hex.EncodeToString(mh[:])))
query = qb.String()
return
}
// Encode encodes the values into ``URL encoded'' form
// ("bar=baz&foo=quux") sorted by key.
func (d *Dao) encode(v url.Values) string {
if v == nil {
return ""
}
var buf bytes.Buffer
keys := make([]string, 0, len(v))
for k := range v {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
found := false
for _, p := range signParams {
if p == k {
found = true
break
}
}
if !found {
continue
}
vs := v[k]
prefix := k
for _, v := range vs {
buf.WriteString(prefix)
buf.WriteString(v)
}
}
return buf.String()
}

View File

@@ -0,0 +1,130 @@
package dataplatform
import (
"context"
"net/url"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDataplatformsetParams(t *testing.T) {
convey.Convey("setParams", t, func(ctx convey.C) {
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.setParams()
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDataplatformGetArchiveByMID(t *testing.T) {
convey.Convey("GetArchiveByMID", t, func(ctx convey.C) {
var (
c = context.Background()
query = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
ids, err := d.GetArchiveByMID(c, query)
ctx.Convey("Then err should be nil.ids should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ids, convey.ShouldNotBeNil)
})
})
})
}
func TestDataplatformSend(t *testing.T) {
convey.Convey("Send", t, func(ctx convey.C) {
var (
c = context.Background()
query = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
infos, err := d.Send(c, query)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldBeNil)
})
})
})
}
func TestDataplatformSendSpyRequest(t *testing.T) {
convey.Convey("SendSpyRequest", t, func(ctx convey.C) {
var (
c = context.Background()
query = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
infos, err := d.SendSpyRequest(c, query)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldBeNil)
})
})
})
}
func TestDataplatformSendBGMRequest(t *testing.T) {
convey.Convey("SendBGMRequest", t, func(ctx convey.C) {
var (
c = context.Background()
query = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
infos, err := d.SendBGMRequest(c, query)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldBeNil)
})
})
})
}
func TestDataplatformNewRequest(t *testing.T) {
convey.Convey("NewRequest", t, func(ctx convey.C) {
var (
c = context.Background()
url = ""
realIP = ""
res = interface{}(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.NewRequest(c, url, realIP, d.setParams(), res)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDataplatformsign(t *testing.T) {
convey.Convey("sign", t, func(ctx convey.C) {
var (
params url.Values
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
query, err := d.sign(params)
ctx.Convey("Then err should be nil.query should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(query, convey.ShouldNotBeNil)
})
})
})
}
func TestDataplatformencode(t *testing.T) {
convey.Convey("encode", t, func(ctx convey.C) {
var (
v url.Values
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := d.encode(v)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,148 @@
package dao
import (
"context"
"time"
"go-common/app/job/main/growup/model"
"go-common/library/log"
)
const (
_totalIncome = "SELECT id, av_id, mid, income, total_income, is_deleted FROM av_income WHERE date = ? AND id > ? ORDER BY id LIMIT ?"
_getAV = "SELECT av_id, income, total_income FROM av_income WHERE date = ?"
_tag = "SELECT id, tag, category_id, is_common FROM tag_info WHERE is_deleted = 0 AND start_at <= ? AND end_at >= ?"
_ncMID = "SELECT mid FROM tag_up_info WHERE is_deleted = 0 AND tag_id = ?"
_commonAV = "SELECT av_id, income, total_income, is_deleted FROM av_income WHERE tag_id = ? AND date = ?"
_midAV = "SELECT av_id, income, total_income, is_deleted FROM av_income WHERE mid = ? AND date = ? AND tag_id = ?"
)
// TotalIncome get date totalincome,upcount, avcount
func (d *Dao) TotalIncome(c context.Context, date time.Time, from, limit int64) (infos []*model.IncomeInfo, err error) {
rows, err := d.db.Query(c, _totalIncome, date, from, limit)
if err != nil {
log.Error("growup-job dao.TotalIncome error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
info := &model.IncomeInfo{}
err = rows.Scan(&info.ID, &info.AVID, &info.MID, &info.Income, &info.TotalIncome, &info.IsDeleted)
if err != nil {
log.Error("growup-job dao.TotalIncome rows scan error(%v)", err)
return
}
infos = append(infos, info)
}
err = rows.Err()
return
}
// GetAV get av income.
func (d *Dao) GetAV(c context.Context, date time.Time) (infos []*model.IncomeInfo, err error) {
rows, err := d.db.Query(c, _getAV, date)
if err != nil {
log.Error("growup-job dao.GetAV error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
info := &model.IncomeInfo{}
err = rows.Scan(&info.AVID, &info.Income, &info.TotalIncome)
if err != nil {
log.Error("growup-job dao.GetAV rows scan error(%v)", err)
return
}
infos = append(infos, info)
}
err = rows.Err()
return
}
// GetTag get tag.
func (d *Dao) GetTag(c context.Context, date time.Time) (infos []*model.TagInfo, err error) {
infos = make([]*model.TagInfo, 0)
rows, err := d.db.Query(c, _tag, date, date)
if err != nil {
log.Error("growup-job dao.GetTag error(%v),sql(%s)", err, _tag)
return
}
defer rows.Close()
for rows.Next() {
info := &model.TagInfo{}
err = rows.Scan(&info.ID, &info.Tag, &info.Category, &info.IsCommon)
if err != nil {
log.Error("growup-job dao.GetTag rows scan error(%v)", err)
return
}
infos = append(infos, info)
}
err = rows.Err()
return
}
// GetMID get tag mid.
func (d *Dao) GetMID(c context.Context, TagID int64) (infos []*model.MIDInfo, err error) {
infos = make([]*model.MIDInfo, 0)
rows, err := d.db.Query(c, _ncMID, TagID)
if err != nil {
log.Error("growup-job dao.GetMID error(%v),sql(%s)", err, _ncMID)
return
}
defer rows.Close()
for rows.Next() {
info := &model.MIDInfo{}
err = rows.Scan(&info.MID)
if err != nil {
log.Error("growup-job dao.GetMID rows scan error(%v)", err)
return
}
infos = append(infos, info)
}
err = rows.Err()
return
}
// TagToAV common tag av.
func (d *Dao) TagToAV(c context.Context, category int, date time.Time) (infos []*model.TagInfo, err error) {
infos = make([]*model.TagInfo, 0)
rows, err := d.db.Query(c, _commonAV, category, date)
if err != nil {
log.Error("growup-job dao.TagToAV error(%v),sql(%s)", err, _commonAV)
return
}
defer rows.Close()
for rows.Next() {
info := &model.TagInfo{}
err = rows.Scan(&info.AVID, &info.Income, &info.TotalIncome, &info.IsDeleted)
if err != nil {
log.Error("growup-job dao.TagToAV rows scan error(%v)", err)
return
}
infos = append(infos, info)
}
err = rows.Err()
return
}
// MIDToAV no common tag av.
func (d *Dao) MIDToAV(c context.Context, mid int64, category int, date time.Time) (infos []*model.TagInfo, err error) {
infos = make([]*model.TagInfo, 0)
rows, err := d.db.Query(c, _midAV, mid, date, category)
if err != nil {
log.Error("growup-job dao.MIDToAV error(%v),sql(%s)", err, _midAV)
return
}
defer rows.Close()
for rows.Next() {
info := &model.TagInfo{}
err = rows.Scan(&info.AVID, &info.Income, &info.TotalIncome, &info.IsDeleted)
if err != nil {
log.Error("growup-job dao.MIDToAV rows scan error(%v)", err)
return
}
infos = append(infos, info)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,52 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"email_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"email.go",
],
importpath = "go-common/app/job/main/growup/dao/email",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//library/log:go_default_library",
"//vendor/gopkg.in/gomail.v2: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,28 @@
package email
import (
"crypto/tls"
"go-common/app/job/main/growup/conf"
"gopkg.in/gomail.v2"
)
// Dao is redis dao.
type Dao struct {
c *conf.Config
email *gomail.Dialer
}
// New is new redis dao.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
// mail
email: gomail.NewDialer(c.Mail.Host, c.Mail.Port, c.Mail.Username, c.Mail.Password),
}
d.email.TLSConfig = &tls.Config{
InsecureSkipVerify: true,
}
return
}

View File

@@ -0,0 +1,34 @@
package email
import (
"flag"
"go-common/app/job/main/growup/conf"
"os"
"testing"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "mobile.studio.growup-job")
flag.Set("conf_token", "8781e02680f40996bc01eb1248ac2ac9")
flag.Set("tree_id", "14716")
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-job.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,41 @@
package email
import (
"fmt"
"os"
"time"
"go-common/library/log"
gomail "gopkg.in/gomail.v2"
)
//SendMail send the email.
func (d *Dao) SendMail(date time.Time, body string, subject string, send ...string) (err error) {
log.Info("send mail send:%v", send)
msg := gomail.NewMessage()
msg.SetHeader("From", d.c.Mail.Username)
msg.SetHeader("To", send...)
msg.SetHeader("Subject", fmt.Sprintf(subject, date.Year(), date.Month(), date.Day()))
msg.SetBody("text/html", body)
if err = d.email.DialAndSend(msg); err != nil {
log.Error("s.email.DialAndSend error(%v)", err)
return
}
return
}
//SendMailAttach send the email.
func (d *Dao) SendMailAttach(filename string, subject string, send []string) (err error) {
msg := gomail.NewMessage()
msg.SetHeader("From", d.c.Mail.Username)
msg.SetHeader("To", send...)
msg.SetHeader("Subject", subject)
msg.Attach(filename)
if err = d.email.DialAndSend(msg); err != nil {
log.Error("s.email.DialAndSend error(%v)", err)
return
}
err = os.Remove(filename)
return
}

View File

@@ -0,0 +1,41 @@
package email
import (
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestEmailSendMail(t *testing.T) {
convey.Convey("SendMail", t, func(ctx convey.C) {
var (
date = time.Now()
body = ""
subject = ""
send = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SendMail(date, body, subject, send)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestEmailSendMailAttach(t *testing.T) {
convey.Convey("SendMailAttach", t, func(ctx convey.C) {
var (
filename = ""
subject = ""
send = []string{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SendMailAttach(filename, subject, send)
ctx.Convey("Then err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,116 @@
package dao
import (
"context"
"fmt"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoTotalIncome(t *testing.T) {
convey.Convey("TotalIncome", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, 6, 24, 0, 0, 0, 0, time.Local)
from = int64(0)
limit = int64(100)
)
fmt.Println("date:", date)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_income(av_id,mid,income,date) VALUES (1,2,3,'2018-06-24') ON DUPLICATE KEY UPDATE av_id=VALUES(av_id), date=VALUES(date)")
infos, err := d.TotalIncome(c, date, from, limit)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAV(t *testing.T) {
convey.Convey("GetAV", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, 6, 24, 0, 0, 0, 0, time.Local)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_income(av_id, mid, income, date) VALUES (1,2,3,'2018-06-24') ON DUPLICATE KEY UPDATE av_id=VALUES(av_id), date=VALUES(date)")
infos, err := d.GetAV(c, date)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetTag(t *testing.T) {
convey.Convey("GetTag", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Date(2018, 6, 24, 0, 0, 0, 0, time.Local)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO tag_info(tag,category_id,is_common,date) VALUES(2,3,1, '2018-06-24')")
infos, err := d.GetTag(c, date)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetMID(t *testing.T) {
convey.Convey("GetMID", t, func(ctx convey.C) {
var (
c = context.Background()
TagID = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO tag_up_info(mid) VALUES(10) ON DUPLICATE KEY UPDATE mid=VALUES(mid)")
infos, err := d.GetMID(c, TagID)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTagToAV(t *testing.T) {
convey.Convey("TagToAV", t, func(ctx convey.C) {
var (
c = context.Background()
category = int(2)
date = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
infos, err := d.TagToAV(c, category, date)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoMIDToAV(t *testing.T) {
convey.Convey("MIDToAV", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(100)
category = int(10)
date = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
infos, err := d.MIDToAV(c, mid, category, date)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,67 @@
package dao
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"strconv"
"time"
"go-common/app/job/main/growup/model"
"go-common/library/log"
)
var (
//HBaseArchiveTablePrefix 分类分端播放
HBaseArchiveTablePrefix = "video_play_category_"
//HBaseFamilyPlat family
HBaseFamilyPlat = []byte("v")
)
func hbaseMd5Key(aid int64) string {
hasher := md5.New()
hasher.Write([]byte(strconv.Itoa(int(aid))))
return hex.EncodeToString(hasher.Sum(nil))
}
// ArchiveStat get the stat of archive.
func (d *Dao) ArchiveStat(c context.Context, aid int64, date time.Time) (stat *model.ArchiveStat, err error) {
var (
ctx, cancel = context.WithTimeout(c, time.Duration(d.c.HBase.ReadTimeout))
tableName = HBaseArchiveTablePrefix + date.Format("20060102")
key = hbaseMd5Key(aid)
)
defer cancel()
result, err := d.hbase.GetStr(ctx, tableName, key)
if err != nil {
log.Error("ArchiveStat d.hbase.GetStr tableName(%s)|aid(%d)|key(%v)|error(%v)", tableName, aid, key, err)
return
}
if result == nil {
return
}
stat = &model.ArchiveStat{}
for _, c := range result.Cells {
if c == nil {
continue
}
v, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
if !bytes.Equal(c.Family, HBaseFamilyPlat) {
continue
}
switch {
case bytes.Equal(c.Qualifier, []byte("play")):
stat.Play = v
case bytes.Equal(c.Qualifier, []byte("dm")):
stat.Dm = v
case bytes.Equal(c.Qualifier, []byte("reply")):
stat.Reply = v
case bytes.Equal(c.Qualifier, []byte("like")):
stat.Like = v
case bytes.Equal(c.Qualifier, []byte("sh")):
stat.Share = v
}
}
return
}

View File

@@ -0,0 +1,40 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaohbaseMd5Key(t *testing.T) {
convey.Convey("hbaseMd5Key", t, func(ctx convey.C) {
var (
aid = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
p1 := hbaseMd5Key(aid)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoArchiveStat(t *testing.T) {
convey.Convey("ArchiveStat", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(2)
date = time.Now()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
stat, err := d.ArchiveStat(c, aid, date)
ctx.Convey("Then err should not be nil.stat should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(stat, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,259 @@
package dao
import (
"context"
"fmt"
"time"
"go-common/app/job/main/growup/model"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
// select
_avTagRatioSQL = "SELECT id, tag_id, av_id FROM av_charge_ratio WHERE id > ? ORDER BY id LIMIT ?"
_avIncomeInfoSQL = "SELECT av_id, mid, income, date FROM av_income WHERE av_id = ? AND date = ? AND is_deleted = 0"
_tagAVTotalIncomeSQL = "SELECT total_income, date FROM up_tag_income WHERE tag_id = ? AND av_id = ? AND is_deleted = 0"
_avIncomeDateSQL = "SELECT id, av_id, mid, tag_id, income, total_income, date FROM av_income where id > ? LIMIT ?"
_upAccuntSQL = "SELECT mid, total_income, total_unwithdraw_income, withdraw_date_version FROM up_account WHERE withdraw_date_version = ? AND ctime < ? AND total_unwithdraw_income > 0 AND is_deleted = 0 LIMIT ?,?"
_upWithdrawSQL = "SELECT mid, withdraw_income FROM up_income_withdraw WHERE date_version = ? AND state = 2 LIMIT ?,?"
_upIncomeSQL = "SELECT id, mid, av_count, av_income, column_count, column_income, bgm_count, bgm_income, income, tax_money, total_income, date FROM %s WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
_upTotalIncomeSQL = "SELECT id, total_income, is_deleted FROM up_account WHERE id > ? ORDER BY id LIMIT ?"
_upDateIncomeSQL = "SELECT id, mid, income, total_income, is_deleted FROM up_income WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
_avDateIncomeSQL = "SELECT id, av_id, mid, tag_id, income, base_income, total_income,tax_money,upload_time,date,is_deleted FROM av_income WHERE id > ? AND date = ? AND is_deleted = 0 ORDER BY id LIMIT ?"
_getUpTotalIncomeCntSQL = "SELECT count(*) FROM up_account WHERE total_income > 0 AND is_deleted = 0"
_avIncomeStatisCount = "SELECT count(*) FROM av_income_statis"
// insert
_insertTagIncomeSQL = "INSERT INTO up_tag_income(tag_id, mid, av_id, income, total_income, date) VALUES %s ON DUPLICATE KEY UPDATE tag_id = values(tag_id), mid = values(mid), av_id = values(av_id), income = values(income), total_income = values(total_income), date = values(date)"
)
// GetAvTagRatio get av tag info from av_charge_ratio.
func (d *Dao) GetAvTagRatio(c context.Context, from, limit int64) (infos []*model.ActivityAVInfo, err error) {
rows, err := d.db.Query(c, _avTagRatioSQL, from, limit)
if err != nil {
log.Error("dao.GetAvTagRatio query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
a := &model.ActivityAVInfo{}
if err = rows.Scan(&a.MID, &a.TagID, &a.AVID); err != nil {
log.Error("dao.GetAvTagRatio scan error(%v)", err)
return
}
infos = append(infos, a)
}
return
}
// GetAvIncomeInfo get av income from av_income.
func (d *Dao) GetAvIncomeInfo(c context.Context, avID int64, date time.Time) (info *model.TagAvIncome, err error) {
info = new(model.TagAvIncome)
row := d.db.QueryRow(c, _avIncomeInfoSQL, avID, date)
err = row.Scan(&info.AVID, &info.MID, &info.Income, &info.Date)
if err != nil {
if err == sql.ErrNoRows {
err = nil
info = nil
return
}
log.Error("dao.GetAvInfoInfo scan error(%v)", err)
}
return
}
// TxInsertTagIncome insert tag_income.
func (d *Dao) TxInsertTagIncome(tx *sql.Tx, sql string) (rows int64, err error) {
res, err := tx.Exec(fmt.Sprintf(_insertTagIncomeSQL, sql))
if err != nil {
log.Error("dao.TxInsertTagIncome exec error(%v)", err)
return
}
return res.RowsAffected()
}
// GetTagAvTotalIncome get av total_income from up_tag_income.
func (d *Dao) GetTagAvTotalIncome(c context.Context, tagID, avID int64) (infos []*model.AvIncome, err error) {
rows, err := d.db.Query(c, _tagAVTotalIncomeSQL, tagID, avID)
if err != nil {
log.Error("dao.GetTagAvTotalIncome query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
a := &model.AvIncome{}
if err = rows.Scan(&a.TotalIncome, &a.Date); err != nil {
log.Error("dao.GetTagAvTotalIncome scan error(%v)", err)
return
}
infos = append(infos, a)
}
return
}
// ListAvIncome list av income by query
func (d *Dao) ListAvIncome(c context.Context, id int64, limit int) (avIncome []*model.AvIncome, err error) {
avIncome = make([]*model.AvIncome, 0)
rows, err := d.db.Query(c, _avIncomeDateSQL, id, limit)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.AvIncome{}
err = rows.Scan(&list.ID, &list.AvID, &list.MID, &list.TagID, &list.Income, &list.TotalIncome, &list.Date)
if err != nil {
log.Error("ListAvIncome rows scan error(%v)", err)
return
}
avIncome = append(avIncome, list)
}
err = rows.Err()
return
}
// ListUpAccount list up_acoount by date
func (d *Dao) ListUpAccount(c context.Context, withdrawDate, ctime string, from, limit int) (upAct []*model.UpAccount, err error) {
upAct = make([]*model.UpAccount, 0)
rows, err := d.db.Query(c, _upAccuntSQL, withdrawDate, ctime, from, limit)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.UpAccount{}
err = rows.Scan(&list.MID, &list.TotalIncome, &list.TotalUnwithdrawIncome, &list.WithdrawDateVersion)
if err != nil {
log.Error("ListUpAccount rows scan error(%v)", err)
return
}
upAct = append(upAct, list)
}
err = rows.Err()
return
}
// ListUpIncome list up_income_? by date
func (d *Dao) ListUpIncome(c context.Context, table, date string, id int64, limit int) (um []*model.UpIncome, err error) {
um = make([]*model.UpIncome, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeSQL, table), id, date, limit)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.UpIncome{}
err = rows.Scan(&list.ID, &list.MID, &list.AvCount, &list.AvIncome, &list.ColumnCount, &list.ColumnIncome, &list.BgmCount, &list.BgmIncome, &list.Income, &list.TaxMoney, &list.TotalIncome, &list.Date)
if err != nil {
log.Error("ListUpIncome rows scan error(%v)", err)
return
}
um = append(um, list)
}
err = rows.Err()
return
}
// ListUpWithdraw list up_withdraw_income by date
func (d *Dao) ListUpWithdraw(c context.Context, date string, from, limit int) (ups map[int64]int64, err error) {
ups = make(map[int64]int64)
rows, err := d.db.Query(c, _upWithdrawSQL, date, from, limit)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var mid, income int64
err = rows.Scan(&mid, &income)
if err != nil {
log.Error("ListUpWithdraw rows scan error(%v)", err)
return
}
ups[mid] = income
}
err = rows.Err()
return
}
// GetUpTotalIncome get up totalIncome.
func (d *Dao) GetUpTotalIncome(c context.Context, from, limit int64) (infos []*model.MIDInfo, err error) {
infos = make([]*model.MIDInfo, 0)
rows, err := d.db.Query(c, _upTotalIncomeSQL, from, limit)
if err != nil {
log.Error("dao.GetUpTotalIncome query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
info := &model.MIDInfo{}
if err = rows.Scan(&info.ID, &info.TotalIncome, &info.IsDeleted); err != nil {
log.Error("dao.GetUpTotalIncome scan error(%v)", err)
return
}
infos = append(infos, info)
}
return
}
// GetUpIncome get up date income.
func (d *Dao) GetUpIncome(c context.Context, date time.Time, from, limit int64) (infos []*model.MIDInfo, err error) {
infos = make([]*model.MIDInfo, 0)
rows, err := d.db.Query(c, _upDateIncomeSQL, from, date, limit)
if err != nil {
log.Error("dao.GetUpIncome query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
info := &model.MIDInfo{}
if err = rows.Scan(&info.ID, &info.MID, &info.Income, &info.TotalIncome, &info.IsDeleted); err != nil {
log.Error("dao.GetUpIncome scan error(%v)", err)
return
}
infos = append(infos, info)
}
return
}
// GetAvIncome get av income info from av_income.
func (d *Dao) GetAvIncome(c context.Context, date time.Time, id, limit int64) (infos []*model.IncomeInfo, err error) {
infos = make([]*model.IncomeInfo, 0)
rows, err := d.db.Query(c, _avDateIncomeSQL, id, date, limit)
if err != nil {
log.Error("dao.GetIncome query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
info := &model.IncomeInfo{}
if err = rows.Scan(&info.ID, &info.AVID, &info.MID, &info.TagID, &info.Income, &info.BaseIncome, &info.TotalIncome, &info.TaxMoney, &info.UploadTime, &info.Date, &info.IsDeleted); err != nil {
log.Error("dao.GetAvIncome scan error(%v)", err)
return
}
infos = append(infos, info)
}
return
}
// GetUpTotalIncomeCnt get up t-2 total income > 0 upcnt
func (d *Dao) GetUpTotalIncomeCnt(c context.Context) (upCnt int, err error) {
row := d.db.QueryRow(c, _getUpTotalIncomeCntSQL)
if err = row.Scan(&upCnt); err != nil {
log.Error("growup-job dao.GetUpTotalIncomeCnt scan error(%v)", err)
}
return
}
// GetAvStatisCount get av_income_statis count
func (d *Dao) GetAvStatisCount(c context.Context) (cnt int, err error) {
err = d.db.QueryRow(c, _avIncomeStatisCount).Scan(&cnt)
return
}

View File

@@ -0,0 +1,98 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"archive_test.go",
"av_charge_statis_test.go",
"av_charge_test.go",
"av_income_stat_test.go",
"av_income_test.go",
"bgm_income_stat_test.go",
"bgm_income_test.go",
"bgm_test.go",
"blacklist_test.go",
"business_order_test.go",
"charge_ratio_test.go",
"column_charge_test.go",
"column_income_stat_test.go",
"column_income_test.go",
"dao_test.go",
"income_date_statis_test.go",
"lottery_test.go",
"up_account_test.go",
"up_charge_test.go",
"up_income_date_test.go",
"up_income_stat_test.go",
"up_income_test.go",
"up_info_video_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//app/job/main/growup/model/income:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"archive.go",
"av_charge.go",
"av_charge_statis.go",
"av_income.go",
"av_income_stat.go",
"bgm.go",
"bgm_income.go",
"bgm_income_stat.go",
"blacklist.go",
"business_order.go",
"charge_ratio.go",
"column_charge.go",
"column_income.go",
"column_income_stat.go",
"dao.go",
"income_date_statis.go",
"lottery.go",
"up_account.go",
"up_charge.go",
"up_income.go",
"up_income_date.go",
"up_income_stat.go",
"up_info_video.go",
],
importpath = "go-common/app/job/main/growup/dao/income",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/job/main/growup/conf:go_default_library",
"//app/job/main/growup/model/income:go_default_library",
"//library/database/sql:go_default_library",
"//library/log: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,58 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_getArchiveByDateSQL = "SELECT id, %s, mid, tag_id, income, date FROM %s WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
_getBgmIncomeByDateSQL = "SELECT id, sid, mid, income, date FROM bgm_income WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
)
// GetArchiveByDate get archive by date
func (d *Dao) GetArchiveByDate(c context.Context, aid, table, date string, id int64, limit int) (archives []*model.ArchiveIncome, err error) {
archives = make([]*model.ArchiveIncome, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_getArchiveByDateSQL, aid, table), id, date, limit)
if err != nil {
log.Error("GetArchiveByDate d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
archive := &model.ArchiveIncome{}
err = rows.Scan(&archive.ID, &archive.AID, &archive.MID, &archive.TagID, &archive.Income, &archive.Date)
if err != nil {
log.Error("GetArchiveByDate rows.Scan error(%v)", err)
return
}
archives = append(archives, archive)
}
return
}
// GetBgmIncomeByDate get bgm income by date
func (d *Dao) GetBgmIncomeByDate(c context.Context, date string, id int64, limit int) (archives []*model.ArchiveIncome, err error) {
archives = make([]*model.ArchiveIncome, 0)
rows, err := d.db.Query(c, _getBgmIncomeByDateSQL, id, date, limit)
if err != nil {
log.Error("GetArchiveByDate d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
archive := &model.ArchiveIncome{}
err = rows.Scan(&archive.ID, &archive.AID, &archive.MID, &archive.Income, &archive.Date)
if err != nil {
log.Error("GetArchiveByDate rows.Scan error(%v)", err)
return
}
archives = append(archives, archive)
}
return
}

View File

@@ -0,0 +1,46 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeGetArchiveByDate(t *testing.T) {
convey.Convey("GetArchiveByDate", t, func(ctx convey.C) {
var (
c = context.Background()
aid = "av_id"
table = "av_income"
date = "2018-06-24"
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
archives, err := d.GetArchiveByDate(c, aid, table, date, id, limit)
ctx.Convey("Then err should be nil.archives should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(archives, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetBgmIncomeByDate(t *testing.T) {
convey.Convey("GetBgmIncomeByDate", t, func(ctx convey.C) {
var (
c = context.Background()
date = "2018-06-24"
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
archives, err := d.GetBgmIncomeByDate(c, date, id, limit)
ctx.Convey("Then err should be nil.archives should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(archives, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,94 @@
package income
import (
"context"
"fmt"
"strconv"
"time"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_layout = "2006-01-02"
_avDailyChargeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_daily_charge_%s WHERE id > ? AND date = ? AND inc_charge > 0 ORDER BY id LIMIT ?"
_avWeeklyChargeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_weekly_charge WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
_avMonthlyChargeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,is_deleted,ctime,mtime FROM av_monthly_charge WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
_inAvChargeTableSQL = "INSERT INTO %s(av_id,mid,tag_id,is_original,danmaku_count,comment_count,collect_count,coin_count,share_count,elec_pay_count,total_play_count,web_play_count,app_play_count,h5_play_count,lv_unknown,lv_0,lv_1,lv_2,lv_3,lv_4,lv_5,lv_6,v_score,inc_charge,total_charge,date,upload_time) VALUES %s ON DUPLICATE KEY UPDATE danmaku_count=VALUES(danmaku_count),comment_count=VALUES(comment_count),collect_count=VALUES(collect_count),coin_count=VALUES(coin_count),share_count=VALUES(share_count),elec_pay_count=VALUES(elec_pay_count),total_play_count=VALUES(total_play_count),web_play_count=VALUES(web_play_count),app_play_count=VALUES(app_play_count),h5_play_count=VALUES(h5_play_count),lv_unknown=VALUES(lv_unknown),lv_0=VALUES(lv_0),lv_1=VALUES(lv_1),lv_2=VALUES(lv_2),lv_3=VALUES(lv_3),lv_4=VALUES(lv_4),lv_5=VALUES(lv_5),lv_6=VALUES(lv_6),v_score=VALUES(v_score),inc_charge=VALUES(inc_charge),total_charge=VALUES(total_charge)"
_inColumnChargeTableSQL = "INSERT INTO %s(aid,title,mid,tag_id,inc_charge,date,upload_time) VALUES %s"
)
// IAvCharge av charge func
type IAvCharge func(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error)
// AvDailyCharge get av daily charge by date
func (d *Dao) AvDailyCharge(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error) {
month := date.Month()
monthStr := strconv.Itoa(int(month))
if month < 10 {
monthStr = "0" + monthStr
}
return d.GetAvCharge(c, fmt.Sprintf(_avDailyChargeSQL, monthStr), date.Format(_layout), id, limit)
}
// AvWeeklyCharge get av weekly charge
func (d *Dao) AvWeeklyCharge(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error) {
return d.GetAvCharge(c, _avWeeklyChargeSQL, date.Format(_layout), id, limit)
}
// AvMonthlyCharge get av monthly charge
func (d *Dao) AvMonthlyCharge(c context.Context, date time.Time, id int64, limit int) (data []*model.AvCharge, err error) {
return d.GetAvCharge(c, _avMonthlyChargeSQL, date.Format(_layout), id, limit)
}
// GetAvCharge get av charge by sql required
func (d *Dao) GetAvCharge(c context.Context, sql string, date string, id int64, limit int) (data []*model.AvCharge, err error) {
rows, err := d.db.Query(c, sql, id, date, limit)
if err != nil {
log.Error("d.Query av_charge(%s) error(%v)", sql, err)
return
}
defer rows.Close()
for rows.Next() {
adc := &model.AvCharge{}
err = rows.Scan(&adc.ID, &adc.AvID, &adc.MID, &adc.TagID, &adc.IsOriginal, &adc.UploadTime, &adc.DanmakuCount, &adc.CommentCount, &adc.CollectCount, &adc.CoinCount, &adc.ShareCount, &adc.ElecPayCount, &adc.TotalPlayCount, &adc.WebPlayCount, &adc.AppPlayCount, &adc.H5PlayCount, &adc.LvUnknown, &adc.Lv0, &adc.Lv1, &adc.Lv2, &adc.Lv3, &adc.Lv4, &adc.Lv5, &adc.Lv6, &adc.VScore, &adc.IncCharge, &adc.TotalCharge, &adc.Date, &adc.IsDeleted, &adc.CTime, &adc.MTime)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
data = append(data, adc)
}
return
}
// InsertAvChargeTable add av charge batch
func (d *Dao) InsertAvChargeTable(c context.Context, vals, table string) (rows int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inAvChargeTableSQL, table, vals))
if err != nil {
log.Error("InsertAvChargeTable(%s) tx.Exec error(%v)", table, err)
return
}
return res.RowsAffected()
}
// InsertColumnChargeTable add av charge batch
func (d *Dao) InsertColumnChargeTable(c context.Context, vals, table string) (rows int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inColumnChargeTableSQL, table, vals))
if err != nil {
log.Error("InsertColumnChargeTable(%s) tx.Exec error(%v)", table, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,49 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_avChargeStatisSQL = "SELECT id,av_id,mid,total_charge FROM av_charge_statis WHERE id > ? ORDER BY id LIMIT ?"
_inAvChargeStatisSQL = "INSERT INTO av_charge_statis(av_id,mid,tag_id,is_original,total_charge,upload_time) VALUES %s ON DUPLICATE KEY UPDATE total_charge=VALUES(total_charge)"
)
// AvChargeStatis get av_charge_statis
func (d *Dao) AvChargeStatis(c context.Context, id int64, limit int) (data []*model.AvChargeStatis, err error) {
rows, err := d.db.Query(c, _avChargeStatisSQL, id, limit)
if err != nil {
log.Error("d.Query av_charge_statis error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
ads := &model.AvChargeStatis{}
err = rows.Scan(&ads.ID, &ads.AvID, &ads.MID, &ads.TotalCharge)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
data = append(data, ads)
}
return
}
// InsertAvChargeStatisBatch add av charge statis batch
func (d *Dao) InsertAvChargeStatisBatch(c context.Context, vals string) (count int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inAvChargeStatisSQL, vals))
if err != nil {
log.Error("InsertAvChargeStatisBatch d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,96 @@
package income
import (
"bytes"
"context"
"strconv"
"testing"
"time"
model "go-common/app/job/main/growup/model/income"
xtime "go-common/library/time"
. "github.com/smartystreets/goconvey/convey"
)
func Test_AvChargeStatis(t *testing.T) {
Convey("AvChargeStatis", t, func() {
_, err := d.AvChargeStatis(context.Background(), 0, 2000)
So(err, ShouldBeNil)
})
}
func Test_InsertAvChargeStatisBatch(t *testing.T) {
Convey("InsertAvChargeStatisBatch", t, func() {
c := context.Background()
d.db.Exec(c, "DELETE FROM av_charge_statis where av_id = 11")
avChargeStatis := []*model.AvChargeStatis{}
value := &model.AvChargeStatis{
AvID: 11,
MID: 11,
TagID: 11,
}
avChargeStatis = append(avChargeStatis, value)
vals := assembleAvChargeStatis(avChargeStatis)
count, err := d.InsertAvChargeStatisBatch(c, vals)
So(err, ShouldBeNil)
So(count, ShouldEqual, 1)
d.db.Exec(c, "DELETE FROM av_charge_statis where av_id = 11")
})
}
func benchmarkInsertAvChargeStatisBatch(size int64, b *testing.B) {
avChargeStatis := make([]*model.AvChargeStatis, size)
var i int64
for i = 0; i < size; i++ {
avChargeStatis[i] = &model.AvChargeStatis{
AvID: i,
MID: i,
TagID: i,
IsOriginal: int(i),
UploadTime: xtime.Time(time.Now().Unix()),
TotalCharge: i,
}
}
vals := assembleAvChargeStatis(avChargeStatis)
for n := 0; n < b.N; n++ {
d.InsertAvChargeStatisBatch(context.Background(), vals)
}
}
func BenchmarkInsertAvChargeStatisBatch100(b *testing.B) { benchmarkInsertAvChargeStatisBatch(100, b) }
func BenchmarkInsertAvChargeStatisBatch1000(b *testing.B) { benchmarkInsertAvChargeStatisBatch(1000, b) }
func BenchmarkInsertAvChargeStatisBatch10000(b *testing.B) {
benchmarkInsertAvChargeStatisBatch(10000, b)
}
func assembleAvChargeStatis(avChargeStatis []*model.AvChargeStatis) (vals string) {
var buf bytes.Buffer
for _, row := range avChargeStatis {
buf.WriteString("(")
buf.WriteString(strconv.FormatInt(row.AvID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.MID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.TagID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.Itoa(row.IsOriginal))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.TotalCharge, 10))
buf.WriteByte(',')
buf.WriteString(row.UploadTime.Time().Format(_layout))
buf.WriteString(")")
buf.WriteByte(',')
}
if buf.Len() > 0 {
buf.Truncate(buf.Len() - 1)
}
vals = buf.String()
buf.Reset()
return
}

View File

@@ -0,0 +1,129 @@
package income
import (
"bytes"
"context"
"strconv"
"testing"
"time"
model "go-common/app/job/main/growup/model/income"
//xtime "go-common/library/time"
. "github.com/smartystreets/goconvey/convey"
)
func Test_AvDailyCharge(t *testing.T) {
Convey("AvDailyCharge", t, func() {
_, err := d.AvDailyCharge(context.Background(), time.Now(), 0, 2000)
So(err, ShouldBeNil)
})
}
func Test_AvWeeklyCharge(t *testing.T) {
Convey("AvWeeklyCharge", t, func() {
_, err := d.AvWeeklyCharge(context.Background(), time.Now(), 0, 2000)
So(err, ShouldBeNil)
})
}
func Test_AvMonthlyCharge(t *testing.T) {
Convey("AvMonthlyCharge", t, func() {
_, err := d.AvMonthlyCharge(context.Background(), time.Now(), 0, 2000)
So(err, ShouldBeNil)
})
}
func Test_InsertAvChargeTable(t *testing.T) {
Convey("InsertAvChargeTable", t, func() {
c := context.Background()
d.db.Exec(c, "DELETE FROM av_weekly_charge where av_id = 11")
avCharge := []*model.AvCharge{}
value := &model.AvCharge{
AvID: 11,
MID: 11,
TagID: 11,
}
avCharge = append(avCharge, value)
vals := assembleAvCharge(avCharge)
count, err := d.InsertAvChargeTable(c, vals, "av_weekly_charge")
So(err, ShouldBeNil)
So(count, ShouldEqual, 1)
d.db.Exec(c, "DELETE FROM av_weekly_charge where av_id = 11")
})
}
func assembleAvCharge(avCharge []*model.AvCharge) (vals string) {
var buf bytes.Buffer
for _, row := range avCharge {
buf.WriteString("(")
buf.WriteString(strconv.FormatInt(row.AvID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.MID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.TagID, 10))
buf.WriteByte(',')
buf.WriteString(strconv.Itoa(row.IsOriginal))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.DanmakuCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.CommentCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.CollectCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.CoinCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.ShareCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.ElecPayCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.TotalPlayCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.WebPlayCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.AppPlayCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.H5PlayCount, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.LvUnknown, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.Lv0, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.Lv1, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.Lv2, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.Lv3, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.Lv4, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.Lv5, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.Lv6, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.VScore, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.IncCharge, 10))
buf.WriteByte(',')
buf.WriteString(strconv.FormatInt(row.TotalCharge, 10))
buf.WriteByte(',')
buf.WriteByte('\'')
buf.WriteString(row.Date.Time().Format(_layout))
buf.WriteByte('\'')
buf.WriteByte(',')
buf.WriteByte('\'')
buf.WriteString(row.UploadTime.Time().Format(_layout))
buf.WriteByte('\'')
buf.WriteString(")")
buf.WriteByte(',')
}
if buf.Len() > 0 {
buf.Truncate(buf.Len() - 1)
}
vals = buf.String()
buf.Reset()
return
}

View File

@@ -0,0 +1,19 @@
package income
import (
"context"
"fmt"
)
const (
_inAvIncomeSQL = "INSERT INTO av_income(av_id,mid,tag_id,is_original,upload_time,play_count,total_income,income,tax_money,date,base_income) VALUES %s ON DUPLICATE KEY UPDATE av_id=VALUES(av_id),mid=VALUES(mid),tag_id=VALUES(tag_id),is_original=VALUES(is_original),upload_time=VALUES(upload_time),play_count=VALUES(play_count),total_income=VALUES(total_income),income=VALUES(income),tax_money=VALUES(tax_money),date=VALUES(date),base_income=VALUES(base_income)"
)
// InsertAvIncome batch insert av income
func (d *Dao) InsertAvIncome(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inAvIncomeSQL, values))
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,45 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_avIncomeStatSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,total_income,ctime FROM av_income_statis WHERE id > ? ORDER BY id LIMIT ?"
_inAvIncomeStatSQL = "INSERT INTO av_income_statis(av_id,mid,tag_id,is_original,upload_time,total_income) VALUES %s ON DUPLICATE KEY UPDATE av_id=VALUES(av_id),mid=VALUES(mid),tag_id=VALUES(tag_id),is_original=VALUES(is_original),upload_time=VALUES(upload_time),total_income=VALUES(total_income)"
)
// AvIncomeStat key: av_id
func (d *Dao) AvIncomeStat(c context.Context, id int64, limit int64) (m map[int64]*model.AvIncomeStat, last int64, err error) {
rows, err := d.db.Query(c, _avIncomeStatSQL, id, limit)
if err != nil {
log.Error("d.db.Query AvIncomeStat error(%v)", err)
return
}
defer rows.Close()
m = make(map[int64]*model.AvIncomeStat)
for rows.Next() {
a := &model.AvIncomeStat{}
err = rows.Scan(&last, &a.AvID, &a.MID, &a.TagID, &a.IsOriginal, &a.UploadTime, &a.TotalIncome, &a.CTime)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
m[a.AvID] = a
}
return
}
// InsertAvIncomeStat batch insert av income stat
func (d *Dao) InsertAvIncomeStat(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inAvIncomeStatSQL, values))
if err != nil {
log.Error("d.db.Exec InsertAvIncomeStat error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,22 @@
package income
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_AvIncomeStat(t *testing.T) {
Convey("AvIncomeStat", t, func() {
_, _, err := d.AvIncomeStat(context.Background(), 0, 2000)
So(err, ShouldBeNil)
})
}
func Test_InsertAvIncomeStat(t *testing.T) {
Convey("InsertAvIncomeStat", t, func() {
_, err := d.InsertAvIncomeStat(context.Background(), "(123,2,6,1,'2018-06-01',100)")
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,17 @@
package income
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_InsertAvIncome(t *testing.T) {
Convey("InsertAvIncome", t, func() {
c := context.Background()
d.db.Exec(c, "DELETE FROM av_income WHERE date='2018-06-01'")
_, err := d.InsertAvIncome(c, "(123,2,6,1,'2018-06-01',100,100,100,50,'2018-06-01',100)")
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,53 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_insertBGMSQL = "INSERT INTO background_music(mid,sid,aid,cid,join_at,title) VALUES %s"
_getBGMSQL = "SELECT id,mid,sid,aid,cid,join_at FROM background_music WHERE id > ? ORDER BY id LIMIT ?"
_delBGMSQL = "DELETE FROM background_music LIMIT ?"
)
// InsertBGM insert bgm from data platform
func (d *Dao) InsertBGM(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_insertBGMSQL, values))
if err != nil {
log.Error("insert bgm error(%v)", err)
return
}
return res.RowsAffected()
}
// GetBGM get bgms
func (d *Dao) GetBGM(c context.Context, id int64, limit int64) (bs []*model.BGM, last int64, err error) {
rows, err := d.db.Query(c, _getBGMSQL, id, limit)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
b := &model.BGM{}
err = rows.Scan(&last, &b.MID, &b.SID, &b.AID, &b.CID, &b.JoinAt)
if err != nil {
return
}
bs = append(bs, b)
}
return
}
// DelBGM del bgm
func (d *Dao) DelBGM(c context.Context, limit int64) (rows int64, err error) {
res, err := d.db.Exec(c, _delBGMSQL, limit)
if err != nil {
log.Error("del bgm error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,19 @@
package income
import (
"context"
"fmt"
)
const (
_inBGMIncomeSQL = "INSERT INTO bgm_income(aid,sid,mid,cid,income,total_income,tax_money,date,base_income,daily_total_income) VALUES %s ON DUPLICATE KEY UPDATE income=VALUES(income),total_income=VALUES(total_income),tax_money=VALUES(tax_money),base_income=VALUES(base_income),daily_total_income=VALUES(daily_total_income)"
)
// InsertBgmIncome batch insert bgm income
func (d *Dao) InsertBgmIncome(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inBGMIncomeSQL, values))
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,45 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_bgmIncomeStatSQL = "SELECT id,sid,total_income FROM bgm_income_statis WHERE id > ? ORDER BY id LIMIT ?"
_inBgmIncomeStatSQL = "INSERT INTO bgm_income_statis(sid,total_income) VALUES %s ON DUPLICATE KEY UPDATE sid=VALUES(sid),total_income=VALUES(total_income)"
)
// BgmIncomeStat key: sid
func (d *Dao) BgmIncomeStat(c context.Context, id int64, limit int64) (m map[int64]*model.BgmIncomeStat, last int64, err error) {
rows, err := d.db.Query(c, _bgmIncomeStatSQL, id, limit)
if err != nil {
log.Error("d.db.Query BgmIncomeStat error(%v)", err)
return
}
defer rows.Close()
m = make(map[int64]*model.BgmIncomeStat)
for rows.Next() {
b := &model.BgmIncomeStat{}
err = rows.Scan(&last, &b.SID, &b.TotalIncome)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
m[b.SID] = b
}
return
}
// InsertBgmIncomeStat batch insert bgm income stat
func (d *Dao) InsertBgmIncomeStat(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inBgmIncomeStatSQL, values))
if err != nil {
log.Error("d.db.Exec InsertBgmIncomeStat error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,42 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeBgmIncomeStat(t *testing.T) {
convey.Convey("BgmIncomeStat", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
m, last, err := d.BgmIncomeStat(c, id, limit)
ctx.Convey("Then err should be nil.m,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(m, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeInsertBgmIncomeStat(t *testing.T) {
convey.Convey("InsertBgmIncomeStat", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(100,200)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertBgmIncomeStat(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,24 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeInsertBgmIncome(t *testing.T) {
convey.Convey("InsertBgmIncome", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1,2,3,4,5,6,7,'2018-06-24',100,100)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertBgmIncome(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,58 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeInsertBGM(t *testing.T) {
convey.Convey("InsertBGM", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1,2,3,4,'2018-06-24','test')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertBGM(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetBGM(t *testing.T) {
convey.Convey("GetBGM", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
bs, last, err := d.GetBGM(c, id, limit)
ctx.Convey("Then err should be nil.bs,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(bs, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeDelBGM(t *testing.T) {
convey.Convey("DelBGM", t, func(ctx convey.C) {
var (
c = context.Background()
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.DelBGM(c, limit)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,37 @@
package income
import (
"context"
"go-common/library/log"
)
const (
_blacklistSQL = "SELECT id,av_id,ctype,is_delete FROM av_black_list WHERE id > ? ORDER BY id LIMIT ?"
)
// Blacklist map[ctype][]id
func (d *Dao) Blacklist(c context.Context, id int64, limit int64) (m map[int][]int64, last int64, err error) {
rows, err := d.db.Query(c, _blacklistSQL, id, limit)
if err != nil {
return
}
m = make(map[int][]int64)
for rows.Next() {
var ctype, isDeleted int
var avID int64
err = rows.Scan(&last, &avID, &ctype, &isDeleted)
if err != nil {
log.Error("Rows Scan error(%v)", err)
return
}
if isDeleted == 0 {
if _, ok := m[ctype]; ok {
m[ctype] = append(m[ctype], avID)
} else {
m[ctype] = []int64{avID}
}
}
}
return
}

View File

@@ -0,0 +1,15 @@
package income
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Blacklist(t *testing.T) {
Convey("Blacklist", t, func() {
_, _, err := d.Blacklist(context.Background(), 0, 2000)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,32 @@
package income
import (
"context"
"go-common/library/log"
)
const (
_businessOrderSQL = "SELECT id,av_id FROM business_order_sheet WHERE id > ? ORDER BY id LIMIT ?"
)
// BusinessOrders get business order
func (d *Dao) BusinessOrders(c context.Context, offset, limit int64) (last int64, m map[int64]bool, err error) {
rows, err := d.rddb.Query(c, _businessOrderSQL, offset, limit)
if err != nil {
log.Error("d.rddb.Query BusinessOrders error(%v)", err)
return
}
defer rows.Close()
m = make(map[int64]bool)
for rows.Next() {
var avID int64
err = rows.Scan(&last, &avID)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
m[avID] = true
}
return
}

View File

@@ -0,0 +1,26 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeBusinessOrders(t *testing.T) {
convey.Convey("BusinessOrders", t, func(ctx convey.C) {
var (
c = context.Background()
offset = int64(0)
limit = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
last, m, err := d.BusinessOrders(c, offset, limit)
ctx.Convey("Then err should not be nil.last,m should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(m, convey.ShouldNotBeNil)
ctx.So(last, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,68 @@
package income
import (
"context"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_avChargeRatioSQL = "SELECT id,av_id,ratio,adjust_type,ctype FROM av_charge_ratio WHERE id > ? ORDER BY id LIMIT ?"
_upChargeRatioSQL = "SELECT id,mid,ratio,adjust_type,ctype FROM up_charge_ratio WHERE id > ? ORDER BY id LIMIT ?"
)
// ArchiveChargeRatio map[ctype]map[archive_id]*archiveChargeRatio
func (d *Dao) ArchiveChargeRatio(c context.Context, id int64, limit int64) (m map[int]map[int64]*model.ArchiveChargeRatio, last int64, err error) {
rows, err := d.db.Query(c, _avChargeRatioSQL, id, limit)
if err != nil {
log.Error("d.db.Query AvChargeRatio error(%v)", err)
return
}
m = make(map[int]map[int64]*model.ArchiveChargeRatio)
defer rows.Close()
for rows.Next() {
ratio := &model.ArchiveChargeRatio{}
err = rows.Scan(&last, &ratio.ArchiveID, &ratio.Ratio, &ratio.AdjustType, &ratio.CType)
if err != nil {
log.Error("AvChargeRatio scan error(%v)", err)
return
}
if ac, ok := m[ratio.CType]; ok {
ac[ratio.ArchiveID] = ratio
} else {
m[ratio.CType] = map[int64]*model.ArchiveChargeRatio{
ratio.ArchiveID: ratio,
}
}
}
return
}
// UpChargeRatio get every day up charge ratio
func (d *Dao) UpChargeRatio(c context.Context, id int64, limit int64) (m map[int]map[int64]*model.UpChargeRatio, last int64, err error) {
rows, err := d.db.Query(c, _upChargeRatioSQL, id, limit)
if err != nil {
log.Error("d.db.Query UpChargeRatio error(%v)", err)
return
}
m = make(map[int]map[int64]*model.UpChargeRatio)
defer rows.Close()
for rows.Next() {
ratio := &model.UpChargeRatio{}
err = rows.Scan(&last, &ratio.MID, &ratio.Ratio, &ratio.AdjustType, &ratio.CType)
if err != nil {
log.Error("UpChargeRatio scan error(%v)", err)
return
}
if ur, ok := m[ratio.CType]; ok {
ur[ratio.MID] = ratio
} else {
m[ratio.CType] = map[int64]*model.UpChargeRatio{
ratio.MID: ratio,
}
}
}
return
}

View File

@@ -0,0 +1,22 @@
package income
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_AvChargeRatio(t *testing.T) {
Convey("AvChargeRatio", t, func() {
_, _, err := d.ArchiveChargeRatio(context.Background(), 0, 2000)
So(err, ShouldBeNil)
})
}
func Test_UpChargeRatio(t *testing.T) {
Convey("UpChargeRatio", t, func() {
_, _, err := d.UpChargeRatio(context.Background(), 0, 2000)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,38 @@
package income
import (
"context"
"time"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
xtime "go-common/library/time"
)
const (
_columnChargeSQL = "SELECT id,aid,title,mid,tag_id,upload_time,inc_charge,view_c,date FROM column_daily_charge WHERE id > ? AND date = ? AND inc_charge > 0 ORDER BY id LIMIT ?"
)
// ColumnDailyCharge get column daily charge by date
func (d *Dao) ColumnDailyCharge(c context.Context, date time.Time, id int64, limit int) (columns []*model.ColumnCharge, err error) {
columns = make([]*model.ColumnCharge, 0)
rows, err := d.db.Query(c, _columnChargeSQL, id, date, limit)
if err != nil {
log.Error("ColumnDailyCharge d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
column := &model.ColumnCharge{}
var uploadTime int64
err = rows.Scan(&column.ID, &column.ArticleID, &column.Title, &column.MID, &column.TagID, &uploadTime, &column.IncCharge, &column.IncViewCount, &column.Date)
if err != nil {
log.Error("ColumnDailyCharge rows.Scan error(%v)", err)
return
}
column.UploadTime = xtime.Time(uploadTime)
columns = append(columns, column)
}
return
}

View File

@@ -0,0 +1,27 @@
package income
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeColumnDailyCharge(t *testing.T) {
convey.Convey("ColumnDailyCharge", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Now()
id = int64(0)
limit = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
columns, err := d.ColumnDailyCharge(c, date, id, limit)
ctx.Convey("Then err should be nil.columns should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(columns, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,19 @@
package income
import (
"context"
"fmt"
)
const (
_inColumnIncomeSQL = "INSERT INTO column_income(aid,mid,tag_id,upload_time,view_count,income,total_income,tax_money,date,base_income) VALUES %s ON DUPLICATE KEY UPDATE income=VALUES(income),total_income=VALUES(total_income),base_income=VALUES(base_income)"
)
// InsertColumnIncome batch insert column income
func (d *Dao) InsertColumnIncome(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inColumnIncomeSQL, values))
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,45 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_columnIncomeStatSQL = "SELECT id,aid,mid,upload_time,total_income,title FROM column_income_statis WHERE id > ? ORDER BY id LIMIT ?"
_inColumnIncomeStatSQL = "INSERT INTO column_income_statis(aid,title,mid,tag_id,upload_time,total_income) VALUES %s ON DUPLICATE KEY UPDATE aid=VALUES(aid),title=VALUES(title),mid=VALUES(mid),upload_time=VALUES(upload_time),total_income=VALUES(total_income)"
)
// ColumnIncomeStat key: av_id
func (d *Dao) ColumnIncomeStat(c context.Context, id int64, limit int64) (m map[int64]*model.ColumnIncomeStat, last int64, err error) {
rows, err := d.db.Query(c, _columnIncomeStatSQL, id, limit)
if err != nil {
log.Error("d.db.Query AvIncomeStat error(%v)", err)
return
}
defer rows.Close()
m = make(map[int64]*model.ColumnIncomeStat)
for rows.Next() {
c := &model.ColumnIncomeStat{}
err = rows.Scan(&last, &c.ArticleID, &c.MID, &c.UploadTime, &c.TotalIncome, &c.Title)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
m[c.ArticleID] = c
}
return
}
// InsertColumnIncomeStat batch insert column income stat
func (d *Dao) InsertColumnIncomeStat(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inColumnIncomeStatSQL, values))
if err != nil {
log.Error("d.db.Exec InsertColumnIncomeStat error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,42 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeColumnIncomeStat(t *testing.T) {
convey.Convey("ColumnIncomeStat", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
m, last, err := d.ColumnIncomeStat(c, id, limit)
ctx.Convey("Then err should be nil.m,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(m, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeInsertColumnIncomeStat(t *testing.T) {
convey.Convey("InsertColumnIncomeStat", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1,'test',12,2,'2018-06-24',100)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertColumnIncomeStat(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,24 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeInsertColumnIncome(t *testing.T) {
convey.Convey("InsertColumnIncome", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1,2,3,'2018-06-24',100,10,100,10,'2018-06-24',100)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertColumnIncome(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,68 @@
package income
import (
"context"
"go-common/app/job/main/growup/conf"
"go-common/library/database/sql"
"go-common/library/log"
)
// Dao dao
type Dao struct {
c *conf.Config
db *sql.DB
rddb *sql.DB
Tx *sql.Tx
}
// New fn
func New(c *conf.Config) (d *Dao) {
log.Info("dao start")
d = &Dao{
c: c,
db: sql.NewMySQL(c.Mysql.Growup),
rddb: sql.NewMySQL(c.Mysql.Allowance),
}
d.Tx, _ = d.BeginTran(context.TODO())
//d.db.State = prom.LibClient
return d
}
// Ping ping health.
func (d *Dao) Ping(c context.Context) (err error) {
return d.db.Ping(c)
}
// Close close connections of mc, redis, db.
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
}
// BeginTran begin transcation
func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
return d.db.Begin(c)
}
// Exec exec sql
func (d *Dao) Exec(c context.Context, sql string) (err error) {
_, err = d.db.Exec(c, sql)
return
}
// QueryRow query row
func (d *Dao) QueryRow(c context.Context, sql string) (rows *sql.Row) {
return d.db.QueryRow(c, sql)
}
// Query query
func (d *Dao) Query(c context.Context, sql string) (rows *sql.Rows, err error) {
return d.db.Query(c, sql)
}
// test
// func (d *Dao) Truncate(c context.Context, table string) {
// d.db.Exec(c, fmt.Sprintf("truncate %s", table))
// }

View File

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

View File

@@ -0,0 +1,61 @@
package income
import (
"context"
"fmt"
"go-common/library/log"
)
const (
_inIncomeStatisTableSQL = "INSERT INTO %s(avs, money_section, money_tips, income, category_id, cdate) VALUES %s ON DUPLICATE KEY UPDATE avs=VALUES(avs),income=VALUES(income),cdate=VALUES(cdate)"
_delIncomeStatisTableSQL = "DELETE FROM %s WHERE cdate = ?"
_inUpIncomeDailyStatisSQL = "INSERT INTO %s(ups, money_section, money_tips, income, cdate) VALUES %s ON DUPLICATE KEY UPDATE ups=VALUES(ups),income=VALUES(income),cdate=VALUES(cdate)"
)
// InsertIncomeStatisTable add av_income_date_statis batch
func (d *Dao) InsertIncomeStatisTable(c context.Context, table, vals string) (rows int64, err error) {
if table == "" {
err = fmt.Errorf("InsertIncomeStatisTable table(%s) val(%s) error", table, vals)
return
}
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inIncomeStatisTableSQL, table, vals))
if err != nil {
log.Error("incomeStatisTableBatch d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// InsertUpIncomeDailyStatis add up_income_daily_statis batch
func (d *Dao) InsertUpIncomeDailyStatis(c context.Context, table string, vals string) (rows int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inUpIncomeDailyStatisSQL, table, vals))
if err != nil {
log.Error("InsertUpIncomeDailyStatis d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}
// DelIncomeStatisTable del income statis by table
func (d *Dao) DelIncomeStatisTable(c context.Context, table, date string) (rows int64, err error) {
if table == "" || date == "" {
err = fmt.Errorf("DelIncomeStatisTable table(%s) date(%s) error", table, date)
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_delIncomeStatisTableSQL, table), date)
if err != nil {
log.Error("DelIncomeStatisTable d.db.Exec error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,59 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeInsertIncomeStatisTable(t *testing.T) {
convey.Convey("InsertIncomeStatisTable", t, func(ctx convey.C) {
var (
c = context.Background()
table = "av_income_daily_statis"
vals = "(1,2,'100-200',100,1,'2018-06-24')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertIncomeStatisTable(c, table, vals)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeInsertUpIncomeDailyStatis(t *testing.T) {
convey.Convey("InsertUpIncomeDailyStatis", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_income_daily_statis"
vals = "(10,1,'100-200',1,'2018-06-24')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertUpIncomeDailyStatis(c, table, vals)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeDelIncomeStatisTable(t *testing.T) {
convey.Convey("DelIncomeStatisTable", t, func(ctx convey.C) {
var (
c = context.Background()
table = "av_income_daily_statis"
date = "2018-06-24"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.DelIncomeStatisTable(c, table, date)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,31 @@
package income
import (
"context"
)
const (
_getBubbleMetaSQL = "SELECT id,av_id,b_type FROM lottery_av_info WHERE id > ? ORDER BY id LIMIT ?"
)
// GetBubbleMeta get bubble meta
func (d *Dao) GetBubbleMeta(c context.Context, id int64, limit int64) (data map[int64][]int, last int64, err error) {
data = make(map[int64][]int)
rows, err := d.db.Query(c, _getBubbleMetaSQL, id, limit)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var (
avID int64
bType int
)
err = rows.Scan(&last, &avID, &bType)
if err != nil {
return
}
data[avID] = append(data[avID], bType)
}
return
}

View File

@@ -0,0 +1,26 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeGetBubbleMeta(t *testing.T) {
convey.Convey("GetBubbleMeta", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int64(20)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
data, last, err := d.GetBubbleMeta(c, id, limit)
ctx.Convey("Then err should be nil.data,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,77 @@
package income
import (
"context"
"database/sql"
"fmt"
"go-common/library/log"
model "go-common/app/job/main/growup/model/income"
)
const (
// select
_upAccountsSQL = "SELECT id,mid,has_sign_contract,state,total_income,total_unwithdraw_income,total_withdraw_income,last_withdraw_time,version,allowance_state,nick_name,withdraw_date_version,is_deleted FROM up_account WHERE id > ? ORDER BY id LIMIT ?"
_upAccountSQL = "SELECT total_income,total_unwithdraw_income,version,withdraw_date_version FROM up_account WHERE mid=?"
// for batch insert
_insertUpAccountSQL = "INSERT INTO up_account(mid,has_sign_contract,total_income,total_unwithdraw_income,withdraw_date_version,version) VALUES %s ON DUPLICATE KEY UPDATE total_income=VALUES(total_income),total_unwithdraw_income=VALUES(total_unwithdraw_income),version=VALUES(version)"
_updateUpAccountSQL = "UPDATE up_account SET total_income=?,total_unwithdraw_income=?,version=? WHERE mid=? AND version=?"
)
// UpAccounts batch read up account
func (d *Dao) UpAccounts(c context.Context, id int64, limit int64) (m map[int64]*model.UpAccount, last int64, err error) {
rows, err := d.db.Query(c, _upAccountsSQL, id, limit)
if err != nil {
log.Error("d.db.UpAccounts error(%v)", err)
return
}
m = make(map[int64]*model.UpAccount)
defer rows.Close()
for rows.Next() {
ua := &model.UpAccount{}
err = rows.Scan(&last, &ua.MID, &ua.HasSignContract, &ua.State, &ua.TotalIncome, &ua.TotalUnwithdrawIncome, &ua.TotalWithdrawIncome, &ua.LastWithdrawTime, &ua.Version, &ua.AllowanceState, &ua.Nickname, &ua.WithdrawDateVersion, &ua.IsDeleted)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
m[ua.MID] = ua
}
return
}
// UpAccount get up account by mid
func (d *Dao) UpAccount(c context.Context, mid int64) (a *model.UpAccount, err error) {
row := d.db.QueryRow(c, _upAccountSQL, mid)
a = &model.UpAccount{}
if err = row.Scan(&a.TotalIncome, &a.TotalUnwithdrawIncome, &a.Version, &a.WithdrawDateVersion); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("dao.UpAccount error(%v)", err)
}
}
return
}
// InsertUpAccount batch insert up account
func (d *Dao) InsertUpAccount(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_insertUpAccountSQL, values))
if err != nil {
log.Error("d.db.Exec InsertUpAccount error(%v)", err)
return
}
return res.RowsAffected()
}
// UpdateUpAccount update up account by mid and version instead batch update
func (d *Dao) UpdateUpAccount(c context.Context, mid, ver, totalIncome, totalUnwithdrawIncome int64) (rows int64, err error) {
res, err := d.db.Exec(c, _updateUpAccountSQL, totalIncome, totalUnwithdrawIncome, ver+1, mid, ver)
if err != nil {
log.Error("d.db.Exec UpdateUpAccount error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,36 @@
package income
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_UpAccounts(t *testing.T) {
Convey("UpAccounts", t, func() {
_, _, err := d.UpAccounts(context.Background(), 0, 2000)
So(err, ShouldBeNil)
})
}
func Test_UpAccount(t *testing.T) {
Convey("UpAccount", t, func() {
_, err := d.UpAccount(context.Background(), 123)
So(err, ShouldBeNil)
})
}
func Test_InsertUpAccount(t *testing.T) {
Convey("InsertUpAccount", t, func() {
_, err := d.InsertUpAccount(context.Background(), "(123,1,100,300,2018-05,22)")
So(err, ShouldBeNil)
})
}
func Test_UpdateUpAccount(t *testing.T) {
Convey("UpdateUpAccount", t, func() {
_, err := d.UpdateUpAccount(context.Background(), int64(100), int64(22), int64(123), int64(100))
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,46 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_insertUpChargeSQL = "INSERT INTO %s (mid,inc_charge,total_charge,date) VALUES %s ON DUPLICATE KEY UPDATE inc_charge=VALUES(inc_charge),total_charge=VALUES(total_charge)"
_upChargeSQL = "SELECT id,mid,inc_charge,total_charge,date FROM %s WHERE date=? AND id > ? ORDER BY id LIMIT ?"
)
// GetUpCharges get up charges
func (d *Dao) GetUpCharges(c context.Context, table string, date string, offset, limit int64) (last int64, charges map[int64]*model.UpCharge, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_upChargeSQL, table), date, offset, limit)
if err != nil {
log.Error("d.db.Query GetUpCharges error(%v)", err)
return
}
charges = make(map[int64]*model.UpCharge)
defer rows.Close()
for rows.Next() {
c := &model.UpCharge{}
err = rows.Scan(&last, &c.MID, &c.IncCharge, &c.TotalCharge, &c.Date)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
charges[c.MID] = c
}
return
}
// InsertUpCharge batch insert up charge
func (d *Dao) InsertUpCharge(c context.Context, table string, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_insertUpChargeSQL, table, values))
if err != nil {
log.Error("d.db.Exec InsertUpCharge error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,46 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeGetUpCharges(t *testing.T) {
convey.Convey("GetUpCharges", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_daily_charge"
date = "2018-06-24"
offset = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_daily_charge(mid,date) VALUES(1,'2018-06-24') ON DUPLICATE KEY UPDATE date=VALUES(date)")
last, charges, err := d.GetUpCharges(c, table, date, offset, limit)
ctx.Convey("Then err should be nil.last,charges should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(charges, convey.ShouldNotBeNil)
ctx.So(last, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeInsertUpCharge(t *testing.T) {
convey.Convey("InsertUpCharge", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_daily_charge"
values = "(1,2,3,'2018-06-24')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertUpCharge(c, table, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,57 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_inUpIncomeSQL = "INSERT INTO up_income(mid,av_count,play_count,av_income,audio_income,column_count,column_income,tax_money,income,total_income,av_base_income,av_tax,column_base_income,column_tax,bgm_base_income,bgm_tax,date,base_income,bgm_income,bgm_count,av_total_income,column_total_income,bgm_total_income) VALUES %s ON DUPLICATE KEY UPDATE mid=VALUES(mid),av_count=VALUES(av_count),play_count=VALUES(play_count),av_income=VALUES(av_income),audio_income=VALUES(audio_income),column_count=VALUES(column_count),column_income=VALUES(column_income),tax_money=VALUES(tax_money),income=VALUES(income),total_income=VALUES(total_income),av_base_income=VALUES(av_base_income),av_tax=VALUES(av_tax),column_base_income=VALUES(column_base_income),column_tax=VALUES(column_tax),bgm_base_income=VALUES(bgm_base_income),bgm_tax=VALUES(bgm_tax),date=VALUES(date),base_income=VALUES(base_income),bgm_income=VALUES(bgm_income),bgm_count=VALUES(bgm_count),av_total_income=VALUES(av_total_income),column_total_income=VALUES(column_total_income),bgm_total_income=VALUES(bgm_total_income)"
_upIncomeSQL = "SELECT id,mid,av_income,audio_income,column_income,bgm_income,date FROM up_income WHERE id > ? ORDER BY id LIMIT ?"
_fixInUpIncomeSQL = "INSERT INTO up_income(mid,av_total_income,column_total_income,bgm_total_income,date) VALUES %s ON DUPLICATE KEY UPDATE av_total_income=VALUES(av_total_income),column_total_income=VALUES(column_total_income),bgm_total_income=VALUES(bgm_total_income)"
)
// UpIncomes up incomes
func (d *Dao) UpIncomes(c context.Context, id int64, limit int64) (last int64, us []*model.UpIncome, err error) {
rows, err := d.db.Query(c, _upIncomeSQL, id, limit)
if err != nil {
log.Error("d.db.Query UpIncomes error (%v)", err)
return
}
defer rows.Close()
for rows.Next() {
u := &model.UpIncome{}
err = rows.Scan(&last, &u.MID, &u.AvIncome, &u.AudioIncome, &u.ColumnIncome, &u.BgmIncome, &u.Date)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
us = append(us, u)
}
return
}
// InsertUpIncome batch insert up income
func (d *Dao) InsertUpIncome(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inUpIncomeSQL, values))
if err != nil {
log.Error("d.db.Exec InsertUpIncome error (%v)", err)
return
}
return res.RowsAffected()
}
// FixInsertUpIncome batch insert up income
func (d *Dao) FixInsertUpIncome(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_fixInUpIncomeSQL, values))
if err != nil {
log.Error("d.db.Exec InsertUpIncome error (%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,47 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_getUpIncomeTableSQL = "SELECT id,mid,av_count,play_count,av_income,audio_income,column_count,column_income,bgm_income,av_tax,column_tax,bgm_tax,tax_money,income,total_income,av_base_income,column_base_income,bgm_base_income,base_income,av_total_income,column_total_income,bgm_total_income,date FROM %s WHERE id > ? AND date = ? ORDER BY id LIMIT ?"
_inUpIncomeTableSQL = "INSERT INTO %s(mid,av_count,play_count,av_income,audio_income,column_count,column_income,bgm_count,bgm_income,tax_money,income,total_income,av_base_income,av_tax,column_base_income,column_tax,bgm_base_income,bgm_tax,date,base_income,av_total_income,column_total_income,bgm_total_income) VALUES %s ON DUPLICATE KEY UPDATE mid=VALUES(mid),av_count=VALUES(av_count),play_count=VALUES(play_count),av_income=VALUES(av_income),audio_income=VALUES(audio_income),column_count=VALUES(column_count),column_income=VALUES(column_income),bgm_count=VALUES(bgm_count),bgm_income=VALUES(bgm_income),tax_money=VALUES(tax_money),income=VALUES(income),total_income=VALUES(total_income),av_base_income=VALUES(av_base_income),av_tax=VALUES(av_tax),column_base_income=VALUES(column_base_income),column_tax=VALUES(column_tax),bgm_base_income=VALUES(bgm_base_income),bgm_tax=VALUES(bgm_tax),date=VALUES(date),base_income=VALUES(base_income),av_total_income=VALUES(av_total_income),column_total_income=VALUES(column_total_income),bgm_total_income=VALUES(bgm_total_income)"
)
// GetUpIncomeTable get up_income up_income_weekly up_income_monthly
func (d *Dao) GetUpIncomeTable(c context.Context, table, date string, id int64, limit int) (ups []*model.UpIncome, err error) {
ups = make([]*model.UpIncome, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_getUpIncomeTableSQL, table), id, date, limit)
if err != nil {
log.Error("GetUpIncomeTable d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
up := &model.UpIncome{}
err = rows.Scan(&up.ID, &up.MID, &up.AvCount, &up.PlayCount, &up.AvIncome, &up.AudioIncome, &up.ColumnCount, &up.ColumnIncome, &up.BgmIncome, &up.AvTax, &up.ColumnTax, &up.BgmTax, &up.TaxMoney, &up.Income, &up.TotalIncome, &up.AvBaseIncome, &up.ColumnBaseIncome, &up.BgmBaseIncome, &up.BaseIncome, &up.AvTotalIncome, &up.ColumnTotalIncome, &up.BgmTotalIncome, &up.Date)
if err != nil {
log.Error("GetUpIncomeTable rows.Scan error(%v)", err)
return
}
ups = append(ups, up)
}
return
}
// InsertUpIncomeTable insert up_income up_income_weekly up_income_monthly
func (d *Dao) InsertUpIncomeTable(c context.Context, table, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inUpIncomeTableSQL, table, values))
if err != nil {
log.Error("InsertUpIncome error (%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,22 @@
package income
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_GetUpIncomeTable(t *testing.T) {
Convey("GetUpIncomeTable", t, func() {
_, err := d.GetUpIncomeTable(context.Background(), "up_income_weekly", "2018-06-01", 0, 2000)
So(err, ShouldBeNil)
})
}
func Test_InsertUpIncomeTable(t *testing.T) {
Convey("InsertUpIncomeTable", t, func() {
_, err := d.InsertUpIncomeTable(context.Background(), "up_income_weekly", "(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,'2018-06-24',1,2,3,4)")
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,60 @@
package income
import (
"context"
"fmt"
"go-common/library/log"
model "go-common/app/job/main/growup/model/income"
)
const (
_upIncomeStatSQL = "SELECT id,mid,total_income,av_total_income,column_total_income,bgm_total_income FROM up_income_statis WHERE id > ? ORDER BY id LIMIT ?"
_inUpIncomeStatSQL = "INSERT INTO up_income_statis(mid,total_income,av_total_income,column_total_income,bgm_total_income) VALUES %s ON DUPLICATE KEY UPDATE mid=VALUES(mid),total_income=VALUES(total_income),av_total_income=VALUES(av_total_income),column_total_income=VALUES(column_total_income),bgm_total_income=VALUES(bgm_total_income)"
_fixUpIncomeStatSQL = "INSERT INTO up_income_statis(mid,av_total_income,column_total_income,bgm_total_income) VALUES %s ON DUPLICATE KEY UPDATE mid=VALUES(mid),av_total_income=VALUES(av_total_income),column_total_income=VALUES(column_total_income),bgm_total_income=VALUES(bgm_total_income)"
)
// UpIncomeStat return m key: mid, value: total_income
func (d *Dao) UpIncomeStat(c context.Context, id int64, limit int64) (m map[int64]*model.UpIncomeStat, last int64, err error) {
rows, err := d.db.Query(c, _upIncomeStatSQL, id, limit)
if err != nil {
log.Error("UpIncomeStat Query (%s, %d, %d) error(%v)", _upIncomeStatSQL, id, limit, err)
return
}
defer rows.Close()
m = make(map[int64]*model.UpIncomeStat)
for rows.Next() {
u := &model.UpIncomeStat{}
err = rows.Scan(&last, &u.MID, &u.TotalIncome, &u.AvTotalIncome, &u.ColumnTotalIncome, &u.BgmTotalIncome)
if err != nil {
log.Error("UpIncomeStat rows scan error(%v)", err)
return
}
m[u.MID] = u
}
return
}
// InsertUpIncomeStat batch insert up income stat
func (d *Dao) InsertUpIncomeStat(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inUpIncomeStatSQL, values))
if err != nil {
log.Error("d.db.Exec InsertUpIncomeStat error(%v)", err)
return
}
return res.RowsAffected()
}
// FixInsertUpIncomeStat fix insert up income stat
func (d *Dao) FixInsertUpIncomeStat(c context.Context, values string) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_fixUpIncomeStatSQL, values))
if err != nil {
log.Error("d.db.Exec InsertUpIncomeStat error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,59 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeUpIncomeStat(t *testing.T) {
convey.Convey("UpIncomeStat", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
d.db.Exec(c, "INSERT INTO up_income(mid,date) VALUES(1,'2018-06-24') ON DUPLICATE KEY UPDATE date=VALUES(date)")
m, last, err := d.UpIncomeStat(c, id, limit)
ctx.Convey("Then err should be nil.m,last should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(last, convey.ShouldNotBeNil)
ctx.So(m, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeInsertUpIncomeStat(t *testing.T) {
convey.Convey("InsertUpIncomeStat", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1,2,3,4,5)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.InsertUpIncomeStat(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeFixInsertUpIncomeStat(t *testing.T) {
convey.Convey("FixInsertUpIncomeStat", t, func(ctx convey.C) {
var (
c = context.Background()
values = "(1,2,3,4)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.FixInsertUpIncomeStat(c, values)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,15 @@
package income
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_InsertUpIncome(t *testing.T) {
Convey("InsertUpIncome", t, func() {
_, err := d.InsertUpIncome(context.Background(), "(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,'2018-06-24',1,2,3,4,5,6)")
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,36 @@
package income
import (
"context"
"fmt"
model "go-common/app/job/main/growup/model/income"
"go-common/library/log"
)
const (
_signedSQL = "SELECT id,mid,signed_at,quit_at,account_state,is_deleted FROM up_info_%s WHERE id > ? ORDER BY id LIMIT ?"
)
// Ups get ups by business type
func (d *Dao) Ups(c context.Context, business string, offset int64, limit int64) (last int64, ups map[int64]*model.Signed, err error) {
ups = make(map[int64]*model.Signed)
rows, err := d.db.Query(c, fmt.Sprintf(_signedSQL, business), offset, limit)
if err != nil {
log.Error("db Query Signed up Info error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
up := &model.Signed{}
err = rows.Scan(&last, &up.MID, &up.SignedAt, &up.QuitAt, &up.AccountState, &up.IsDeleted)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
if up.IsDeleted == 0 {
ups[up.MID] = up
}
}
return
}

View File

@@ -0,0 +1,15 @@
package income
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_Ups(t *testing.T) {
Convey("Ups", t, func() {
_, _, err := d.Ups(context.Background(), "video", 0, 2000)
So(err, ShouldBeNil)
})
}

View File

@@ -0,0 +1,237 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetAvTagRatio(t *testing.T) {
convey.Convey("GetAvTagRatio", t, func(ctx convey.C) {
var (
c = context.Background()
from = int64(0)
limit = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_charge_ratio(tag_id,av_id) VALUES(1,2) ON DUPLICATE KEY UPDATE tag_id=VALUES(tag_id), av_id=VALUES(av_id)")
infos, err := d.GetAvTagRatio(c, from, limit)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvIncomeInfo(t *testing.T) {
convey.Convey("GetAvIncomeInfo", t, func(ctx convey.C) {
var (
c = context.Background()
avID = int64(1)
date = time.Date(2018, 6, 24, 0, 0, 0, 0, time.Local)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_income(av_id,mid,income,date) VALUES(1,2,3,'2018-06-24') ON DUPLICATE KEY UPDATE income=VALUES(income), av_id=VALUES(av_id)")
info, err := d.GetAvIncomeInfo(c, avID, date)
ctx.Convey("Then err should be nil.info should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(info, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTxInsertTagIncome(t *testing.T) {
convey.Convey("TxInsertTagIncome", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
sql = "(1, 2, 3, 4, 5, '2018-06-23')"
)
defer tx.Commit()
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.TxInsertTagIncome(tx, sql)
ctx.Convey("Then err should be nil.rows should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetTagAvTotalIncome(t *testing.T) {
convey.Convey("GetTagAvTotalIncome", t, func(ctx convey.C) {
var (
c = context.Background()
tagID = int64(2)
avID = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_tag_income (tag_id, av_id, total_income, date) VALUES(2, 1, 100,'2018-06-24') ON DUPLICATE KEY UPDATE tag_id=VALUES(tag_id),av_id=VALUES(av_id)")
infos, err := d.GetTagAvTotalIncome(c, tagID, avID)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListAvIncome(t *testing.T) {
convey.Convey("ListAvIncome", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
avIncome, err := d.ListAvIncome(c, id, limit)
ctx.Convey("Then err should be nil.avIncome should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(avIncome, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListUpAccount(t *testing.T) {
convey.Convey("ListUpAccount", t, func(ctx convey.C) {
var (
c = context.Background()
withdrawDate = "2018-06"
ctime = "2018-06-23"
from = int(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
upAct, err := d.ListUpAccount(c, withdrawDate, ctime, from, limit)
ctx.Convey("Then err should be nil.upAct should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(upAct, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListUpIncome(t *testing.T) {
convey.Convey("ListUpIncome", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_income"
date = "2018-06-23"
id = int64(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
um, err := d.ListUpIncome(c, table, date, id, limit)
ctx.Convey("Then err should be nil.um should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(um, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListUpWithdraw(t *testing.T) {
convey.Convey("ListUpWithdraw", t, func(ctx convey.C) {
var (
c = context.Background()
date = "2018-06-23"
from = int(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ups, err := d.ListUpWithdraw(c, date, from, limit)
ctx.Convey("Then err should be nil.ups should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ups, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUpTotalIncome(t *testing.T) {
convey.Convey("GetUpTotalIncome", t, func(ctx convey.C) {
var (
c = context.Background()
from = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
infos, err := d.GetUpTotalIncome(c, from, limit)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUpIncome(t *testing.T) {
convey.Convey("GetUpIncome", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Now()
from = int64(0)
limit = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
infos, err := d.GetUpIncome(c, date, from, limit)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvIncome(t *testing.T) {
convey.Convey("GetAvIncome", t, func(ctx convey.C) {
var (
c = context.Background()
date = time.Now()
id = int64(0)
limit = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
infos, err := d.GetAvIncome(c, date, id, limit)
ctx.Convey("Then err should be nil.infos should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(infos, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUpTotalIncomeCnt(t *testing.T) {
convey.Convey("GetUpTotalIncomeCnt", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
upCnt, err := d.GetUpTotalIncomeCnt(c)
ctx.Convey("Then err should be nil.upCnt should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(upCnt, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvStatisCount(t *testing.T) {
convey.Convey("GetAvStatisCount", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
cnt, err := d.GetAvStatisCount(c)
ctx.Convey("Then err should be nil.cnt should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cnt, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,99 @@
package dao
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/app/job/main/growup/model"
"go-common/library/ecode"
"go-common/library/log"
)
const (
// insert
_inBubbleMetaSQL = "INSERT INTO lottery_av_info(av_id, date, b_type) VALUES %s ON DUPLICATE KEY UPDATE date=VALUES(date)"
_inBubbleIncomeSQL = "INSERT INTO lottery_av_income(av_id,mid,tag_id,upload_time,total_income,income,tax_money,date,base_income,b_type) VALUES %s ON DUPLICATE KEY UPDATE av_id=VALUES(av_id),mid=VALUES(mid),tag_id=VALUES(tag_id),upload_time=VALUES(upload_time),total_income=VALUES(total_income),income=VALUES(income),tax_money=VALUES(tax_money),date=VALUES(date),base_income=VALUES(base_income)"
)
// InsertBubbleMeta insert lottery avs
func (d *Dao) InsertBubbleMeta(c context.Context, values string) (rows int64, err error) {
if values == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inBubbleMetaSQL, values))
if err != nil {
log.Error("dao.InsertBubbleMeta exec error(%v)", err)
return
}
return res.RowsAffected()
}
// InsertBubbleIncome insert lottery income
func (d *Dao) InsertBubbleIncome(c context.Context, vals string) (rows int64, err error) {
if vals == "" {
return
}
res, err := d.db.Exec(c, fmt.Sprintf(_inBubbleIncomeSQL, vals))
if err != nil {
log.Error("dao.InsertBubbleIncome exec error(%v)", err)
return
}
return res.RowsAffected()
}
// GetLotteryRIDs get lottery id
func (d *Dao) GetLotteryRIDs(c context.Context, start, end, offset int64) (info *model.LotteryRes, err error) {
info = &model.LotteryRes{}
params := url.Values{}
params.Set("begin", strconv.FormatInt(start, 10))
params.Set("end", strconv.FormatInt(end, 10))
params.Set("offset", strconv.FormatInt(offset, 10))
var res struct {
Code int `json:"code"`
Message string `json:"message"`
Data *model.LotteryRes `json:"data"`
}
uri := d.avLotteryURL
if err = d.client.Get(c, uri, "", params, &res); err != nil {
log.Error("d.client.Get uri(%s) error(%v)", uri+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("GetLotteryRIDs code != 0. res.Code(%d) | uri(%s) error(%v)", res.Code, uri+"?"+params.Encode(), err)
err = ecode.GrowupGetActivityError
return
}
info = res.Data
return
}
// VoteBIZArchive fetch avs in vote biz
func (d *Dao) VoteBIZArchive(c context.Context, start, end int64) (data []*model.VoteBIZArchive, err error) {
var (
uri = d.avVoteURL
params = url.Values{}
res struct {
Code int `json:"code"`
Message string `json:"message"`
Data []*model.VoteBIZArchive `json:"data"`
}
)
params.Set("start", strconv.FormatInt(start, 10))
params.Set("end", strconv.FormatInt(end, 10))
if err = d.client.Get(c, uri, "", params, &res); err != nil {
log.Error("d.client.Get uri(%s) error(%v)", uri+"?"+params.Encode(), err)
return
}
if res.Code != 0 {
log.Error("VoteBIZArchive code != 0. res.Code(%d) | uri(%s) errorMsg(%s)", res.Code, uri+"?"+params.Encode(), res.Message)
err = ecode.Errorf(ecode.ServerErr, "获取参与投票的视频失败")
return
}
data = res.Data
return
}

Some files were not shown because too many files have changed in this diff Show More