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,78 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"action_test.go",
"dao_test.go",
"extend_test.go",
"like_test.go",
"match_test.go",
"redis_test.go",
"subject_test.go",
"web_data_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/job/main/activity/conf:go_default_library",
"//app/job/main/activity/model/like:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
"//vendor/gopkg.in/h2non/gock.v1:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"action.go",
"dao.go",
"extend.go",
"like.go",
"match.go",
"redis.go",
"subject.go",
"web_data.go",
],
importpath = "go-common/app/job/main/activity/dao/like",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/activity/model:go_default_library",
"//app/job/main/activity/conf:go_default_library",
"//app/job/main/activity/model/like:go_default_library",
"//app/job/main/activity/model/match:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/elastic: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",
"@org_golang_x_net//context: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,71 @@
package like
import (
"context"
"database/sql"
"fmt"
"go-common/app/admin/main/activity/model"
"go-common/library/xstr"
"github.com/pkg/errors"
)
const (
_likeActSumSQL = "SELECT SUM(`action`) AS `like`,lid FROM like_action WHERE lid IN(%s) GROUP BY lid"
_likeActListSQL = "SELECT id,mid FROM like_action WHERE lid = ? AND id > ? ORDER BY id LIMIT ?"
)
// BatchLikeActSum .
func (d *Dao) BatchLikeActSum(c context.Context, lids []int64) (res map[int64]int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_likeActSumSQL, xstr.JoinInts(lids)))
if err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
err = errors.Wrap(err, "d.db.Query()")
}
return
}
defer rows.Close()
res = make(map[int64]int64)
for rows.Next() {
like := sql.NullInt64{}
lid := sql.NullInt64{}
if err = rows.Scan(&like, &lid); err != nil {
err = errors.Wrap(err, "rows.Scan()")
return
}
res[lid.Int64] = like.Int64
}
if err = rows.Err(); err != nil {
err = errors.Wrap(err, "rows.Err()")
}
return
}
// LikeActList .
func (d *Dao) LikeActList(c context.Context, lid, minID, limit int64) (res []*model.LikeAction, err error) {
rows, err := d.db.Query(c, _likeActListSQL, lid, minID, limit)
if err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
err = errors.Wrap(err, "d.db.Query()")
}
return
}
defer rows.Close()
for rows.Next() {
action := new(model.LikeAction)
if err = rows.Scan(&action.ID, &action.Mid); err != nil {
err = errors.Wrap(err, "rows.Scan()")
return
}
res = append(res, action)
}
if err = rows.Err(); err != nil {
err = errors.Wrap(err, "rows.Err()")
}
return
}

View File

