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,79 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"discount_gift_test.go",
"discount_plan_test.go",
"gift_online_test.go",
"gift_plan_test.go",
"redis_test.go",
"ugift_day_test.go",
"ugift_week_test.go",
"user_gift_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/service/live/gift/api/grpc/v1:go_default_library",
"//app/service/live/gift/conf:go_default_library",
"//app/service/live/gift/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"discount_gift.go",
"discount_plan.go",
"gift_online.go",
"gift_plan.go",
"redis.go",
"ugift_day.go",
"ugift_week.go",
"user_gift.go",
],
importpath = "go-common/app/service/live/gift/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/live/activity/api/liverpc:go_default_library",
"//app/service/live/fans_medal/api/liverpc:go_default_library",
"//app/service/live/gift/api/grpc/v1:go_default_library",
"//app/service/live/gift/conf:go_default_library",
"//app/service/live/gift/model:go_default_library",
"//app/service/live/live_user/api/liverpc:go_default_library",
"//app/service/live/room/api/liverpc:go_default_library",
"//app/service/live/user/api/liverpc:go_default_library",
"//app/service/live/xuser/api/grpc/v1:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/rpc/liverpc:go_default_library",
"//library/xstr:go_default_library",
],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,86 @@
package dao
import (
"context"
"go-common/app/service/live/gift/conf"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
xsql "go-common/library/database/sql"
"go-common/library/net/rpc/liverpc"
activity "go-common/app/service/live/activity/api/liverpc"
fans_medal "go-common/app/service/live/fans_medal/api/liverpc"
live_user "go-common/app/service/live/live_user/api/liverpc"
room "go-common/app/service/live/room/api/liverpc"
user "go-common/app/service/live/user/api/liverpc"
xuser "go-common/app/service/live/xuser/api/grpc/v1"
)
// Dao dao
type Dao struct {
c *conf.Config
mc *memcache.Pool
redis *redis.Pool
db *xsql.DB
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
mc: memcache.NewPool(c.Memcache),
redis: redis.NewPool(c.Redis),
db: xsql.NewMySQL(c.MySQL),
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.mc.Close()
d.redis.Close()
d.db.Close()
}
// Ping dao ping
func (d *Dao) Ping(ctx context.Context) error {
// TODO: add mc,redis... if you use
return d.db.Ping(ctx)
}
var (
// RoomApi RoomApi
RoomApi *room.Client
// LiveUserApi LiveUserApi
LiveUserApi *live_user.Client
// UserApi UserApi
UserApi *user.Client
// FansMedalApi FansMedalApi
FansMedalApi *fans_medal.Client
// ActivityApi ActivityApi
ActivityApi *activity.Client
// XuserClient XuserClient
XuserClient *xuser.Client
)
//InitApi InitApi
func InitApi() {
RoomApi = room.New(getConf("room"))
LiveUserApi = live_user.New(getConf("live_user"))
UserApi = user.New(getConf("user"))
FansMedalApi = fans_medal.New(getConf("fans_medal"))
ActivityApi = activity.New(getConf("activity"))
var err error
XuserClient, err = xuser.NewClient(nil)
if err != nil {
panic(err)
}
}
func getConf(appName string) *liverpc.ClientConfig {
c := conf.Conf.LiveRpc
if c != nil {
return c[appName]
}
return nil
}

View File

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

View File

