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,106 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"account_test.go",
"activity_test.go",
"archive_stat_test.go",
"av_breach_test.go",
"banner_test.go",
"bgm_test.go",
"column_income_test.go",
"dao_test.go",
"exchange_test.go",
"hbase_test.go",
"income_test.go",
"notice_test.go",
"redis_test.go",
"special_award_test.go",
"tool_test.go",
"up_bill_test.go",
"up_test.go",
"withdraw_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/growup/conf:go_default_library",
"//app/interface/main/growup/model:go_default_library",
"//library/net/metadata:go_default_library",
"//library/time:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"account.go",
"activity.go",
"archive_stat.go",
"av_breach.go",
"banner.go",
"bgm.go",
"column_income.go",
"dao.go",
"exchange.go",
"hbase.go",
"income.go",
"notice.go",
"redis.go",
"special_award.go",
"tool.go",
"up.go",
"up_bill.go",
"up_year.go",
"withdraw.go",
],
importpath = "go-common/app/interface/main/growup/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/growup/conf:go_default_library",
"//app/interface/main/growup/model:go_default_library",
"//app/interface/openplatform/article/model:go_default_library",
"//app/interface/openplatform/article/rpc/client:go_default_library",
"//app/service/main/account/model:go_default_library",
"//app/service/main/account/rpc/client:go_default_library",
"//app/service/main/vip/model:go_default_library",
"//app/service/main/vip/rpc/client:go_default_library",
"//library/cache/redis: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",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/github.com/tsuna/gohbase/hrpc:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [
":package-srcs",
"//app/interface/main/growup/dao/newbiedao:all-srcs",
],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,79 @@
package dao
import (
"context"
"fmt"
"net/url"
"strconv"
"go-common/app/interface/main/growup/model"
account "go-common/app/service/main/account/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/xstr"
"github.com/pkg/errors"
)
// AccountInfos get account infos
func (d *Dao) AccountInfos(c context.Context, mids []int64) (infos map[int64]*model.ActUpInfo, err error) {
if len(mids) == 0 {
return
}
infos = make(map[int64]*model.ActUpInfo)
results := new(model.AccountInfosResult)
uv := url.Values{}
uv.Set("mids", xstr.JoinInts(mids))
if err = d.httpRead.Get(c, d.c.Host.AccountURI, "", uv, results); err != nil {
return
}
if results.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(results.Code), fmt.Sprintf("search account failed: %s?%s", d.c.Host.AccountURI, uv.Get("mids")))
return
}
for mid, account := range results.Data {
infos[mid] = &model.ActUpInfo{Nickname: account.Name, Face: account.Face}
}
return
}
// UpBusinessInfos get business infos
func (d *Dao) UpBusinessInfos(c context.Context, mid int64) (identify *model.UpIdentify, err error) {
identify = new(model.UpIdentify)
results := new(model.UperInfosResult)
uv := url.Values{}
uv.Set("mid", strconv.FormatInt(mid, 10))
if err = d.httpRead.Get(c, d.c.Host.UperURI, "", uv, results); err != nil {
return
}
if results.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(results.Code), fmt.Sprintf("search uper failed: %s?%s", d.c.Host.UperURI, uv.Get("mid")))
return
}
identify = results.Data["identify"]
return
}
// Card get account.
func (d *Dao) Card(c context.Context, mid int64) (res *account.Card, err error) {
var arg = &account.ArgMid{
Mid: mid,
}
if res, err = d.acc.Card3(c, arg); err != nil {
log.Error("d.acc.Card3() error(%v)", err)
err = ecode.CreativeAccServiceErr
}
return
}
// ProfileWithStat get account.
func (d *Dao) ProfileWithStat(c context.Context, mid int64) (res *account.ProfileStat, err error) {
var arg = &account.ArgMid{
Mid: mid,
}
if res, err = d.acc.ProfileWithStat3(c, arg); err != nil {
log.Error("d.acc.ProfileWithStat3() error(%v)", err)
err = ecode.CreativeAccServiceErr
}
return
}

View File

