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,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)
})
})
})
}