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,68 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"code.go",
"dao.go",
"http.go",
"memcache.go",
"mysql.go",
"remote.go",
],
importpath = "go-common/app/admin/main/coupon/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/coupon/conf:go_default_library",
"//app/admin/main/coupon/model:go_default_library",
"//app/service/openplatform/pgc-season/api/grpc/season/v1:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/elastic:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/http/blademaster: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 = [
"code_test.go",
"dao_test.go",
"http_test.go",
"memcache_test.go",
"mysql_test.go",
"remote_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/coupon/conf:go_default_library",
"//app/admin/main/coupon/model:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)

View File

@@ -0,0 +1,126 @@
package dao
import (
"bytes"
"context"
"fmt"
"go-common/app/admin/main/coupon/model"
"go-common/library/database/sql"
"github.com/pkg/errors"
)
const (
_codeCountSQL = "SELECT COUNT(1) FROM bilibili_coupon.coupon_code WHERE 1=1 %s"
_codeListSQL = "SELECT id,batch_token,state,code,mid,coupon_token,coupon_type,ver,ctime,mtime FROM coupon_code WHERE 1=1 %s %s"
_codeBlockSQL = "UPDATE coupon_code SET state = ?, ver = ver +1 WHERE id = ? AND ver =?;"
_codeByIDSQL = "SELECT id,batch_token,state,code,mid,coupon_token,coupon_type,ver,ctime,mtime FROM coupon_code WHERE id = ?"
_batchAddCodeSQL = "INSERT IGNORE INTO coupon_code(batch_token,state,code,coupon_type)VALUES "
)
// CountCode coupon code count.
func (d *Dao) CountCode(c context.Context, a *model.ArgCouponCode) (count int64, err error) {
sql := fmt.Sprintf(_codeCountSQL, whereSQL(a))
if err = d.db.QueryRow(c, sql).Scan(&count); err != nil {
err = errors.Wrapf(err, "dao code list")
}
return
}
// CodeList code list.
func (d *Dao) CodeList(c context.Context, a *model.ArgCouponCode) (res []*model.CouponCode, err error) {
listSQL := fmt.Sprintf(_codeListSQL, whereSQL(a), pageSQL(a.Pn, a.Ps))
var rows *sql.Rows
if rows, err = d.db.Query(c, listSQL); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.CouponCode)
if err = rows.Scan(&r.ID, &r.BatchToken, &r.State, &r.Code, &r.Mid, &r.CouponToken, &r.CouponType, &r.Ver, &r.Ctime, &r.Mtime); err != nil {
err = errors.WithStack(err)
res = nil
return
}
res = append(res, r)
}
err = rows.Err()
return
}
// UpdateCodeBlock update code block.
func (d *Dao) UpdateCodeBlock(c context.Context, a *model.CouponCode) (err error) {
if _, err = d.db.Exec(c, _codeBlockSQL, a.State, a.ID, a.Ver); err != nil {
err = errors.Wrapf(err, "dao update code block(%+v)", a)
}
return
}
// CodeByID code by id.
func (d *Dao) CodeByID(c context.Context, id int64) (r *model.CouponCode, err error) {
r = new(model.CouponCode)
if err = d.db.QueryRow(c, _codeByIDSQL, id).
Scan(&r.ID, &r.BatchToken, &r.State, &r.Code, &r.Mid, &r.CouponToken, &r.CouponType, &r.Ver, &r.Ctime, &r.Mtime); err != nil {
if err == sql.ErrNoRows {
r = nil
err = nil
return
}
err = errors.Wrapf(err, "dao query code by id")
}
return
}
// BatchAddCode batch add code.
func (d *Dao) BatchAddCode(c context.Context, cs []*model.CouponCode) (err error) {
var (
buf bytes.Buffer
sql string
)
buf.WriteString(_batchAddCodeSQL)
for _, v := range cs {
buf.WriteString("('")
buf.WriteString(v.BatchToken)
buf.WriteString("',")
buf.WriteString(fmt.Sprintf("%d", v.State))
buf.WriteString(",'")
buf.WriteString(v.Code)
buf.WriteString("',")
buf.WriteString(fmt.Sprintf("%d", v.CouponType))
buf.WriteString("),")
}
sql = buf.String()
if _, err = d.db.Exec(c, sql[0:len(sql)-1]); err != nil {
err = errors.Wrapf(err, "dao insert codes")
}
return
}
func whereSQL(a *model.ArgCouponCode) (sql string) {
if a == nil {
return
}
if a.Mid > 0 {
sql += " AND mid = " + fmt.Sprintf("%d", a.Mid)
}
if a.Code != "" {
sql += " AND code = '" + a.Code + "'"
}
if a.BatchToken != "" {
sql += " AND batch_token = '" + a.BatchToken + "'"
}
return sql
}
func pageSQL(pn, ps int) (sql string) {
if pn <= 0 {
pn = 1
}
if ps <= 0 {
ps = 20
}
return " ORDER BY ID DESC LIMIT " + fmt.Sprintf("%d,%d", (pn-1)*ps, ps)
}

View File