@@ -0,0 +1,70 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoAccountInfos(t *testing.T) {
convey.Convey("AccountInfos", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{1001}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
infos, err := d.AccountInfos(c, mids)
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 TestDaoUpBusinessInfos(t *testing.T) {
convey.Convey("UpBusinessInfos", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.UpBusinessInfos(c, mid)
ctx.Convey("Then err should be nil.identify should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoCard(t *testing.T) {
convey.Convey("Card", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.Card(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoProfileWithStat(t *testing.T) {
convey.Convey("ProfileWithStat", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.ProfileWithStat(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,55 @@
package dao
import (
"context"
"go-common/app/interface/main/growup/model"
"go-common/library/log"
// "go-common/library/database/sql"
)
const (
_activitySQL = "SELECT id,name,signed_start,signed_end,sign_up_start,sign_up_end,sign_up,win_type,progress_start,progress_end,progress_sync,update_page,bonus_query,bonus_query_start,bonus_query_end,background,win_desc,unwin_desc,details FROM creative_activity WHERE id = ?"
_upActivitySQL = "SELECT mid,nickname,ranking,state FROM up_activity WHERE activity_id = ? AND is_deleted = 0"
_inUpActivitySQL = "INSERT INTO up_activity(mid,nickname,activity_id,state,sign_up_time) VALUES(?,?,?,?,?)"
)
// GetActivity get activity by query
func (d *Dao) GetActivity(c context.Context, id int64) (ac *model.CActivity, err error) {
ac = &model.CActivity{}
err = d.db.QueryRow(c, _activitySQL, id).Scan(&ac.ID, &ac.Name, &ac.SignedStart, &ac.SignedEnd, &ac.SignUpStart, &ac.SignUpEnd, &ac.SignUp, &ac.WinType, &ac.ProgressStart, &ac.ProgressEnd, &ac.ProgressSync, &ac.UpdatePage, &ac.BonusQuery, &ac.BonusQueryStart, &ac.BonusQueryEnd, &ac.Background, &ac.WinDesc, &ac.UnwinDesc, &ac.Details)
return
}
// ListUpActivity get up from up_activity
func (d *Dao) ListUpActivity(c context.Context, id int64) (ups []*model.UpBonus, err error) {
ups = make([]*model.UpBonus, 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.UpBonus{}
err = rows.Scan(&up.MID, &up.Nickname, &up.Rank, &up.State)
if err != nil {
log.Error("ListUpActivity rows.Scan error(%v)", err)
return
}
ups = append(ups, up)
}
err = rows.Err()
return
}
// SignUpActivity up sign up activity
func (d *Dao) SignUpActivity(c context.Context, up *model.UpBonus) (rows int64, err error) {
res, err := d.db.Exec(c, _inUpActivitySQL, up.MID, up.Nickname, up.ActivityID, up.State, up.SignUpTime)
if err != nil {
log.Error("SignUpActivity.Exec(%s) error(%v)", _inUpActivitySQL, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,65 @@
package dao
import (
"context"
"go-common/app/interface/main/growup/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetActivity(t *testing.T) {
convey.Convey("GetActivity", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(1000)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO creative_activity(id, name) VALUES(1000, 'dao-test')")
ac, err := d.GetActivity(c, id)
ctx.Convey("Then err should be nil.ac should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ac, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListUpActivity(t *testing.T) {
convey.Convey("ListUpActivity", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(1000)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_activity(mid,activity_id,state) VALUES(1001, 1000, 2)")
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 TestDaoSignUpActivity(t *testing.T) {
convey.Convey("SignUpActivity", t, func(ctx convey.C) {
var (
c = context.Background()
up = &model.UpBonus{
MID: 1002,
Nickname: "test",
ActivityID: 1000,
State: 2,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "DELETE FROM up_activity WHERE mid = 1002")
rows, err := d.SignUpActivity(c, up)
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,73 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"strconv"
"go-common/app/interface/main/growup/model"
article "go-common/app/interface/openplatform/article/model"
"go-common/library/ecode"
"go-common/library/log"
)
// ArticleStat article stat
func (d *Dao) ArticleStat(c context.Context, mid int64, ip string) (res article.UpStat, err error) {
arg := &article.ArgMid{Mid: mid, RealIP: ip}
if res, err = d.art.CreationUpStat(c, arg); err != nil {
log.Error("d.art.CreationUpStat(%+v) error(%v)", arg, err)
if _, er := strconv.ParseInt(err.Error(), 10, 64); er != nil {
err = ecode.CreativeArticleRPCErr
}
}
return
}
func (d *Dao) getUpBaseStatCache(c context.Context, mid int64, date string) (data *model.UpBaseStat, err error) {
key := fmt.Sprintf("growup-up-status:%d-%s", mid, date)
res, err := d.getCacheVal(c, key)
if err != nil {
log.Error("d.getCacheVal error(%v)", err)
return
}
if res == nil {
return
}
err = json.Unmarshal(res, &data)
if err != nil {
log.Error("json.Unmarshal(%v) error(%v)", res, err)
}
return
}
// setUpBaseStatCache add stat cache.
func (d *Dao) setUpBaseStatCache(c context.Context, mid int64, date string, st *model.UpBaseStat) (err error) {
key := fmt.Sprintf("growup-up-status:%d-%s", mid, date)
v, err := json.Marshal(st)
if err != nil {
log.Error("json.Marshal error(%v)", err)
return
}
return d.setCacheKV(c, key, v, d.redisExpire)
}
// UpStat get up stat from hbase
func (d *Dao) UpStat(c context.Context, mid int64, dt string) (st *model.UpBaseStat, err error) {
// try cache
st, err = d.getUpBaseStatCache(c, mid, dt)
if err != nil {
log.Error("d.getUpBaseStatCache(%d) error(%v)", mid, err)
return
}
if st != nil {
return
}
// from hbase
if st, err = d.BaseUpStat(c, mid, dt); st != nil {
d.AddCache(func() {
d.setUpBaseStatCache(context.TODO(), mid, dt, st)
})
}
return
}

View File

@@ -0,0 +1,75 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
"go-common/app/interface/main/growup/model"
"go-common/library/net/metadata"
)
func TestDaoArticleStat(t *testing.T) {
convey.Convey("ArticleStat", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
ip = metadata.String(c, metadata.RemoteIP)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.ArticleStat(c, mid, ip)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaogetUpBaseStatCache(t *testing.T) {
convey.Convey("getUpBaseStatCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
date = "2018-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.getUpBaseStatCache(c, mid, date)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaosetUpBaseStatCache(t *testing.T) {
convey.Convey("setUpBaseStatCache", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
date = "2018-01-01"
st = &model.UpBaseStat{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.setUpBaseStatCache(c, mid, date, st)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpStat(t *testing.T) {
convey.Convey("UpStat", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
dt = "20180101"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.UpStat(c, mid, dt)
ctx.Convey("Then err should be nil.st should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,87 @@
package dao
import (
"context"
"fmt"
"go-common/app/interface/main/growup/model"
"go-common/library/log"
xtime "go-common/library/time"
"go-common/library/xstr"
)
const (
// select
_avBreachSQL = "SELECT av_id, mid, cdate, money, ctype, reason FROM av_breach_record WHERE mid = ? AND cdate >= ? AND cdate <= ?"
_avBreachByAvIDSQL = "SELECT av_id, mid, cdate, money, reason FROM av_breach_record WHERE av_id in (%s) AND ctype = ?"
_avBreachByTypeSQL = "SELECT cdate, money FROM av_breach_record WHERE mid=? AND cdate >= ? AND cdate <= ? AND ctype in (%s)"
)
// ListAvBreach list av_breach_record by mid
func (d *Dao) ListAvBreach(c context.Context, mid int64, startTime, endTime string) (records []*model.AvBreach, err error) {
records = make([]*model.AvBreach, 0)
rows, err := d.db.Query(c, _avBreachSQL, mid, startTime, endTime)
if err != nil {
log.Error("ListAvBreach d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
r := &model.AvBreach{}
err = rows.Scan(&r.AvID, &r.MID, &r.CDate, &r.Money, &r.CType, &r.Reason)
if err != nil {
log.Error("ListAvBreach rows.Scan error(%v)", err)
return
}
records = append(records, r)
}
err = rows.Err()
return
}
// GetAvBreachByType get av breach record
func (d *Dao) GetAvBreachByType(c context.Context, mid int64, begin string, end string, typ []int64) (rs map[xtime.Time]int64, err error) {
if len(typ) == 0 {
typ = []int64{0, 1, 2, 3}
}
rows, err := d.db.Query(c, fmt.Sprintf(_avBreachByTypeSQL, xstr.JoinInts(typ)), mid, begin, end)
if err != nil {
log.Error("GetAvBreachByType d.db.Query error(%v)", err)
return
}
rs = make(map[xtime.Time]int64)
defer rows.Close()
for rows.Next() {
var cdate xtime.Time
var money int64
err = rows.Scan(&cdate, &money)
if err != nil {
log.Error("GetAvBreachByType rows.Scan error(%v)", err)
return
}
rs[cdate] += money
}
return
}
// GetAvBreachs get av_breach map by avids
func (d *Dao) GetAvBreachs(c context.Context, avs []int64, ctype int) (breachs map[int64]*model.AvBreach, err error) {
breachs = make(map[int64]*model.AvBreach)
rows, err := d.db.Query(c, fmt.Sprintf(_avBreachByAvIDSQL, xstr.JoinInts(avs)), ctype)
if err != nil {
log.Error("ListAvBreach d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
r := &model.AvBreach{}
if err = rows.Scan(&r.AvID, &r.MID, &r.CDate, &r.Money, &r.Reason); err != nil {
log.Error("ListAvBreach rows.Scan error(%v)", err)
return
}
breachs[r.AvID] = r
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,65 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoListAvBreach(t *testing.T) {
convey.Convey("ListAvBreach", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
startTime = "2018-01-01"
endTime = "2019-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_breach_record(av_id, mid, cdate) VALUES(1000, 1001, '2018-06-01') ON DUPLICATE KEY UPDATE cdate = '2018-06-01'")
records, err := d.ListAvBreach(c, mid, startTime, endTime)
ctx.Convey("Then err should be nil.records should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(records, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvBreachByType(t *testing.T) {
convey.Convey("GetAvBreachByType", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1002)
begin = "2018-01-01"
end = "2019-01-01"
typ = []int64{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_breach_record(av_id, mid, cdate, ctype) VALUES(1001, 1002, '2018-06-01', 0) ON DUPLICATE KEY UPDATE cdate = '2018-06-01', ctype = 0")
rs, err := d.GetAvBreachByType(c, mid, begin, end, typ)
ctx.Convey("Then err should be nil.rs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(rs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAvBreachs(t *testing.T) {
convey.Convey("GetAvBreachs", t, func(ctx convey.C) {
var (
c = context.Background()
avs = []int64{1000}
ctype = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_breach_record(av_id, mid, cdate, ctype) VALUES(1001, 1002, '2018-06-01', 0) ON DUPLICATE KEY UPDATE cdate = '2018-06-01', ctype = 0")
breachs, err := d.GetAvBreachs(c, avs, ctype)
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)
})
})
})
}

View File

@@ -0,0 +1,29 @@
package dao
import (
"context"
"database/sql"
"go-common/library/log"
"go-common/library/time"
"go-common/app/interface/main/growup/model"
)
const (
_bannerSQL = "SELECT id,image,link,start_at,end_at FROM banner WHERE start_at <= ? AND end_at >= ? LIMIT 1"
)
// Banner get banner
func (d *Dao) Banner(c context.Context, t int64) (b *model.Banner, err error) {
b = &model.Banner{}
row := d.db.QueryRow(c, _bannerSQL, time.Time(t), time.Time(t))
if err = row.Scan(&b.ID, &b.Image, &b.Link, &b.StartAt, &b.EndAt); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row scan error(%v)", err)
}
}
return
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoBanner(t *testing.T) {
convey.Convey("Banner", t, func(ctx convey.C) {
var (
c = context.Background()
no = int64(1541409804)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
b, err := d.Banner(c, no)
Exec(c, "INSERT INTO banner(id, start_at, end_at) VALUES(1000, '2018-01-01', '2019-01-01')")
ctx.Convey("Then err should be nil.b should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(b, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,111 @@
package dao
import (
"context"
"fmt"
"go-common/app/interface/main/growup/model"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_bgmCountSQL = "SELECT count(distinct sid) FROM background_music WHERE mid=?"
_bgmTitleSQL = "SELECT sid,title FROM background_music WHERE sid IN (%s)"
_bgmWhiteSQL = "SELECT COUNT(*) FROM bgm_white_list WHERE mid = ?"
_bgmIncomeByMIDSQL = "SELECT sid,income,total_income,date FROM bgm_income WHERE mid = ? AND date >= ? AND date <= ?"
_bgmIncomeBySIDSQL = "SELECT aid,income,date FROM bgm_income WHERE sid = ? AND date <= ?"
)
// BgmWhiteList bgm_white_list
func (d *Dao) BgmWhiteList(c context.Context, mid int64) (count int, err error) {
row := d.db.QueryRow(c, _bgmWhiteSQL, mid)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row scan error(%v)", err)
}
}
return
}
// BGMCount get up bgm count
func (d *Dao) BGMCount(c context.Context, mid int64) (count int, err error) {
row := d.db.QueryRow(c, _bgmCountSQL, mid)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row scan error(%v)", err)
}
}
return
}
// GetBgmTitle get bgm titles
func (d *Dao) GetBgmTitle(c context.Context, ids []int64) (titles map[int64]string, err error) {
titles = make(map[int64]string)
rows, err := d.db.Query(c, fmt.Sprintf(_bgmTitleSQL, xstr.JoinInts(ids)))
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
var sid int64
var title string
err = rows.Scan(&sid, &title)
if err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
titles[sid] = title
}
return
}
// ListBgmIncome list bgm income
func (d *Dao) ListBgmIncome(c context.Context, mid int64, startTime, endTime string) (bgms []*model.ArchiveIncome, err error) {
bgms = make([]*model.ArchiveIncome, 0)
rows, err := d.db.Query(c, _bgmIncomeByMIDSQL, mid, startTime, endTime)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
bgm := &model.ArchiveIncome{}
err = rows.Scan(&bgm.ArchiveID, &bgm.Income, &bgm.TotalIncome, &bgm.Date)
if err != nil {
log.Error("ListColumnIncome rows.Scan error(%v)", err)
return
}
bgms = append(bgms, bgm)
}
err = rows.Err()
return
}
// ListBgmIncomeByID list bgm_income by sid
func (d *Dao) ListBgmIncomeByID(c context.Context, id int64, endTime string) (bgms []*model.ArchiveIncome, err error) {
bgms = make([]*model.ArchiveIncome, 0)
rows, err := d.db.Query(c, _bgmIncomeBySIDSQL, id, endTime)
if err != nil {
log.Error("ListBgmIncomeByID d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
bgm := &model.ArchiveIncome{}
err = rows.Scan(&bgm.ArchiveID, &bgm.Income, &bgm.Date)
if err != nil {
log.Error("ListBgmIncomeByID rows.Scan error(%v)", err)
return
}
bgms = append(bgms, bgm)
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,79 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoBGMCount(t *testing.T) {
convey.Convey("BGMCount", 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 background_music(sid, mid) VALUES(1000, 1001)")
count, err := d.BGMCount(c, mid)
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 TestDaoGetBgmTitle(t *testing.T) {
convey.Convey("GetBgmTitle", t, func(ctx convey.C) {
var (
c = context.Background()
ids = []int64{1001}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO background_music(sid, mid, title) VALUES(1001, 1001, 'test')")
titles, err := d.GetBgmTitle(c, ids)
ctx.Convey("Then err should be nil.titles should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(titles, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListBgmIncome(t *testing.T) {
convey.Convey("ListBgmIncome", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
startTime = "2018-01-01"
endTime = "2019-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO bgm_income(sid, mid, total_income, date) VALUES(1000, 1001, 100, '2018-06-01')")
bgms, err := d.ListBgmIncome(c, mid, startTime, endTime)
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 TestDaoListBgmIncomeByID(t *testing.T) {
convey.Convey("ListBgmIncomeByID", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(1000)
endTime = "2019-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO bgm_income(sid, mid, total_income, date) VALUES(1000, 1001, 100, '2018-06-01')")
bgms, err := d.ListBgmIncomeByID(c, id, endTime)
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,85 @@
package dao
import (
"context"
"fmt"
"go-common/app/interface/main/growup/model"
"go-common/library/log"
"go-common/library/xstr"
)
const (
// select
_columnIncomeByMIDSQL = "SELECT aid, income, total_income, date FROM column_income WHERE mid = ? AND date >= ? AND date <= ?"
_columnIncomeByAvIDSQL = "SELECT income, date FROM column_income WHERE aid = ? AND date <= ?"
_columnStatisTitleSQL = "SELECT aid, title FROM column_income_statis WHERE aid in (%s)"
)
// ListColumnIncome list column_income by mid
func (d *Dao) ListColumnIncome(c context.Context, mid int64, startTime, endTime string) (columns []*model.ArchiveIncome, err error) {
columns = make([]*model.ArchiveIncome, 0)
rows, err := d.db.Query(c, _columnIncomeByMIDSQL, mid, startTime, endTime)
if err != nil {
log.Error("ListColumnIncome d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
column := &model.ArchiveIncome{}
err = rows.Scan(&column.ArchiveID, &column.Income, &column.TotalIncome, &column.Date)
if err != nil {
log.Error("ListColumnIncome rows.Scan error(%v)", err)
return
}
columns = append(columns, column)
}
err = rows.Err()
return
}
// ListColumnIncomeByID list column_income by aid
func (d *Dao) ListColumnIncomeByID(c context.Context, id int64, endTime string) (columns []*model.ArchiveIncome, err error) {
columns = make([]*model.ArchiveIncome, 0)
rows, err := d.db.Query(c, _columnIncomeByAvIDSQL, id, endTime)
if err != nil {
log.Error("ListColumnIncomeByID d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
column := &model.ArchiveIncome{}
err = rows.Scan(&column.Income, &column.Date)
if err != nil {
log.Error("ListColumnIncomeByID rows.Scan error(%v)", err)
return
}
columns = append(columns, column)
}
err = rows.Err()
return
}
// GetColumnTitle get column title by id
func (d *Dao) GetColumnTitle(c context.Context, ids []int64) (titles map[int64]string, err error) {
titles = make(map[int64]string)
rows, err := d.db.Query(c, fmt.Sprintf(_columnStatisTitleSQL, xstr.JoinInts(ids)))
if err != nil {
log.Error("GetColumnTitle d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var mid int64
var title string
err = rows.Scan(&mid, &title)
if err != nil {
log.Error("GetColumnTitle rows.Scan error(%v)", err)
return
}
titles[mid] = title
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,62 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoListColumnIncome(t *testing.T) {
convey.Convey("ListColumnIncome", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
startTime = "2018-01-01"
endTime = "2019-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO column_income(aid, mid, total_income, date) VALUES(1000, 1001, 100, '2018-06-01')")
columns, err := d.ListColumnIncome(c, mid, startTime, endTime)
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 TestDaoListColumnIncomeByID(t *testing.T) {
convey.Convey("ListColumnIncomeByID", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(1000)
endTime = "2019-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO column_income(aid, mid, total_income, date) VALUES(1000, 1001, 100, '2018-06-01')")
columns, err := d.ListColumnIncomeByID(c, id, endTime)
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 TestDaoGetColumnTitle(t *testing.T) {
convey.Convey("GetColumnTitle", t, func(ctx convey.C) {
var (
c = context.Background()
ids = []int64{1000}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO column_income_statis(aid, title) VALUES(1000, 'test')")
titles, err := d.GetColumnTitle(c, ids)
ctx.Convey("Then err should be nil.titles should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(titles, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,117 @@
package dao
import (
"context"
"time"
"go-common/app/interface/main/growup/conf"
article "go-common/app/interface/openplatform/article/rpc/client"
account "go-common/app/service/main/account/rpc/client"
vip "go-common/app/service/main/vip/rpc/client"
"go-common/library/cache/redis"
"go-common/library/database/hbase.v2"
"go-common/library/database/sql"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
// Dao def dao struct
type Dao struct {
c *conf.Config
db *sql.DB
rddb *sql.DB
// redis
redis *redis.Pool
redisExpire int64
// search
httpRead *bm.Client
// rpc
acc *account.Service3
art *article.Service
vip *vip.Service
// hbase
hbase *hbase.Client
hbaseTimeOut time.Duration
// chan
missch chan func()
}
// New fn
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.DB.Growup),
rddb: sql.NewMySQL(c.DB.Allowance),
// redis
redis: redis.NewPool(c.Redis.Config),
redisExpire: int64(time.Duration(c.Redis.Expire) / time.Second),
// search
httpRead: bm.NewClient(c.HTTPClient.Read),
// rpc
acc: account.New3(c.AccountRPC),
art: article.New(c.ArticleRPC),
vip: vip.New(c.VipRPC),
// hbase
hbase: hbase.NewClient(c.HBase.Config),
hbaseTimeOut: time.Duration(time.Millisecond * 200),
// chan
missch: make(chan func(), 1024),
}
go d.cacheproc()
return d
}
// Ping ping db
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.Ping(c); err != nil {
log.Error("d.db.Ping error(%v)", err)
return
}
if err = d.pingRedis(c); err != nil {
log.Error("d.pingRedis error(%v)", err)
}
return
}
func (d *Dao) pingRedis(c context.Context) (err error) {
conn := d.redis.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
conn.Close()
return
}
// Close close db conn
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
if d.redis != nil {
d.redis.Close()
}
if d.hbase != nil {
d.hbase.Close()
}
}
// BeginTran begin transcation
func (d *Dao) BeginTran(c context.Context) (tx *sql.Tx, err error) {
return d.db.Begin(c)
}
// AddCache add to chan for cache
func (d *Dao) AddCache(f func()) {
select {
case d.missch <- f:
default:
log.Warn("cacheproc chan full")
}
}
// cacheproc is a routine for execute closure.
func (d *Dao) cacheproc() {
for {
f := <-d.missch
f()
}
}

View File

@@ -0,0 +1,44 @@
package dao
import (
"context"
"flag"
"os"
"testing"
"go-common/app/interface/main/growup/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "mobile.studio.growup-interface")
flag.Set("conf_token", "c68ad4f01bc8c39a3fa6242623e79ffb")
flag.Set("tree_id", "13584")
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-interface.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,153 @@
package dao
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/url"
"strconv"
"go-common/app/interface/main/growup/model"
vip "go-common/app/service/main/vip/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
)
const (
_panelType = "incentive"
)
const (
_goodsInfoSQL = "SELECT ex_product_id,ex_resource_id,display_on_time,goods_type,discount FROM creative_goods WHERE is_display = ?"
_goodsResourceIDSQL = "SELECT ex_resource_id,discount,goods_type FROM creative_goods WHERE ex_product_id = ? AND goods_type = ? AND is_display = 2"
_goodsOrderSQL = "SELECT order_time,goods_name,goods_price,goods_cost FROM creative_order WHERE mid = ? ORDER BY order_time DESC LIMIT ?,?"
_goodsOrderCountSQL = "SELECT count(*) FROM creative_order WHERE mid = ?"
// insert
_txInGoodsOrderSQL = "INSERT INTO creative_order(mid,order_no,order_time,goods_type,goods_id,goods_name,goods_price,goods_cost) VALUES(?,?,?,?,?,?,?,?)"
)
// GetGoodsByProductID get goods by product id
func (d *Dao) GetGoodsByProductID(c context.Context, productID string, goodsType int) (goods *model.GoodsInfo, err error) {
goods = &model.GoodsInfo{}
err = d.db.QueryRow(c, _goodsResourceIDSQL, productID, goodsType).Scan(&goods.ResourceID, &goods.Discount, &goods.GoodsType)
if err == sql.ErrNoRows {
err = nil
}
return
}
// GetGoodsOrderCount get order count
func (d *Dao) GetGoodsOrderCount(c context.Context, mid int64) (count int, err error) {
err = d.db.QueryRow(c, _goodsOrderCountSQL, mid).Scan(&count)
if err == sql.ErrNoRows {
err = nil
}
return
}
// GetGoodsOrders get orders
func (d *Dao) GetGoodsOrders(c context.Context, mid int64, start, end int) (orders []*model.GoodsOrder, err error) {
orders = make([]*model.GoodsOrder, 0)
rows, err := d.db.Query(c, _goodsOrderSQL, mid, start, end)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
g := &model.GoodsOrder{}
err = rows.Scan(&g.OrderTime, &g.GoodsName, &g.GoodsPrice, &g.GoodsCost)
if err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
orders = append(orders, g)
}
err = rows.Err()
return
}
// GetDisplayGoods get up from up_activity
func (d *Dao) GetDisplayGoods(c context.Context, isDisplay int) (goods []*model.GoodsInfo, err error) {
goods = make([]*model.GoodsInfo, 0)
rows, err := d.db.Query(c, _goodsInfoSQL, isDisplay)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
g := &model.GoodsInfo{}
err = rows.Scan(&g.ProductID, &g.ResourceID, &g.DisplayOnTime, &g.GoodsType, &g.Discount)
if err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
goods = append(goods, g)
}
err = rows.Err()
return
}
// TxInsertGoodsOrder insert goods order
func (d *Dao) TxInsertGoodsOrder(tx *xsql.Tx, o *model.GoodsOrder) (rows int64, err error) {
res, err := tx.Exec(_txInGoodsOrderSQL, o.MID, o.OrderNo, o.OrderTime, o.GoodsType, o.GoodsID, o.GoodsName, o.GoodsPrice, o.GoodsCost)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", _txInGoodsOrderSQL, err)
return
}
return res.RowsAffected()
}
// ListVipProducts list vip products
func (d *Dao) ListVipProducts(c context.Context, mid int64) (r map[string]*model.GoodsInfo, err error) {
r = make(map[string]*model.GoodsInfo)
res, err := d.vip.VipPanelInfo5(c, &vip.ArgPanel{PanelType: _panelType, Mid: mid})
if err != nil {
log.Error("vipRPC.VipPanelInfo5 err(%v)", err)
return
}
if _, err = json.Marshal(res.Vps); err != nil {
log.Error("json.Marshal err(%v)", err)
return
}
for _, v := range res.Vps {
m := new(model.GoodsInfo)
m.ProductID = v.PdID
m.ProductName = v.PdName
// 大会员实时价格 = 激励兑换实时成本价; 单位元转换为单位分
m.OriginPrice = int64(Round(Mul(v.DPrice, float64(100)), 0))
m.Month = v.Month
r[v.PdID] = m
}
return
}
// ExchangeBigVIP exchange big vip
func (d *Dao) ExchangeBigVIP(c context.Context, mid, resourceID, orderNo int64, remark string) (err error) {
params := url.Values{}
params.Set("batchId", strconv.FormatInt(resourceID, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
params.Set("orderNo", strconv.FormatInt(orderNo, 10))
params.Set("remark", remark)
var res struct {
Code int `json:"code"`
Message string `json:"message"`
}
url := d.c.Host.VipURI
if err = d.httpRead.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("ExchangeBigVIP code != 0. res.Code(%d) | url(%s) error(%v)", res.Code, url+"?"+params.Encode(), res.Message)
err = fmt.Errorf("ExchangeBigVIP error(%s)", res.Message)
}
return
}

View File

@@ -0,0 +1,110 @@
package dao
import (
"context"
"go-common/app/interface/main/growup/model"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetGoodsByProductID(t *testing.T) {
convey.Convey("GetGoodsByProductID", t, func(ctx convey.C) {
var (
c = context.Background()
productID = "aaaaa"
goodsType = int(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
Exec(c, "INSERT INTO creative_goods(ex_resource_id,discount,goods_type,is_display) values('aaaaa', 100, 1, 2)")
goods, err := d.GetGoodsByProductID(c, productID, goodsType)
ctx.Convey("Then err should be nil.goods should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(goods, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetDisplayGoods(t *testing.T) {
convey.Convey("GetDisplayGoods", t, func(ctx convey.C) {
var (
c = context.Background()
isDisplay = int(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
Exec(c, "INSERT INTO creative_goods(ex_resource_id,discount,goods_type,is_display) values('aaaaa', 100, 1, 2)")
goods, err := d.GetDisplayGoods(c, isDisplay)
ctx.Convey("Then err should be nil.goods should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(goods, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTxInsertGoodsOrder(t *testing.T) {
convey.Convey("TxInsertGoodsOrder", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
o = &model.GoodsOrder{
MID: 253550886,
OrderNo: "DHY-20181122172812-1617825777068670976-253550886",
GoodsType: 1,
GoodsID: "incentive-1mon",
GoodsName: "月度大会员",
GoodsPrice: 100,
GoodsCost: 100,
}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer tx.Commit()
Exec(c, "DELETE FROM creative_order WHERE order_no = 'DHY-20181122172812-1617825777068670976-253550886'")
rows, err := d.TxInsertGoodsOrder(tx, o)
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)
})
})
ctx.Reset(func() {
tx.Commit()
})
})
}
func TestDaoListVipProducts(t *testing.T) {
convey.Convey("ListVipProducts", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(212312)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
r, err := d.ListVipProducts(c, mid)
ctx.Convey("Then err should be nil.r should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(r, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoExchangeBigVIP(t *testing.T) {
convey.Convey("ExchangeBigVIP", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(253550886)
resourceID = int64(16)
orderNo = time.Now().UnixNano()
remark = "creative"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.ExchangeBigVIP(c, mid, resourceID, orderNo, remark)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,72 @@
package dao
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"strconv"
"go-common/app/interface/main/growup/model"
"go-common/library/ecode"
"go-common/library/log"
"github.com/tsuna/gohbase/hrpc"
)
const (
// HBaseUpStatTablePrefix hbase up_stat_date
HBaseUpStatTablePrefix = "up_stats_"
)
func hbaseMd5Key(aid int64) string {
hasher := md5.New()
hasher.Write([]byte(strconv.Itoa(int(aid))))
return hex.EncodeToString(hasher.Sum(nil))
}
// BaseUpStat get base up stat.
func (d *Dao) BaseUpStat(c context.Context, mid int64, date string) (stat *model.UpBaseStat, err error) {
var (
result *hrpc.Result
ctx, cancel = context.WithTimeout(c, d.hbaseTimeOut)
tableName = HBaseUpStatTablePrefix + date // change table at 12:00am
key = hbaseMd5Key(mid)
)
defer cancel()
if result, err = d.hbase.GetStr(ctx, tableName, key); err != nil {
log.Error("BaseUpStat d.hbase.GetStr tableName(%s)|mid(%d)|key(%v)|error(%v)", tableName, mid, key, err)
err = ecode.CreativeDataErr
return
}
if result == nil {
return
}
stat = &model.UpBaseStat{}
for _, c := range result.Cells {
if c == nil {
continue
}
v, _ := strconv.ParseInt(string(c.Value[:]), 10, 64)
if !bytes.Equal(c.Family, []byte("u")) {
continue
}
switch {
case bytes.Equal(c.Qualifier, []byte("play")):
stat.View = 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("fans")):
stat.Fans = v
case bytes.Equal(c.Qualifier, []byte("fav")):
stat.Fav = 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,38 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaohbaseMd5Key(t *testing.T) {
convey.Convey("hbaseMd5Key", t, func(ctx convey.C) {
var (
aid = int64(1000)
)
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 TestDaoBaseUpStat(t *testing.T) {
convey.Convey("BaseUpStat", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1000)
date = "2018-06-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.BaseUpStat(c, mid, date)
ctx.Convey("Then err should be nil.stat should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,235 @@
package dao
import (
"context"
"database/sql"
"fmt"
"go-common/app/interface/main/growup/model"
"go-common/library/log"
"go-common/library/time"
"go-common/library/xstr"
)
const (
// select
_avIncomeByMIDSQL = "SELECT av_id, income, total_income, date FROM av_income WHERE mid = ? AND date >= ? AND date <= ?"
_avIncomeByAvIDSQL = "SELECT income, date FROM av_income WHERE av_id = ? AND date <= ?"
_blacklistByAvIDSQL = "SELECT av_id FROM av_black_list WHERE av_id in (%s) AND ctype = ? AND is_delete = 0"
_activityInfoByAvIDSQL = "SELECT archive_id, tag_id FROM activity_info WHERE archive_id in (%s)"
_tagInfoByTagIDSQL = "SELECT id, ratio, icon FROM tag_info WHERE id in (%s) and is_deleted = 0"
_upIncomeTableSQL = "SELECT mid,income,av_income,column_income,bgm_income,total_income,base_income,av_base_income,column_base_income,bgm_base_income,date FROM %s WHERE mid = ? AND date >= ? AND date <= ? AND is_deleted = 0"
_upAccountSQL = "SELECT mid, total_income, total_unwithdraw_income, withdraw_date_version, version FROM up_account WHERE mid = ? AND is_deleted = 0"
_upIncomeSQL = "SELECT mid,base_income,income,date FROM up_income WHERE mid=? AND date>=? AND date <=? ORDER BY date"
_firstUpIncomeSQL = "SELECT date FROM up_income WHERE mid = ? ORDER BY date LIMIT 1"
_upIncomeCountSQL = "SELECT count(*) FROM up_income WHERE date = ?"
_upDailyCharge = "SELECT inc_charge FROM up_daily_charge WHERE mid=? AND date>=?"
)
// GetUpDailyCharge get up daily charge
func (d *Dao) GetUpDailyCharge(c context.Context, mid int64, begin string) (incs []int, err error) {
rows, err := d.db.Query(c, _upDailyCharge, mid, begin)
if err != nil {
log.Error("GetUpDailyCharge d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var incCharge int
err = rows.Scan(&incCharge)
if err != nil {
log.Error("rows Scan error(%v)", err)
return
}
incs = append(incs, incCharge)
}
return
}
// ListAvIncome list av_income by mid
func (d *Dao) ListAvIncome(c context.Context, mid int64, startTime, endTime string) (avs []*model.ArchiveIncome, err error) {
avs = make([]*model.ArchiveIncome, 0)
rows, err := d.db.Query(c, _avIncomeByMIDSQL, mid, startTime, endTime)
if err != nil {
log.Error("ListAvIncome d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
av := &model.ArchiveIncome{}
err = rows.Scan(&av.ArchiveID, &av.Income, &av.TotalIncome, &av.Date)
if err != nil {
log.Error("ListAvIncome rows scan error(%v)", err)
return
}
avs = append(avs, av)
}
err = rows.Err()
return
}
// ListAvIncomeByID list av_income by av_id
func (d *Dao) ListAvIncomeByID(c context.Context, avID int64, endTime string) (avs []*model.ArchiveIncome, err error) {
avs = make([]*model.ArchiveIncome, 0)
rows, err := d.db.Query(c, _avIncomeByAvIDSQL, avID, endTime)
if err != nil {
log.Error("ListAvIncomeByID d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
av := &model.ArchiveIncome{}
err = rows.Scan(&av.Income, &av.Date)
if err != nil {
log.Error("ListAvIncomeByID rows scan error(%v)", err)
return
}
avs = append(avs, av)
}
err = rows.Err()
return
}
// ListAvBlackList list av_blakc_list by av_id
func (d *Dao) ListAvBlackList(c context.Context, avIds []int64, typ int) (avb map[int64]struct{}, err error) {
avb = make(map[int64]struct{})
rows, err := d.db.Query(c, fmt.Sprintf(_blacklistByAvIDSQL, xstr.JoinInts(avIds)), typ)
if err != nil {
log.Error("ListBlackList 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("ListBlackList rows scan error(%v)", err)
return
}
avb[avID] = struct{}{}
}
err = rows.Err()
return
}
// ListActiveInfo list active_info by avid
func (d *Dao) ListActiveInfo(c context.Context, avIds []int64) (acM map[int64]int64, err error) {
acM = make(map[int64]int64)
rows, err := d.db.Query(c, fmt.Sprintf(_activityInfoByAvIDSQL, xstr.JoinInts(avIds)))
if err != nil {
log.Error("ListActiveInfo d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var avID, tagID int64
err = rows.Scan(&avID, &tagID)
if err != nil {
log.Error("ListActiveInfo rows scan error(%v)", err)
return
}
acM[avID] = tagID
}
err = rows.Err()
return
}
// ListTagInfo list tag_info by avid
func (d *Dao) ListTagInfo(c context.Context, tagIds []int64) (tagM map[int64]*model.TagInfo, err error) {
tagM = make(map[int64]*model.TagInfo)
rows, err := d.db.Query(c, fmt.Sprintf(_tagInfoByTagIDSQL, xstr.JoinInts(tagIds)))
if err != nil {
log.Error("ListTagInfo d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
tagInfo := model.TagInfo{}
err = rows.Scan(&tagInfo.ID, &tagInfo.Radio, &tagInfo.Icon)
if err != nil {
log.Error("ListTagInfo rows scan error(%v)", err)
return
}
if val, ok := tagM[tagInfo.ID]; !ok {
tagM[tagInfo.ID] = &tagInfo
} else {
if val.Radio < tagInfo.Radio {
tagM[tagInfo.ID] = &tagInfo
}
}
}
err = rows.Err()
return
}
// ListUpIncome list up_income by mid
func (d *Dao) ListUpIncome(c context.Context, mid int64, table, startTime, endTime string) (ups []*model.UpIncome, err error) {
ups = make([]*model.UpIncome, 0)
rows, err := d.db.Query(c, fmt.Sprintf(_upIncomeTableSQL, table), mid, startTime, endTime)
if err != nil {
log.Error("ListUpIncome d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
up := &model.UpIncome{}
err = rows.Scan(&up.MID, &up.Income, &up.AvIncome, &up.ColumnIncome, &up.BgmIncome, &up.TotalIncome, &up.BaseIncome, &up.AvBaseIncome, &up.ColumnBaseIncome, &up.BgmBaseIncome, &up.Date)
if err != nil {
log.Error("ListUpIncome rows scan error(%v)", err)
return
}
ups = append(ups, up)
}
err = rows.Err()
return
}
// ListUpAccount list up_account by mid
func (d *Dao) ListUpAccount(c context.Context, mid int64) (up *model.UpAccount, err error) {
up = &model.UpAccount{}
row := d.db.QueryRow(c, _upAccountSQL, mid)
if err = row.Scan(&up.MID, &up.TotalIncome, &up.TotalUnwithdrawIncome, &up.WithdrawDateVersion, &up.Version); err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
log.Error("row.Scan error(%v)", err)
}
return
}
// GetUpIncome list up income by date
func (d *Dao) GetUpIncome(c context.Context, mid int64, begin string, end string) (result []*model.UpIncomeStat, err error) {
rows, err := d.db.Query(c, _upIncomeSQL, mid, begin, end)
if err != nil {
return
}
defer rows.Close()
for rows.Next() {
stat := &model.UpIncomeStat{}
err = rows.Scan(&stat.MID, &stat.BaseIncome, &stat.Income, &stat.Date)
if err != nil {
return
}
stat.ExtraIncome = stat.Income - stat.BaseIncome
result = append(result, stat)
}
return
}
// GetFirstUpIncome get first up income
func (d *Dao) GetFirstUpIncome(c context.Context, mid int64) (date time.Time, err error) {
err = d.db.QueryRow(c, _firstUpIncomeSQL, mid).Scan(&date)
if err == sql.ErrNoRows {
err = nil
date = time.Time(0)
}
return
}
// GetUpIncomeCount get up income count by date
func (d *Dao) GetUpIncomeCount(c context.Context, date string) (count int, err error) {
err = d.db.QueryRow(c, _upIncomeCountSQL, date).Scan(&count)
return
}

View File

@@ -0,0 +1,188 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetUpDailyCharge(t *testing.T) {
convey.Convey("GetUpDailyCharge", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
begin = "2018-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_daily_charge(mid, date, inc_charge) VALUES(1001, '2018-06-01', 100)")
incs, err := d.GetUpDailyCharge(c, mid, begin)
ctx.Convey("Then err should be nil.incs should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(incs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListAvIncome(t *testing.T) {
convey.Convey("ListAvIncome", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
startTime = "2018-01-01"
endTime = "2019-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_income(av_id, mid, date, income) VALUES(1000, 1001, '2018-06-01', 100)")
avs, err := d.ListAvIncome(c, mid, startTime, endTime)
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 TestDaoListAvIncomeByID(t *testing.T) {
convey.Convey("ListAvIncomeByID", t, func(ctx convey.C) {
var (
c = context.Background()
avID = int64(1000)
endTime = "2019-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_income(av_id, mid, date, income) VALUES(1000, 1001, '2018-06-01', 100)")
avs, err := d.ListAvIncomeByID(c, avID, endTime)
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 TestDaoListAvBlackList(t *testing.T) {
convey.Convey("ListAvBlackList", t, func(ctx convey.C) {
var (
c = context.Background()
avIds = []int64{1000}
typ = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO av_black_list(av_id, ctype) VALUES(1000, 0) ON DUPLICATE KEY UPDATE ctype = 0")
avb, err := d.ListAvBlackList(c, avIds, 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 TestDaoListActiveInfo(t *testing.T) {
convey.Convey("ListActiveInfo", t, func(ctx convey.C) {
var (
c = context.Background()
avIds = []int64{1000}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO activity_info(archive_id, tag_id) VALUES(1000, 1)")
acM, err := d.ListActiveInfo(c, avIds)
ctx.Convey("Then err should be nil.acM should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(acM, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListTagInfo(t *testing.T) {
convey.Convey("ListTagInfo", t, func(ctx convey.C) {
var (
c = context.Background()
tagIds = []int64{1000}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO tag_info(id, ratio, icon) VALUES(1000, 10, 'aaaaaa')")
tagM, err := d.ListTagInfo(c, tagIds)
ctx.Convey("Then err should be nil.tagM should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(tagM, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoListUpIncome(t *testing.T) {
convey.Convey("ListUpIncome", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
table = "up_income"
startTime = "2018-01-01"
endTime = "2019-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_income(mid, date) VALUES(1001, '2018-06-01')")
ups, err := d.ListUpIncome(c, mid, table, startTime, endTime)
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 TestDaoListUpAccount(t *testing.T) {
convey.Convey("ListUpAccount", 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, is_deleted) VALUES(1001, 0) ON DUPLICATE KEY UPDATE is_deleted = 0")
up, err := d.ListUpAccount(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 TestDaoGetUpIncome(t *testing.T) {
convey.Convey("GetUpIncome", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1001)
begin = "2018-01-01"
end = "2019-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_income(mid, date) VALUES(1001, '2018-06-01')")
result, err := d.GetUpIncome(c, mid, begin, end)
ctx.Convey("Then err should be nil.result should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(result, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUpIncomeCount(t *testing.T) {
convey.Convey("GetUpIncomeCount", t, func(ctx convey.C) {
var (
c = context.Background()
date = "2018-06-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_income(mid, date) VALUES(1001, '2018-06-01')")
count, err := d.GetUpIncomeCount(c, date)
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,69 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"account.go",
"activity.go",
"category.go",
"dao.go",
"recommend_up.go",
"relation.go",
"video_up.go",
],
importpath = "go-common/app/interface/main/growup/dao/newbiedao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/growup/conf:go_default_library",
"//app/interface/main/growup/model:go_default_library",
"//app/service/main/account/api: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/net/metadata:go_default_library",
"//library/xstr:go_default_library",
"//vendor/github.com/pkg/errors: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 = [
"account_test.go",
"activity_test.go",
"category_test.go",
"dao_test.go",
"recommend_up_test.go",
"relation_test.go",
"video_up_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/interface/main/growup/conf:go_default_library",
"//app/interface/main/growup/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,38 @@
package newbiedao
import (
"context"
accApi "go-common/app/service/main/account/api"
"go-common/library/ecode"
"go-common/library/log"
)
// GetInfo get info
func (d *Dao) GetInfo(c context.Context, mid int64) (res *accApi.InfoReply, err error) {
res, err = d.accGRPC.Info3(c, &accApi.MidReq{Mid: mid})
if err != nil {
return
}
if res == nil || res.Info == nil {
err = ecode.GrowupUpInfoNotExist
log.Error("s.dao.GetInfo get up info is empty")
return
}
return
}
// GetInfos get infos
func (d *Dao) GetInfos(c context.Context, mids []int64) (res *accApi.InfosReply, err error) {
res, err = d.accGRPC.Infos3(c, &accApi.MidsReq{Mids: mids})
if err != nil {
return
}
if res == nil {
err = ecode.GrowupUpInfoNotExist
log.Error("s.dao.GetInfos get ups info are empty")
return
}
return
}

View File

@@ -0,0 +1,40 @@
package newbiedao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNewbiedaoGetInfo(t *testing.T) {
convey.Convey("GetInfo", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(27515398)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.GetInfo(c, mid)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestNewbiedaoGetInfos(t *testing.T) {
convey.Convey("GetInfo", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{27515398}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.GetInfos(c, mids)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,36 @@
package newbiedao
import (
"context"
"go-common/app/interface/main/growup/model"
"go-common/library/ecode"
"go-common/library/log"
//"go-common/library/net/http/blademaster"
"go-common/library/net/metadata"
"net/url"
)
// const text
const (
// ActivityTypeVideo type "videoall"
ActivityTypeVideo = "videoall"
)
// GetActivities get activities
func (d *Dao) GetActivities(c context.Context) (res []*model.Activity, err error) {
activitiesRes := new(model.ActivitiesRes)
err = d.httpRead.Get(c, d.c.Host.ActivitiesURI+ActivityTypeVideo, metadata.String(c, metadata.RemoteIP), url.Values{}, &activitiesRes)
if err != nil {
log.Error("s.dao.GetActivities error(%v)", err)
return
}
if activitiesRes.Code != ecode.OK.Code() {
err = ecode.Int(activitiesRes.Code)
log.Error("s.dao.GetActivities get activities failed, ecode: %d", activitiesRes.Code)
return
}
res = activitiesRes.Data
return
}

View File

@@ -0,0 +1,23 @@
package newbiedao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNewbiedaoGetActivities(t *testing.T) {
convey.Convey("GetActivities", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.GetActivities(c)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,32 @@
package newbiedao
import (
"context"
"github.com/pkg/errors"
"go-common/app/interface/main/growup/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"net/url"
)
// GetCategories get categories
func (d *Dao) GetCategories(c context.Context) (err error) {
categoriesRes := new(model.CategoriesRes)
err = d.httpRead.Get(c, d.c.Host.CategoriesURI, metadata.String(c, metadata.RemoteIP), url.Values{}, categoriesRes)
if err != nil {
log.Error("s.dao.GetCategories error(%v)", err)
return
}
if categoriesRes.Code != ecode.OK.Code() || len(categoriesRes.Data) <= 0 {
err = errors.Wrap(ecode.Int(categoriesRes.Code), "get categories failed")
log.Error("s.dao.GetCategories failed, ecode: %d", categoriesRes.Code)
return
}
Categories = categoriesRes.Data
return
}

View File

@@ -0,0 +1,22 @@
package newbiedao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNewbiedaoGetCategories(t *testing.T) {
convey.Convey("GetCategories", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.GetCategories(c)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,80 @@
package newbiedao
import (
"context"
"time"
"go-common/app/interface/main/growup/conf"
"go-common/app/interface/main/growup/model"
accApi "go-common/app/service/main/account/api"
"go-common/library/database/sql"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
var (
// Categories cache categories
Categories map[int64]*model.Category
// RecommendUpList cache recommend up list
RecommendUpList map[int64]map[int64]*model.RecommendUp
)
// Dao def dao struct
type Dao struct {
c *conf.Config
db *sql.DB
// search
httpRead *bm.Client
// grpc
accGRPC accApi.AccountClient
}
// New fn
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.DB.Growup),
// search
httpRead: bm.NewClient(c.HTTPClient.Read),
}
var err error
if d.accGRPC, err = accApi.NewClient(c.AccCliConf); err != nil {
panic(err)
}
d.loadCache()
go func() {
t := time.Tick(10 * time.Minute)
for {
d.loadCache()
<-t
}
}()
return d
}
// Ping ping db
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.Ping(c); err != nil {
log.Error("d.db.Ping error(%v)", err)
return
}
return
}
// Close close db conn
func (d *Dao) Close() {
if d.db != nil {
d.db.Close()
}
}
// loodCache load cache
func (d *Dao) loadCache() {
_ = d.GetCategories(context.Background())
log.Info("refresh categories cache: %+v", Categories)
_ = d.GetRecommendUpList(context.Background())
log.Info("refresh recommend up list cache: %+v", RecommendUpList)
}

View File

@@ -0,0 +1,37 @@
package newbiedao
import (
"flag"
"go-common/app/interface/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-interface")
flag.Set("conf_token", "c68ad4f01bc8c39a3fa6242623e79ffb")
flag.Set("tree_id", "13584")
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-interface.toml")
}
if os.Getenv("UT_LOCAL_TEST") != "" {
flag.Set("conf", "../../cmd/growup-interface.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
os.Exit(m.Run())
}

View File

@@ -0,0 +1,37 @@
package newbiedao
import (
"context"
"go-common/app/interface/main/growup/model"
"go-common/library/log"
)
const _RecommendUpSql = "SELECT mid, tid FROM recommend_up_white"
// GetRecommendUpList get recommend up list
func (d *Dao) GetRecommendUpList(c context.Context) error {
recUps := make(map[int64]map[int64]*model.RecommendUp)
rows, err := d.db.Query(c, _RecommendUpSql)
if err != nil {
log.Error("d.db.Query recommend up error(%v)", err)
return err
}
defer rows.Close()
for rows.Next() {
recUp := new(model.RecommendUp)
err = rows.Scan(&recUp.Mid, &recUp.Tid)
if err != nil {
log.Error("rows scan error(%v)", err)
return err
}
if _, ok := recUps[recUp.Tid]; !ok {
recUps[recUp.Tid] = make(map[int64]*model.RecommendUp)
}
recUps[recUp.Tid][recUp.Mid] = recUp
}
RecommendUpList = recUps
return nil
}

View File

@@ -0,0 +1,22 @@
package newbiedao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNewbiedaoGetRecommendUpList(t *testing.T) {
convey.Convey("GetRecommendUpList", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.GetRecommendUpList(c)
ctx.Convey("Then err should be nil.recUps should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,36 @@
package newbiedao
import (
"context"
"go-common/app/interface/main/growup/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"go-common/library/xstr"
"net/url"
"strconv"
)
// GetRelations get relations
func (d *Dao) GetRelations(c context.Context, mid int64, fids []int64) (res map[int64]*model.Relation, err error) {
relationsRes := new(model.RelationsRes)
uv := url.Values{}
uv.Set("mid", strconv.FormatInt(mid, 10))
uv.Set("fids", xstr.JoinInts(fids))
err = d.httpRead.Get(c, d.c.Host.RelationsURI, metadata.String(c, metadata.RemoteIP), uv, relationsRes)
if err != nil {
log.Error("s.dao.GetRelations error(%v)", err)
return
}
if relationsRes.Code != ecode.OK.Code() {
err = ecode.Int(relationsRes.Code)
log.Error("s.dao.GetRelations get relations failed, ecode: %d", relationsRes.Code)
return
}
res = relationsRes.Data
return
}

View File

@@ -0,0 +1,25 @@
package newbiedao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNewbiedaoGetRelations(t *testing.T) {
convey.Convey("GetRelations", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(27515398)
fids = []int64{389088, 6810019, 4578433}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.GetRelations(c, mid, fids)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,37 @@
package newbiedao
import (
"context"
"github.com/pkg/errors"
"go-common/app/interface/main/growup/model"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"net/url"
"strconv"
)
// GetVideoUp get video up
func (d *Dao) GetVideoUp(c context.Context, aid int64) (videoUpArchive *model.VideoUpArchive, err error) {
uv := url.Values{}
uv.Set("aid", strconv.FormatInt(aid, 10))
videoUpRes := new(model.VideoUpRes)
err = d.httpRead.Get(c, d.c.Host.VideoUpURI, metadata.String(c, metadata.RemoteIP), uv, videoUpRes)
if err != nil {
log.Error("s.dao.GetVideoUp error(%v)", err)
return
}
if videoUpRes.Code != ecode.OK.Code() {
err = errors.Wrap(ecode.Int(videoUpRes.Code), "get video up failed")
log.Error("s.dao.GetVideoUp get video up failed, ecode: %d", videoUpRes.Code)
return
}
if videoUpRes.Data == nil || videoUpRes.Data.Archive == nil {
err = errors.Wrap(ecode.Int(videoUpRes.Code), "get video up nil")
log.Error("s.dao.GetVideoUp get video up is empty")
return
}
videoUpArchive = videoUpRes.Data.Archive
return
}

View File

@@ -0,0 +1,25 @@
package newbiedao
import (
"context"
"go-common/app/interface/main/growup/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestNewbiedaoGetVideoUp(t *testing.T) {
convey.Convey("GetVideoUp", t, func(ctx convey.C) {
var (
c = context.Background()
req = &model.NewbieLetterReq{Aid: 10110467, Mid: 27515398}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
videoUpArchive, err := d.GetVideoUp(c, req.Aid)
ctx.Convey("Then err should be nil.videoUpArchive should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(videoUpArchive, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,60 @@
package dao
import (
"context"
"database/sql"
"fmt"
"go-common/library/log"
"go-common/app/interface/main/growup/model"
)
const (
_latestNoticeSQL = "SELECT title,link,type FROM notice WHERE status = 1 AND platform IN (?, 3) ORDER BY mtime DESC LIMIT 1"
_noticeSQL = "SELECT id,title,link,ctime,type FROM notice WHERE %s platform IN (?, 3) AND status=1 ORDER BY mtime DESC LIMIT ?,?"
_noticeCountSQL = "SELECT count(*) from notice WHERE %s platform IN (?, 3) AND status=1"
)
// LatestNotice latest notice
func (d *Dao) LatestNotice(c context.Context, platform int) (n *model.Notice, err error) {
row := d.rddb.QueryRow(c, _latestNoticeSQL, platform)
n = &model.Notice{}
if err = row.Scan(&n.Title, &n.Link, &n.Type); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row scan error(%v)", err)
}
}
return
}
// Notices notices
func (d *Dao) Notices(c context.Context, typ string, platform int, offset, limit int) (notices []*model.Notice, err error) {
rows, err := d.rddb.Query(c, fmt.Sprintf(_noticeSQL, typ), platform, offset, limit)
if err != nil {
log.Error("d.db.Query Notices error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
n := &model.Notice{}
err = rows.Scan(&n.ID, &n.Title, &n.Link, &n.CTime, &n.Type)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
notices = append(notices, n)
}
return
}
// NoticeCount notice count
func (d *Dao) NoticeCount(c context.Context, typ string, platform int) (count int64, err error) {
row := d.rddb.QueryRow(c, fmt.Sprintf(_noticeCountSQL, typ), platform)
if err = row.Scan(&count); err != nil {
log.Error("row scan error(%v)", err)
}
return
}

View File

@@ -0,0 +1,63 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoLatestNotice(t *testing.T) {
convey.Convey("LatestNotice", t, func(ctx convey.C) {
var (
c = context.Background()
platform = int(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO notice(status, platform, title, type) VALUES(1, 3, 'test', 0)")
n, err := d.LatestNotice(c, platform)
ctx.Convey("Then err should be nil.n should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(n, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoNotices(t *testing.T) {
convey.Convey("Notices", t, func(ctx convey.C) {
var (
c = context.Background()
typ = ""
platform = int(3)
offset = int(0)
limit = int(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO notice(status, platform, title, type) VALUES(1, 3, 'test', 0)")
notices, err := d.Notices(c, typ, platform, offset, limit)
ctx.Convey("Then err should be nil.notices should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(notices, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoNoticeCount(t *testing.T) {
convey.Convey("NoticeCount", t, func(ctx convey.C) {
var (
c = context.Background()
typ = ""
platform = int(3)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO notice(status, platform, title, type) VALUES(1, 3, 'test', 0)")
count, err := d.NoticeCount(c, typ, platform)
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,99 @@
package dao
import (
"context"
"encoding/json"
"go-common/library/cache/redis"
"go-common/library/log"
)
// SetIncomeCache set income cache
func (d *Dao) SetIncomeCache(c context.Context, key string, value map[string]interface{}) (err error) {
v, err := json.Marshal(value)
if err != nil {
log.Error("json.Marshal error(%v)", err)
return
}
return d.setCacheKV(c, key, v, d.redisExpire)
}
// GetIncomeCache get income cache
func (d *Dao) GetIncomeCache(c context.Context, key string) (data map[string]interface{}, err error) {
res, err := d.getCacheVal(c, key)
if err != nil {
log.Error("d.getCacheVal error(%v)", err)
return
}
if res == nil {
return
}
err = json.Unmarshal(res, &data)
if err != nil {
log.Error("json.Unmarshal(%v) error(%v)", res, err)
}
return
}
// DelCacheKey del redis key
func (d *Dao) DelCacheKey(c context.Context, key string) (err error) {
return d.delCacheKey(c, key)
}
func (d *Dao) setCacheKV(c context.Context, key string, value []byte, expire int64) (err error) {
conn := d.redis.Get(c)
defer conn.Close()
if err = conn.Send("SET", key, value); err != nil {
log.Error("conn.Send(SET, %s, %s) error(%v)", key, value, err)
return
}
if err = conn.Send("EXPIRE", key, d.redisExpire); err != nil {
log.Error("conn.Send(Expire, %s, %d) error(%v)", key, d.redisExpire, err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
for i := 0; i < 2; i++ {
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
return
}
}
return
}
func (d *Dao) getCacheVal(c context.Context, key string) (res []byte, err error) {
conn := d.redis.Get(c)
defer conn.Close()
if res, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
res, err = nil, nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
return
}
func (d *Dao) delCacheKey(c context.Context, key string) (err error) {
conn := d.redis.Get(c)
defer conn.Close()
if err = conn.Send("DEL", key); err != nil {
log.Error("conn.Send(DEL, %s) error(%v)", key, err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush error(%v)", err)
return
}
if _, err = conn.Receive(); err != nil {
log.Error("conn.Receive() error(%v)", err)
}
return
}

View File

@@ -0,0 +1,73 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoSetIncomeCache(t *testing.T) {
convey.Convey("SetIncomeCache", t, func(ctx convey.C) {
var (
c = context.Background()
key = "100"
value = map[string]interface{}{
"aa": "bb",
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.SetIncomeCache(c, key, value)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetIncomeCache(t *testing.T) {
convey.Convey("GetIncomeCache", t, func(ctx convey.C) {
var (
c = context.Background()
key = "100"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.GetIncomeCache(c, key)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaosetCacheKV(t *testing.T) {
convey.Convey("setCacheKV", t, func(ctx convey.C) {
var (
c = context.Background()
key = "200"
value = []byte("aaaaaa")
expire = int64(100)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
err := d.setCacheKV(c, key, value, expire)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaogetCacheVal(t *testing.T) {
convey.Convey("getCacheVal", t, func(ctx convey.C) {
var (
c = context.Background()
key = "200"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
_, err := d.getCacheVal(c, key)
ctx.Convey("Then err should be nil.res should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,305 @@
package dao
import (
"context"
"database/sql"
"fmt"
"go-common/app/interface/main/growup/model"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_openAwardsSQL = "SELECT award_id,award_name,cycle_start FROM special_award WHERE open_status=2"
_joinedAwardsSQL = "SELECT award_id,award_name,cycle_start,cycle_end,open_status FROM special_award WHERE award_id IN (%s)"
_awardScheSQL = "SELECT award_name,cycle_start,cycle_end,announce_date FROM special_award WHERE award_id=?"
_awardResourceSQL = "SELECT resource_type,content,resource_index FROM special_award_resource WHERE award_id=? AND is_deleted=0"
_awardWinnerSQL = "SELECT mid FROM special_award_winner WHERE award_id=? AND is_deleted=0"
_winnerIDsSQL = "SELECT award_id,prize_id FROM special_award_winner WHERE mid=? AND is_deleted=0"
_winnerDivisionSQL = "SELECT award_id,division_name FROM special_award_winner WHERE mid=? AND is_deleted=0"
_awardBonusSQL = "SELECT bonus FROM special_award_prize WHERE award_id=? AND prize_id=?"
_inSpecialAwardRecordSQL = "INSERT INTO special_award_record(mid,award_id) VALUES(?,?)"
_joinedCountSQL = "SELECT count(*) FROM special_award_record WHERE mid=? AND award_id=?"
_specialAwardsSQL = "SELECT award_id, award_name, cycle_start, cycle_end, announce_date FROM special_award WHERE display_status = 2 AND is_deleted = 0"
_awardDivisionSQL = "SELECT division_name FROM special_award_division WHERE award_id = ? AND is_deleted = 0"
_awardWinRecordSQL = "SELECT award_id FROM special_award_winner WHERE mid = ? AND is_deleted = 0"
_awardJoinRecordSQL = "SELECT award_id FROM special_award_record WHERE mid = ? AND is_deleted = 0"
)
// PastAwards get award basic info
func (d *Dao) PastAwards(c context.Context) (as []*model.SimpleSpecialAward, err error) {
rows, err := d.db.Query(c, _openAwardsSQL)
if err != nil {
return
}
defer rows.Close()
as = make([]*model.SimpleSpecialAward, 0)
for rows.Next() {
a := &model.SimpleSpecialAward{}
err = rows.Scan(&a.AwardID, &a.AwardName, &a.CycleStart)
if err != nil {
return
}
as = append(as, a)
}
err = rows.Err()
return
}
// JoinedSpecialAwards get joined awards
func (d *Dao) JoinedSpecialAwards(c context.Context, awardIDs []int64) (sas []*model.SpecialAward, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_joinedAwardsSQL, xstr.JoinInts(awardIDs)))
if err != nil {
return
}
defer rows.Close()
sas = make([]*model.SpecialAward, 0)
for rows.Next() {
sa := &model.SpecialAward{}
err = rows.Scan(&sa.AwardID, &sa.AwardName, &sa.CycleStart, &sa.CycleEnd, &sa.OpenStatus)
if err != nil {
log.Error("row scan error(%v)", err)
return
}
sas = append(sas, sa)
}
err = rows.Err()
return
}
// GetAwardSchedule get special award by award id
func (d *Dao) GetAwardSchedule(c context.Context, awardID int64) (award *model.SpecialAward, err error) {
award = &model.SpecialAward{}
row := d.db.QueryRow(c, _awardScheSQL, awardID)
if err = row.Scan(&award.AwardName, &award.CycleStart, &award.CycleEnd, &award.AnnounceDate); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row scan error(%v)", err)
}
}
return
}
// GetResources get special award resource map[type]map[index]content
func (d *Dao) GetResources(c context.Context, awardID int64) (res map[int]map[int]string, err error) {
rows, err := d.db.Query(c, _awardResourceSQL, awardID)
if err != nil {
log.Error("GetResources d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make(map[int]map[int]string)
for rows.Next() {
var rtype, index int
var content string
err = rows.Scan(&rtype, &content, &index)
if err != nil {
log.Error("GetDivisions rows.Scan error(%v)", err)
return
}
if cs, ok := res[rtype]; ok {
cs[index] = content
} else {
m := make(map[int]string)
m[index] = content
res[rtype] = m
}
}
err = rows.Err()
return
}
// GetWinners get winner mids by award_id
func (d *Dao) GetWinners(c context.Context, awardID int64) (mids []int64, err error) {
rows, err := d.db.Query(c, _awardWinnerSQL, awardID)
if err != nil {
log.Error("GetWinners d.db.Query error(%v)", err)
return
}
defer rows.Close()
mids = make([]int64, 0)
for rows.Next() {
var mid int64
err = rows.Scan(&mid)
if err != nil {
log.Error("GetWinners rows.Scan error(%v)", err)
return
}
mids = append(mids, mid)
}
err = rows.Err()
return
}
// AwardIDsByWinner get winner's record, am map[award_id]prize_id
func (d *Dao) AwardIDsByWinner(c context.Context, mid int64) (am map[int64]int64, err error) {
rows, err := d.db.Query(c, _winnerIDsSQL, mid)
if err != nil {
return
}
defer rows.Close()
am = make(map[int64]int64)
for rows.Next() {
var awardID, prizeID int64
err = rows.Scan(&awardID, &prizeID)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
am[awardID] = prizeID
}
err = rows.Err()
return
}
// DivisionName division name by mid, names map[award_id]division_name
func (d *Dao) DivisionName(c context.Context, mid int64) (names map[int64]string, err error) {
rows, err := d.db.Query(c, _winnerDivisionSQL, mid)
if err != nil {
return
}
defer rows.Close()
names = make(map[int64]string)
for rows.Next() {
var awardID int64
var name string
err = rows.Scan(&awardID, &name)
if err != nil {
log.Error("rows scan error(%v)", err)
return
}
names[awardID] = name
}
err = rows.Err()
return
}
// JoinedCount get signed up count by mid, award_id
func (d *Dao) JoinedCount(c context.Context, mid, awardID int64) (count int64, err error) {
row := d.db.QueryRow(c, _joinedCountSQL, mid, awardID)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row scan error(%v)", err)
}
}
return
}
// AwardBonus get bonus by award_id and prize_id
func (d *Dao) AwardBonus(c context.Context, awardID, prizeID int64) (bonus int64, err error) {
row := d.db.QueryRow(c, _awardBonusSQL, awardID, prizeID)
if err = row.Scan(&bonus); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row scan error(%v)", err)
}
}
return
}
// AddToAwardRecord add to award record
func (d *Dao) AddToAwardRecord(c context.Context, mid, awardID int64) (rows int64, err error) {
res, err := d.db.Exec(c, _inSpecialAwardRecordSQL, mid, awardID)
if err != nil {
return
}
return res.RowsAffected()
}
// GetSpecialAwards get all display sepcial award
func (d *Dao) GetSpecialAwards(c context.Context) (awards []*model.SpecialAward, err error) {
awards = make([]*model.SpecialAward, 0)
rows, err := d.db.Query(c, _specialAwardsSQL)
if err != nil {
log.Error("GetSpecialAwards d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
sa := &model.SpecialAward{}
err = rows.Scan(&sa.AwardID, &sa.AwardName, &sa.CycleStart, &sa.CycleEnd, &sa.AnnounceDate)
if err != nil {
log.Error("GetSpecialAwards rows.Scan error(%v)", err)
return
}
awards = append(awards, sa)
}
err = rows.Err()
return
}
// GetSpecialAwardDivision get special award division name
func (d *Dao) GetSpecialAwardDivision(c context.Context, awardID int64) (divisions []string, err error) {
divisions = make([]string, 0)
rows, err := d.db.Query(c, _awardDivisionSQL, awardID)
if err != nil {
log.Error("GetSpecialAwardDivision d.db.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var name string
err = rows.Scan(&name)
if err != nil {
log.Error("GetSpecialAwardDivision rows.Scan error(%v)", err)
return
}
divisions = append(divisions, name)
}
err = rows.Err()
return
}
// GetAwardWinRecord get award win record
func (d *Dao) GetAwardWinRecord(c context.Context, mid int64) (awardIDs map[int64]bool, err error) {
awardIDs = make(map[int64]bool)
rows, err := d.db.Query(c, _awardWinRecordSQL, mid)
if err != nil {
log.Error("GetAwardWinRecord 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("GetAwardWinRecord rows.Scan error(%v)", err)
return
}
awardIDs[id] = true
}
err = rows.Err()
return
}
// GetAwardJoinRecord get award join record
func (d *Dao) GetAwardJoinRecord(c context.Context, mid int64) (awardIDs map[int64]bool, err error) {
awardIDs = make(map[int64]bool)
rows, err := d.db.Query(c, _awardJoinRecordSQL, mid)
if err != nil {
log.Error("GetAwardJoinRecord 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("GetAwardJoinRecord rows.Scan error(%v)", err)
return
}
awardIDs[id] = true
}
err = rows.Err()
return
}

View File

@@ -0,0 +1,242 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoPastAwards(t *testing.T) {
convey.Convey("PastAwards", t, func(convCtx convey.C) {
var (
c = context.Background()
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
Exec(c, "INSERT INTO special_award(award_id,award_name,open_status) VALUES(666,'test','2') ON DUPLICATE KEY UPDATE award_id=VALUES(award_id)")
as, err := d.PastAwards(c)
convCtx.Convey("Then err should be nil.as should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(as, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoJoinedSpecialAwards(t *testing.T) {
convey.Convey("JoinedSpecialAwards", t, func(convCtx convey.C) {
var (
c = context.Background()
awardIDs = []int64{666}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
Exec(c, "INSERT INTO special_award(award_id,award_name,open_status) VALUES(666,'test','2') ON DUPLICATE KEY UPDATE award_id=VALUES(award_id)")
sas, err := d.JoinedSpecialAwards(c, awardIDs)
convCtx.Convey("Then err should be nil.sas should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(sas, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAwardSchedule(t *testing.T) {
convey.Convey("GetAwardSchedule", t, func(convCtx convey.C) {
var (
c = context.Background()
awardID = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
Exec(c, "INSERT INTO special_award(award_id,award_name,open_status) VALUES(666,'test','2') ON DUPLICATE KEY UPDATE award_id=VALUES(award_id)")
award, err := d.GetAwardSchedule(c, awardID)
convCtx.Convey("Then err should be nil.award should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(award, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetResources(t *testing.T) {
convey.Convey("GetResources", t, func(convCtx convey.C) {
var (
c = context.Background()
awardID = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
Exec(c, "INSERT INTO special_award_resource(award_id,resource_type,content,resource_index) VALUES(666,1,'test',1) ON DUPLICATE KEY UPDATE award_id=VALUES(award_id)")
res, err := d.GetResources(c, awardID)
convCtx.Convey("Then err should be nil.res should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetWinners(t *testing.T) {
convey.Convey("GetWinners", t, func(convCtx convey.C) {
var (
c = context.Background()
awardID = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
Exec(c, "INSERT INTO special_award_winner(award_id,mid,division_name) VALUES(666,2,'test') ON DUPLICATE KEY UPDATE award_id=VALUES(award_id)")
mids, err := d.GetWinners(c, awardID)
convCtx.Convey("Then err should be nil.mids should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(mids, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAwardIDsByWinner(t *testing.T) {
convey.Convey("AwardIDsByWinner", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(2)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
am, err := d.AwardIDsByWinner(c, mid)
convCtx.Convey("Then err should be nil.am should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(am, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDivisionName(t *testing.T) {
convey.Convey("DivisionName", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(2)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
names, err := d.DivisionName(c, mid)
convCtx.Convey("Then err should be nil.names should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(names, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoJoinedCount(t *testing.T) {
convey.Convey("JoinedCount", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(2)
awardID = int64(666)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
Exec(c, "INSERT INTO special_award_record(award_id,mid) VALUES(666,2) ON DUPLICATE KEY UPDATE award_id=VALUES(award_id)")
count, err := d.JoinedCount(c, mid, awardID)
convCtx.Convey("Then err should be nil.count should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(count, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAwardBonus(t *testing.T) {
convey.Convey("AwardBonus", t, func(convCtx convey.C) {
var (
c = context.Background()
awardID = int64(66)
prizeID = int64(2)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
Exec(c, "INSERT INTO special_award_prize(award_id,prize_id, bonus) VALUES(66,2,100) ON DUPLICATE KEY UPDATE award_id=VALUES(award_id)")
bonus, err := d.AwardBonus(c, awardID, prizeID)
convCtx.Convey("Then err should be nil.bonus should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(bonus, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAddToAwardRecord(t *testing.T) {
convey.Convey("AddToAwardRecord", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(2)
awardID = int64(666)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
Exec(c, "DELETE FROM special_award_record WHERE mid=2")
rows, err := d.AddToAwardRecord(c, mid, awardID)
convCtx.Convey("Then err should be nil.rows should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(rows, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetSpecialAwards(t *testing.T) {
convey.Convey("GetSpecialAwards", t, func(convCtx convey.C) {
var (
c = context.Background()
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
awards, err := d.GetSpecialAwards(c)
convCtx.Convey("Then err should be nil.awards should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(awards, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetSpecialAwardDivision(t *testing.T) {
convey.Convey("GetSpecialAwardDivision", t, func(convCtx convey.C) {
var (
c = context.Background()
awardID = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
Exec(c, "INSERT INTO special_award_division(award_id,division_name) VALUES(666,'test') ON DUPLICATE KEY UPDATE award_id=VALUES(award_id)")
divisions, err := d.GetSpecialAwardDivision(c, awardID)
convCtx.Convey("Then err should be nil.divisions should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(divisions, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAwardWinRecord(t *testing.T) {
convey.Convey("GetAwardWinRecord", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(2)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
awardIDs, err := d.GetAwardWinRecord(c, mid)
convCtx.Convey("Then err should be nil.awardIDs should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(awardIDs, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetAwardJoinRecord(t *testing.T) {
convey.Convey("GetAwardJoinRecord", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(2)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
awardIDs, err := d.GetAwardJoinRecord(c, mid)
convCtx.Convey("Then err should be nil.awardIDs should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(awardIDs, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,56 @@
package dao
import (
"math"
"math/big"
)
// Div div
func Div(x, y float64) float64 {
a := big.NewFloat(x)
b := big.NewFloat(y)
c := new(big.Float).Quo(a, b)
d, _ := c.Float64()
return d
}
// Mul mul
func Mul(x, y float64) float64 {
a := big.NewFloat(x)
b := big.NewFloat(y)
c := new(big.Float).Mul(a, b)
d, _ := c.Float64()
return d
}
// DivWithRound div with round
func DivWithRound(x, y float64, places int) float64 {
a := big.NewFloat(x)
b := big.NewFloat(y)
c := new(big.Float).Quo(a, b)
d, _ := c.Float64()
return Round(d, places)
}
// MulWithRound mul with round
func MulWithRound(x, y float64, places int) float64 {
a := big.NewFloat(x)
b := big.NewFloat(y)
c := new(big.Float).Mul(a, b)
d, _ := c.Float64()
return Round(d, places)
}
// Round round
func Round(val float64, places int) float64 {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= 0.5 {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
return round / pow
}

View File

@@ -0,0 +1,84 @@
package dao
import (
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoDiv(t *testing.T) {
convey.Convey("Div", t, func(ctx convey.C) {
var (
x = float64(1)
y = float64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := Div(x, y)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoMul(t *testing.T) {
convey.Convey("Mul", t, func(ctx convey.C) {
var (
x = float64(1)
y = float64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := Mul(x, y)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDivWithRound(t *testing.T) {
convey.Convey("DivWithRound", t, func(ctx convey.C) {
var (
x = float64(1)
y = float64(1)
places = int(2)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := DivWithRound(x, y, places)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoMulWithRound(t *testing.T) {
convey.Convey("MulWithRound", t, func(ctx convey.C) {
var (
x = float64(1)
y = float64(1)
places = int(2)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := MulWithRound(x, y, places)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoRound(t *testing.T) {
convey.Convey("Round", t, func(ctx convey.C) {
var (
val = float64(1)
places = int(2)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := Round(val, places)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,268 @@
package dao
import (
"context"
"fmt"
"go-common/app/interface/main/growup/model"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/time"
)
const (
// insert
_inUpsSQL = "INSERT INTO %s (mid,nickname,account_type,category_id,fans,account_state,sign_type,apply_at,%s,is_deleted) VALUES (?,?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE nickname=?,account_type=?,category_id=?,fans=?,account_state=?,sign_type=?,apply_at=?,%s=?,is_deleted=?"
_inUpBgmSQL = "INSERT INTO up_info_bgm(mid,nickname,bgms,account_type,fans,account_state,sign_type,signed_at,is_deleted) VALUES(?,?,?,?,?,3,?,?,0) ON DUPLICATE KEY UPDATE nickname=VALUES(nickname),bgms=VALUES(bgms),account_type=VALUES(account_type),fans=VALUES(fans),account_state=3,signed_at=VALUES(signed_at),sign_type=VALUES(sign_type),is_deleted=0"
_inCreditRecordSQL = "INSERT INTO credit_score_record (mid,operate_at,operator,reason,deducted,remaining) VALUES(?,?,?,?,?,?)"
_inCreditScoreSQL = "INSERT INTO credit_score(mid) VALUES(?) ON DUPLICATE KEY UPDATE mid=VALUES(mid)"
// select
_upsCateInfoSQL = "SELECT nick_name,main_category_id FROM up_category_info WHERE mid=?"
_upsStatInfoSQL = "SELECT fans FROM up_base_statistics WHERE mid=?"
_blockMIDSQL = "SELECT mid FROM up_blocked WHERE mid=? AND is_deleted=0"
_upNicknameSQL = "SELECT nickname FROM up_info_video WHERE mid = ?"
_upCreditScoreSQL = "SELECT score FROM credit_score WHERE mid = ?"
_accountStateSQL = "SELECT account_state FROM %s WHERE mid = ? AND is_deleted = 0"
_upSignedAtSQL = "SELECT signed_at FROM %s WHERE mid = ? AND account_state = 3 AND is_deleted = 0"
// will delete next version
_upWhiteListSQL = "SELECT type FROM up_white_list WHERE mid=? AND is_deleted=0"
// _upsArchiveSQL = "SELECT account_type,account_state,reason,expired_in,quit_at,ctime FROM up_info_video WHERE mid=? AND is_deleted=0"
_avAccountStateSQL = "SELECT account_type,account_state,reason,expired_in,quit_at,ctime FROM up_info_video WHERE mid=? AND is_deleted=0"
_bgmAccountStateSQL = "SELECT account_type,account_state,reason,expired_in,quit_at,ctime FROM up_info_bgm WHERE mid=? AND is_deleted=0"
_columnAccountStateSQL = "SELECT account_type,account_state,reason,expired_in,quit_at,ctime FROM up_info_column WHERE mid=? AND is_deleted=0"
_bgmUpCountSQL = "SELECT count(*) FROM background_music WHERE mid=?"
// update
_upQuitSQL = "UPDATE %s SET account_state=5,quit_at=?,expired_in=?,reason=? WHERE mid=? AND account_state=3 AND is_deleted=0"
_deductCreditScoreSQL = "UPDATE credit_score SET score=score-%d WHERE mid=?"
)
// GetAccountState account state
func (d *Dao) GetAccountState(c context.Context, table string, mid int64) (state int, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_accountStateSQL, table), mid)
if err = row.Scan(&state); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row scan error(%v)", err)
}
}
return
}
// GetUpSignedAt get up signed_at
func (d *Dao) GetUpSignedAt(c context.Context, table string, mid int64) (signedAt time.Time, err error) {
if err = d.db.QueryRow(c, fmt.Sprintf(_upSignedAtSQL, table), mid).Scan(&signedAt); err != nil {
if err == sql.ErrNoRows {
err = nil
signedAt = 0
} else {
log.Error("row scan error(%v)", err)
}
}
return
}
// InsertUpInfo add upinfo
func (d *Dao) InsertUpInfo(c context.Context, table string, totalCountField string, v *model.UpInfo) (rows int64, err error) {
res, err := d.db.Exec(c, fmt.Sprintf(_inUpsSQL, table, totalCountField, totalCountField), v.MID, v.Nickname, v.AccountType, v.MainCategory, v.Fans, v.AccountState, v.SignType, v.ApplyAt, v.TotalPlayCount, 0, v.Nickname, v.AccountType, v.MainCategory, v.Fans, v.AccountState, v.SignType, v.ApplyAt, v.TotalPlayCount, 0)
if err != nil {
log.Error("db.inUpsStmt.Exec(%s) error(%v)", _inUpsSQL, err)
return
}
return res.RowsAffected()
}
// TxInsertBgmUpInfo insert bgm up info
func (d *Dao) TxInsertBgmUpInfo(tx *sql.Tx, v *model.UpInfo) (rows int64, err error) {
res, err := tx.Exec(_inUpBgmSQL, v.MID, v.Nickname, v.Bgms, v.AccountType, v.Fans, v.SignType, v.SignedAt)
if err != nil {
log.Error("db.inBgmUpStmt.Exec(%s) error(%v)", _inUpsSQL, err)
return
}
return res.RowsAffected()
}
// TxInsertCreditScore insert credit score
func (d *Dao) TxInsertCreditScore(tx *sql.Tx, mid int64) (rows int64, err error) {
res, err := tx.Exec(_inCreditScoreSQL, mid)
if err != nil {
log.Error("db.Exec(%s) error(%v)", _inCreditScoreSQL, err)
return
}
return res.RowsAffected()
}
// Blocked query mid in blacklist
func (d *Dao) Blocked(c context.Context, mid int64) (id int64, err error) {
row := d.db.QueryRow(c, _blockMIDSQL, mid)
if err = row.Scan(&id); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// White map: key: type value: bool
func (d *Dao) White(c context.Context, mid int64) (m map[int]bool, err error) {
m = make(map[int]bool)
rows, err := d.db.Query(c, _upWhiteListSQL, mid)
if err != nil {
log.Error("row.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
var typ int
err = rows.Scan(&typ)
if err != nil {
log.Error("rows.Scan error(%v)", err)
}
m[typ] = true
}
return
}
// AvUpStatus return av up status
func (d *Dao) AvUpStatus(c context.Context, mid int64) (status *model.BusinessStatus, err error) {
status = &model.BusinessStatus{}
row := d.db.QueryRow(c, _avAccountStateSQL, mid)
if err = row.Scan(&status.AccountType, &status.AccountState, &status.Reason, &status.ExpiredIn, &status.QuitAt, &status.CTime); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// BgmUpStatus return bgm up status
func (d *Dao) BgmUpStatus(c context.Context, mid int64) (status *model.BusinessStatus, err error) {
status = &model.BusinessStatus{}
row := d.db.QueryRow(c, _bgmAccountStateSQL, mid)
if err = row.Scan(&status.AccountType, &status.AccountState, &status.Reason, &status.ExpiredIn, &status.QuitAt, &status.CTime); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// ColumnUpStatus return bgm up status
func (d *Dao) ColumnUpStatus(c context.Context, mid int64) (status *model.BusinessStatus, err error) {
status = &model.BusinessStatus{}
row := d.db.QueryRow(c, _columnAccountStateSQL, mid)
if err = row.Scan(&status.AccountType, &status.AccountState, &status.Reason, &status.ExpiredIn, &status.QuitAt, &status.CTime); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// CategoryInfo return nickname & categoryID
func (d *Dao) CategoryInfo(c context.Context, mid int64) (nickname string, categoryID int, err error) {
row := d.rddb.QueryRow(c, _upsCateInfoSQL, mid)
if err = row.Scan(&nickname, &categoryID); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// Fans return fans count
func (d *Dao) Fans(c context.Context, mid int64) (fans int, err error) {
row := d.rddb.QueryRow(c, _upsStatInfoSQL, mid)
if err = row.Scan(&fans); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// TxQuit update up status
func (d *Dao) TxQuit(tx *sql.Tx, table string, mid int64, quitAt, expiredIn time.Time, reason string) (rows int64, err error) {
res, err := tx.Exec(fmt.Sprintf(_upQuitSQL, table), quitAt, expiredIn, reason, mid)
if err != nil {
log.Error("db.TxQuit.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// TxInsertCreditRecord tx insert credit deduct record
func (d *Dao) TxInsertCreditRecord(tx *sql.Tx, cr *model.CreditRecord) (rows int64, err error) {
res, err := tx.Exec(_inCreditRecordSQL, cr.MID, cr.OperateAt, cr.Operator, cr.Reason, cr.Deducted, cr.Remaining)
if err != nil {
log.Error("db.TxInsertCreditRecord.Exec() error(%v)", err)
return
}
return res.RowsAffected()
}
// Nickname get nickname from up_info_video
func (d *Dao) Nickname(c context.Context, mid int64) (nickname string, err error) {
row := d.rddb.QueryRow(c, _upNicknameSQL, mid)
if err = row.Scan(&nickname); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// CreditScore get current credit score from up_info_video
func (d *Dao) CreditScore(c context.Context, mid int64) (score int, err error) {
row := d.rddb.QueryRow(c, _upCreditScoreSQL, mid)
if err = row.Scan(&score); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
}
return
}
// BgmUpCount bgm up count in table background_music
func (d *Dao) BgmUpCount(c context.Context, mid int64) (count int, err error) {
row := d.db.QueryRow(c, _bgmUpCountSQL, mid)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
count = 0
} else {
log.Error("db.QueryRow(%s) error(%v)", _bgmUpCountSQL, err)
}
}
return
}
// TxDeductCreditScore tx update credit score
func (d *Dao) TxDeductCreditScore(tx *sql.Tx, score int, mid int64) (rows int64, err error) {
res, err := tx.Exec(fmt.Sprintf(_deductCreditScoreSQL, score), mid)
if err != nil {
log.Error("tx.Exec(%s) error(%v)", _deductCreditScoreSQL, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,18 @@
package dao
import (
"context"
"go-common/app/interface/main/growup/model"
)
const (
_upBillSQL = "SELECT mid,first_income,max_income,total_income,av_count,av_max_income,av_id,quality_value,defeat_num,title,share_items,first_time,max_time,signed_at,end_at FROM up_bill WHERE mid = ?"
)
// GetUpBill get up bill by mid
func (d *Dao) GetUpBill(c context.Context, mid int64) (up *model.UpBill, err error) {
up = &model.UpBill{}
err = d.db.QueryRow(c, _upBillSQL, mid).Scan(&up.MID, &up.FirstIncome, &up.MaxIncome, &up.TotalIncome, &up.AvCount, &up.AvMaxIncome, &up.AvID, &up.QualityValue, &up.DefeatNum, &up.Title, &up.ShareItems, &up.FirstTime, &up.MaxTime, &up.SignedAt, &up.EndAt)
return
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetUpBill(t *testing.T) {
convey.Convey("GetUpBill", 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_bill(mid) VALUES(1001)")
up, err := d.GetUpBill(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)
})
})
})
}

View File

@@ -0,0 +1,343 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
"go-common/app/interface/main/growup/model"
xtime "go-common/library/time"
)
func TestDaoGetAccountState(t *testing.T) {
convey.Convey("GetAccountState", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_info_video"
mid = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_info_video(mid, account_state, is_deleted) VALUES(1001, 3, 0) ON DUPLICATE KEY UPDATE account_state = 3, is_deleted = 0")
state, err := d.GetAccountState(c, table, mid)
ctx.Convey("Then err should be nil.state should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(state, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetUpSignedAt(t *testing.T) {
convey.Convey("GetUpSignedAt", 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_info_video(mid, account_state, is_deleted) VALUES(1001, 3, 0) ON DUPLICATE KEY UPDATE account_state = 3, is_deleted = 0")
signedAt, err := d.GetUpSignedAt(c, "up_info_video", mid)
ctx.Convey("Then err should be nil.signedAt should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(signedAt, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertUpInfo(t *testing.T) {
convey.Convey("InsertUpInfo", t, func(ctx convey.C) {
var (
c = context.Background()
table = "up_info_video"
totalCountField = "total_play_count"
v = &model.UpInfo{
MID: 1002,
AccountState: 3,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "DELETE FROM up_info_video WHERE mid = 1002")
rows, err := d.InsertUpInfo(c, table, totalCountField, v)
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 TestDaoTxInsertBgmUpInfo(t *testing.T) {
convey.Convey("TxInsertBgmUpInfo", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
v = &model.UpInfo{
MID: 1002,
AccountState: 3,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer tx.Commit()
Exec(c, "DELETE FROM up_info_bgm WHERE mid = 1002")
rows, err := d.TxInsertBgmUpInfo(tx, v)
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 TestDaoTxInsertCreditScore(t *testing.T) {
convey.Convey("TxInsertCreditScore", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
mid = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer tx.Commit()
rows, err := d.TxInsertCreditScore(tx, 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)
})
})
})
}
func TestDaoBlocked(t *testing.T) {
convey.Convey("Blocked", 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_blocked(mid) VALUES(1001)")
id, err := d.Blocked(c, mid)
ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoWhite(t *testing.T) {
convey.Convey("White", 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_white_list(mid) VALUES(1001)")
m, err := d.White(c, mid)
ctx.Convey("Then err should be nil.m should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(m, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAvUpStatus(t *testing.T) {
convey.Convey("AvUpStatus", 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_info_video(mid, account_state, is_deleted) VALUES(1001, 3, 0) ON DUPLICATE KEY UPDATE account_state = 3, is_deleted = 0")
status, err := d.AvUpStatus(c, mid)
ctx.Convey("Then err should be nil.status should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(status, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoBgmUpStatus(t *testing.T) {
convey.Convey("BgmUpStatus", 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_info_bgm(mid, account_state, is_deleted) VALUES(1001, 3, 0) ON DUPLICATE KEY UPDATE account_state = 3, is_deleted = 0")
status, err := d.BgmUpStatus(c, mid)
ctx.Convey("Then err should be nil.status should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(status, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoColumnUpStatus(t *testing.T) {
convey.Convey("ColumnUpStatus", 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_info_column(mid, account_state, is_deleted) VALUES(1001, 3, 0) ON DUPLICATE KEY UPDATE account_state = 3, is_deleted = 0")
status, err := d.ColumnUpStatus(c, mid)
ctx.Convey("Then err should be nil.status should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(status, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoCategoryInfo(t *testing.T) {
convey.Convey("CategoryInfo", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
nickname, categoryID, err := d.CategoryInfo(c, mid)
ctx.Convey("Then err should be nil.nickname,categoryID should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(categoryID, convey.ShouldNotBeNil)
ctx.So(nickname, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoFans(t *testing.T) {
convey.Convey("Fans", 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_base_statistics(mid, fans) VALUES(1001, 100)")
fans, err := d.Fans(c, mid)
ctx.Convey("Then err should be nil.fans should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(fans, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTxQuit(t *testing.T) {
convey.Convey("TxQuit", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
table = "up_info_video"
mid = int64(1003)
quitAt = xtime.Time(time.Now().Unix())
expiredIn = xtime.Time(time.Now().Unix())
reason = "test"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer tx.Commit()
Exec(c, "INSERT INTO up_info_video(mid, account_state, is_deleted) VALUES(1003, 3, 0) ON DUPLICATE KEY UPDATE account_state = 3, is_deleted = 0")
rows, err := d.TxQuit(tx, table, mid, quitAt, expiredIn, reason)
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 TestDaoTxInsertCreditRecord(t *testing.T) {
convey.Convey("TxInsertCreditRecord", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
cr = &model.CreditRecord{
MID: 1001,
Reason: 1,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer tx.Commit()
Exec(c, "DELETE FROM credit_score_record WHERE mid = 1001")
rows, err := d.TxInsertCreditRecord(tx, cr)
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 TestDaoNickname(t *testing.T) {
convey.Convey("Nickname", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(1004)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_info_video(mid, account_state, is_deleted, nickname) VALUES(1004, 3, 0, 'test') ON DUPLICATE KEY UPDATE account_state = 3, is_deleted = 0")
nickname, err := d.Nickname(c, mid)
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 TestDaoCreditScore(t *testing.T) {
convey.Convey("CreditScore", 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 credit_score(mid, score) VALUES(1001, 100)")
score, err := d.CreditScore(c, mid)
ctx.Convey("Then err should be nil.score should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(score, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoBgmUpCount(t *testing.T) {
convey.Convey("BgmUpCount", 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 background_music(mid) VALUES(1001)")
count, err := d.BgmUpCount(c, mid)
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 TestDaoTxDeductCreditScore(t *testing.T) {
convey.Convey("TxDeductCreditScore", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
score = int(10)
mid = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
defer tx.Commit()
Exec(c, "INSERT INTO credit_score(mid, score) VALUES(1001, 100)")
rows, err := d.TxDeductCreditScore(tx, 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,20 @@
package dao
import (
"context"
"database/sql"
)
const (
_upTagYearSQL = "SELECT tag1, tag2, tag3, tag4, tag5, tag6, total_income FROM up_tag_year WHERE mid = ?"
)
// GetUpYearTag get up year tag income
func (d *Dao) GetUpYearTag(c context.Context, mid int64) (tags []int64, income int64, err error) {
tags = make([]int64, 6)
err = d.db.QueryRow(c, _upTagYearSQL, mid).Scan(&tags[0], &tags[1], &tags[2], &tags[3], &tags[4], &tags[5], &income)
if err == sql.ErrNoRows {
err = nil
}
return
}

View File

@@ -0,0 +1,205 @@
package dao
import (
"context"
"database/sql"
"fmt"
"time"
"go-common/app/interface/main/growup/model"
xsql "go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
// get up_account withdraw count
_upAccountCount = "SELECT count(1) FROM up_account where is_deleted = 0 AND has_sign_contract = 1 AND total_unwithdraw_income > 0 AND withdraw_date_version != ?"
// query up_account by date
_queryUpAccountByDate = "SELECT mid, total_unwithdraw_income, withdraw_date_version FROM up_account WHERE is_deleted = 0 AND has_sign_contract = 1 AND total_unwithdraw_income > 0 AND withdraw_date_version != ? LIMIT ?,?"
// query up_account version by mid
_queryUpAccountVersion = "SELECT version FROM up_account WHERE is_deleted = 0 AND mid = ?"
// update up_account withdraw
_updateUpAccountWithdraw = "UPDATE up_account up SET up.total_unwithdraw_income = up.total_unwithdraw_income - %d, up.total_withdraw_income = up.total_withdraw_income + %d, up.last_withdraw_time = ? WHERE up.mid = ? AND up.total_unwithdraw_income > 0 AND is_deleted = 0"
// update up_account unwithdraw income
_updateUpAccountUnwithdrawIncome = "UPDATE up_account up SET up.total_unwithdraw_income = up.total_income - up.total_withdraw_income - up.exchange_income, up.withdraw_date_version = ?, up.version = up.version + 1 WHERE up.is_deleted = 0 AND up.mid = ? AND up.version = ?"
// update up_account exchange and unwithdraw income
_updateUpAccountExchangeIncome = "UPDATE up_account SET total_unwithdraw_income = total_unwithdraw_income - %d, exchange_income = exchange_income + %d, version = version + 1 WHERE is_deleted = 0 AND mid = ? AND version = ?"
// query up_income_withdraw by mid
_queryUpWithdrawByMID = "SELECT withdraw_income, date_version, state, ctime FROM up_income_withdraw WHERE mid = ? AND is_deleted = 0"
// query up_income_withdraw by mids and date_version
_queryUpWithdrawByMIDs = "SELECT id, mid, withdraw_income, date_version, state, ctime FROM up_income_withdraw WHERE is_deleted = 0 AND mid in (%s) AND date_version = ?"
// query up_income_withdraw by id
_queryUpWithdrawByID = "SELECT id, mid, withdraw_income, date_version, state, ctime FROM up_income_withdraw WHERE is_deleted = 0 AND id = ?"
// query up_income_withdraw max date_version by mid
_queryMaxUpWithdrawDateVersion = "SELECT MAX(date_version) FROM up_income_withdraw where is_deleted = 0 AND mid = ?"
// insert record into up_income_withdraw
_insertUpWithdrawRecord = "INSERT INTO up_income_withdraw(mid, withdraw_income, date_version, state) VALUES(?,?,?,?)"
// update up_income_withdraw
_updateUpWithdrawState = "UPDATE up_income_withdraw up SET up.state = ? WHERE up.id = ? AND is_deleted = 0"
)
// GetUpAccountCount get up account withdraw count
func (d *Dao) GetUpAccountCount(c context.Context, dateVersion string) (count int, err error) {
row := d.db.QueryRow(c, _upAccountCount, dateVersion)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
count = 0
} else {
log.Error("db.QueryRow(%s) error(%v)", _upAccountCount, err)
}
}
return
}
// QueryUpAccountByDate query up_account by date
func (d *Dao) QueryUpAccountByDate(c context.Context, dateVersion string, from, limit int) (upAccounts []*model.UpAccount, err error) {
upAccounts = make([]*model.UpAccount, 0)
rows, err := d.db.Query(c, _queryUpAccountByDate, dateVersion, from, limit)
if err != nil {
log.Error("d.db.Query(%s) error(%v)", _queryUpAccountByDate, err)
return
}
defer rows.Close()
for rows.Next() {
up := &model.UpAccount{}
err = rows.Scan(&up.MID, &up.TotalUnwithdrawIncome, &up.WithdrawDateVersion)
if err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
upAccounts = append(upAccounts, up)
}
err = rows.Err()
return
}
// QueryUpWithdrawByMID query up_income_withdraw by mid
func (d *Dao) QueryUpWithdrawByMID(c context.Context, mid int64) (upWithdraws []*model.UpIncomeWithdraw, err error) {
upWithdraws = make([]*model.UpIncomeWithdraw, 0)
rows, err := d.db.Query(c, _queryUpWithdrawByMID, mid)
if err != nil {
log.Error("d.db.Query(%s) error(%v)", _queryUpWithdrawByMID, err)
return
}
defer rows.Close()
for rows.Next() {
upWithdraw := &model.UpIncomeWithdraw{}
err = rows.Scan(&upWithdraw.WithdrawIncome, &upWithdraw.DateVersion, &upWithdraw.State, &upWithdraw.CTime)
if err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
upWithdraws = append(upWithdraws, upWithdraw)
}
err = rows.Err()
return
}
// QueryUpWithdrawByMids query up_income_withdraw by mids
func (d *Dao) QueryUpWithdrawByMids(c context.Context, mids []int64, dateVersion string) (upWithdraws map[int64]*model.UpIncomeWithdraw, err error) {
upWithdraws = make(map[int64]*model.UpIncomeWithdraw)
rows, err := d.db.Query(c, fmt.Sprintf(_queryUpWithdrawByMIDs, xstr.JoinInts(mids)), dateVersion)
if err != nil {
log.Error("d.db.Query(%s) error(%v)", _queryUpWithdrawByMIDs, err)
return
}
defer rows.Close()
for rows.Next() {
upWithdraw := &model.UpIncomeWithdraw{}
err = rows.Scan(&upWithdraw.ID, &upWithdraw.MID, &upWithdraw.WithdrawIncome, &upWithdraw.DateVersion, &upWithdraw.State, &upWithdraw.CTime)
if err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
upWithdraws[upWithdraw.MID] = upWithdraw
}
err = rows.Err()
return
}
// InsertUpWithdrawRecord insert record into up_income_withdraw
func (d *Dao) InsertUpWithdrawRecord(c context.Context, upWithdraw *model.UpIncomeWithdraw) (result int64, err error) {
res, err := d.db.Exec(c, _insertUpWithdrawRecord, upWithdraw.MID, upWithdraw.WithdrawIncome, upWithdraw.DateVersion, upWithdraw.State)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", _insertUpWithdrawRecord, err)
return
}
return res.RowsAffected()
}
// QueryUpWithdrawByID get up_income_withdraw by id
func (d *Dao) QueryUpWithdrawByID(c context.Context, id int64) (upWithdraw *model.UpIncomeWithdraw, err error) {
upWithdraw = &model.UpIncomeWithdraw{}
row := d.db.QueryRow(c, _queryUpWithdrawByID, id)
err = row.Scan(&upWithdraw.ID, &upWithdraw.MID, &upWithdraw.WithdrawIncome, &upWithdraw.DateVersion, &upWithdraw.State, &upWithdraw.CTime)
return
}
// TxUpdateUpWithdrawState update up_income_withdraw state
func (d *Dao) TxUpdateUpWithdrawState(tx *xsql.Tx, id int64, state int) (result int64, err error) {
res, err := tx.Exec(_updateUpWithdrawState, state, id)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", _updateUpWithdrawState)
return
}
return res.RowsAffected()
}
// TxUpdateUpAccountWithdraw update up_account withdraw
func (d *Dao) TxUpdateUpAccountWithdraw(tx *xsql.Tx, mid, thirdCoin int64) (result int64, err error) {
res, err := tx.Exec(fmt.Sprintf(_updateUpAccountWithdraw, thirdCoin, thirdCoin), time.Now(), mid)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", _updateUpAccountWithdraw)
return
}
return res.RowsAffected()
}
// TxQueryMaxUpWithdrawDateVersion query max date_version from up_income_withdraw by mid
func (d *Dao) TxQueryMaxUpWithdrawDateVersion(tx *xsql.Tx, mid int64) (dateVersion string, err error) {
row := tx.QueryRow(_queryMaxUpWithdrawDateVersion, mid)
if err = row.Scan(&dateVersion); err != nil {
if err == sql.ErrNoRows {
err = nil
dateVersion = ""
} else {
log.Error("db.QueryRow(%s) error(%v)", _queryMaxUpWithdrawDateVersion, err)
}
}
return
}
// TxQueryUpAccountVersion query up_account version
func (d *Dao) TxQueryUpAccountVersion(tx *xsql.Tx, mid int64) (version int64, err error) {
row := tx.QueryRow(_queryUpAccountVersion, mid)
err = row.Scan(&version)
return
}
// TxUpdateUpAccountUnwithdrawIncome update up_account unwithdraw and version
func (d *Dao) TxUpdateUpAccountUnwithdrawIncome(tx *xsql.Tx, mid int64, dateVersion string, version int64) (result int64, err error) {
res, err := tx.Exec(_updateUpAccountUnwithdrawIncome, dateVersion, mid, version)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", _updateUpAccountUnwithdrawIncome)
return
}
return res.RowsAffected()
}
// TxUpdateUpAccountExchangeIncome update up_account unwithdraw and exchange_income
func (d *Dao) TxUpdateUpAccountExchangeIncome(tx *xsql.Tx, mid, income, version int64) (result int64, err error) {
res, err := tx.Exec(fmt.Sprintf(_updateUpAccountExchangeIncome, income, income), mid, version)
if err != nil {
log.Error("d.db.Exec(%s) error(%v)", _updateUpAccountExchangeIncome, err)
return
}
return res.RowsAffected()
}

View File

@@ -0,0 +1,227 @@
package dao
import (
"context"
"go-common/app/interface/main/growup/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetUpAccountCount(t *testing.T) {
convey.Convey("GetUpAccountCount", t, func(ctx convey.C) {
var (
c = context.Background()
dateVersion = "2017-01-01"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_account(mid, has_sign_contract, total_unwithdraw_income, withdraw_date_version, is_deleted) VALUES(1001, 1, 100, '2018-10', 0) ON DUPLICATE KEY UPDATE has_sign_contract = 1, is_deleted = 0")
count, err := d.GetUpAccountCount(c, dateVersion)
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 TestDaoQueryUpAccountByDate(t *testing.T) {
convey.Convey("QueryUpAccountByDate", t, func(ctx convey.C) {
var (
c = context.Background()
dateVersion = "2017-01-01"
from = int(0)
limit = int(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_account(mid, has_sign_contract, total_unwithdraw_income, withdraw_date_version, is_deleted) VALUES(1001, 1, 100, '2018-10', 0) ON DUPLICATE KEY UPDATE has_sign_contract = 1, is_deleted = 0")
upAccounts, err := d.QueryUpAccountByDate(c, dateVersion, from, limit)
ctx.Convey("Then err should be nil.upAccounts should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(upAccounts, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryUpWithdrawByMID(t *testing.T) {
convey.Convey("QueryUpWithdrawByMID", 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_income_withdraw(mid, date_version) VALUES(1001, '2018-08')")
upWithdraws, err := d.QueryUpWithdrawByMID(c, mid)
ctx.Convey("Then err should be nil.upWithdraws should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(upWithdraws, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryUpWithdrawByMids(t *testing.T) {
convey.Convey("QueryUpWithdrawByMids", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{1001}
dateVersion = "2018-08"
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_income_withdraw(mid, date_version, is_deleted) VALUES(1001, '2018-08', 0) ON DUPLICATE KEY UPDATE is_deleted = 0")
upWithdraws, err := d.QueryUpWithdrawByMids(c, mids, dateVersion)
ctx.Convey("Then err should be nil.upWithdraws should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(upWithdraws, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertUpWithdrawRecord(t *testing.T) {
convey.Convey("InsertUpWithdrawRecord", t, func(ctx convey.C) {
var (
c = context.Background()
upWithdraw = &model.UpIncomeWithdraw{
MID: 1002,
DateVersion: "2018-09",
State: 1,
}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "DELETE FROM up_income_withdraw WHERE mid = 1002")
result, err := d.InsertUpWithdrawRecord(c, upWithdraw)
ctx.Convey("Then err should be nil.result should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(result, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoQueryUpWithdrawByID(t *testing.T) {
convey.Convey("QueryUpWithdrawByID", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(100001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_income_withdraw(id, mid, date_version, is_deleted) VALUES(100001, 1006, '2018-08', 0) ON DUPLICATE KEY UPDATE is_deleted = 0")
upWithdraw, err := d.QueryUpWithdrawByID(c, id)
ctx.Convey("Then err should be nil.upWithdraw should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(upWithdraw, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoTxUpdateUpWithdrawState(t *testing.T) {
convey.Convey("TxUpdateUpWithdrawState", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
id = int64(100001)
state = int(2)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_income_withdraw(id, mid, date_version, is_deleted, state) VALUES(100001, 1001, '2018-08', 0, 1) ON DUPLICATE KEY UPDATE is_deleted = 0, state = 1")
result, err := d.TxUpdateUpWithdrawState(tx, id, state)
ctx.Convey("Then err should be nil.result should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(result, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
tx.Commit()
})
})
}
func TestDaoTxUpdateUpAccountWithdraw(t *testing.T) {
convey.Convey("TxUpdateUpAccountWithdraw", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
mid = int64(1001)
thirdCoin = int64(10)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_account(mid, has_sign_contract, total_unwithdraw_income, withdraw_date_version, is_deleted) VALUES(1001, 1, 100, '2018-10', 0) ON DUPLICATE KEY UPDATE has_sign_contract = 1, is_deleted = 0")
result, err := d.TxUpdateUpAccountWithdraw(tx, mid, thirdCoin)
ctx.Convey("Then err should be nil.result should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(result, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
tx.Commit()
})
})
}
func TestDaoTxQueryMaxUpWithdrawDateVersion(t *testing.T) {
convey.Convey("TxQueryMaxUpWithdrawDateVersion", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
mid = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_income_withdraw(id, mid, date_version, is_deleted, state) VALUES(10001, 1001, '2018-08', 0, 1) ON DUPLICATE KEY UPDATE is_deleted = 0, state = 1")
dateVersion, err := d.TxQueryMaxUpWithdrawDateVersion(tx, mid)
ctx.Convey("Then err should be nil.dateVersion should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(dateVersion, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
tx.Commit()
})
})
}
func TestDaoTxQueryUpAccountVersion(t *testing.T) {
convey.Convey("TxQueryUpAccountVersion", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
mid = int64(1001)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_account(mid, has_sign_contract, total_unwithdraw_income, withdraw_date_version, is_deleted) VALUES(1001, 1, 100, '2018-10', 0) ON DUPLICATE KEY UPDATE has_sign_contract = 1, is_deleted = 0")
version, err := d.TxQueryUpAccountVersion(tx, mid)
ctx.Convey("Then err should be nil.version should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(version, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
tx.Commit()
})
})
}
func TestDaoTxUpdateUpAccountUnwithdrawIncome(t *testing.T) {
convey.Convey("TxUpdateUpAccountUnwithdrawIncome", t, func(ctx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(c)
mid = int64(1005)
dateVersion = "2018-10"
version = int64(1)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
Exec(c, "INSERT INTO up_account(mid, version, is_deleted) VALUES(1001, 1, 0) ON DUPLICATE KEY UPDATE version = 1, is_deleted = 0")
result, err := d.TxUpdateUpAccountUnwithdrawIncome(tx, mid, dateVersion, version)
ctx.Convey("Then err should be nil.result should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(result, convey.ShouldNotBeNil)
})
})
ctx.Reset(func() {
tx.Commit()
})
})
}