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,41 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"push.go",
"user.go",
],
importpath = "go-common/app/service/bbq/notice-service/internal/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/service/bbq/notice-service/api/v1:go_default_library",
"//app/service/bbq/notice-service/internal/conf:go_default_library",
"//app/service/bbq/push/api/grpc/v1:go_default_library",
"//library/cache/redis:go_default_library",
"//library/database/sql:go_default_library",
"//library/log:go_default_library",
"//library/net/rpc/warden: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,184 @@
package dao
import (
"context"
"fmt"
"go-common/app/service/bbq/notice-service/api/v1"
"go-common/app/service/bbq/notice-service/internal/conf"
push "go-common/app/service/bbq/push/api/grpc/v1"
"go-common/library/cache/redis"
xsql "go-common/library/database/sql"
"go-common/library/log"
"go-common/library/net/rpc/warden"
)
const (
_listSQL = "select id, mid, action_mid, svid, notice_type, title, text, jump_url, biz_type, biz_id, ctime from notice_%02d where mid = ? and notice_type = ? and id < ? order by id desc limit %d"
_insertSQL = "insert into notice_%02d (mid, action_mid, svid, notice_type, title, text, jump_url, biz_type, biz_id) values (?,?,?,?,?,?,?,?,?)"
_noticeLen = 10
_redisUnreadKey = "notice:unread:%d"
_redisExpireTime = 7776000 // 90days
)
// Dao dao
type Dao struct {
c *conf.Config
db *xsql.DB
redis *redis.Pool
pushClient push.PushClient
}
// New init mysql db
func New(c *conf.Config) (dao *Dao) {
dao = &Dao{
c: c,
db: xsql.NewMySQL(c.MySQL),
redis: redis.NewPool(c.Redis),
pushClient: newPushClient(c.GRPCClient["push"]),
}
return
}
// Close close the resource.
func (d *Dao) 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)
}
func getTableIndex(id int64) int64 {
return id % 100
}
// newPushClient .
func newPushClient(cfg *conf.GRPCClientConfig) push.PushClient {
cc, err := warden.NewClient(cfg.WardenConf).Dial(context.Background(), cfg.Addr)
if err != nil {
panic(err)
}
return push.NewPushClient(cc)
}
// ListNotices 获取通知列表
func (d *Dao) ListNotices(ctx context.Context, mid, cursorID int64, noticeType int32) (list []*v1.NoticeBase, err error) {
querySQL := fmt.Sprintf(_listSQL, getTableIndex(mid), _noticeLen)
log.V(1).Infov(ctx, log.KV("mid", mid), log.KV("mid", mid), log.KV("notice_type", noticeType), log.KV("cursor_id", cursorID), log.KV("sql", querySQL))
rows, err := d.db.Query(ctx, querySQL, mid, noticeType, cursorID)
if err != nil {
log.Errorv(ctx, log.KV("log", "query mysql notice list fail"), log.KV("sql", querySQL), log.KV("mid", mid), log.KV("biz_type", noticeType), log.KV("cursor_id", cursorID))
return
}
defer rows.Close()
for rows.Next() {
var notice v1.NoticeBase
if err = rows.Scan(&notice.Id, &notice.Mid, &notice.ActionMid, &notice.SvId, &notice.NoticeType, &notice.Title, &notice.Text, &notice.JumpUrl, &notice.BizType, &notice.BizId, &notice.NoticeTime); err != nil {
log.Errorv(ctx, log.KV("log", "scan mysql notice list fail"), log.KV("sql", querySQL), log.KV("mid", mid), log.KV("biz_type", noticeType), log.KV("mid", mid), log.KV("cursor_id", cursorID))
return
}
list = append(list, &notice)
}
// 只要用户读取数据,即清理未读数
conn := d.redis.Get(ctx)
defer conn.Close()
redisKey := fmt.Sprintf(_redisUnreadKey, mid)
if _, tmpErr := conn.Do("HSET", redisKey, noticeType, 0); tmpErr != nil {
log.Warnv(ctx, log.KV("log", "clear unread info redis fail: key="+redisKey))
}
log.V(1).Infov(ctx, log.KV("req_size", _noticeLen), log.KV("rsp_size", len(list)))
return
}
// CreateNotice 创建通知
func (d *Dao) CreateNotice(ctx context.Context, notice *v1.NoticeBase) (id int64, err error) {
querySQL := fmt.Sprintf(_insertSQL, getTableIndex(notice.Mid))
res, err := d.db.Exec(ctx, querySQL, notice.Mid, notice.ActionMid, notice.SvId, notice.NoticeType, notice.Title, notice.Text, notice.JumpUrl, notice.BizType, notice.BizId)
if err != nil {
log.Errorv(ctx, log.KV("log", "exec mysql fail: create notice"), log.KV("sql", querySQL))
return
}
id, _ = res.LastInsertId()
return
}
// IncreaseUnread 增加未读
func (d *Dao) IncreaseUnread(ctx context.Context, mid int64, noticeType int32, num int64) (err error) {
conn := d.redis.Get(ctx)
defer conn.Close()
redisKey := fmt.Sprintf(_redisUnreadKey, mid)
expireResult, _ := redis.Int(conn.Do("EXPIRE", redisKey, _redisExpireTime))
if expireResult == 0 {
log.Infov(ctx, log.KV("log", "expire fail: key="+redisKey))
}
_, err = conn.Do("HINCRBY", redisKey, noticeType, num)
if err != nil {
log.Errorv(ctx, log.KV("log", "HINCRBY notice unread fail: err="+err.Error()))
return
}
log.V(1).Infov(ctx, log.KV("log", "hincrby notice unread : key="+redisKey), log.KV("notice_type", noticeType), log.KV("num", num))
return
}
// ClearUnread 清理未读
func (d *Dao) ClearUnread(ctx context.Context, mid int64, noticeType int32) (err error) {
conn := d.redis.Get(ctx)
defer conn.Close()
redisKey := fmt.Sprintf(_redisUnreadKey, mid)
expireResult, _ := redis.Int(conn.Do("EXPIRE", redisKey, _redisExpireTime))
if expireResult == 0 {
log.Infov(ctx, log.KV("log", "expire fail and return: key="+redisKey))
return
}
_, err = conn.Do("HSET", redisKey, noticeType, 0)
if err != nil {
log.Errorv(ctx, log.KV("log", "HSET notice unread fail: err="+err.Error()))
return
}
log.V(1).Infov(ctx, log.KV("log", "HSET clear notice unread : key="+redisKey), log.KV("notice_type", noticeType))
// 清理推送用户
err = d.ClearPushActionMid(ctx, mid, noticeType)
if err != nil {
log.Errorv(ctx, log.KV("log", "ClearPushActionMid fail: err="+err.Error()))
return
}
return
}
// GetUnreadInfo 获取未读情况
func (d *Dao) GetUnreadInfo(ctx context.Context, mid int64) (list []*v1.UnreadItem, err error) {
redisKey := fmt.Sprintf(_redisUnreadKey, mid)
conn := d.redis.Get(ctx)
defer conn.Close()
expireResult, _ := redis.Int(conn.Do("EXPIRE", redisKey, _redisExpireTime))
if expireResult == 0 {
log.V(1).Infov(ctx, log.KV("log", "expire fail: key="+redisKey))
return
}
result, err := redis.Int64s(conn.Do("HMGET", redisKey, 1, 2, 3, 4))
if err != nil {
log.Errorv(ctx, log.KV("log", "hmget notice unread fail: err="+err.Error()))
return
}
for i, val := range result {
var item v1.UnreadItem
item.NoticeType = int32(i + 1)
item.UnreadNum = val
list = append(list, &item)
}
return
}