@@ -0,0 +1,125 @@
package dao
import (
"context"
"fmt"
"testing"
"time"
"go-common/app/admin/main/coupon/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoCountCode(t *testing.T) {
convey.Convey("CountCode", t, func(convCtx convey.C) {
var (
c = context.Background()
a = &model.ArgCouponCode{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
count, err := d.CountCode(c, a)
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 TestDaoCodeList(t *testing.T) {
convey.Convey("CodeList", t, func(convCtx convey.C) {
var (
c = context.Background()
a = &model.ArgCouponCode{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.CodeList(c, a)
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 TestDaoUpdateCodeBlock(t *testing.T) {
convey.Convey("UpdateCodeBlock", t, func(convCtx convey.C) {
var (
c = context.Background()
a = &model.CouponCode{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.UpdateCodeBlock(c, a)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoCodeByID(t *testing.T) {
convey.Convey("CodeByID", t, func(convCtx convey.C) {
var (
c = context.Background()
id = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
_, err := d.CodeByID(c, id)
convCtx.Convey("Then err should be nil.r should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoBatchAddCode(t *testing.T) {
convey.Convey("BatchAddCode", t, func(convCtx convey.C) {
var (
c = context.Background()
cs = []*model.CouponCode{
{
BatchToken: fmt.Sprintf("%d", time.Now().Unix()),
Code: fmt.Sprintf("%d", time.Now().Unix()),
CouponType: 3,
State: model.CodeStateNotUse,
},
}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.BatchAddCode(c, cs)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaowhereSQL(t *testing.T) {
convey.Convey("whereSQL", t, func(convCtx convey.C) {
var (
a = &model.ArgCouponCode{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
sql := whereSQL(a)
convCtx.Convey("Then sql should not be nil.", func(convCtx convey.C) {
convCtx.So(sql, convey.ShouldNotBeNil)
})
})
})
}
func TestDaopageSQL(t *testing.T) {
convey.Convey("pageSQL", t, func(convCtx convey.C) {
var (
pn = int(0)
ps = int(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
sql := pageSQL(pn, ps)
convCtx.Convey("Then sql should not be nil.", func(convCtx convey.C) {
convCtx.So(sql, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,51 @@
package dao
import (
"context"
"go-common/app/admin/main/coupon/conf"
seasongrpc "go-common/app/service/openplatform/pgc-season/api/grpc/season/v1"
"go-common/library/cache/memcache"
"go-common/library/database/elastic"
xsql "go-common/library/database/sql"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
)
// Dao dao
type Dao struct {
c *conf.Config
db *xsql.DB
es *elastic.Elastic
mc *memcache.Pool
// grpc
rpcClient seasongrpc.SeasonClient
client *bm.Client
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
db: xsql.NewMySQL(c.MySQL),
// es
es: elastic.NewElastic(nil),
mc: memcache.NewPool(c.Memcache.Config),
client: bm.NewClient(c.HTTPClient),
}
var err error
if dao.rpcClient, err = seasongrpc.NewClient(c.PGCRPC); err != nil {
log.Error("seasongrpc NewClientt error(%v)", err)
}
return
}
// Close close the resource.
func (dao *Dao) Close() {
dao.db.Close()
}
// Ping dao ping
func (dao *Dao) Ping(c context.Context) error {
return dao.db.Ping(c)
}

View File

@@ -0,0 +1,47 @@
package dao
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/admin/main/coupon/conf"
gock "gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.account.coupon-admin")
flag.Set("conf_appid", "main.account.coupon-admin")
flag.Set("conf_token", "9b7cd88bef452958c66efec00a334d4d")
flag.Set("tree_id", "38771")
flag.Set("conf_version", "docker-1")
flag.Set("deploy_env", "uat")
flag.Set("conf_env", "10")
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/coupon-admin.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
d.client.SetTransport(gock.DefaultTransport)
os.Exit(m.Run())
}
func httpMock(method, url string) *gock.Request {
r := gock.New(url)
r.Method = strings.ToUpper(method)
return r
}

View File

@@ -0,0 +1,32 @@
package dao
import (
"context"
"net/url"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
couponSystemNotify = "4"
couponMC = "10_99_1"
sendMessage = "/api/notify/send.user.notify.do"
)
//SendMessage send message.
func (d *Dao) SendMessage(c context.Context, mids, title, content, ip string) (err error) {
params := url.Values{}
params.Set("mc", couponMC)
params.Set("title", title)
params.Set("context", content)
params.Set("data_type", couponSystemNotify)
params.Set("mid_list", mids)
if err = d.client.Post(c, d.c.Prop.MessageURL+sendMessage, ip, params, nil); err != nil {
err = errors.WithStack(err)
}
log.Info("send message url:%+v params:%+v err:%+v", d.c.Prop.MessageURL+sendMessage, params, err)
return
}

View File

@@ -0,0 +1,29 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
gock "gopkg.in/h2non/gock.v1"
)
func TestDaoSendMessage(t *testing.T) {
convey.Convey("SendMessage", t, func(convCtx convey.C) {
var (
c = context.Background()
mids = ""
title = ""
content = ""
ip = ""
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
defer gock.OffAll()
httpMock("POST", sendMessage).Reply(200).JSON(`{"code":0}`)
err := d.SendMessage(c, mids, title, content, ip)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,81 @@
package dao
import (
"context"
"fmt"
gmc "go-common/library/cache/memcache"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
_prefixCouponAllowances = "cas:%d:%d"
_prefixCoupons = "cs:%d:%d"
_prefixGrantUnique = "gu:%s"
)
func couponAllowancesKey(mid int64, state int8) string {
return fmt.Sprintf(_prefixCouponAllowances, mid, state)
}
func couponsKey(mid int64, ct int8) string {
return fmt.Sprintf(_prefixCoupons, ct, mid)
}
func grantUnique(token string) string {
return fmt.Sprintf(_prefixGrantUnique, token)
}
// DelCouponAllowancesKey delete allowances cache.
func (d *Dao) DelCouponAllowancesKey(c context.Context, mid int64, state int8) (err error) {
return d.delCache(c, couponAllowancesKey(mid, state))
}
// DelCache del cache.
func (d *Dao) delCache(c context.Context, key string) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Delete(key); err != nil {
if err == gmc.ErrNotFound {
err = nil
} else {
err = errors.Wrapf(err, "mc.Delete(%s)", key)
}
}
return
}
//DelCouponTypeCache del coupon.
func (d *Dao) DelCouponTypeCache(c context.Context, mid int64, ct int8) (err error) {
return d.delCache(c, couponsKey(mid, ct))
}
//DelGrantUniqueLock del lock.
func (d *Dao) DelGrantUniqueLock(c context.Context, token string) (err error) {
return d.delCache(c, grantUnique(token))
}
// AddGrantUniqueLock add grant coupon use lock.
func (d *Dao) AddGrantUniqueLock(c context.Context, token string, seconds int32) (succeed bool) {
var (
key = grantUnique(token)
conn = d.mc.Get(c)
err error
)
defer conn.Close()
item := &gmc.Item{
Key: key,
Value: []byte("0"),
Expiration: seconds,
}
if err = conn.Add(item); err != nil {
if err != gmc.ErrNotStored {
log.Error("mc.Add(%s) error(%v)", key, err)
}
return
}
succeed = true
return
}

View File

@@ -0,0 +1,128 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaocouponAllowancesKey(t *testing.T) {
convey.Convey("couponAllowancesKey", t, func(convCtx convey.C) {
var (
mid = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := couponAllowancesKey(mid, 0)
convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaocouponsKey(t *testing.T) {
convey.Convey("couponsKey", t, func(convCtx convey.C) {
var (
mid = int64(0)
ct = int8(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := couponsKey(mid, ct)
convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaograntUnique(t *testing.T) {
convey.Convey("grantUnique", t, func(convCtx convey.C) {
var (
token = ""
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := grantUnique(token)
convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoDelCouponAllowancesKey(t *testing.T) {
convey.Convey("DelCouponAllowancesKey", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.DelCouponAllowancesKey(c, mid, 0)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaodelCache(t *testing.T) {
convey.Convey("delCache", t, func(convCtx convey.C) {
var (
c = context.Background()
key = "123"
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.delCache(c, key)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelCouponTypeCache(t *testing.T) {
convey.Convey("DelCouponTypeCache", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(0)
ct = int8(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.DelCouponTypeCache(c, mid, ct)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoDelGrantUniqueLock(t *testing.T) {
convey.Convey("DelGrantUniqueLock", t, func(convCtx convey.C) {
var (
c = context.Background()
token = ""
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.DelGrantUniqueLock(c, token)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoAddGrantUniqueLock(t *testing.T) {
convey.Convey("AddGrantUniqueLock", t, func(convCtx convey.C) {
var (
c = context.Background()
token = ""
seconds = int32(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
succeed := d.AddGrantUniqueLock(c, token, seconds)
convCtx.Convey("Then succeed should not be nil.", func(convCtx convey.C) {
convCtx.So(succeed, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,481 @@
package dao
import (
"bytes"
"context"
xsql "database/sql"
"fmt"
"strconv"
"strings"
"go-common/app/admin/main/coupon/model"
"go-common/library/database/sql"
"github.com/pkg/errors"
)
const (
_addbatch = "INSERT INTO coupon_batch_info(app_id,name,batch_token,max_count,current_count,start_time,expire_time,ver,ctime,limit_count,operator)VALUES(?,?,?,?,?,?,?,?,?,?,?);"
_addAllowancebatch = "INSERT INTO coupon_batch_info(app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,limit_count,operator,full_amount,amount,state,coupon_type,platform_limit,product_limit_month,product_limit_renewal)VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);"
_batchList = "SELECT id,app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,mtime,limit_count,operator,full_amount,amount,state,coupon_type,platform_limit,product_limit_month,product_limit_renewal FROM coupon_batch_info WHERE 1=1 "
_appAppInfoSQL = "SELECT id,name,app_key,notify_url,ctime,mtime FROM coupon_app_info;"
_updateAllowanceBatchSQL = "UPDATE coupon_batch_info SET app_id = ?,name = ?,max_count = ?,limit_count = ?,operator = ?,platform_limit = ?,product_limit_month = ?,product_limit_renewal = ? WHERE id = ?;"
_updateBatchStatusSQL = "UPDATE coupon_batch_info SET state = ?, operator = ? WHERE id = ?;"
_batchInfoSQL = "SELECT id,app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,mtime,limit_count,operator,full_amount,amount,state,coupon_type,platform_limit,product_limit_month,product_limit_renewal FROM coupon_batch_info WHERE batch_token = ? "
_batchInfoByIDSQL = "SELECT id,app_id,name,batch_token,max_count,current_count,start_time,expire_time,expire_day,ver,ctime,mtime,limit_count,operator,full_amount,amount,state,coupon_type,platform_limit,product_limit_month,product_limit_renewal FROM coupon_batch_info WHERE id = ? "
_updateAllowanceStateSQL = "UPDATE coupon_allowance_info_%02d SET state = ?,ver = ver+1 WHERE coupon_token = ? AND ver = ?;"
_couponAllowanceByTokenSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,ver,batch_token,order_no,amount,full_amount,ctime,mtime FROM coupon_allowance_info_%02d WHERE coupon_token = ?;"
_addCouponAllowanceChangeLogSQL = "INSERT INTO coupon_allowance_change_log_%02d (coupon_token,order_no,mid,state,ctime, change_type) VALUES(?,?,?,?,?,?);"
_couponAllowancePageSQL = "SELECT id,coupon_token,mid,state,start_time,expire_time,origin,ver,batch_token,order_no,amount,full_amount,ctime,mtime,remark FROM coupon_allowance_info_%02d WHERE mid = ? %s ORDER BY id DESC"
_batchAddAllowanceCouponSQL = "INSERT INTO coupon_allowance_info_%02d(coupon_token,mid,state,start_time,expire_time,origin,batch_token,amount,full_amount,ctime,app_id) VALUES "
_updateBatchSQL = "UPDATE coupon_batch_info SET current_count = current_count + ? WHERE batch_token = ?;"
_updateCodeBatchSQL = "UPDATE coupon_batch_info SET app_id = ?,name = ?,limit_count = ?,operator = ?,platform_limit = ?,product_limit_month = ?,product_limit_renewal = ? WHERE id = ?;"
//view
_addViewBatchSQL = "INSERT INTO coupon_batch_info(app_id,name,batch_token,max_count,current_count,start_time,expire_time,ver,limit_count,coupon_type,operator)VALUES(?,?,?,?,?,?,?,?,?,?,?)"
_updateViewBatchSQL = "UPDATE coupon_batch_info SET app_id=?,name=?,max_count=?,limit_count=?,operator = ?,ver=? WHERE id=?"
_updateViewSQL = "UPDATE coupon_info_%02d SET state=? WHERE coupon_token=?"
_searchViewSQL = "SELECT coupon_token,mid,batch_token,state,order_no,oid,start_time,expire_time,ctime,mtime FROM coupon_info_%02d WHERE 1=1"
_searchViewCountSQL = "SELECT COUNT(1) FROM coupon_info_%02d WHERE 1=1"
_viewInfoSQL = "SELECT coupon_token,mid,batch_token,state,order_no,oid,start_time,expire_time,ctime FROM coupon_info_%02d WHERE coupon_token=?"
_addViewChangeLog = "INSERT INTO coupon_change_log_%02d(coupon_token,mid,state) VALUES(?,?,?)"
)
// BeginTran begin transaction.
func (d *Dao) BeginTran(c context.Context) (*sql.Tx, error) {
return d.db.Begin(c)
}
func hitAllowanceInfo(mid int64) int64 {
return mid % 10
}
func hitAllowanceChangeLog(mid int64) int64 {
return mid % 10
}
func hitViewInfo(mid int64) int64 {
return mid % 100
}
// BatchList query batch list.
func (d *Dao) BatchList(c context.Context, appid int64, t int8) (res []*model.CouponBatchInfo, err error) {
var (
rows *sql.Rows
sql = _batchList
)
if appid != 0 {
sql += fmt.Sprintf(" AND `app_id` = %d", appid)
}
if t != 0 {
sql += fmt.Sprintf(" AND `coupon_type` = %d", t)
}
if rows, err = d.db.Query(c, sql); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
r := &model.CouponBatchInfo{}
if err = rows.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver,
&r.Ctime, &r.Mtime, &r.LimitCount, &r.Operator, &r.FullAmount, &r.Amount, &r.State, &r.CouponType, &r.PlatformLimit, &r.ProdLimMonth, &r.ProdLimRenewal); err != nil {
err = errors.WithStack(err)
res = nil
return
}
res = append(res, r)
}
err = rows.Err()
return
}
// BatchViewList query batch list.
func (d *Dao) BatchViewList(c context.Context, appid int64, batchToken string, t int8) (res []*model.CouponBatchInfo, err error) {
var (
rows *sql.Rows
sql = _batchList
)
if appid != 0 {
sql += fmt.Sprintf(" AND app_id = %d", appid)
}
if t != 0 {
sql += fmt.Sprintf(" AND coupon_type = %d", t)
}
if len(batchToken) > 0 {
sql += fmt.Sprintf(" AND batch_token='%v'", batchToken)
}
if rows, err = d.db.Query(c, sql); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
r := &model.CouponBatchInfo{}
if err = rows.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver,
&r.Ctime, &r.Mtime, &r.LimitCount, &r.Operator, &r.FullAmount, &r.Amount, &r.State, &r.CouponType, &r.PlatformLimit, &r.ProdLimMonth, &r.ProdLimRenewal); err != nil {
err = errors.WithStack(err)
res = nil
return
}
res = append(res, r)
}
err = rows.Err()
return
}
// AddBatchInfo add batch info.
func (d *Dao) AddBatchInfo(c context.Context, b *model.CouponBatchInfo) (a int64, err error) {
var res xsql.Result
if res, err = d.db.Exec(c, _addbatch, b.AppID, b.Name, b.BatchToken, b.MaxCount, b.CurrentCount, b.StartTime, b.ExpireTime,
b.Ver, b.Ctime, b.LimitCount, b.Operator); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
}
return
}
// AllAppInfo all app info.
func (d *Dao) AllAppInfo(c context.Context) (res []*model.AppInfo, err error) {
var rows *sql.Rows
if rows, err = d.db.Query(c, _appAppInfoSQL); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
r := &model.AppInfo{}
if err = rows.Scan(&r.ID, &r.Name, &r.Appkey, &r.NotifyURL, &r.Ctime, &r.Mtime); err != nil {
err = errors.WithStack(err)
res = nil
return
}
res = append(res, r)
}
err = rows.Err()
return
}
// AddAllowanceBatchInfo add allowance batch info.
func (d *Dao) AddAllowanceBatchInfo(c context.Context, b *model.CouponBatchInfo) (a int64, err error) {
var res xsql.Result
if res, err = d.db.Exec(c, _addAllowancebatch, b.AppID, b.Name, b.BatchToken, b.MaxCount, b.CurrentCount, b.StartTime, b.ExpireTime, b.ExpireDay,
b.Ver, b.Ctime, b.LimitCount, b.Operator, b.FullAmount, b.Amount, b.State, b.CouponType, b.PlatformLimit, b.ProdLimMonth, b.ProdLimRenewal); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
}
return
}
// UpdateAllowanceBatchInfo update allowance batch info.
func (d *Dao) UpdateAllowanceBatchInfo(c context.Context, b *model.CouponBatchInfo) (a int64, err error) {
var res xsql.Result
if res, err = d.db.Exec(c, _updateAllowanceBatchSQL, b.AppID, b.Name, b.MaxCount, b.LimitCount, b.Operator, b.PlatformLimit, b.ProdLimMonth, b.ProdLimRenewal, b.ID); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
}
return
}
// UpdateCodeBatchInfo update code batch info.
func (d *Dao) UpdateCodeBatchInfo(c context.Context, b *model.CouponBatchInfo) (a int64, err error) {
var res xsql.Result
if res, err = d.db.Exec(c, _updateCodeBatchSQL, b.AppID, b.Name, b.LimitCount, b.Operator, b.PlatformLimit, b.ProdLimMonth, b.ProdLimRenewal, b.ID); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
}
return
}
// UpdateBatchStatus update batch status.
func (d *Dao) UpdateBatchStatus(c context.Context, status int8, operator string, id int64) (a int64, err error) {
var res xsql.Result
if res, err = d.db.Exec(c, _updateBatchStatusSQL, status, operator, id); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
}
return
}
//BatchInfo batch info.
func (d *Dao) BatchInfo(c context.Context, token string) (r *model.CouponBatchInfo, err error) {
var row *sql.Row
r = new(model.CouponBatchInfo)
row = d.db.Master().QueryRow(c, _batchInfoSQL, token)
if err = row.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver,
&r.Ctime, &r.Mtime, &r.LimitCount, &r.Operator, &r.FullAmount, &r.Amount, &r.State, &r.CouponType, &r.PlatformLimit, &r.ProdLimMonth, &r.ProdLimRenewal); err != nil {
if err == sql.ErrNoRows {
err = nil
r = nil
return
}
err = errors.WithStack(err)
return
}
return
}
//BatchInfoByID batch info by id.
func (d *Dao) BatchInfoByID(c context.Context, id int64) (r *model.CouponBatchInfo, err error) {
var row *sql.Row
r = new(model.CouponBatchInfo)
row = d.db.QueryRow(c, _batchInfoByIDSQL, id)
if err = row.Scan(&r.ID, &r.AppID, &r.Name, &r.BatchToken, &r.MaxCount, &r.CurrentCount, &r.StartTime, &r.ExpireTime, &r.ExpireDay, &r.Ver,
&r.Ctime, &r.Mtime, &r.LimitCount, &r.Operator, &r.FullAmount, &r.Amount, &r.State, &r.CouponType, &r.PlatformLimit, &r.ProdLimMonth, &r.ProdLimRenewal); err != nil {
if err == sql.ErrNoRows {
err = nil
r = nil
return
}
err = errors.WithStack(err)
return
}
return
}
// UpdateAllowanceStatus update allowance status.
func (d *Dao) UpdateAllowanceStatus(c context.Context, tx *sql.Tx, state int8, mid int64, token string, ver int64) (a int64, err error) {
var res xsql.Result
if res, err = tx.Exec(fmt.Sprintf(_updateAllowanceStateSQL, hitAllowanceInfo(mid)), state, token, ver); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
}
return
}
// AllowanceByToken query coupon by token.
func (d *Dao) AllowanceByToken(c context.Context, mid int64, token string) (r *model.CouponAllowanceInfo, err error) {
var row *sql.Row
r = &model.CouponAllowanceInfo{}
row = d.db.QueryRow(c, fmt.Sprintf(_couponAllowanceByTokenSQL, hitAllowanceInfo(mid)), token)
if err = row.Scan(&r.ID, &r.CouponToken, &r.Mid, &r.State, &r.StartTime, &r.ExpireTime, &r.Origin, &r.Ver, &r.BatchToken,
&r.OrderNO, &r.Amount, &r.FullAmount, &r.CTime, &r.MTime); err != nil {
if err == sql.ErrNoRows {
err = nil
r = nil
return
}
err = errors.WithStack(err)
return
}
return
}
//InsertCouponAllowanceHistory insert coupon history .
func (d *Dao) InsertCouponAllowanceHistory(c context.Context, tx *sql.Tx, l *model.CouponAllowanceChangeLog) (a int64, err error) {
var res xsql.Result
if res, err = tx.Exec(fmt.Sprintf(_addCouponAllowanceChangeLogSQL, hitAllowanceChangeLog(l.Mid)), l.CouponToken, l.OrderNO, l.Mid, l.State, l.Ctime, l.ChangeType); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
}
return
}
//AllowanceList allowance list.
func (d *Dao) AllowanceList(c context.Context, arg *model.ArgAllowanceSearch) (res []*model.CouponAllowanceInfo, err error) {
var (
rows *sql.Rows
whereSQL = " "
)
if arg.AppID != 0 {
whereSQL += fmt.Sprintf(" AND `app_id` = %d ", arg.AppID)
}
if arg.CouponToken != "" {
whereSQL += fmt.Sprintf(" AND `coupon_token` = '%s' ", arg.CouponToken)
}
if arg.OrderNO != "" {
whereSQL += fmt.Sprintf(" AND `order_no` = '%s' ", arg.OrderNO)
}
if arg.BatchToken != "" {
whereSQL += fmt.Sprintf(" AND `batch_token` = '%s' ", arg.BatchToken)
}
if rows, err = d.db.Query(c, fmt.Sprintf(_couponAllowancePageSQL, hitAllowanceInfo(arg.Mid), whereSQL), arg.Mid); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.CouponAllowanceInfo)
if err = rows.Scan(&r.ID, &r.CouponToken, &r.Mid, &r.State, &r.StartTime, &r.ExpireTime, &r.Origin, &r.Ver, &r.BatchToken, &r.OrderNO, &r.Amount, &r.FullAmount,
&r.CTime, &r.MTime, &r.Remark); err != nil {
err = errors.WithStack(err)
res = nil
return
}
res = append(res, r)
}
err = rows.Err()
return
}
//AddViewBatch add view batch.
func (d *Dao) AddViewBatch(c context.Context, arg *model.ArgCouponViewBatch) (err error) {
if _, err = d.db.Exec(c, _addViewBatchSQL, arg.AppID, arg.Name, arg.BatchToken, arg.MaxCount, arg.CurrentCount, arg.StartTime, arg.ExpireTime, arg.Ver, arg.LimitCount, arg.CouponType, arg.Operator); err != nil {
err = errors.WithStack(err)
}
return
}
//UpdateViewBatch update viewBatch
func (d *Dao) UpdateViewBatch(c context.Context, arg *model.ArgCouponViewBatch) (err error) {
if _, err = d.db.Exec(c, _updateViewBatchSQL, arg.AppID, arg.Name, arg.MaxCount, arg.LimitCount, arg.Operator, arg.Ver, arg.ID); err != nil {
err = errors.WithStack(err)
}
return
}
//TxUpdateViewInfo update view info.
func (d *Dao) TxUpdateViewInfo(tx *sql.Tx, status int8, couponToken string, mid int64) (err error) {
if _, err = tx.Exec(fmt.Sprintf(_updateViewSQL, hitViewInfo(mid)), status, couponToken); err != nil {
err = errors.WithStack(err)
}
return
}
//TxCouponViewLog tx add view log.
func (d *Dao) TxCouponViewLog(tx *sql.Tx, arg *model.CouponChangeLog) (err error) {
if _, err = tx.Exec(fmt.Sprintf(_addViewChangeLog, hitViewInfo(arg.Mid)), arg.CouponToken, arg.Mid, arg.State); err != nil {
err = errors.WithStack(err)
}
return
}
//CouponViewInfo .
func (d *Dao) CouponViewInfo(c context.Context, couponToken string, mid int64) (r *model.CouponInfo, err error) {
row := d.db.QueryRow(c, fmt.Sprintf(_viewInfoSQL, hitViewInfo(mid)), couponToken)
r = new(model.CouponInfo)
if err = row.Scan(&r.CouponToken, &r.Mid, &r.BatchToken, &r.State, &r.OrderNo, &r.OID, &r.StartTime, &r.ExpireTime, &r.Ctime); err != nil {
err = errors.WithStack(err)
}
return
}
//SearchViewCouponCount search view count.
func (d *Dao) SearchViewCouponCount(c context.Context, arg *model.ArgSearchCouponView) (count int64, err error) {
whereSQL := fmt.Sprintf(_searchViewCountSQL, hitViewInfo(arg.Mid))
whereSQL += fmt.Sprintf(" AND mid=%v", arg.Mid)
if len(arg.CouponToken) > 0 {
whereSQL += fmt.Sprintf(" AND coupon_token='%v'", arg.CouponToken)
}
if len(arg.BatchTokens) > 0 {
whereSQL += fmt.Sprintf(" AND batch_token IN('%v')", strings.Join(arg.BatchTokens, "','"))
}
row := d.db.QueryRow(c, whereSQL)
if err = row.Scan(&count); err != nil {
err = errors.WithStack(err)
}
return
}
//SearchViewCouponInfo search view coupon info .
func (d *Dao) SearchViewCouponInfo(c context.Context, arg *model.ArgSearchCouponView) (res []*model.CouponInfo, err error) {
var rows *sql.Rows
whereSQL := fmt.Sprintf(_searchViewSQL, hitViewInfo(arg.Mid))
whereSQL += fmt.Sprintf(" AND mid=%v", arg.Mid)
if len(arg.CouponToken) > 0 {
whereSQL += fmt.Sprintf(" AND coupon_token='%v'", arg.CouponToken)
}
if len(arg.BatchTokens) > 0 {
whereSQL += fmt.Sprintf(" AND batch_token IN('%v')", strings.Join(arg.BatchTokens, "','"))
}
if arg.PN <= 0 {
arg.PN = 1
}
if arg.PS <= 0 || arg.PS >= 100 {
arg.PS = 20
}
whereSQL += fmt.Sprintf(" ORDER BY ID DESC LIMIT %v,%v", (arg.PN-1)*arg.PS, arg.PS)
if rows, err = d.db.Query(c, whereSQL); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
r := new(model.CouponInfo)
if err = rows.Scan(&r.CouponToken, &r.Mid, &r.BatchToken, &r.State, &r.OrderNo, &r.OID, &r.StartTime, &r.ExpireTime, &r.Ctime, &r.Mtime); err != nil {
err = errors.WithStack(err)
return
}
res = append(res, r)
}
err = rows.Err()
return
}
// BatchAddAllowanceCoupon batch add allowance coupon.
func (d *Dao) BatchAddAllowanceCoupon(c context.Context, tx *sql.Tx, cps []*model.CouponAllowanceInfo) (a int64, err error) {
var (
buf bytes.Buffer
res xsql.Result
sql string
)
buf.WriteString(fmt.Sprintf(_batchAddAllowanceCouponSQL, hitAllowanceInfo(cps[0].Mid)))
for _, v := range cps {
buf.WriteString("('")
buf.WriteString(v.CouponToken)
buf.WriteString("',")
buf.WriteString(strconv.FormatInt(v.Mid, 10))
buf.WriteString(",")
buf.WriteString(fmt.Sprintf("%d", v.State))
buf.WriteString(",")
buf.WriteString(strconv.FormatInt(v.StartTime, 10))
buf.WriteString(",")
buf.WriteString(strconv.FormatInt(v.ExpireTime, 10))
buf.WriteString(",")
buf.WriteString(fmt.Sprintf("%d", v.Origin))
buf.WriteString(",'")
buf.WriteString(v.BatchToken)
buf.WriteString("',")
buf.WriteString(fmt.Sprintf("%f", v.Amount))
buf.WriteString(",")
buf.WriteString(fmt.Sprintf("%f", v.FullAmount))
buf.WriteString(",'")
buf.WriteString(v.CTime.Time().Format("2006-01-02 15:04:05"))
buf.WriteString("',")
buf.WriteString(strconv.FormatInt(v.AppID, 10))
buf.WriteString("),")
}
sql = buf.String()
if res, err = tx.Exec(sql[0 : len(sql)-1]); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
}
return
}
// UpdateBatchInfo update batch info.
func (d *Dao) UpdateBatchInfo(c context.Context, tx *sql.Tx, token string, count int) (a int64, err error) {
var res xsql.Result
if res, err = tx.Exec(_updateBatchSQL, count, token); err != nil {
err = errors.WithStack(err)
return
}
if a, err = res.RowsAffected(); err != nil {
err = errors.WithStack(err)
return
}
return
}

View File

@@ -0,0 +1,484 @@
package dao
import (
"bytes"
"context"
"fmt"
"math/rand"
"time"
// xsql"database/sql"
"go-common/app/admin/main/coupon/model"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoBeginTran(t *testing.T) {
convey.Convey("BeginTran", t, func(convCtx convey.C) {
var (
c = context.Background()
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1, err := d.BeginTran(c)
convCtx.Convey("Then err should be nil.p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaohitAllowanceInfo(t *testing.T) {
convey.Convey("hitAllowanceInfo", t, func(convCtx convey.C) {
var (
mid = int64(1)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := hitAllowanceInfo(mid)
convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaohitAllowanceChangeLog(t *testing.T) {
convey.Convey("hitAllowanceChangeLog", t, func(convCtx convey.C) {
var (
mid = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := hitAllowanceChangeLog(mid)
convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestDaohitViewInfo(t *testing.T) {
convey.Convey("hitViewInfo", t, func(convCtx convey.C) {
var (
mid = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
p1 := hitViewInfo(mid)
convCtx.Convey("Then p1 should not be nil.", func(convCtx convey.C) {
convCtx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
// go test -test.v -test.run TestBatchList
func TestDaoBatchList(t *testing.T) {
convey.Convey("BatchList", t, func(convCtx convey.C) {
var (
c = context.Background()
appid = int64(1)
t = int8(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.BatchList(c, appid, t)
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 TestDaoBatchViewList(t *testing.T) {
convey.Convey("BatchViewList", t, func(convCtx convey.C) {
var (
c = context.Background()
appid = int64(0)
batchToken = ""
no = int8(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.BatchViewList(c, appid, batchToken, no)
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 TestDaoAddBatchInfo(t *testing.T) {
convey.Convey("AddBatchInfo", t, func(convCtx convey.C) {
var (
c = context.Background()
bi = &model.CouponBatchInfo{}
b bytes.Buffer
)
b.WriteString(fmt.Sprintf("%07d", rand.Int63n(9999999)))
b.WriteString(fmt.Sprintf("%03d", time.Now().UnixNano()/1e6%1000))
b.WriteString(time.Now().Format("20060102150405"))
bi.BatchToken = b.String()
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
a, err := d.AddBatchInfo(c, bi)
convCtx.Convey("Then err should be nil.a should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAllAppInfo(t *testing.T) {
convey.Convey("AllAppInfo", t, func(convCtx convey.C) {
var (
c = context.Background()
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.AllAppInfo(c)
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 TestDaoAddAllowanceBatchInfo(t *testing.T) {
convey.Convey("AddAllowanceBatchInfo", t, func(convCtx convey.C) {
var (
c = context.Background()
b bytes.Buffer
bi = &model.CouponBatchInfo{}
)
b.WriteString(fmt.Sprintf("%07d", rand.Int63n(9999999)))
b.WriteString(fmt.Sprintf("%03d", time.Now().UnixNano()/1e6%1000))
b.WriteString(time.Now().Format("20060102150405"))
bi.BatchToken = b.String()
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
a, err := d.AddAllowanceBatchInfo(c, bi)
convCtx.Convey("Then err should be nil.a should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateAllowanceBatchInfo(t *testing.T) {
convey.Convey("UpdateAllowanceBatchInfo", t, func(convCtx convey.C) {
var (
c = context.Background()
b = &model.CouponBatchInfo{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
a, err := d.UpdateAllowanceBatchInfo(c, b)
convCtx.Convey("Then err should be nil.a should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateBatchStatus(t *testing.T) {
convey.Convey("UpdateBatchStatus", t, func(convCtx convey.C) {
var (
c = context.Background()
status = int8(0)
operator = ""
id = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
a, err := d.UpdateBatchStatus(c, status, operator, id)
convCtx.Convey("Then err should be nil.a should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoBatchInfo(t *testing.T) {
convey.Convey("BatchInfo", t, func(convCtx convey.C) {
var (
c = context.Background()
token = ""
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
r, err := d.BatchInfo(c, token)
convCtx.Convey("Then err should be nil.r should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(r, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateAllowanceStatus(t *testing.T) {
convey.Convey("UpdateAllowanceStatus", t, func(convCtx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(context.Background())
state = int8(0)
mid = int64(0)
token = ""
ver = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
a, err := d.UpdateAllowanceStatus(c, tx, state, mid, token, ver)
if err == nil {
if err = tx.Commit(); err != nil {
tx.Rollback()
}
} else {
tx.Rollback()
}
convCtx.Convey("Then err should be nil.a should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAllowanceByToken(t *testing.T) {
convey.Convey("AllowanceByToken", t, func(convCtx convey.C) {
var (
c = context.Background()
mid = int64(13)
token = "000000119720180929180009"
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
r, err := d.AllowanceByToken(c, mid, token)
convCtx.Convey("Then err should be nil.r should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(r, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoInsertCouponAllowanceHistory(t *testing.T) {
convey.Convey("InsertCouponAllowanceHistory", t, func(convCtx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(context.Background())
l = &model.CouponAllowanceChangeLog{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
a, err := d.InsertCouponAllowanceHistory(c, tx, l)
if err == nil {
if err = tx.Commit(); err != nil {
tx.Rollback()
}
} else {
tx.Rollback()
}
convCtx.Convey("Then err should be nil.a should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoAllowanceList(t *testing.T) {
convey.Convey("AllowanceList", t, func(convCtx convey.C) {
var (
c = context.Background()
arg = &model.ArgAllowanceSearch{Mid: 3}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.AllowanceList(c, arg)
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 TestDaoAddViewBatch(t *testing.T) {
convey.Convey("AddViewBatch", t, func(convCtx convey.C) {
var (
c = context.Background()
arg = &model.ArgCouponViewBatch{}
b bytes.Buffer
)
b.WriteString(fmt.Sprintf("%07d", rand.Int63n(9999999)))
b.WriteString(fmt.Sprintf("%03d", time.Now().UnixNano()/1e6%1000))
b.WriteString(time.Now().Format("20060102150405"))
arg.BatchToken = b.String()
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.AddViewBatch(c, arg)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoUpdateViewBatch(t *testing.T) {
convey.Convey("UpdateViewBatch", t, func(convCtx convey.C) {
var (
c = context.Background()
arg = &model.ArgCouponViewBatch{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.UpdateViewBatch(c, arg)
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTxUpdateViewInfo(t *testing.T) {
convey.Convey("TxUpdateViewInfo", t, func(convCtx convey.C) {
var (
tx, _ = d.BeginTran(context.Background())
status = int8(0)
couponToken = ""
mid = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.TxUpdateViewInfo(tx, status, couponToken, mid)
if err == nil {
if err = tx.Commit(); err != nil {
tx.Rollback()
}
} else {
tx.Rollback()
}
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoTxCouponViewLog(t *testing.T) {
convey.Convey("TxCouponViewLog", t, func(convCtx convey.C) {
var (
tx, _ = d.BeginTran(context.Background())
arg = &model.CouponChangeLog{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
err := d.TxCouponViewLog(tx, arg)
if err == nil {
if err = tx.Commit(); err != nil {
tx.Rollback()
}
} else {
tx.Rollback()
}
convCtx.Convey("Then err should be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoCouponViewInfo(t *testing.T) {
convey.Convey("CouponViewInfo", t, func(convCtx convey.C) {
var (
c = context.Background()
couponToken = ""
mid = int64(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
r, err := d.CouponViewInfo(c, couponToken, mid)
convCtx.Convey("Then err should be nil.r should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(r, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSearchViewCouponCount(t *testing.T) {
convey.Convey("SearchViewCouponCount", t, func(convCtx convey.C) {
var (
c = context.Background()
arg = &model.ArgSearchCouponView{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
count, err := d.SearchViewCouponCount(c, arg)
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 TestDaoSearchViewCouponInfo(t *testing.T) {
convey.Convey("SearchViewCouponInfo", t, func(convCtx convey.C) {
var (
c = context.Background()
arg = &model.ArgSearchCouponView{}
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
res, err := d.SearchViewCouponInfo(c, arg)
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 TestDaoBatchAddAllowanceCoupon(t *testing.T) {
convey.Convey("BatchAddAllowanceCoupon", t, func(convCtx convey.C) {
var (
c = context.Background()
b bytes.Buffer
tx, _ = d.BeginTran(context.Background())
cps = []*model.CouponAllowanceInfo{}
)
b.WriteString(fmt.Sprintf("%05d", 1))
b.WriteString(fmt.Sprintf("%02d", rand.Int63n(99)))
b.WriteString(fmt.Sprintf("%03d", time.Now().UnixNano()/1e6%1000))
b.WriteString(time.Now().Format("20060102150405"))
cp := &model.CouponAllowanceInfo{CouponToken: b.String()}
cps = append(cps, cp)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
a, err := d.BatchAddAllowanceCoupon(c, tx, cps)
if err == nil {
if err = tx.Commit(); err != nil {
tx.Rollback()
}
} else {
tx.Rollback()
}
convCtx.Convey("Then err should be nil.a should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoUpdateBatchInfo(t *testing.T) {
convey.Convey("UpdateBatchInfo", t, func(convCtx convey.C) {
var (
c = context.Background()
tx, _ = d.BeginTran(context.Background())
token = ""
count = int(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
a, err := d.UpdateBatchInfo(c, tx, token, count)
if err == nil {
if err = tx.Commit(); err != nil {
tx.Rollback()
}
} else {
tx.Rollback()
}
convCtx.Convey("Then err should be nil.a should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
convCtx.So(a, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,32 @@
package dao
import (
"context"
"go-common/app/admin/main/coupon/model"
seasongrpc "go-common/app/service/openplatform/pgc-season/api/grpc/season/v1"
"github.com/pkg/errors"
)
//GetPGCInfo get pgc info.
func (d *Dao) GetPGCInfo(c context.Context, oid int32) (r *model.PGCInfoResq, err error) {
var (
params *seasongrpc.SeasonInfoReq
oids = make([]int32, 0)
reply *seasongrpc.CardsInfoReply
)
oids = append(oids, oid)
params = &seasongrpc.SeasonInfoReq{
SeasonIds: oids,
}
if reply, err = d.rpcClient.Cards(c, params); err != nil {
err = errors.WithStack(err)
return
}
if proto, success := reply.Cards[oid]; success {
r = new(model.PGCInfoResq)
r.Title = proto.Title
}
return
}

View File

@@ -0,0 +1,23 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoGetPGCInfo(t *testing.T) {
convey.Convey("GetPGCInfo", t, func(convCtx convey.C) {
var (
c = context.Background()
oid = int32(0)
)
convCtx.Convey("When everything goes positive", func(convCtx convey.C) {
_, err := d.GetPGCInfo(c, oid)
convCtx.Convey("Then err should be nil.r should not be nil.", func(convCtx convey.C) {
convCtx.So(err, convey.ShouldBeNil)
})
})
})
}