@@ -0,0 +1,24 @@
package like
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestLikeBatchLikeActSum(t *testing.T) {
convey.Convey("BatchLikeActSum", t, func(ctx convey.C) {
var (
c = context.Background()
lids = []int64{13511, 13512, 13510}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.BatchLikeActSum(c, lids)
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,62 @@
package like
import (
"context"
"time"
"go-common/app/job/main/activity/conf"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
"go-common/library/database/elastic"
"go-common/library/database/sql"
"go-common/library/net/http/blademaster"
)
const _activity = "activity"
// Dao dao
type Dao struct {
db *sql.DB
subjectStmt *sql.Stmt
inOnlineLog *sql.Stmt
mcLike *memcache.Pool
mcLikeExpire int32
redis *redis.Pool
redisExpire int32
httpClient *blademaster.Client
es *elastic.Elastic
setObjStatURL string
setViewRankURL string
setLikeContentURL string
addLotteryTimesURL string
}
// New init
func New(c *conf.Config) (d *Dao) {
d = &Dao{
db: sql.NewMySQL(c.MySQL.Like),
mcLike: memcache.NewPool(c.Memcache.Like),
mcLikeExpire: int32(time.Duration(c.Memcache.LikeExpire) / time.Second),
redis: redis.NewPool(c.Redis.Config),
redisExpire: int32(time.Duration(c.Redis.Expire) / time.Second),
httpClient: blademaster.NewClient(c.HTTPClient),
es: elastic.NewElastic(c.Elastic),
setObjStatURL: c.Host.APICo + _setObjStatURI,
setViewRankURL: c.Host.APICo + _setViewRankURI,
setLikeContentURL: c.Host.APICo + _setLikeContentURI,
addLotteryTimesURL: c.Host.Activity + _addLotteryTimesURI,
}
d.subjectStmt = d.db.Prepared(_selSubjectSQL)
d.inOnlineLog = d.db.Prepared(_inOnlineLogSQL)
return
}
// Close close
func (d *Dao) Close() {
d.db.Close()
}
// Ping ping
func (d *Dao) Ping(c context.Context) error {
return d.db.Ping(c)
}

View File

@@ -0,0 +1,44 @@
package like
import (
"flag"
"os"
"strings"
"testing"
"go-common/app/job/main/activity/conf"
"gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.web-svr.activity-job")
flag.Set("conf_token", "7c164822b6da4198f6348599bedf1797")
flag.Set("tree_id", "2703")
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/activity-job-test.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
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,22 @@
package like
import (
"context"
"fmt"
"github.com/pkg/errors"
)
var (
_batchUpExtSQL = "INSERT INTO like_extend (`lid`,`like`) VALUES %s ON DUPLICATE KEY UPDATE `like`=values(`like`)"
)
// AddExtend .
func (dao *Dao) AddExtend(c context.Context, query string) (res int64, err error) {
rows, err := dao.db.Exec(c, fmt.Sprintf(_batchUpExtSQL, query))
if err != nil {
err = errors.Wrap(err, " dao.db.Exec()")
return
}
return rows.RowsAffected()
}

View File

@@ -0,0 +1,24 @@
package like
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestLikeAddExtend(t *testing.T) {
convey.Convey("AddExtend", t, func(ctx convey.C) {
var (
c = context.Background()
query = "(13511,100)"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.AddExtend(c, query)
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,136 @@
package like
import (
"context"
"database/sql"
"net/url"
"strconv"
"go-common/app/job/main/activity/model/like"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"go-common/library/xstr"
"github.com/pkg/errors"
)
const (
_selLikeSQL = "SELECT id,wid FROM likes WHERE state=1 AND sid=? ORDER BY type"
_likeListSQL = "SELECT id,wid FROM likes WHERE state= 1 AND sid = ? ORDER BY id LIMIT ?,?"
_likesCntSQL = "SELECT COUNT(1) AS cnt FROM likes WHERE state = 1 AND sid = ?"
_setObjStatURI = "/x/internal/activity/object/stat/set"
_setViewRankURI = "/x/internal/activity/view/rank/set"
_setLikeContentURI = "/x/internal/activity/like/content/set"
)
// Like get like by sid
func (d *Dao) Like(c context.Context, sid int64) (ns []*like.Like, err error) {
rows, err := d.db.Query(c, _selLikeSQL, sid)
if err != nil {
log.Error("notice.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
n := &like.Like{}
if err = rows.Scan(&n.ID, &n.Wid); err != nil {
log.Error("row.Scan error(%v)", err)
return
}
ns = append(ns, n)
}
return
}
// LikeList get like list by sid.
func (d *Dao) LikeList(c context.Context, sid int64, offset, limit int) (list []*like.Like, err error) {
rows, err := d.db.Query(c, _likeListSQL, sid, offset, limit)
if err != nil {
err = errors.Wrapf(err, "LikeList:d.db.Query(%d,%d,%d)", sid, offset, limit)
return
}
defer rows.Close()
for rows.Next() {
n := new(like.Like)
if err = rows.Scan(&n.ID, &n.Wid); err != nil {
err = errors.Wrapf(err, "LikeList:row.Scan row (%d,%d,%d)", sid, offset, limit)
return
}
list = append(list, n)
}
if err = rows.Err(); err != nil {
err = errors.Wrapf(err, "LikeList:rowsErr(%d,%d,%d)", sid, offset, limit)
}
return
}
// LikeCnt get like list total count by sid.
func (d *Dao) LikeCnt(c context.Context, sid int64) (count int, err error) {
row := d.db.QueryRow(c, _likesCntSQL, sid)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
err = errors.Wrapf(err, "LikeCnt:QueryRow(%d)", sid)
}
}
return
}
// SetObjectStat .
func (d *Dao) SetObjectStat(c context.Context, sid int64, stat *like.SubjectTotalStat, count int) (err error) {
params := url.Values{}
params.Set("sid", strconv.FormatInt(sid, 10))
params.Set("like", strconv.FormatInt(stat.SumLike, 10))
params.Set("view", strconv.FormatInt(stat.SumView, 10))
params.Set("fav", strconv.FormatInt(stat.SumFav, 10))
params.Set("coin", strconv.FormatInt(stat.SumCoin, 10))
params.Set("count", strconv.Itoa(count))
var res struct {
Code int `json:"code"`
}
if err = d.httpClient.Get(c, d.setObjStatURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
err = errors.Wrapf(err, "SetObjectStat(%d,%v)", sid, stat)
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrapf(ecode.Int(res.Code), "SetObjectStat Code(%d,%v)", sid, stat)
}
return
}
// SetViewRank set view rank list.
func (d *Dao) SetViewRank(c context.Context, sid int64, aids []int64) (err error) {
params := url.Values{}
params.Set("sid", strconv.FormatInt(sid, 10))
params.Set("aids", xstr.JoinInts(aids))
var res struct {
Code int `json:"code"`
}
if err = d.httpClient.Get(c, d.setViewRankURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
err = errors.Wrapf(err, "SetViewRank(%d,%v)", sid, aids)
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrapf(ecode.Int(res.Code), "SetViewRank Code(%d,%v)", sid, aids)
}
return
}
// SetLikeContent .
func (d *Dao) SetLikeContent(c context.Context, id int64) (err error) {
var res struct {
Code int `json:"code"`
}
params := url.Values{}
params.Set("lid", strconv.FormatInt(id, 10))
if err = d.httpClient.Get(c, d.setLikeContentURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
err = errors.Wrapf(err, "SetLikeContent(%d)", id)
return
}
if res.Code != ecode.OK.Code() {
err = errors.Wrapf(ecode.Int(res.Code), "SetLikeContent Code(%d)", id)
}
return
}

View File

@@ -0,0 +1,115 @@
package like
import (
"context"
"testing"
"go-common/app/job/main/activity/model/like"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestLikeLike(t *testing.T) {
convey.Convey("Like", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(10297)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
ns, err := d.Like(c, sid)
ctx.Convey("Then err should be nil.ns should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ns, convey.ShouldNotBeNil)
})
})
})
}
func TestLikeLikeList(t *testing.T) {
convey.Convey("LikeList", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(10297)
offset = int(1)
limit = int(10)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
list, err := d.LikeList(c, sid, offset, limit)
ctx.Convey("Then err should be nil.list should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(list, convey.ShouldNotBeNil)
})
})
})
}
func TestLikeLikeCnt(t *testing.T) {
convey.Convey("LikeCnt", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(10297)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
count, err := d.LikeCnt(c, sid)
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 TestLikeSetObjectStat(t *testing.T) {
convey.Convey("SetObjectStat", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(10297)
stat = &like.SubjectTotalStat{}
count = int(10)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.setObjStatURL).Reply(200).JSON(`{"code":0}`)
err := d.SetObjectStat(c, sid, stat, count)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestLikeSetViewRank(t *testing.T) {
convey.Convey("SetViewRank", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(10297)
aids = []int64{1, 2}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.setViewRankURL).Reply(200).JSON(`{"code":0}`)
err := d.SetViewRank(c, sid, aids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestLikeSetLikeContent(t *testing.T) {
convey.Convey("SetLikeContent", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.setLikeContentURL).Reply(200).JSON(`{"code":0}`)
err := d.SetLikeContent(c, id)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,58 @@
package like
import (
"context"
"database/sql"
"fmt"
"go-common/app/job/main/activity/model/match"
"go-common/library/log"
"go-common/library/xstr"
)
const (
_unDoMatchSQL = "SELECT mid,stake,result FROM act_match_user_log WHERE m_o_id = ? AND status = 0 LIMIT ?"
_upMatchUserSQL = "UPDATE act_match_user_log SET status = 1 WHERE m_o_id = ? AND mid IN (%s)"
_matchObjSQL = "SELECT id,match_id,sid,result FROM act_matchs_object WHERE status = 0 AND id = ?"
)
// UnDoMatchUsers un finish users.
func (d *Dao) UnDoMatchUsers(c context.Context, matchObjID int64, limit int) (list []*match.ActMatchUser, err error) {
rows, err := d.db.Query(c, _unDoMatchSQL, matchObjID, limit)
if err != nil {
log.Error("UnDoMatchUsers.Query error(%v)", err)
return
}
defer rows.Close()
for rows.Next() {
user := new(match.ActMatchUser)
if err = rows.Scan(&user.Mid, &user.Stake, &user.Result); err != nil {
log.Error("UnDoMatchUsers row.Scan error(%v)", err)
return
}
list = append(list, user)
}
return
}
// UpMatchUserResult update match user result.
func (d *Dao) UpMatchUserResult(c context.Context, matchObjID int64, mids []int64) (err error) {
if _, err = d.db.Exec(c, fmt.Sprintf(_upMatchUserSQL, xstr.JoinInts(mids)), matchObjID); err != nil {
log.Error("UpMatchUserResult d.db.Exec mids(%v) error(%v)", mids, err)
}
return
}
// MatchObjInfo get match object info from db.
func (d *Dao) MatchObjInfo(c context.Context, matchObjID int64) (data *match.ActMatchObj, err error) {
row := d.db.QueryRow(c, _matchObjSQL, matchObjID)
data = new(match.ActMatchObj)
if err = row.Scan(&data.ID, &data.MatchID, &data.SID, &data.Result); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
log.Error("MatchObjInfo row.Scan() error(%v)", err)
}
}
return
}

View File

@@ -0,0 +1,59 @@
package like
import (
"context"
"testing"
"fmt"
"github.com/smartystreets/goconvey/convey"
)
func TestLikeUnDoMatchUsers(t *testing.T) {
convey.Convey("UnDoMatchUsers", t, func(ctx convey.C) {
var (
c = context.Background()
matchObjID = int64(1)
limit = int(10)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
list, err := d.UnDoMatchUsers(c, matchObjID, limit)
ctx.Convey("Then err should be nil.list should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
fmt.Printf("%+v", list)
})
})
})
}
func TestLikeUpMatchUserResult(t *testing.T) {
convey.Convey("UpMatchUserResult", t, func(ctx convey.C) {
var (
c = context.Background()
matchObjID = int64(1)
mids = []int64{111}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.UpMatchUserResult(c, matchObjID, mids)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestLikeMatchObjInfo(t *testing.T) {
convey.Convey("MatchObjInfo", t, func(ctx convey.C) {
var (
c = context.Background()
matchObjID = int64(37)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
data, err := d.MatchObjInfo(c, matchObjID)
ctx.Convey("Then err should be nil.data should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(data, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,154 @@
package like
import (
"strconv"
"go-common/library/cache/redis"
"go-common/library/log"
"golang.org/x/net/context"
)
const (
_prefixAttention = "lg_"
)
func redisKey(key string) string {
return _prefixAttention + key
}
//RsSet set res
func (d *Dao) RsSet(c context.Context, key string, value string) (err error) {
var (
rkey = redisKey(key)
conn = d.redis.Get(c)
)
defer conn.Close()
if _, err = conn.Do("SET", rkey, value); err != nil {
log.Error("conn.Send(SET, %s, %s) error(%v)", rkey, value, err)
return
}
return
}
// RbSet setRb
func (d *Dao) RbSet(c context.Context, key string, value []byte) (err error) {
var (
rkey = redisKey(key)
conn = d.redis.Get(c)
)
defer conn.Close()
if _, err = conn.Do("SET", rkey, value); err != nil {
log.Error("conn.Send(SET, %s, %d) error(%v)", rkey, value, err)
return
}
return
}
// RsGet getRs
func (d *Dao) RsGet(c context.Context, key string) (res string, err error) {
var (
rkey = redisKey(key)
conn = d.redis.Get(c)
)
defer conn.Close()
if res, err = redis.String(conn.Do("GET", rkey)); err != nil {
if err == redis.ErrNil {
err = nil
} else {
log.Error("conn.Do(GET key(%s)) error(%v)", rkey, err)
}
return
}
return
}
// RsSetNX NXset get
func (d *Dao) RsSetNX(c context.Context, key string) (res bool, err error) {
var (
rkey = redisKey(key)
conn = d.redis.Get(c)
)
defer conn.Close()
if res, err = redis.Bool(conn.Do("SETNX", rkey, 1)); err != nil {
log.Error("conn.Do(SETNX key(%s)) error(%v)", rkey, err)
return
}
return
}
// Incr incr
func (d *Dao) Incr(c context.Context, key string) (res bool, err error) {
var (
rkey = redisKey(key)
conn = d.redis.Get(c)
)
defer conn.Close()
if res, err = redis.Bool(conn.Do("incr", rkey)); err != nil {
log.Error("conn.Do(incr key(%s)) error(%v)", rkey, err)
return
}
return
}
// CreateSelection Create selection
func (d *Dao) CreateSelection(c context.Context, aid int64, stage int64) (err error) {
key := strconv.FormatInt(aid, 10) + ":" + strconv.FormatInt(stage, 10)
var (
rkeyYes = redisKey(key + ":yes")
rkeyNo = redisKey(key + ":no")
conn = d.redis.Get(c)
)
defer conn.Close()
if err = conn.Send("SET", rkeyYes, 0); err != nil {
log.Error("conn.Send(SET %s) error(%v)", rkeyYes, err)
return
}
if err = conn.Send("SET", rkeyNo, 0); err != nil {
log.Error("conn.Send(SET %s) error(%v)", rkeyNo, 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
}
// Selection selection
func (d *Dao) Selection(c context.Context, aid int64, stage int64) (yes int64, no int64, err error) {
key := strconv.FormatInt(aid, 10) + ":" + strconv.FormatInt(stage, 10)
var (
rkeyYes = redisKey(key + ":yes")
rkeyNo = redisKey(key + ":no")
conn = d.redis.Get(c)
)
defer conn.Close()
if err = conn.Send("GET", rkeyYes); err != nil {
log.Error("conn.Send(SET %s) error(%v)", rkeyYes, err)
return
}
if err = conn.Send("GET", rkeyNo); err != nil {
log.Error("conn.Send(SET %s) error(%v)", rkeyNo, err)
return
}
if err = conn.Flush(); err != nil {
log.Error("conn.Flush() error(%v)", err)
return
}
if yes, err = redis.Int64(conn.Receive()); err != nil {
log.Error("conn.Receive(yes) error(%v)", err)
return
}
if no, err = redis.Int64(conn.Receive()); err != nil {
log.Error("conn.Receive(no) error(%v)", err)
return
}
return
}

View File

@@ -0,0 +1,136 @@
package like
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestLikeredisKey(t *testing.T) {
convey.Convey("redisKey", t, func(ctx convey.C) {
var (
key = "1"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := redisKey(key)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldNotBeNil)
})
})
})
}
func TestLikeRsSet(t *testing.T) {
convey.Convey("RsSet", t, func(ctx convey.C) {
var (
c = context.Background()
key = "111"
value = "1"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.RsSet(c, key, value)
ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestLikeRbSet(t *testing.T) {
convey.Convey("RbSet", t, func(ctx convey.C) {
var (
c = context.Background()
key = "111"
value = []byte("1")
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.RbSet(c, key, value)
ctx.Convey("Then err should be nil.ok should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestLikeRsGet(t *testing.T) {
convey.Convey("RsGet", t, func(ctx convey.C) {
var (
c = context.Background()
key = "1"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.RsGet(c, key)
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 TestLikeRsSetNX(t *testing.T) {
convey.Convey("RsSetNX", t, func(ctx convey.C) {
var (
c = context.Background()
key = "2"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.RsSetNX(c, key)
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 TestLikeIncr(t *testing.T) {
convey.Convey("Incr", t, func(ctx convey.C) {
var (
c = context.Background()
key = "1"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.Incr(c, key)
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 TestLikeCreateSelection(t *testing.T) {
convey.Convey("CreateSelection", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(1)
stage = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.CreateSelection(c, aid, stage)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestLikeSelection(t *testing.T) {
convey.Convey("Selection", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(1)
stage = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
yes, no, err := d.Selection(c, aid, stage)
ctx.Convey("Then err should be nil.yes,no should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(no, convey.ShouldNotBeNil)
ctx.So(yes, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,127 @@
package like
import (
"context"
"database/sql"
"fmt"
"net/url"
"strconv"
"time"
"go-common/app/job/main/activity/model/like"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
"go-common/library/xstr"
"github.com/pkg/errors"
)
const (
_selSubjectSQL = "SELECT s.id,s.name,s.dic,s.cover,s.stime,f.interval,f.ltime,f.tlimit FROM act_subject s INNER JOIN act_time_config f ON s.id=f.sid WHERE s.id=?"
_inOnlineLogSQL = "INSERT INTO act_online_vote_end_log(sid,aid,stage,yes,no) VALUES(?,?,?,?,?)"
_subjectsSQL = "SELECT id,name,dic,cover,stime,etime FROM act_subject WHERE state = 1 AND type IN (%s) AND stime <= ? AND etime>= ?"
_addLotteryTimesURI = "/matsuri/api/add/times"
)
// Subject subject
func (dao *Dao) Subject(c context.Context, sid int64) (n *like.Subject, err error) {
rows := dao.subjectStmt.QueryRow(c, sid)
n = &like.Subject{}
if err = rows.Scan(&n.ID, &n.Name, &n.Dic, &n.Cover, &n.Stime, &n.Interval, &n.Ltime, &n.Tlimit); err != nil {
if err == sql.ErrNoRows {
n = nil
err = nil
} else {
log.Error("row.Scan error(%v)", err)
}
return
}
return
}
// InOnlinelog InOnlinelog
func (dao *Dao) InOnlinelog(c context.Context, sid, aid, stage, yes, no int64) (rows int64, err error) {
rs, err := dao.inOnlineLog.Exec(c, sid, aid, stage, yes, no)
if err != nil {
log.Error("d.InOnlinelog.Exec(%d, %d, %d, %d, %d) error(%v)", sid, aid, stage, yes, no, err)
return
}
return rs.RowsAffected()
}
// SubjectList get online subject list by type.
func (dao *Dao) SubjectList(c context.Context, types []int64, ts time.Time) (res []*like.Subject, err error) {
rows, err := dao.db.Query(c, fmt.Sprintf(_subjectsSQL, xstr.JoinInts(types)), ts, ts)
if err != nil {
err = errors.Wrapf(err, "SubjectList:d.db.Query(%v,%d)", types, ts.Unix())
return
}
defer rows.Close()
for rows.Next() {
n := new(like.Subject)
if err = rows.Scan(&n.ID, &n.Name, &n.Dic, &n.Cover, &n.Stime, &n.Etime); err != nil {
err = errors.Wrapf(err, "SubjectList:row.Scan row (%v,%d)", types, ts.Unix())
return
}
res = append(res, n)
}
if err = rows.Err(); err != nil {
err = errors.Wrapf(err, "SubjectList:rowsErr(%v,%d)", types, ts.Unix())
}
return
}
// SubjectTotalStat total stat.
func (dao *Dao) SubjectTotalStat(c context.Context, sid int64) (rs *like.SubjectTotalStat, err error) {
req := dao.es.NewRequest(_activity).Index(_activity).WhereEq("state", 1).WhereEq("sid", sid).Sum("click").Sum("likes").Sum("fav").Sum("coin")
res := new(struct {
Result struct {
SumCoin []struct {
Value float64 `json:"value"`
} `json:"sum_coin"`
SumFav []struct {
Value float64 `json:"value"`
} `json:"sum_fav"`
SumLikes []struct {
Value float64 `json:"value"`
} `json:"sum_likes"`
SumClick []struct {
Value float64 `json:"value"`
} `json:"sum_click"`
}
Page struct {
Total int `json:"total"`
}
})
if err = req.Scan(c, &res); err != nil || res == nil {
log.Error("SearchArc req.Scan error(%v)", err)
return
}
rs = &like.SubjectTotalStat{
SumCoin: int64(res.Result.SumCoin[0].Value),
SumFav: int64(res.Result.SumFav[0].Value),
SumLike: int64(res.Result.SumLikes[0].Value),
SumView: int64(res.Result.SumClick[0].Value),
Count: res.Page.Total,
}
return
}
// AddLotteryTimes .
func (dao *Dao) AddLotteryTimes(c context.Context, sid, mid int64) (err error) {
params := url.Values{}
params.Set("act_id", strconv.FormatInt(sid, 10))
params.Set("mid", strconv.FormatInt(mid, 10))
var res struct {
Code int `json:"code"`
}
if err = dao.httpClient.Get(c, dao.addLotteryTimesURL, metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
err = errors.Wrapf(err, "dao.client.Get(%s)", dao.addLotteryTimesURL+"?"+params.Encode())
return
}
if res.Code != ecode.OK.Code() {
err = ecode.Int(res.Code)
}
return
}

View File

@@ -0,0 +1,99 @@
package like
import (
"context"
"testing"
"time"
"github.com/smartystreets/goconvey/convey"
"gopkg.in/h2non/gock.v1"
)
func TestLikeSubject(t *testing.T) {
convey.Convey("Subject", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(10193)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
n, err := d.Subject(c, sid)
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 TestLikeInOnlinelog(t *testing.T) {
convey.Convey("InOnlinelog", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(10193)
aid = int64(1)
stage = int64(1)
yes = int64(1)
no = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
rows, err := d.InOnlinelog(c, sid, aid, stage, yes, no)
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 TestLikeSubjectList(t *testing.T) {
convey.Convey("SubjectList", t, func(ctx convey.C) {
var (
c = context.Background()
types = []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16, 17, 18, 19}
ts = time.Now()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.SubjectList(c, types, ts)
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 TestLikeSubjectTotalStat(t *testing.T) {
convey.Convey("SubjectTotalStat", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(10338)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
rs, err := d.SubjectTotalStat(c, sid)
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 TestLikeAddLotteryTimes(t *testing.T) {
convey.Convey("AddLotteryTimes", t, func(ctx convey.C) {
var (
c = context.Background()
sid = int64(315)
mid = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.addLotteryTimesURL).Reply(200).SetHeaders(map[string]string{
"Code": "0",
})
err := d.AddLotteryTimes(c, sid, mid)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,50 @@
package like
import (
"context"
"database/sql"
"go-common/app/job/main/activity/model/like"
"github.com/pkg/errors"
)
const (
_webDataCntSQL = "SELECT COUNT(1) AS cnt FROM act_web_data WHERE state = 1 AND vid = ?"
_webDataListSQL = "SELECT id,vid,data FROM act_web_data WHERE state= 1 AND vid = ? ORDER BY id LIMIT ?,?"
)
// WebDataCnt get web data count.
func (d *Dao) WebDataCnt(c context.Context, vid int64) (count int, err error) {
row := d.db.QueryRow(c, _webDataCntSQL, vid)
if err = row.Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
} else {
err = errors.Wrapf(err, "WebDataCnt:QueryRow(%d)", vid)
}
}
return
}
// WebDataList get web data list by vid.
func (d *Dao) WebDataList(c context.Context, vid int64, offset, limit int) (list []*like.WebData, err error) {
rows, err := d.db.Query(c, _webDataListSQL, vid, offset, limit)
if err != nil {
err = errors.Wrapf(err, "WebDataList:d.db.Query(%d,%d,%d)", vid, offset, limit)
return
}
defer rows.Close()
for rows.Next() {
n := new(like.WebData)
if err = rows.Scan(&n.ID, &n.Vid, &n.Data); err != nil {
err = errors.Wrapf(err, "WebDataList:row.Scan row (%d,%d,%d)", vid, offset, limit)
return
}
list = append(list, n)
}
if err = rows.Err(); err != nil {
err = errors.Wrapf(err, "LikeList:rowsErr(%d,%d,%d)", vid, offset, limit)
}
return
}

View File

@@ -0,0 +1,41 @@
package like
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestLikeWebDataCnt(t *testing.T) {
convey.Convey("WebDataCnt", t, func(ctx convey.C) {
var (
c = context.Background()
vid = int64(0)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
_, err := d.WebDataCnt(c, vid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestLikeWebDataList(t *testing.T) {
convey.Convey("WebDataList", t, func(ctx convey.C) {
var (
c = context.Background()
vid = int64(36)
offset = int(1)
limit = int(10)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
list, err := d.WebDataList(c, vid, offset, limit)
ctx.Convey("Then err should be nil.list should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(list, convey.ShouldNotBeNil)
})
})
})
}