View File

@@ -0,0 +1,225 @@
package dao
import (
"context"
"database/sql"
"fmt"
"strconv"
"strings"
"time"
"go-common/app/service/bbq/notice-service/api/v1"
push "go-common/app/service/bbq/push/api/grpc/v1"
"go-common/library/cache/redis"
"go-common/library/log"
)
const (
_queryUserPushDev = "select `reg_id`, `sdk`, `platform` from `user_push_device` where `mid` = ? and `state` = 0 order by `last_login_time` desc limit 1;"
_queryUserName = "select `uname` from `user_base` where mid in %s;"
)
// PushNotice .
func (d *Dao) PushNotice(c context.Context, req *push.NotificationRequest) (result []*push.PushResult, err error) {
resp, err := d.pushClient.AsyncNotification(c, req)
if err != nil {
log.Errorv(c, log.KV("log", "push notification error"), log.KV("error", err))
return
}
result = resp.Result
for _, item := range result {
if item.Error != nil {
log.Errorv(c, log.KV("log", "push device notifiaction error"), log.KV("error", item.Error))
}
}
return
}
// PushMessage .
func (d *Dao) PushMessage(c context.Context, req *push.MessageRequest) (result []*push.PushResult, err error) {
resp, err := d.pushClient.AsyncMessage(c, req)
if err != nil {
log.Errorv(c, log.KV("log", "push message error"), log.KV("error", err))
return
}
result = resp.Result
for _, item := range result {
if item.Error != nil {
log.Errorv(c, log.KV("log", "push device message error"), log.KV("error", item.Error))
}
}
return nil, err
}
// FetchPushDev .
func (d *Dao) FetchPushDev(c context.Context, mid int64) (result *push.Device, err error) {
result = &push.Device{}
err = d.db.QueryRow(c, _queryUserPushDev, mid).Scan(&result.RegisterID, &result.SDK, &result.Platform)
if err == sql.ErrNoRows {
err = nil
log.Warnw(c, "log", "no row in push", "sql", _queryUserPushDev, "mid", mid)
}
return
}
// IncrDailyPushCount .
func (d *Dao) IncrDailyPushCount(c context.Context, mid int64) (count int, err error) {
dt := time.Now().Format("20060102")
key := fmt.Sprintf("bbq:push:u:%d:%s:count", mid, dt)
conn := d.redis.Get(c)
defer conn.Close()
count, err = redis.Int(conn.Do("INCR", key, 1))
if err != nil {
return
}
_, err = conn.Do("EXPIRE", 86400)
return
}
// IncrHourPushAction .
func (d *Dao) IncrHourPushAction(c context.Context, mid int64, noticeType int32, t int) (count int, err error) {
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:push:u:%d:%s:action:%d", mid, dt, noticeType)
conn := d.redis.Get(c)
defer conn.Close()
count, err = redis.Int(conn.Do("INCR", key))
if err != nil {
return
}
_, err = conn.Do("EXPIRE", t)
return
}
// ClearHourPushAction .
func (d *Dao) ClearHourPushAction(c context.Context, mid int64, noticeType int32) error {
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:push:u:%d:%s:action:%d", mid, dt, noticeType)
conn := d.redis.Get(c)
defer conn.Close()
_, err := conn.Do("DEL", key)
return err
}
// SetPushActionMid .
func (d *Dao) SetPushActionMid(c context.Context, mid int64, actionMid int64, noticeType int32) error {
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:v1:push:u:%d:%s:action:%d", mid, dt, noticeType)
conn := d.redis.Get(c)
defer conn.Close()
values, _ := redis.Values(conn.Do("HGETALL", key))
if len(values) >= 4 {
return nil
}
_, err := conn.Do("HMSET", key, actionMid, actionMid)
return err
}
// GetPushActionMid .
func (d *Dao) GetPushActionMid(c context.Context, mid int64, noticeType int32) ([]int64, error) {
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:v1:push:u:%d:%s:action:%d", mid, dt, noticeType)
conn := d.redis.Get(c)
defer conn.Close()
m, err := redis.Int64Map(conn.Do("HGETALL", key))
if err != nil {
return nil, err
}
result := make([]int64, 0)
for _, v := range m {
result = append(result, v)
}
return result, nil
}
// ClearPushActionMid .
func (d *Dao) ClearPushActionMid(c context.Context, mid int64, noticeType int32) error {
conn := d.redis.Get(c)
defer conn.Close()
dt := time.Now().Format("2006010215")
key := fmt.Sprintf("bbq:v1:push:u:%d:%s:action:%d", mid, dt, noticeType)
_, err := conn.Do("DEL", key)
return err
}
// GetUserName .
func (d *Dao) GetUserName(c context.Context, midList []int64, count int) (names []string, err error) {
mids := []string{}
list := midList
if len(midList) > count {
list = midList[:count]
}
for _, v := range list {
mids = append(mids, strconv.Itoa(int(v)))
}
where := "(" + strings.Join(mids, ",") + ")"
row, err := d.db.Query(c, fmt.Sprintf(_queryUserName, where))
if err != nil {
return
}
for row.Next() {
n := ""
err = row.Scan(&n)
if err != nil {
return
}
names = append(names, n)
}
return
}
// FetchUserPushDev .
func (d *Dao) FetchUserPushDev(c context.Context, mid int64, buvid string) (result *v1.UserPushDev, err error) {
querySQL := "select `id` from `user_push_device` where `mid` = ? and `buvid` = ?"
row := d.db.QueryRow(c, querySQL, mid, buvid)
result = &v1.UserPushDev{}
row.Scan(&result.Id)
return
}
// InsertUserPushDev .
func (d *Dao) InsertUserPushDev(c context.Context, req *v1.UserPushDev) (int64, error) {
_insertUserPushDev := "insert into `user_push_device` (`mid`, `reg_id`, `buvid`, `sdk`, `platform`) values (?, ?, ?, ?, ?);"
result, err := d.db.Exec(c, _insertUserPushDev, req.Mid, req.RegisterId, req.Buvid, req.Sdk, req.Platform)
if err != nil {
return 0, err
}
return result.LastInsertId()
}
// UpdateUserPushDev .
func (d *Dao) UpdateUserPushDev(c context.Context, req *v1.UserPushDev) (int64, error) {
_updateUserPushDev := "update `user_push_device` set `reg_id`=?,`sdk`=?,`platform`=?,`state`=?,`last_login_time`=now() where `mid`=? and `buvid`=?"
result, err := d.db.Exec(c, _updateUserPushDev, req.RegisterId, req.Sdk, req.Platform, req.State, req.Mid, req.Buvid)
if err != nil {
return 0, err
}
return result.RowsAffected()
}
// DeleteUserPushDev .
func (d *Dao) DeleteUserPushDev(c context.Context, req *v1.UserPushDev) (int64, error) {
_updateUserPushDev := "update `user_push_device` set `state`=1 where `mid`=? and `buvid`=?"
result, err := d.db.Exec(c, _updateUserPushDev, req.Mid, req.Buvid)
if err != nil {
return 0, err
}
return result.RowsAffected()
}

View File

@@ -0,0 +1,16 @@
package dao
import (
"context"
)
const (
_queryUserFansNum = "select `fan_total` from `user_statistics` where mid = ?;"
)
// FetchUserFansNum .
func (d *Dao) FetchUserFansNum(c context.Context, mid int64) (num int, err error) {
row := d.db.QueryRow(c, _queryUserFansNum, mid)
err = row.Scan(&num)
return
}