@@ -0,0 +1,39 @@
package dao
import (
"context"
"errors"
"fmt"
"go-common/app/service/live/gift/model"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
var _getDiscountGift = "SELECT id,discount_id,gift_id,user_type,discount_price,corner_mark,corner_position FROM discount_gift WHERE discount_id in (%s)"
// GetByDiscountIds GetByDiscountIds
func (d *Dao) GetByDiscountIds(ctx context.Context, ids []int64) (res []*model.DiscountGift, err error) {
log.Info("GetByDiscountIds,ids:%v", ids)
if len(ids) == 0 {
log.Error("query GetByDiscountIds params null")
err = errors.New("params error")
return
}
var rows *sql.Rows
if rows, err = d.db.Query(ctx, fmt.Sprintf(_getDiscountGift, xstr.JoinInts(ids))); err != nil {
log.Error("query GetByDiscountIds error,err %v", err)
return
}
defer rows.Close()
for rows.Next() {
d := &model.DiscountGift{}
if err = rows.Scan(&d.Id, &d.DiscountId, &d.GiftId, &d.UserType, &d.DiscountPrice, &d.CornerMark, &d.CornerPosition); err != nil {
log.Error("GetByDiscountIds scan error,err %v", err)
return
}
res = append(res, d)
}
return
}

View File

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

View File

@@ -0,0 +1,34 @@
package dao
import (
"context"
"go-common/app/service/live/gift/model"
"go-common/library/database/sql"
"go-common/library/log"
"time"
)
var _getDiscountPlan = "SELECT id,scene_key,scene_value,platform FROM discount_plan WHERE online_time < ? AND offline_time > ? ORDER BY ctime"
// GetDiscountPlan GetDiscountPlan
func (d *Dao) GetDiscountPlan(ctx context.Context, now time.Time) (plans []*model.DiscountPlan, err error) {
log.Info("GetDiscountPlan")
var rows *sql.Rows
var curTime = now.Format("2006-01-02 15:04:05")
curTime = "2018-07-20 00:00:00"
if rows, err = d.db.Query(ctx, _getDiscountPlan, curTime, curTime); err != nil {
log.Error("query GetDiscountPlan error,err %v", err)
return
}
defer rows.Close()
for rows.Next() {
d := &model.DiscountPlan{}
if err = rows.Scan(&d.Id, &d.SceneKey, &d.SceneValue, &d.Platform); err != nil {
log.Error("GetDiscountPlan scan error,err %v", err)
return
}
plans = append(plans, d)
}
return
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetDiscountPlan(t *testing.T) {
convey.Convey("GetDiscountPlan", t, func(c convey.C) {
var (
ctx = context.Background()
now = time.Now()
)
c.Convey("When everything gose positive", func(c convey.C) {
plans, err := d.GetDiscountPlan(ctx, now)
c.Convey("Then err should be nil.plans should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(plans, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,36 @@
package dao
import (
"context"
"go-common/app/service/live/gift/model"
"go-common/library/database/sql"
"go-common/library/log"
)
var (
_getAllGift = "SELECT id,gift_id,name,price,coin_type,type,effect,corner_mark,broadcast,draw,asset_img_basic,asset_img_dynamic,asset_frame_animation,animation_frame_num,asset_gif,asset_webp,asset_full_sc_web,asset_full_sc_horizontal,asset_full_sc_vertical,asset_full_sc_horizontal_svga,asset_full_sc_vertical_svga,asset_bullet_head,asset_bullet_tail,`desc`,rights,rule,limit_interval FROM gift_online"
)
// GetAllGift GetAllGift
func (d *Dao) GetAllGift(ctx context.Context) (gifts []*model.GiftOnline, err error) {
log.Info("GetAllGift")
var rows *sql.Rows
if rows, err = d.db.Query(ctx, _getAllGift); err != nil {
log.Error("query getAllGift error,err %v", err)
return
}
defer rows.Close()
for rows.Next() {
g := &model.GiftOnline{}
if err = rows.Scan(&g.Id, &g.GiftId, &g.Name, &g.Price, &g.CoinType, &g.Type, &g.Effect, &g.CornerMark, &g.Broadcast,
&g.Draw, &g.AssetImgBasic, &g.AssetImgDynamic, &g.AssetFrameAnimation, &g.AnimationFrameNum, &g.AssetGif, &g.AssetWebp,
&g.AssetFullScWeb, &g.AssetFullScHorizontal, &g.AssetFullScVertical, &g.AssetFullScHorizontalSvga, &g.AssetFullScVerticalSvga,
&g.AssetBulletHead, &g.AssetBulletTail, &g.Desc, &g.Rights, &g.Rule, &g.LimitInterval); err != nil {
log.Error("getAllGift scan error,err %v", err)
return
}
gifts = append(gifts, g)
}
return
}

View File

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

View File

@@ -0,0 +1,33 @@
package dao
import (
"context"
"go-common/app/service/live/gift/model"
"go-common/library/database/sql"
"go-common/library/log"
"time"
)
var _getOnlinePlan = "SELECT id,list,silver_list,scene_key,scene_value,mtime,platform FROM gift_plan WHERE online_time <= ? AND offline_time >= ? ORDER BY scene_key DESC,mtime DESC"
// GetOnlinePlan GetOnlinePlan
func (d *Dao) GetOnlinePlan(ctx context.Context) (plans []*model.GiftPlan, err error) {
log.Info("GetOnlinePlan")
var rows *sql.Rows
var curTime = time.Now().Format("2006-01-02 15:04:05")
if rows, err = d.db.Query(ctx, _getOnlinePlan, curTime, curTime); err != nil {
log.Error("query getOnlinePlan error,err %v", err)
return
}
defer rows.Close()
for rows.Next() {
p := &model.GiftPlan{}
if err = rows.Scan(&p.Id, &p.List, &p.SilverList, &p.SceneKey, &p.SceneValue, &p.Mtime, &p.Platform); err != nil {
log.Error("getOnlinePlan scan error,err %v", err)
return
}
plans = append(plans, p)
}
return
}

View File

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

View File

@@ -0,0 +1,392 @@
package dao
import (
"context"
"encoding/json"
"errors"
"fmt"
v1pb "go-common/app/service/live/gift/api/grpc/v1"
"go-common/app/service/live/gift/model"
"go-common/library/cache/redis"
"go-common/library/log"
"math/rand"
"time"
)
func dailyBagKey(uid int64) string {
return fmt.Sprintf("gift:daily_bag:%s:%d", time.Now().Format("20060102"), uid)
}
// GetDailyBagCache GetDailyBagCache
func (d *Dao) GetDailyBagCache(ctx context.Context, uid int64) (res []*v1pb.DailyBagResp_BagList, err error) {
key := dailyBagKey(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
item, err := redis.Bytes(conn.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
err = nil
res = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
if err = json.Unmarshal(item, &res); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(item), err)
}
return
}
// SetDailyBagCache SetDailyBagCache
func (d *Dao) SetDailyBagCache(ctx context.Context, uid int64, data []*v1pb.DailyBagResp_BagList, expire int64) (err error) {
key := dailyBagKey(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
bs, err := json.Marshal(data)
if err != nil {
log.Error("json.Marshal(%v) err(%v)", data, err)
return
}
_, err = conn.Do("SETEX", key, expire, bs)
if err != nil {
log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
}
return
}
func dailyMedalBagKey(uid int64) string {
return fmt.Sprintf("gift:medal:daily_gift_bag:%s:%d", time.Now().Format("20060102"), uid)
}
// GetMedalDailyBagCache GetMedalDailyBagCache
func (d *Dao) GetMedalDailyBagCache(ctx context.Context, uid int64) (res *model.BagGiftStatus, err error) {
key := dailyMedalBagKey(uid)
fmt.Println(key)
res = &model.BagGiftStatus{}
conn := d.redis.Get(ctx)
defer conn.Close()
item, err := redis.Bytes(conn.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
err = nil
res = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
if err = json.Unmarshal(item, &res); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(item), err)
}
return
}
// SetMedalDailyBagCache SetMedalDailyBagCache
func (d *Dao) SetMedalDailyBagCache(ctx context.Context, uid int64, data *model.BagGiftStatus, expire int64) (err error) {
key := dailyMedalBagKey(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
bs, err := json.Marshal(data)
if err != nil {
log.Error("json.Marshal(%v) err(%v)", data, err)
return
}
_, err = conn.Do("SETEX", key, expire, bs)
if err != nil {
log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
}
return
}
func weekLevelBagKey(uid, level int64) string {
_, week := time.Now().ISOWeek()
return fmt.Sprintf("gift:level:week_gift_bag:%d:%d:%d", week, uid, level)
}
// GetWeekLevelBagCache GetWeekLevelBagCache
func (d *Dao) GetWeekLevelBagCache(ctx context.Context, uid, level int64) (res *model.BagGiftStatus, err error) {
key := weekLevelBagKey(uid, level)
res = &model.BagGiftStatus{}
conn := d.redis.Get(ctx)
defer conn.Close()
item, err := redis.Bytes(conn.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
err = nil
res = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
if err = json.Unmarshal(item, &res); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(item), err)
}
return
}
// SetWeekLevelBagCache SetWeekLevelBagCache
func (d *Dao) SetWeekLevelBagCache(ctx context.Context, uid, level int64, data *model.BagGiftStatus, expire int64) (err error) {
key := weekLevelBagKey(uid, level)
conn := d.redis.Get(ctx)
defer conn.Close()
bs, err := json.Marshal(data)
if err != nil {
log.Error("json.Marshal(%v) err(%v)", data, err)
return
}
_, err = conn.Do("SETEX", key, expire, bs)
if err != nil {
log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
}
return
}
//Lock Lock
func (d *Dao) Lock(ctx context.Context, key string, ttl int, retry int, retryDelay int) (gotLock bool, lockValue string, err error) {
if retry <= 0 {
retry = 1
}
lockValue = "locked:" + randomString(5)
retryTimes := 0
conn := d.redis.Get(ctx)
defer conn.Close()
realKey := lockKey(key)
for ; retryTimes < retry; retryTimes++ {
var res interface{}
res, err = conn.Do("SET", realKey, lockValue, "PX", ttl, "NX")
if err != nil {
log.Error("redis_lock failed:%s:%v", realKey, err)
break
}
if res != nil {
gotLock = true
break
}
time.Sleep(time.Duration(retryDelay) * time.Millisecond)
}
return
}
// UnLock UnLock
func (d *Dao) UnLock(ctx context.Context, key string, lockValue string) (err error) {
conn := d.redis.Get(ctx)
defer conn.Close()
realKey := lockKey(key)
res, err := redis.String(conn.Do("GET", realKey))
if err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
if res != lockValue {
err = errors.New("unlock value error")
return
}
_, err = conn.Do("DEL", realKey)
return
}
//ForceUnLock UnLock without lockValue
func (d *Dao) ForceUnLock(ctx context.Context, key string) (err error) {
realKey := lockKey(key)
conn := d.redis.Get(ctx)
defer conn.Close()
_, err = conn.Do("DEL", realKey)
return
}
func lockKey(key string) string {
return fmt.Sprintf("gift_lock:%s", key)
}
func randomString(l int) string {
str := "0123456789abcdefghijklmnopqrstuvwxyz"
bytes := []byte(str)
result := []byte{}
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < l; i++ {
result = append(result, bytes[r.Intn(len(bytes))])
}
return string(result)
}
func bagIDCache(uid, giftID, expireAt int64) string {
return fmt.Sprintf("bag_id:%d:%d:%d", uid, giftID, expireAt)
}
// GetBagIDCache GetBagIDCache
func (d *Dao) GetBagIDCache(ctx context.Context, uid, giftID, expireAt int64) (bagID int64, err error) {
key := bagIDCache(uid, giftID, expireAt)
conn := d.redis.Get(ctx)
defer conn.Close()
bagID, err = redis.Int64(conn.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
return
}
// SetBagIDCache SetBagIDCache
func (d *Dao) SetBagIDCache(ctx context.Context, uid, giftID, expireAt, bagID, expire int64) (err error) {
key := bagIDCache(uid, giftID, expireAt)
conn := d.redis.Get(ctx)
defer conn.Close()
_, err = conn.Do("SETEX", key, expire, bagID)
if err != nil {
log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
}
return
}
func bagListKey(uid int64) string {
return fmt.Sprintf("bag_list:%d", uid)
}
// GetBagListCache GetBagListCache
func (d *Dao) GetBagListCache(ctx context.Context, uid int64) (res []*model.BagGiftList, err error) {
key := bagListKey(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
item, err := redis.Bytes(conn.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
err = nil
res = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
if err = json.Unmarshal(item, &res); err != nil {
log.Error("json.Unmarshal(%s) error(%v)", string(item), err)
}
return
}
// SetBagListCache SetBagListCache
func (d *Dao) SetBagListCache(ctx context.Context, uid int64, data []*model.BagGiftList, expire int64) (err error) {
key := bagListKey(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
bs, err := json.Marshal(data)
if err != nil {
log.Error("json.Marshal(%v) err(%v)", data, err)
return
}
_, err = conn.Do("SETEX", key, expire, bs)
if err != nil {
log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
}
return
}
// ClearBagListCache ClearBagListCache
func (d *Dao) ClearBagListCache(ctx context.Context, uid int64) (err error) {
key := bagListKey(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
_, err = conn.Do("DEL", key)
if err != nil {
log.Error("conn.Do(DEL, %s) error(%v)", key, err)
}
return
}
func bagNumKey(uid, giftID, expireAt int64) string {
return fmt.Sprintf("bag_num:%d:%d:%d", uid, giftID, expireAt)
}
// SetBagNumCache SetBagNumCache
func (d *Dao) SetBagNumCache(ctx context.Context, uid, giftID, expireAt, giftNum, expire int64) (err error) {
key := bagNumKey(uid, giftID, expireAt)
conn := d.redis.Get(ctx)
defer conn.Close()
_, err = conn.Do("SETEX", key, expire, giftNum)
if err != nil {
log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
}
return
}
func vipMonthBag(uid int64) string {
return fmt.Sprintf("gift:vip_month:%s:%d", time.Now().Format("200601"), uid)
}
// GetVipStatusCache GetVipStatusCache
func (d *Dao) GetVipStatusCache(ctx context.Context, uid int64) (status int64, err error) {
key := vipMonthBag(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
status, err = redis.Int64(conn.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
return
}
// ClearVipStatusCache ClearVipStatusCache
func (d *Dao) ClearVipStatusCache(ctx context.Context, uid int64) (err error) {
key := vipMonthBag(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
_, err = conn.Do("DEL", key)
if err != nil {
log.Error("conn.Do(DEL, %s) error(%v)", key, err)
}
return
}
func giftBagStatus(uid int64) string {
return fmt.Sprintf("gift:bag:status:%d", uid)
}
// GetBagStatusCache GetBagStatusCache
func (d *Dao) GetBagStatusCache(ctx context.Context, uid int64) (status int64, err error) {
key := giftBagStatus(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
status, err = redis.Int64(conn.Do("GET", key))
if err != nil {
if err == redis.ErrNil {
err = nil
status = -100 // means cache miss
} else {
log.Error("conn.Do(GET, %s) error(%v)", key, err)
}
return
}
return
}
// SetBagStatusCache SetBagStatusCache
func (d *Dao) SetBagStatusCache(ctx context.Context, uid, status int64, expire int64) (err error) {
key := giftBagStatus(uid)
conn := d.redis.Get(ctx)
defer conn.Close()
_, err = conn.Do("SETEX", key, expire, status)
if err != nil {
log.Error("conn.Do(SETEX, %s) error(%v)", key, err)
}
return
}

View File

@@ -0,0 +1,475 @@
package dao
import (
"context"
v1pb "go-common/app/service/live/gift/api/grpc/v1"
"go-common/app/service/live/gift/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaodailyBagKey(t *testing.T) {
convey.Convey("dailyBagKey", t, func(c convey.C) {
var (
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := dailyBagKey(uid)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetDailyBagCache(t *testing.T) {
convey.Convey("GetDailyBagCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
res, err := d.GetDailyBagCache(ctx, uid)
c.Convey("Then err should be nil.res should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetDailyBagCache(t *testing.T) {
convey.Convey("SetDailyBagCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(9527)
data = []*v1pb.DailyBagResp_BagList{}
expire = int64(1)
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.SetDailyBagCache(ctx, uid, data, expire)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaodailyMedalBagKey(t *testing.T) {
convey.Convey("dailyMedalBagKey", t, func(c convey.C) {
var (
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := dailyMedalBagKey(uid)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetMedalDailyBagCache(t *testing.T) {
convey.Convey("GetMedalDailyBagCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(-1)
)
c.Convey("When everything gose positive", func(c convey.C) {
res, err := d.GetMedalDailyBagCache(ctx, uid)
c.Convey("Then err should be nil.res should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetMedalDailyBagCache(t *testing.T) {
convey.Convey("SetMedalDailyBagCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(9527)
data = &model.BagGiftStatus{}
expire = int64(1)
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.SetMedalDailyBagCache(ctx, uid, data, expire)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoweekLevelBagKey(t *testing.T) {
convey.Convey("weekLevelBagKey", t, func(c convey.C) {
var (
uid = int64(0)
level = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := weekLevelBagKey(uid, level)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetWeekLevelBagCache(t *testing.T) {
convey.Convey("GetWeekLevelBagCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(-1)
level = int64(1)
)
c.Convey("When everything gose positive", func(c convey.C) {
res, err := d.GetWeekLevelBagCache(ctx, uid, level)
c.Convey("Then err should be nil.res should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetWeekLevelBagCache(t *testing.T) {
convey.Convey("SetWeekLevelBagCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(9527)
level = int64(0)
data = &model.BagGiftStatus{}
expire = int64(1)
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.SetWeekLevelBagCache(ctx, uid, level, data, expire)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoLock(t *testing.T) {
convey.Convey("Lock", t, func(c convey.C) {
var (
ctx = context.Background()
key = "test lock"
ttl = int(1)
retry = int(0)
retryDelay = int(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
gotLock, lockValue, err := d.Lock(ctx, key, ttl, retry, retryDelay)
c.Convey("Then err should be nil.gotLock,lockValue should not be nil.", func(c convey.C) {
c.So(lockValue, convey.ShouldNotBeNil)
c.So(err, convey.ShouldBeNil)
c.So(gotLock, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUnLock(t *testing.T) {
convey.Convey("UnLock", t, func(c convey.C) {
var (
ctx = context.Background()
key = "test unlock"
lockValue = "1"
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.UnLock(ctx, key, lockValue)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoForceUnLock(t *testing.T) {
convey.Convey("ForceUnLock", t, func(c convey.C) {
var (
ctx = context.Background()
key = ""
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.ForceUnLock(ctx, key)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaolockKey(t *testing.T) {
convey.Convey("lockKey", t, func(c convey.C) {
var (
key = ""
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := lockKey(key)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaorandomString(t *testing.T) {
convey.Convey("randomString", t, func(c convey.C) {
var (
l = int(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := randomString(l)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaobagIDCache(t *testing.T) {
convey.Convey("bagIDCache", t, func(c convey.C) {
var (
uid = int64(0)
giftID = int64(0)
expireAt = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := bagIDCache(uid, giftID, expireAt)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetBagIDCache(t *testing.T) {
convey.Convey("GetBagIDCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
giftID = int64(0)
expireAt = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
bagID, err := d.GetBagIDCache(ctx, uid, giftID, expireAt)
c.Convey("Then err should be nil.bagID should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(bagID, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetBagIDCache(t *testing.T) {
convey.Convey("SetBagIDCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(9527)
giftID = int64(1)
expireAt = int64(1)
bagID = int64(1)
expire = int64(1)
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.SetBagIDCache(ctx, uid, giftID, expireAt, bagID, expire)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaobagListKey(t *testing.T) {
convey.Convey("bagListKey", t, func(c convey.C) {
var (
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := bagListKey(uid)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetBagListCache(t *testing.T) {
convey.Convey("GetBagListCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
res, err := d.GetBagListCache(ctx, uid)
c.Convey("Then err should be nil.res should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(res, convey.ShouldBeNil)
})
})
})
}
func TestDaoSetBagListCache(t *testing.T) {
convey.Convey("SetBagListCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
data = []*model.BagGiftList{}
expire = int64(1)
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.SetBagListCache(ctx, uid, data, expire)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoClearBagListCache(t *testing.T) {
convey.Convey("ClearBagListCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.ClearBagListCache(ctx, uid)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaobagNumKey(t *testing.T) {
convey.Convey("bagNumKey", t, func(c convey.C) {
var (
uid = int64(0)
giftID = int64(0)
expireAt = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := bagNumKey(uid, giftID, expireAt)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetBagNumCache(t *testing.T) {
convey.Convey("SetBagNumCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(9527)
giftID = int64(1)
expireAt = int64(1)
giftNum = int64(1)
expire = int64(1)
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.SetBagNumCache(ctx, uid, giftID, expireAt, giftNum, expire)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaovipMonthBag(t *testing.T) {
convey.Convey("vipMonthBag", t, func(c convey.C) {
var (
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := vipMonthBag(uid)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetVipStatusCache(t *testing.T) {
convey.Convey("GetVipStatusCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
status, err := d.GetVipStatusCache(ctx, uid)
c.Convey("Then err should be nil.status should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(status, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoClearVipStatusCache(t *testing.T) {
convey.Convey("ClearVipStatusCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.ClearVipStatusCache(ctx, uid)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaogiftBagStatus(t *testing.T) {
convey.Convey("giftBagStatus", t, func(c convey.C) {
var (
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := giftBagStatus(uid)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetBagStatusCache(t *testing.T) {
convey.Convey("GetBagStatusCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
status, err := d.GetBagStatusCache(ctx, uid)
c.Convey("Then err should be nil.status should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(status, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSetBagStatusCache(t *testing.T) {
convey.Convey("SetBagStatusCache", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(9527)
status = int64(1)
expire = int64(1)
)
c.Convey("When everything gose positive", func(c convey.C) {
err := d.SetBagStatusCache(ctx, uid, status, expire)
c.Convey("Then err should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,46 @@
package dao
import (
"context"
"encoding/json"
"go-common/app/service/live/gift/model"
"go-common/library/database/sql"
"go-common/library/log"
)
var (
_getBagStatus = "SELECT id,uid,day,day_info FROM ugift_day_status WHERE uid = ? AND day = ? LIMIT 1"
_addDayBag = "INSERT INTO ugift_day_status (uid,day,day_info) VALUES (?,?,?)"
)
// GetDayBagStatus GetDayBagStatus
func (d *Dao) GetDayBagStatus(ctx context.Context, uid int64, date string) (res *model.DayGiftInfo, err error) {
log.Info("GetDayBagStatus,%d,%s", uid, date)
row := d.db.QueryRow(ctx, _getBagStatus, uid, date)
res = &model.DayGiftInfo{}
if err = row.Scan(&res.ID, &res.UID, &res.Day, &res.DayInfo); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("GetUserGiftBagStatus row.Scan error(%v)", err)
}
return
}
// AddDayBag AddDayBag
func (d *Dao) AddDayBag(ctx context.Context, uid int64, date string, dayInfo *model.BagGiftStatus) (affected int64, err error) {
log.Info("AddDayBag,%d,%s,%v", uid, date, dayInfo)
di, err := json.Marshal(dayInfo)
if err != nil {
return
}
res, err := d.db.Exec(ctx, _addDayBag, uid, date, di)
if err != nil {
log.Error("AddUserGiftBag error(%v)", err)
return
}
return res.LastInsertId()
}

View File

@@ -0,0 +1,49 @@
package dao
import (
"context"
"go-common/app/service/live/gift/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetDayBagStatus(t *testing.T) {
convey.Convey("GetDayBagStatus", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
date = ""
)
c.Convey("When everything gose positive", func(c convey.C) {
res, err := d.GetDayBagStatus(ctx, uid, date)
c.Convey("Then err should be nil.res should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAddDayBag(t *testing.T) {
convey.Convey("AddDayBag", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(9527)
date = "2018-07-04 00:00:00"
dayInfo = &model.BagGiftStatus{
Status: 1,
Gift: []*model.GiftInfo{
{GiftID: 1, GiftNum: 2, ExpireAt: "今天"}, {GiftID: 2, GiftNum: 3, ExpireAt: "今天"},
},
}
)
c.Convey("When everything gose positive", func(c convey.C) {
affected, err := d.AddDayBag(ctx, uid, date, dayInfo)
c.Convey("Then err should be nil.affected should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(affected, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,46 @@
package dao
import (
"context"
"encoding/json"
"go-common/app/service/live/gift/model"
"go-common/library/database/sql"
"go-common/library/log"
)
var (
_getWeekBagStatus = "SELECT id,uid,week,level,week_info FROM ugift_week_status WHERE uid = ? AND week = ? AND level =? ORDER BY ctime DESC LIMIT 1"
_addWeekBag = "INSERT INTO ugift_week_status (uid,week,level,week_info) VALUES (?,?,?,?)"
)
// GetWeekBagStatus GetWeekBagStatus
func (d *Dao) GetWeekBagStatus(ctx context.Context, uid int64, week int, level int64) (res *model.WeekGiftInfo, err error) {
log.Info("GetWeekBagStatus,uid:%d,week:%d,level:%d", uid, week, level)
row := d.db.QueryRow(ctx, _getWeekBagStatus, uid, week, level)
res = &model.WeekGiftInfo{}
if err = row.Scan(&res.ID, &res.UID, &res.Week, &res.Level, &res.WeekInfo); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("GetWeekBagStatus row.Scan error(%v)", err)
}
return
}
// AddWeekBag AddWeekBag
func (d *Dao) AddWeekBag(ctx context.Context, uid int64, week int, level int64, weekInfo *model.BagGiftStatus) (affected int64, err error) {
log.Info("AddWeekBag,uid:%d,week:%d,level:%d,weekInfo:%v", uid, week, level, weekInfo)
wi, err := json.Marshal(weekInfo)
if err != nil {
return
}
res, err := d.db.Exec(ctx, _addWeekBag, uid, week, level, wi)
if err != nil {
log.Error("AddUserGiftBag error(%v)", err)
return
}
return res.LastInsertId()
}

View File

@@ -0,0 +1,51 @@
package dao
import (
"context"
"go-common/app/service/live/gift/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetWeekBagStatus(t *testing.T) {
convey.Convey("GetWeekBagStatus", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
week = int(0)
level = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
res, err := d.GetWeekBagStatus(ctx, uid, week, level)
c.Convey("Then err should be nil.res should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAddWeekBag(t *testing.T) {
convey.Convey("AddWeekBag", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(9527)
week = int(1)
level = int64(1)
weekInfo = &model.BagGiftStatus{
Status: 1,
Gift: []*model.GiftInfo{
{GiftID: 1, GiftNum: 2, ExpireAt: "7天"}, {GiftID: 2, GiftNum: 3, ExpireAt: "7天"},
},
}
)
c.Convey("When everything gose positive", func(c convey.C) {
affected, err := d.AddWeekBag(ctx, uid, week, level, weekInfo)
c.Convey("Then err should be nil.affected should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(affected, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,102 @@
package dao
import (
"context"
"crypto/md5"
"encoding/hex"
"fmt"
"go-common/app/service/live/gift/model"
"go-common/library/database/sql"
"go-common/library/log"
"strconv"
"time"
)
var (
_getBag = "SELECT id,gift_num FROM user_gift_%s WHERE uid = ? AND gift_id = ? AND expireat = ? LIMIT 1"
_getBagByID = "SELECT id,gift_num FROM user_gift_%s WHERE id = ?"
_updateBagNum = "UPDATE user_gift_%s SET gift_num = gift_num + ? WHERE id = ?"
_insertBag = "INSERT INTO user_gift_%s (uid,gift_id,gift_num,expireat) VALUES (?,?,?,?)"
_getBagList = "SELECT id,uid,gift_id,gift_num,expireat FROM user_gift_%s WHERE uid = ? AND gift_num > 0 AND (expireat = 0 OR expireat > ?)"
)
// GetBag GetBag
func (d *Dao) GetBag(ctx context.Context, uid, giftID, expireAt int64) (res *model.BagInfo, err error) {
log.Info("GetBag,uid:%d,giftID:%d,expireAt:%d", uid, giftID, expireAt)
row := d.db.QueryRow(ctx, fmt.Sprintf(_getBag, getPostFix(uid)), uid, giftID, expireAt)
res = &model.BagInfo{}
if err = row.Scan(&res.ID, &res.GiftNum); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("GetBag row.Scan error(%v)", err)
}
return
}
// UpdateBagNum UpdateBagNum
func (d *Dao) UpdateBagNum(ctx context.Context, uid, id, num int64) (affected int64, err error) {
log.Info("UpdateBagNum,uid:%d,id:%d,num:%d", uid, id, num)
res, err := d.db.Exec(ctx, fmt.Sprintf(_updateBagNum, getPostFix(uid)), num, id)
if err != nil {
log.Error("UpdateBagNum error(%v)", err)
return
}
return res.RowsAffected()
}
// AddBag AddBag
func (d *Dao) AddBag(ctx context.Context, uid, giftID, giftNum, expireAt int64) (affected int64, err error) {
log.Info("AddBag,uid:%d,giftID:%d,giftNum:%d,expireAt:%d", uid, giftID, giftNum, expireAt)
res, err := d.db.Exec(ctx, fmt.Sprintf(_insertBag, getPostFix(uid)), uid, giftID, giftNum, expireAt)
if err != nil {
log.Error("AddBag error(%v)", err)
return
}
return res.LastInsertId()
}
// GetBagByID GetBagByID
func (d *Dao) GetBagByID(ctx context.Context, uid, id int64) (res *model.BagInfo, err error) {
log.Info("GetBagByID,uid:%d,id:%d", uid, id)
row := d.db.QueryRow(ctx, fmt.Sprintf(_getBagByID, getPostFix(uid)), id)
res = &model.BagInfo{}
if err = row.Scan(&res.ID, &res.GiftNum); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("GetBagByID row.Scan error(%v)", err)
}
return
}
func getPostFix(uid int64) string {
uidStr := strconv.Itoa(int(uid))
h := md5.New()
h.Write([]byte(uidStr))
md5Str := hex.EncodeToString(h.Sum(nil))
return md5Str[0:1]
}
// GetBagList GetBagList
func (d *Dao) GetBagList(ctx context.Context, uid int64) (list []*model.BagGiftList, err error) {
log.Info("GetBagList,uid:%d", uid)
curTime := time.Now().Unix()
rows, err := d.db.Query(ctx, fmt.Sprintf(_getBagList, getPostFix(uid)), uid, curTime)
if err != nil {
log.Error("GetBagGiftList error,err %v", err)
return
}
defer rows.Close()
for rows.Next() {
b := &model.BagGiftList{}
if err = rows.Scan(&b.ID, &b.UID, &b.GiftID, &b.GiftNum, &b.ExpireAt); err != nil {
log.Error("GetBagGiftList scan error,err %v", err)
return
}
list = append(list, b)
}
return
}

View File

@@ -0,0 +1,111 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetBag(t *testing.T) {
convey.Convey("GetBag", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
giftID = int64(0)
expireAt = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
res, err := d.GetBag(ctx, uid, giftID, expireAt)
c.Convey("Then err should be nil.res should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateBagNum(t *testing.T) {
convey.Convey("UpdateBagNum", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
id = int64(0)
num = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
affected, err := d.UpdateBagNum(ctx, uid, id, num)
c.Convey("Then err should be nil.affected should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAddBag(t *testing.T) {
convey.Convey("AddBag", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
giftID = int64(0)
giftNum = int64(0)
expireAt = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
affected, err := d.AddBag(ctx, uid, giftID, giftNum, expireAt)
c.Convey("Then err should be nil.affected should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(affected, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoGetBagByID(t *testing.T) {
convey.Convey("GetBagByID", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
id = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
res, err := d.GetBagByID(ctx, uid, id)
c.Convey("Then err should be nil.res should not be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestDaogetPostFix(t *testing.T) {
convey.Convey("getPostFix", t, func(c convey.C) {
var (
uid = int64(1)
)
c.Convey("When everything gose positive", func(c convey.C) {
p1 := getPostFix(uid)
c.Convey("Then p1 should not be nil.", func(c convey.C) {
c.So(p1, convey.ShouldNotBeNil)
c.So(p1, convey.ShouldEqual, "c")
})
})
})
}
func TestDaoGetBagList(t *testing.T) {
convey.Convey("GetBagList", t, func(c convey.C) {
var (
ctx = context.Background()
uid = int64(0)
)
c.Convey("When everything gose positive", func(c convey.C) {
list, err := d.GetBagList(ctx, uid)
c.Convey("Then err should be nil.list should be nil.", func(c convey.C) {
c.So(err, convey.ShouldBeNil)
c.So(list, convey.ShouldBeNil)
})
})
})
}