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,75 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"av_breach_test.go",
"av_income_test.go",
"bgm_test.go",
"blacklist_test.go",
"charge_test.go",
"dao_test.go",
"lottery_test.go",
"up_account_test.go",
"up_category_info_test.go",
"up_income_test.go",
"up_info_video_test.go",
"up_withdraw_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/growup/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"av_breach.go",
"av_income.go",
"bgm.go",
"blacklist.go",
"charge.go",
"dao.go",
"lottery.go",
"up_account.go",
"up_category_info.go",
"up_income.go",
"up_info_video.go",
"up_withdraw.go",
],
importpath = "go-common/app/admin/main/growup/dao/income",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/growup/conf:go_default_library",
"//app/admin/main/growup/model/income:go_default_library",
"//library/database/sql:go_default_library",
"//library/log: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"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,105 @@
package income
import (
"context"
"fmt"
model "go-common/app/admin/main/growup/model/income"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
// insert
_inAvBreachSQL = "INSERT INTO av_breach_record(av_id,mid,cdate,money,ctype,reason,upload_time) VALUES %s"
// select
_avBreachByMIDsSQL = "SELECT av_id, mid, cdate, money FROM av_breach_record WHERE mid in (%s) AND ctype in (%s)"
_breachSQL = "SELECT av_id,mid,cdate,money,ctype,reason,upload_time FROM av_breach_record WHERE %s"
_breachCountSQL = "SELECT count(*) FROM av_breach_record WHERE %s"
// update
_upAvBreachPreSQL = "UPDATE av_breach_pre SET state = 2 WHERE aid IN (%s) AND ctype = 0 AND cdate <= '%s'"
)
// BreachCount breach count
func (d *Dao) BreachCount(c context.Context, query string) (total int, err error) {
err = d.db.QueryRow(c, fmt.Sprintf(_breachCountSQL, query)).Scan(&total)
if err == sql.ErrNoRows {
err = nil
}
return
}
// ListArchiveBreach list av_breach_record by query
func (d *Dao) ListArchiveBreach(c context.Context, query string) (breachs []*model.AvBreach, err error) {
breachs = make([]*model.AvBreach, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_breachSQL, query))
if err != nil {
log.Error("ListArchiveBreach d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
b := &model.AvBreach{}
err = rows.Scan(&b.AvID, &b.MID, &b.CDate, &b.Money, &b.CType, &b.Reason, &b.UploadTime)
if err != nil {
log.Error("ListArchiveBreach rows.Scan error(%v)", err)
return
}
breachs = append(breachs, b)
}
err = rows.Err()
return
}
// TxInsertAvBreach insert av_breach_record
func (d *Dao) TxInsertAvBreach(tx *sql.Tx, val string) (rows int64, err error) {
if val == "" {
return
}
res, err := tx.Exec(fmt.Sprintf(_inAvBreachSQL, val))
if err != nil {
return
}
return res.RowsAffected()
}
// GetAvBreachByMIDs get av_breach_record by mids
func (d *Dao) GetAvBreachByMIDs(c context.Context, mids []int64, types []int64) (breachs []*model.AvBreach, err error) {
if len(mids) == 0 {
return
}
rows, err := d.db.Query(c, fmt.Sprintf(_avBreachByMIDsSQL, xstr.JoinInts(mids), xstr.JoinInts(types)))
if err != nil {
log.Error("GetAvBreachByMIDs d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
b := &model.AvBreach{}
err = rows.Scan(&b.AvID, &b.MID, &b.CDate, &b.Money)
if err != nil {
log.Error("GetAvBreachByMIDs rows scan error(%v)", err)
return
}
breachs = append(breachs, b)
}
err = rows.Err()
return
}
// TxUpdateBreachPre update av_breach_pre state = 2
func (d *Dao) TxUpdateBreachPre(tx *sql.Tx, aids []int64, cdate string) (rows int64, err error) {
if len(aids) == 0 {
return
}
res, err := tx.Exec(fmt.Sprintf(_upAvBreachPreSQL, xstr.JoinInts(aids), cdate))
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,94 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeBreachCount(t *testing.T) {
convey.Convey("BreachCount", t, func(ctx convey.C) {
var (
c = context.Background()
query = "id > 0"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
total, err := d.BreachCount(c, query)
ctx.Convey("Then err should be nil.total should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeListArchiveBreach(t *testing.T) {
convey.Convey("ListArchiveBreach", t, func(ctx convey.C) {
var (
c = context.Background()
query = "id > 0"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
breachs, err := d.ListArchiveBreach(c, query)
ctx.Convey("Then err should be nil.breachs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(breachs, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeTxInsertAvBreach(t *testing.T) {
convey.Convey("TxInsertAvBreach", t, func(ctx convey.C) {
var (
tx, _ = d.BeginTran(context.Background())
val = "(520,1100,'2018-01-01',10,0,'aa','2018-01-01')"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer tx.Commit()
Exec(context.Background(), "DELETE FROM av_breach_record WHERE av_id = 520")
rows, err := d.TxInsertAvBreach(tx, 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)
})
})
})
}
func TestIncomeGetAvBreachByMIDs(t *testing.T) {
convey.Convey("GetAvBreachByMIDs", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{1000}
types = []int64{1}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_breach_record(mid, ctype) VALUES(1000, 1)")
breachs, err := d.GetAvBreachByMIDs(c, mids, types)
ctx.Convey("Then err should be nil.breachs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(breachs, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeTxUpdateBreachPre(t *testing.T) {
convey.Convey("TxUpdateBreachPre", t, func(ctx convey.C) {
var (
tx, _ = d.BeginTran(context.Background())
aids = []int64{1001}
cdate = "2018-06-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer tx.Commit()
rows, err := d.TxUpdateBreachPre(tx, aids, cdate)
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,119 @@
package income
import (
"context"
"fmt"
model "go-common/app/admin/main/growup/model/income"
"go-common/library/log"
)
var (
_video = 0
_column = 2
_bgm = 3
_lottery = 5
)
const (
// select
_avIncomeStatisTableSQL = "SELECT avs,money_section,money_tips,income,category_id,cdate FROM %s WHERE %s LIMIT ?,?"
_avIncomeSQL = "SELECT id,av_id,mid,tag_id,is_original,upload_time,total_income,income,tax_money,date FROM av_income WHERE id > ? AND %s date >= ? AND date <= ? ORDER BY id LIMIT ?"
_columnIncomeSQL = "SELECT id,aid,mid,tag_id,upload_time,total_income,income,tax_money,date FROM column_income WHERE id > ? AND date >= ? AND date <= ? AND %s is_deleted = 0 ORDER BY id LIMIT ?"
)
// GetArchiveStatis get av/column income statis from table and query
func (d *Dao) GetArchiveStatis(c context.Context, table, query string, from, limit int) (avs []*model.ArchiveStatis, err error) {
avs = make([]*model.ArchiveStatis, 0)
if table == "" || query == "" {
return nil, fmt.Errorf("error args table(%s), query(%s)", table, query)
}
rows, err := d.db.Query(c, fmt.Sprintf(_avIncomeStatisTableSQL, table, query), from, limit)
if err != nil {
log.Error("GetArchiveStatis d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.ArchiveStatis{}
err = rows.Scan(&list.Avs, &list.MoneySection, &list.MoneyTips, &list.Income, &list.CategroyID, &list.CDate)
if err != nil {
log.Error("GetArchiveStatis rows scan error(%v)", err)
return
}
avs = append(avs, list)
}
err = rows.Err()
return
}
// GetArchiveIncome get archive income by query
func (d *Dao) GetArchiveIncome(c context.Context, id int64, query string, from, to string, limit int, typ int) (archs []*model.ArchiveIncome, err error) {
switch typ {
case _video:
return d.GetAvIncome(c, id, query, from, to, limit, typ)
case _column:
return d.GetColumnIncome(c, id, query, from, to, limit, typ)
case _bgm:
return d.GetBgmIncome(c, id, query, from, to, limit, typ)
case _lottery:
return d.GetLotteryIncome(c, id, query, from, to, limit, typ)
}
err = fmt.Errorf("get archive type error(%d)", typ)
return
}
// GetAvIncome get av income by query
func (d *Dao) GetAvIncome(c context.Context, id int64, query string, from, to string, limit int, typ int) (avs []*model.ArchiveIncome, err error) {
avs = make([]*model.ArchiveIncome, 0)
if query != "" {
query += " AND"
}
rows, err := d.db.Query(c, fmt.Sprintf(_avIncomeSQL, query), id, from, to, limit)
if err != nil {
log.Error("GetAvIncome d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.ArchiveIncome{}
err = rows.Scan(&list.ID, &list.AvID, &list.MID, &list.TagID, &list.IsOriginal, &list.UploadTime, &list.TotalIncome, &list.Income, &list.TaxMoney, &list.Date)
if err != nil {
log.Error("GetAvIncome rows scan error(%v)", err)
return
}
list.Type = typ
avs = append(avs, list)
}
err = rows.Err()
return
}
// GetColumnIncome get column income by query
func (d *Dao) GetColumnIncome(c context.Context, id int64, query string, from, to string, limit int, typ int) (columns []*model.ArchiveIncome, err error) {
columns = make([]*model.ArchiveIncome, 0)
if query != "" {
query += " AND"
}
rows, err := d.db.Query(c, fmt.Sprintf(_columnIncomeSQL, query), id, from, to, limit)
if err != nil {
log.Error("GetColumnIncome d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.ArchiveIncome{}
err = rows.Scan(&list.ID, &list.AvID, &list.MID, &list.TagID, &list.UploadTime, &list.TotalIncome, &list.Income, &list.TaxMoney, &list.Date)
if err != nil {
log.Error("GetColumnIncome rows scan error(%v)", err)
return
}
list.Type = typ
columns = append(columns, list)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,152 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeGetArchiveStatis(t *testing.T) {
convey.Convey("GetArchiveStatis", t, func(ctx convey.C) {
var (
c = context.Background()
table = "av_income_daily_statis"
query = "id > 0"
from = int(0)
limit = int(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_income_daily_statis(avs,money_section,money_tips,income,category_id,cdate) VALUES(10, 1, '0-3',1, '2018-01-01')")
avs, err := d.GetArchiveStatis(c, table, query, from, 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 TestIncomeGetArchiveIncome(t *testing.T) {
convey.Convey("GetArchiveIncome av", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
query = "id > 0"
from = "2018-01-01"
to = "2019-01-01"
limit = int(100)
typ = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_income(av_id,mid,date) VALUES(1001, 1000, '2018-05-01')")
archs, err := d.GetArchiveIncome(c, id, query, from, to, limit, typ)
ctx.Convey("Then err should be nil.archs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(archs, convey.ShouldNotBeNil)
})
})
})
convey.Convey("GetArchiveIncome column", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
query = "id > 0"
from = "2018-01-01"
to = "2019-01-01"
limit = int(100)
typ = int(2)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO column_income(aid,mid,date) VALUES(1002, 1000, '2018-05-01')")
archs, err := d.GetArchiveIncome(c, id, query, from, to, limit, typ)
ctx.Convey("Then err should be nil.archs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(archs, convey.ShouldNotBeNil)
})
})
})
convey.Convey("GetArchiveIncome bgm", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
query = "id > 0"
from = "2018-01-01"
to = "2019-01-01"
limit = int(100)
typ = int(3)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO bgm_income(sid,mid,date) VALUES(1003, 1000, '2018-05-01')")
archs, err := d.GetArchiveIncome(c, id, query, from, to, limit, typ)
ctx.Convey("Then err should be nil.archs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(archs, convey.ShouldNotBeNil)
})
})
})
convey.Convey("GetArchiveIncome error type", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
query = "id > 0"
from = "2018-01-01"
to = "2019-01-01"
limit = int(100)
typ = int(4)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.GetArchiveIncome(c, id, query, from, to, limit, typ)
ctx.Convey("Then err should be nil.archs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetAvIncome(t *testing.T) {
convey.Convey("GetAvIncome", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
query = "id > 0"
from = "2018-01-01"
to = "2019-01-01"
limit = int(100)
typ = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_income(av_id,mid,date) VALUES(1001, 1000, '2018-05-01')")
avs, err := d.GetAvIncome(c, id, query, from, to, limit, typ)
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 TestIncomeGetColumnIncome(t *testing.T) {
convey.Convey("GetColumnIncome", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
query = "id > 0"
from = "2018-01-01"
to = "2019-01-01"
limit = int(100)
typ = int(2)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO column_income(aid,mid,date) VALUES(1002, 1000, '2018-05-01')")
columns, err := d.GetColumnIncome(c, id, query, from, to, limit, typ)
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,65 @@
package income
import (
"context"
"fmt"
model "go-common/app/admin/main/growup/model/income"
"go-common/library/log"
)
const (
// select
_getAvByBgmIncome = "SELECT aid FROM bgm_income WHERE sid = ? AND date >= ? AND date <= ?"
_bgmIncomeSQL = "SELECT id,sid,mid,join_at,total_income,income,tax_money,date FROM bgm_income WHERE id > ? AND date >= ? AND date <= ? AND %s is_deleted = 0 ORDER BY id LIMIT ?"
)
// GetAvByBgm get av_id by bgm id
func (d *Dao) GetAvByBgm(c context.Context, sid int64, from, to string) (avs map[int64]struct{}, err error) {
avs = make(map[int64]struct{})
rows, err := d.db.Query(c, _getAvByBgmIncome, sid, from, to)
if err != nil {
log.Error("GetAvByBgm d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var avID int64
err = rows.Scan(&avID)
if err != nil {
log.Error("GetAvByBgm rows scan error(%v)", err)
return
}
avs[avID] = struct{}{}
}
err = rows.Err()
return
}
// GetBgmIncome get bgm income by query
func (d *Dao) GetBgmIncome(c context.Context, id int64, query string, from, to string, limit int, typ int) (bgms []*model.ArchiveIncome, err error) {
bgms = make([]*model.ArchiveIncome, 0)
if query != "" {
query += " AND"
}
rows, err := d.db.Query(c, fmt.Sprintf(_bgmIncomeSQL, query), id, from, to, limit)
if err != nil {
log.Error("GetBgmIncome d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.ArchiveIncome{}
err = rows.Scan(&list.ID, &list.AvID, &list.MID, &list.UploadTime, &list.TotalIncome, &list.Income, &list.TaxMoney, &list.Date)
if err != nil {
log.Error("GetBgmIncome rows scan error(%v)", err)
return
}
list.Type = typ
bgms = append(bgms, list)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,48 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeGetAvByBgm(t *testing.T) {
convey.Convey("GetAvByBgm", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(1003)
from = "2018-01-01"
to = "2019-01-03"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO bgm_income(sid,mid,date) VALUES(1003, 1000, '2018-05-01')")
avs, err := d.GetAvByBgm(c, sid, from, to)
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 TestIncomeGetBgmIncome(t *testing.T) {
convey.Convey("GetBgmIncome", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
query = ""
from = "2018-01-02"
to = "2018-01-03"
limit = int(10)
typ = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
bgms, err := d.GetBgmIncome(c, id, query, from, to, limit, typ)
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)
})
})
})
}

View File

@@ -0,0 +1,77 @@
package income
import (
"context"
"fmt"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
// select
_blackListByMIDSQL = "SELECT av_id FROM av_black_list WHERE mid = ? AND ctype = ? AND is_delete = 0"
_blackListByAvIDSQL = "SELECT av_id FROM av_black_list WHERE av_id in (%s) AND ctype = ? AND is_delete = 0"
// insert
_inBlackListSQL = "INSERT INTO av_black_list(av_id,mid,ctype,reason,nickname,has_signed,is_delete) VALUES %s ON DUPLICATE KEY UPDATE reason=VALUES(reason),nickname=VALUES(nickname),has_signed=VALUES(has_signed),is_delete=VALUES(is_delete)"
)
// ListAvBlackList list av_blakc_list by av_id
func (d *Dao) ListAvBlackList(c context.Context, avID []int64, ctype int) (avb map[int64]struct{}, err error) {
avb = make(map[int64]struct{})
rows, err := d.db.Query(c, fmt.Sprintf(_blackListByAvIDSQL, xstr.JoinInts(avID)), ctype)
if err != nil {
log.Error("ListAvBlackList d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var id int64
err = rows.Scan(&id)
if err != nil {
log.Error("ListAvBlackList rows scan error(%v)", err)
return
}
avb[id] = struct{}{}
}
err = rows.Err()
return
}
// GetAvBlackListByMID list av_blakc_list by av_id
func (d *Dao) GetAvBlackListByMID(c context.Context, mid int64, typ int) (avb map[int64]struct{}, err error) {
avb = make(map[int64]struct{})
rows, err := d.db.Query(c, _blackListByMIDSQL, mid, typ)
if err != nil {
log.Error("GetAvBlackListByMID d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var avID int64
err = rows.Scan(&avID)
if err != nil {
log.Error("GetAvBlackListByMID rows scan error(%v)", err)
return
}
avb[avID] = struct{}{}
}
err = rows.Err()
return
}
// TxInsertAvBlackList insert val into av_black_list
func (d *Dao) TxInsertAvBlackList(tx *sql.Tx, val string) (rows int64, err error) {
if val == "" {
return
}
res, err := tx.Exec(fmt.Sprintf(_inBlackListSQL, val))
if err != nil {
log.Error("TxInsertAvBlackList tx.Exec error(%v)", err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,60 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeListAvBlackList(t *testing.T) {
convey.Convey("ListAvBlackList", t, func(ctx convey.C) {
var (
c = context.Background()
avID = []int64{19930812}
ctype = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_black_list(av_id, mid) VALUES(19930812,19930812)")
avb, err := d.ListAvBlackList(c, avID, ctype)
ctx.Convey("Then err should be nil.avb should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(avb, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetAvBlackListByMID(t *testing.T) {
convey.Convey("GetAvBlackListByMID", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(19930812)
typ = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_black_list(av_id, mid) VALUES(1002,19930812)")
avb, err := d.GetAvBlackListByMID(c, mid, typ)
ctx.Convey("Then err should be nil.avb should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(avb, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeTxInsertAvBlackList(t *testing.T) {
convey.Convey("TxInsertAvBlackList", t, func(ctx convey.C) {
var (
tx, _ = d.BeginTran(context.Background())
val = "(1001,1000,0,'test','szy',1,0)"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.TxInsertAvBlackList(tx, 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,149 @@
package income
import (
"context"
"fmt"
model "go-common/app/admin/main/growup/model/income"
"go-common/library/database/sql"
"go-common/library/log"
xtime "go-common/library/time"
)
const (
// select
_avDailyChargeSQL = "SELECT av_id,mid,tag_id,upload_time,inc_charge,date FROM av_daily_charge_%02d WHERE av_id = ? LIMIT 31"
_cmDailyChargeSQL = "SELECT aid,mid,tag_id,upload_time,inc_charge,date FROM column_daily_charge WHERE aid = ?"
_bgmDailyChargeSQL = "SELECT sid,aid,mid,join_at,inc_charge,date FROM bgm_daily_charge WHERE sid = ?"
_upChargeRatioSQL = "SELECT mid, ratio FROM up_charge_ratio LIMIT ?, ?"
_archiveChargeStatisTableSQL = "SELECT avs,money_section,money_tips,charge,category_id,cdate FROM %s WHERE %s LIMIT ?,?"
_archiveTotalChargeSQL = "SELECT total_charge FROM %s WHERE %s LIMIT 1"
)
// GetAvDailyCharge get av_daily_charge by month
func (d *Dao) GetAvDailyCharge(c context.Context, month int, avID int64) (avs []*model.ArchiveCharge, err error) {
if month < 1 || month > 12 {
return nil, fmt.Errorf("error args month(%d)", month)
}
rows, err := d.db.Query(c, fmt.Sprintf(_avDailyChargeSQL, month), avID)
if err != nil {
log.Error("GetAvDailyCharge d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.ArchiveCharge{}
err = rows.Scan(&list.AID, &list.MID, &list.CategoryID, &list.UploadTime, &list.Charge, &list.Date)
if err != nil {
log.Error("GetAvDailyCharge rows scan error(%v)", err)
return
}
avs = append(avs, list)
}
err = rows.Err()
return
}
// GetColumnCharges get column daily charge
func (d *Dao) GetColumnCharges(c context.Context, aid int64) (cms []*model.ArchiveCharge, err error) {
cms = make([]*model.ArchiveCharge, 0)
rows, err := d.db.Query(c, _cmDailyChargeSQL, aid)
if err != nil {
log.Error("GetColumnCharge d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var uploadTime int64
cm := &model.ArchiveCharge{}
err = rows.Scan(&cm.AID, &cm.MID, &cm.CategoryID, &uploadTime, &cm.Charge, &cm.Date)
if err != nil {
log.Error("GetColumnCharge rows scan error(%v)", err)
return
}
cm.UploadTime = xtime.Time(uploadTime)
cms = append(cms, cm)
}
err = rows.Err()
return
}
// GetBgmCharges get bgm daily charge
func (d *Dao) GetBgmCharges(c context.Context, aid int64) (bgms []*model.ArchiveCharge, err error) {
bgms = make([]*model.ArchiveCharge, 0)
rows, err := d.db.Query(c, _bgmDailyChargeSQL, aid)
if err != nil {
log.Error("GetBgmCharge d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
bgm := &model.ArchiveCharge{}
err = rows.Scan(&bgm.AID, &bgm.AvID, &bgm.MID, &bgm.UploadTime, &bgm.Charge, &bgm.Date)
if err != nil {
log.Error("GetBgmCharge rows scan error(%v)", err)
return
}
bgms = append(bgms, bgm)
}
err = rows.Err()
return
}
// GetArchiveChargeStatis get archive charge statis from table and query
func (d *Dao) GetArchiveChargeStatis(c context.Context, table, query string, from, limit int) (archs []*model.ArchiveChargeStatis, err error) {
archs = make([]*model.ArchiveChargeStatis, 0)
if table == "" || query == "" {
return nil, fmt.Errorf("error args table(%s), query(%s)", table, query)
}
rows, err := d.db.Query(c, fmt.Sprintf(_archiveChargeStatisTableSQL, table, query), from, limit)
if err != nil {
log.Error("GetArchiveChargeStatis d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.ArchiveChargeStatis{}
err = rows.Scan(&list.Avs, &list.MoneySection, &list.MoneyTips, &list.Charge, &list.CategroyID, &list.CDate)
if err != nil {
log.Error("GetArchiveChargeStatis rows scan error(%v)", err)
return
}
archs = append(archs, list)
}
err = rows.Err()
return
}
// GetTotalCharge get total charge by table and aid
func (d *Dao) GetTotalCharge(c context.Context, table, query string) (total int64, err error) {
err = d.db.QueryRow(c, fmt.Sprintf(_archiveTotalChargeSQL, table, query)).Scan(&total)
if err == sql.ErrNoRows {
err = nil
}
return
}
// UpRatio get up charge ratio
func (d *Dao) UpRatio(c context.Context, from, limit int64) (ratio map[int64]int64, err error) {
ratio = make(map[int64]int64)
rows, err := d.db.Query(c, _upChargeRatioSQL, from, limit)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var mid, charge int64
err = rows.Scan(&mid, &charge)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
ratio[mid] = charge
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,111 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeGetAvDailyCharge(t *testing.T) {
convey.Convey("GetAvDailyCharge", t, func(ctx convey.C) {
var (
c = context.Background()
month = int(1)
avID = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_daily_charge_01(av_id, date) VALUES(1001, '2018-01-01')")
_, err := d.GetAvDailyCharge(c, month, avID)
ctx.Convey("Then err should be nil.avs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
convey.Convey("GetAvDailyCharge month error", t, func(ctx convey.C) {
var (
c = context.Background()
month = int(13)
avID = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.GetAvDailyCharge(c, month, avID)
ctx.Convey("Then err should be nil.avs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetColumnCharges(t *testing.T) {
convey.Convey("GetColumnCharges", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(1002)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO column_daily_charge(aid, date) VALUES(1002, '2018-01-01')")
cms, err := d.GetColumnCharges(c, aid)
ctx.Convey("Then err should be nil.cms should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cms, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetBgmCharges(t *testing.T) {
convey.Convey("GetBgmCharges", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(1003)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO bgm_daily_charge(sid, date) VALUES(1003, '2018-01-01')")
bgms, err := d.GetBgmCharges(c, aid)
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 TestIncomeGetArchiveChargeStatis(t *testing.T) {
convey.Convey("GetArchiveChargeStatis", t, func(ctx convey.C) {
var (
c = context.Background()
table = "av_charge_daily_statis"
query = "cdate = '2018-01-01'"
from = int(0)
limit = int(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_charge_daily_statis(avs, cdate) VALUES(10, '2018-01-01')")
archs, err := d.GetArchiveChargeStatis(c, table, query, from, limit)
ctx.Convey("Then err should be nil.archs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(archs, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetTotalCharge(t *testing.T) {
convey.Convey("GetTotalCharge", t, func(ctx convey.C) {
var (
c = context.Background()
table = "av_charge_statis"
query = "av_id = 1001"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_charge_statis(av_id, total_income) VALUES(1001, 10)")
total, err := d.GetTotalCharge(c, table, query)
ctx.Convey("Then err should be nil.total should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,40 @@
package income
import (
"context"
"go-common/app/admin/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.ORM.Allowance),
}
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)
}

View File

@@ -0,0 +1,55 @@
package income
import (
"context"
"flag"
"go-common/app/admin/main/growup/conf"
"os"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
var (
d *Dao
)
func CleanMysql() {
}
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "mobile.studio.growup-admin")
flag.Set("conf_token", "ac1fd397cbc33eb60541e8734844bdd5")
flag.Set("tree_id", "13583")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_host", "config.bilibili.co")
flag.Set("conf_path", "/tmp")
flag.Set("region", "sh")
flag.Set("zone", "sh001")
} else {
flag.Set("conf", "../../cmd/growup-admin.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}
func WithMysql(f func(d *Dao)) func() {
return func() {
Reset(func() { CleanMysql() })
f(d)
}
}
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,41 @@
package income
import (
"context"
"fmt"
model "go-common/app/admin/main/growup/model/income"
"go-common/library/log"
)
const (
// select
_lotteryIncomeSQL = "SELECT id,av_id,mid,tag_id,upload_time,total_income,income,tax_money,date FROM lottery_av_income WHERE id > ? AND %s date >= ? AND date <= ? ORDER BY id LIMIT ?"
)
// GetLotteryIncome get lottery income by query
func (d *Dao) GetLotteryIncome(c context.Context, id int64, query string, from, to string, limit int, typ int) (avs []*model.ArchiveIncome, err error) {
avs = make([]*model.ArchiveIncome, 0)
if query != "" {
query += " AND"
}
rows, err := d.db.Query(c, fmt.Sprintf(_lotteryIncomeSQL, query), id, from, to, limit)
if err != nil {
log.Error("GetLotteryIncome d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.ArchiveIncome{}
err = rows.Scan(&list.ID, &list.AvID, &list.MID, &list.TagID, &list.UploadTime, &list.TotalIncome, &list.Income, &list.TaxMoney, &list.Date)
if err != nil {
log.Error("GetLotteryIncome rows scan error(%v)", err)
return
}
list.Type = typ
avs = append(avs, list)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,30 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeGetLotteryIncome(t *testing.T) {
convey.Convey("GetLotteryIncome", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
query = ""
from = "2018-01-01"
to = "2019-01-01"
limit = int(2000)
typ = int(5)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO lottery_av_income(av_id, mid, income, date) VALUES(10010, 1001, 100, '2018-11-11') ONDUPLICATE KEY UPDATE date = '2018-11-11'")
avs, err := d.GetLotteryIncome(c, id, query, from, to, limit, typ)
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)
})
})
})
}

View File

@@ -0,0 +1,68 @@
package income
import (
"context"
"fmt"
model "go-common/app/admin/main/growup/model/income"
"go-common/library/database/sql"
"go-common/library/log"
)
const (
// count
_upAccountCountSQL = "SELECT count(*) FROM up_account WHERE %s is_deleted = ?"
// select
_upAccountSQL = "SELECT mid,total_income,total_unwithdraw_income,total_withdraw_income,withdraw_date_version,last_withdraw_time,mtime FROM up_account WHERE %s is_deleted = ? LIMIT ?,?"
_upAccountByMIDSQL = "SELECT mid,total_income,total_unwithdraw_income,withdraw_date_version,version FROM up_account WHERE mid = ? AND is_deleted = 0"
// update
_breachUpAccountSQL = "UPDATE up_account SET total_income = ?, total_unwithdraw_income = ?, version = ? WHERE mid = ? AND version = ? AND is_deleted = 0"
)
// UpAccountCount get up_account count
func (d *Dao) UpAccountCount(c context.Context, query string, isDeleted int) (total int64, err error) {
err = d.db.QueryRow(c, fmt.Sprintf(_upAccountCountSQL, query), isDeleted).Scan(&total)
if err == sql.ErrNoRows {
err = nil
}
return
}
// ListUpAccount list up account bu query
func (d *Dao) ListUpAccount(c context.Context, query string, isDeleted, from, limit int) (ups []*model.UpAccount, err error) {
ups = make([]*model.UpAccount, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_upAccountSQL, query), isDeleted, from, limit)
if err != nil {
log.Error("ListUpAccount d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
up := &model.UpAccount{}
err = rows.Scan(&up.MID, &up.TotalIncome, &up.TotalUnwithdrawIncome, &up.TotalWithdrawIncome, &up.WithdrawDateVersion, &up.LastWithdrawTime, &up.MTime)
if err != nil {
log.Error("ListUpAccount rows scan error(%v)", err)
return
}
ups = append(ups, up)
}
err = rows.Err()
return
}
// GetUpAccount get up_account by mid
func (d *Dao) GetUpAccount(c context.Context, mid int64) (up *model.UpAccount, err error) {
up = &model.UpAccount{}
err = d.db.QueryRow(c, _upAccountByMIDSQL, mid).Scan(&up.MID, &up.TotalIncome, &up.TotalUnwithdrawIncome, &up.WithdrawDateVersion, &up.Version)
return
}
// TxBreachUpAccount breach up_account
func (d *Dao) TxBreachUpAccount(tx *sql.Tx, total, unwithdraw, mid, newVersion, oldVersion int64) (rows int64, err error) {
res, err := tx.Exec(_breachUpAccountSQL, total, unwithdraw, newVersion, mid, oldVersion)
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,81 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeUpAccountCount(t *testing.T) {
convey.Convey("UpAccountCount", t, func(ctx convey.C) {
var (
c = context.Background()
query = ""
isDeleted = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
total, err := d.UpAccountCount(c, query, isDeleted)
ctx.Convey("Then err should be nil.total should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeListUpAccount(t *testing.T) {
convey.Convey("ListUpAccount", t, func(ctx convey.C) {
var (
c = context.Background()
query = ""
isDeleted = int(0)
from = int(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
ups, err := d.ListUpAccount(c, query, isDeleted, 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 TestIncomeGetUpAccount(t *testing.T) {
convey.Convey("GetUpAccount", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_account(mid) VALUES(1001)")
up, err := d.GetUpAccount(c, mid)
ctx.Convey("Then err should be nil.up should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(up, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeTxBreachUpAccount(t *testing.T) {
convey.Convey("TxBreachUpAccount", t, func(ctx convey.C) {
var (
tx, _ = d.BeginTran(context.Background())
total = int64(0)
unwithdraw = int64(0)
mid = int64(1001)
newVersion = int64(0)
oldVersion = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
rows, err := d.TxBreachUpAccount(tx, total, unwithdraw, mid, newVersion, oldVersion)
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 income
import (
"context"
"fmt"
"go-common/library/log"
"go-common/library/xstr"
)
const (
// select
_upInfoSQL = "SELECT mid, nick_name FROM up_category_info WHERE mid in (%s) AND is_deleted = 0"
)
// ListUpInfo list up_category_info by mids
func (d *Dao) ListUpInfo(c context.Context, mids []int64) (upInfo map[int64]string, err error) {
upInfo = make(map[int64]string)
if len(mids) == 0 {
return
}
rows, err := d.db.Query(c, fmt.Sprintf(_upInfoSQL, xstr.JoinInts(mids)))
if err != nil {
log.Error("ListUpInfo 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("ListUpInfo rows scan error(%v)", err)
return
}
upInfo[mid] = nickname
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,18 @@
package income
import (
"context"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_ListUpInfo(t *testing.T) {
Convey("ListUpInfo by mids", t, WithMysql(func(d *Dao) {
var (
mids = []int64{1, 2}
)
_, err := d.ListUpInfo(context.Background(), mids)
So(err, ShouldBeNil)
}))
}

View File

@@ -0,0 +1,103 @@
package income
import (
"context"
"fmt"
model "go-common/app/admin/main/growup/model/income"
"go-common/library/log"
)
const (
// select
_upIncomeTableSQL = "SELECT id,mid,%s,date FROM %s WHERE id > ? AND %s LIMIT ?"
_upIncomeTableSortSQL = "SELECT mid,av_count,column_count,bgm_count,%s,date FROM %s WHERE %s ORDER BY date desc,%s desc LIMIT ?,? "
_upIncomeCountSQL = "SELECT count(*) FROM %s WHERE %s"
_upDailyStatisSQL = "SELECT ups,income,cdate FROM %s WHERE cdate >= '%s' AND cdate <= '%s'"
)
// UpIncomeCount count
func (d *Dao) UpIncomeCount(c context.Context, table, query string) (count int, err error) {
if table == "" || query == "" {
return 0, fmt.Errorf("error args table(%s), query(%s)", table, query)
}
err = d.db.QueryRow(c, fmt.Sprintf(_upIncomeCountSQL, table, query)).Scan(&count)
return
}
// GetUpIncome get up_income_(weekly/monthly) from table and query
func (d *Dao) GetUpIncome(c context.Context, table, incomeType, query string, id int64, limit int) (upIncome []*model.UpIncome, err error) {
upIncome = make([]*model.UpIncome, 0)
if table == "" || query == "" {
return nil, fmt.Errorf("error args table(%s), query(%s)", table, query)
}
rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeTableSQL, incomeType, table, query), id, limit)
if err != nil {
log.Error("GetUpIncome d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.UpIncome{}
err = rows.Scan(&list.ID, &list.MID, &list.Income, &list.Date)
if err != nil {
log.Error("GetUpIncome rows scan error(%v)", err)
return
}
upIncome = append(upIncome, list)
}
err = rows.Err()
return
}
// GetUpIncomeBySort get up_income by query
func (d *Dao) GetUpIncomeBySort(c context.Context, table, typeField, sort, query string, from, limit int) (upIncome []*model.UpIncome, err error) {
upIncome = make([]*model.UpIncome, 0)
if table == "" || query == "" || typeField == "" {
return nil, fmt.Errorf("error args table(%s), typeField(%s),query(%s)", table, typeField, query)
}
rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeTableSortSQL, typeField, table, query, sort), from, limit)
if err != nil {
log.Error("GetUpIncomeBySort d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
up := &model.UpIncome{}
err = rows.Scan(&up.MID, &up.AvCount, &up.ColumnCount, &up.BgmCount, &up.Income, &up.TaxMoney, &up.BaseIncome, &up.TotalIncome, &up.Date)
if err != nil {
log.Error("GetUpIncome rows scan error(%v)", err)
return
}
upIncome = append(upIncome, up)
}
err = rows.Err()
return
}
// GetUpDailyStatis get up income daily statis
func (d *Dao) GetUpDailyStatis(c context.Context, table, fromTime, toTime string) (s []*model.UpDailyStatis, err error) {
if table == "" {
return nil, fmt.Errorf("error args table(%s)", table)
}
s = make([]*model.UpDailyStatis, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_upDailyStatisSQL, table, fromTime, toTime))
if err != nil {
log.Error("GetUpDailyStatis d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
list := &model.UpDailyStatis{}
err = rows.Scan(&list.Ups, &list.Income, &list.Date)
if err != nil {
log.Error("GetUpIncome rows scan error(%v)", err)
return
}
s = append(s, list)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,135 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeUpIncomeCount(t *testing.T) {
convey.Convey("UpIncomeCount", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_income"
query = "id > 0"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_income(mid, income, date) VALUS(1993, 10, '2018-01-01')")
count, err := d.UpIncomeCount(c, table, query)
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)
})
})
})
convey.Convey("UpIncomeCount table error", t, func(ctx convey.C) {
var (
c = context.Background()
table = ""
query = ""
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.UpIncomeCount(c, table, query)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetUpIncome(t *testing.T) {
convey.Convey("GetUpIncome", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_income"
incomeType = "av_income"
query = "is_deleted = 0"
id = int64(0)
limit = int(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
upIncome, err := d.GetUpIncome(c, table, incomeType, query, id, limit)
ctx.Convey("Then err should be nil.upIncome should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(upIncome, convey.ShouldNotBeNil)
})
})
})
convey.Convey("GetUpIncome query == nil", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_income"
incomeType = "av_income"
query = ""
id = int64(0)
limit = int(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.GetUpIncome(c, table, incomeType, query, id, limit)
ctx.Convey("Then err should be nil.upIncome should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetUpIncomeBySort(t *testing.T) {
convey.Convey("GetUpIncomeBySort", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_income"
typeField = "av_income,av_tax,av_base_income,av_total_income"
sort = "id"
query = "id > 0"
from = int(0)
limit = int(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
upIncome, err := d.GetUpIncomeBySort(c, table, typeField, sort, query, from, limit)
ctx.Convey("Then err should be nil.upIncome should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(upIncome, convey.ShouldNotBeNil)
})
})
})
convey.Convey("GetUpIncomeBySort table == nil", t, func(ctx convey.C) {
var (
c = context.Background()
table = ""
typeField = "av_income"
sort = "id"
query = "id > 0"
from = int(0)
limit = int(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.GetUpIncomeBySort(c, table, typeField, sort, query, from, limit)
ctx.Convey("Then err should be nil.upIncome should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetUpDailyStatis(t *testing.T) {
convey.Convey("GetUpDailyStatis", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_income_daily_statis"
fromTime = "2018-01-01"
toTime = "2018-01-10"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_income_daily_statis(ups, cdate) VALUES(10, '2018-01-02')")
s, err := d.GetUpDailyStatis(c, table, fromTime, toTime)
ctx.Convey("Then err should be nil.s should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(s, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,62 @@
package income
import (
"context"
"fmt"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
// select
_upInfoNicknameSQL = "SELECT mid, nickname FROM up_info_video WHERE mid in (%s)"
_upInfoNicknameByMIDSQL = "SELECT nickname FROM %s WHERE mid = ? AND account_state = 3 AND is_deleted = 0"
// update
_updateUpInfoScoreSQL = "UPDATE %s set credit_score = credit_score + ? WHERE mid = ? AND is_deleted = 0"
)
// GetUpInfoNickname get nickname
func (d *Dao) GetUpInfoNickname(c context.Context, mids []int64) (upInfo map[int64]string, err error) {
upInfo = make(map[int64]string)
if len(mids) == 0 {
return
}
rows, err := d.db.Query(c, fmt.Sprintf(_upInfoNicknameSQL, xstr.JoinInts(mids)))
if err != nil {
log.Error("GetUpInfoNickname 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("GetUpInfoNickname rows scan error(%v)", err)
return
}
upInfo[mid] = nickname
}
err = rows.Err()
return
}
// GetUpInfoNicknameByMID get nickname by mid
func (d *Dao) GetUpInfoNicknameByMID(c context.Context, mid int64, table string) (nickname string, err error) {
err = d.db.QueryRow(c, fmt.Sprintf(_upInfoNicknameByMIDSQL, table), mid).Scan(&nickname)
if err == sql.ErrNoRows {
return "", nil
}
return
}
// TxUpdateUpInfoScore update up_info_video credit score
func (d *Dao) TxUpdateUpInfoScore(tx *sql.Tx, table string, score int, mid int64) (rows int64, err error) {
res, err := tx.Exec(fmt.Sprintf(_updateUpInfoScoreSQL, table), score, mid)
if err != nil {
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,61 @@
package income
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestIncomeGetUpInfoNickname(t *testing.T) {
convey.Convey("GetUpInfoNickname", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{1993}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_info_video(mid, account_state) VALUES(1993, 3)")
upInfo, err := d.GetUpInfoNickname(c, mids)
ctx.Convey("Then err should be nil.upInfo should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(upInfo, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeGetUpInfoNicknameByMID(t *testing.T) {
convey.Convey("GetUpInfoNicknameByMID", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1993)
table = "up_info_video"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
nickname, err := d.GetUpInfoNicknameByMID(c, mid, table)
ctx.Convey("Then err should be nil.nickname should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(nickname, convey.ShouldNotBeNil)
})
})
})
}
func TestIncomeTxUpdateUpInfoScore(t *testing.T) {
convey.Convey("TxUpdateUpInfoScore", t, func(ctx convey.C) {
var (
tx, _ = d.BeginTran(context.Background())
table = "up_info_video"
score = int(5)
mid = int64(1993)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer tx.Commit()
rows, err := d.TxUpdateUpInfoScore(tx, table, score, 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,37 @@
package income
import (
"context"
"fmt"
model "go-common/app/admin/main/growup/model/income"
"go-common/library/log"
)
const (
// select
_upWithdrawSQL = "SELECT id, mid, withdraw_income, date_version, mtime FROM up_income_withdraw WHERE id > ? AND state = 2 AND %s is_deleted = 0 LIMIT ?"
)
// ListUpWithdraw list up_income_withdraw by query
func (d *Dao) ListUpWithdraw(c context.Context, id int64, query string, limit int) (upWithdraw []*model.UpIncomeWithdraw, err error) {
upWithdraw = make([]*model.UpIncomeWithdraw, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_upWithdrawSQL, query), id, limit)
if err != nil {
log.Error("GetUpWithdraw d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
w := &model.UpIncomeWithdraw{}
err = rows.Scan(&w.ID, &w.MID, &w.WithdrawIncome, &w.DateVersion, &w.MTime)
if err != nil {
log.Error("GetUpWithdraw rows scan error(%v)", err)
return
}
upWithdraw = append(upWithdraw, w)
}
err = rows.Err()
return
}

View File

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