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,61 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["dao_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/block/conf:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"http.go",
"mc.go",
"mysql.go",
"rpc.go",
],
importpath = "go-common/app/admin/main/block/dao",
tags = ["automanaged"],
deps = [
"//app/admin/main/block/conf:go_default_library",
"//app/admin/main/block/model:go_default_library",
"//app/service/main/account/model:go_default_library",
"//app/service/main/account/rpc/client:go_default_library",
"//app/service/main/figure/model:go_default_library",
"//app/service/main/figure/rpc/client:go_default_library",
"//app/service/main/spy/model:go_default_library",
"//app/service/main/spy/rpc/client:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode: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"],
)

View File

@@ -0,0 +1,60 @@
package dao
import (
"context"
"go-common/app/admin/main/block/conf"
rpcaccount "go-common/app/service/main/account/rpc/client"
rpcfigure "go-common/app/service/main/figure/rpc/client"
rpcspy "go-common/app/service/main/spy/rpc/client"
"go-common/library/cache/memcache"
xsql "go-common/library/database/sql"
bm "go-common/library/net/http/blademaster"
"github.com/pkg/errors"
)
// Dao .
type Dao struct {
mc *memcache.Pool
db *xsql.DB
httpClient *bm.Client
spyRPC *rpcspy.Service
figureRPC *rpcfigure.Service
accountRPC *rpcaccount.Service3
}
// New init mysql db
func New() (dao *Dao) {
dao = &Dao{
mc: memcache.NewPool(conf.Conf.Memcache),
db: xsql.NewMySQL(conf.Conf.MySQL),
httpClient: bm.NewClient(conf.Conf.HTTPClient),
spyRPC: rpcspy.New(conf.Conf.RPCClients.Spy),
figureRPC: rpcfigure.New(conf.Conf.RPCClients.Figure),
accountRPC: rpcaccount.New3(conf.Conf.RPCClients.Account),
}
return
}
// BeginTX .
func (d *Dao) BeginTX(c context.Context) (tx *xsql.Tx, err error) {
if tx, err = d.db.Begin(c); err != nil {
err = errors.WithStack(err)
}
return
}
// Close close the resource.
func (d *Dao) Close() {
d.mc.Close()
d.db.Close()
}
// Ping dao ping
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.db.Ping(c); err != nil {
return
}
return
}

View File

@@ -0,0 +1,84 @@
package dao
import (
"context"
"flag"
"os"
"testing"
"go-common/app/admin/main/block/conf"
. "github.com/smartystreets/goconvey/convey"
)
var (
dao *Dao
ctx = context.TODO()
)
func TestMain(m *testing.M) {
defer os.Exit(0)
flag.Set("conf", "../cmd/block-admin-test.toml")
var err error
if err = conf.Init(); err != nil {
panic(err)
}
dao = New()
defer dao.Close()
m.Run()
}
func TestDB(t *testing.T) {
Convey("db", t, func() {
err := dao.SendSysMsg(ctx, "123", []int64{1, 2, 3}, "test title", "test content", "")
So(err, ShouldBeNil)
_, err = dao.HistoryCount(ctx, 46333)
So(err, ShouldBeNil)
_, err = dao.History(ctx, 46333, 0, 100)
So(err, ShouldBeNil)
_, err = dao.User(ctx, 46333)
So(err, ShouldBeNil)
_, err = dao.Users(ctx, []int64{46333})
So(err, ShouldBeNil)
_, err = dao.UserDetails(ctx, []int64{46333})
So(err, ShouldBeNil)
})
}
func TestRPC(t *testing.T) {
Convey("rpc", t, func() {
mid := int64(46333)
_, err := dao.SpyScore(ctx, mid)
So(err, ShouldBeNil)
_, err = dao.FigureRank(ctx, mid)
So(err, ShouldBeNil)
_, _, _, _, err = dao.AccountInfo(ctx, mid)
So(err, ShouldBeNil)
})
}
func TestTool(t *testing.T) {
Convey("tool", t, func() {
var (
mids = []int64{1, 2, 3, 46333, 35858}
)
str := midsToParam(mids)
So(str, ShouldEqual, "1,2,3,46333,35858")
})
}
func TestHTTP(t *testing.T) {
Convey("http", t, func() {
var (
mid int64 = 46333
telInfo string
mailInfo string
err error
)
telInfo, err = dao.TelInfo(ctx, mid)
So(err, ShouldBeNil)
mailInfo, err = dao.MailInfo(ctx, mid)
So(err, ShouldBeNil)
t.Logf("telinfo : %s , mailinfo : %s", telInfo, mailInfo)
})
}

View File

@@ -0,0 +1,202 @@
package dao
import (
"context"
"fmt"
"net/url"
"strings"
"time"
"go-common/app/admin/main/block/conf"
"go-common/app/admin/main/block/model"
"go-common/library/ecode"
"github.com/pkg/errors"
)
// BlackhouseBlock .
func (d *Dao) BlackhouseBlock(c context.Context, p *model.ParamBatchBlock) (err error) {
midStrs := make([]string, len(p.MIDs))
for i, mid := range p.MIDs {
midStrs[i] = fmt.Sprintf("%d", mid)
}
params := url.Values{}
params.Set("mids", strings.Join(midStrs, ","))
params.Set("oper_id", fmt.Sprintf("%d", p.AdminID))
params.Set("operator_name", p.AdminName)
params.Set("blocked_days", fmt.Sprintf("%d", p.Duration))
switch p.Action {
case model.BlockActionForever:
params.Set("blocked_forever", "1")
params.Set("punish_type", "3")
default:
params.Set("blocked_forever", "0")
params.Set("punish_type", "2")
}
params.Set("blocked_remark", p.Comment)
params.Set("origin_type", fmt.Sprintf("%d", p.Area))
params.Set("punish_time", fmt.Sprintf("%d", time.Now().Unix()))
params.Set("reason_type", fmt.Sprintf("%d", parseReasonType(p.Reason)))
var res struct {
Code int `json:"code"`
}
if err = d.httpClient.Post(c, conf.Conf.Property.BlackHouseURL, "", params, &res); err != nil {
err = errors.WithStack(err)
return
}
if res.Code != 0 {
err = errors.WithStack(ecode.Int(res.Code))
return
}
return
}
func parseReasonType(msg string) (t int) {
switch msg {
case "刷屏":
t = 1
case "抢楼":
t = 2
case "发布色情低俗信息":
t = 3
case "发布赌博诈骗信息":
t = 4
case "发布违禁相关信息", "发布违禁信息":
t = 5
case "发布垃圾广告信息":
t = 6
case "发布人身攻击言论":
t = 7
case "发布侵犯他人隐私信息":
t = 8
case "发布引战言论":
t = 9
case "发布剧透信息":
t = 10
case "恶意添加无关标签":
t = 11
case "恶意删除他人标签":
t = 12
case "发布色情信息":
t = 13
case "发布低俗信息":
t = 14
case "发布暴力血腥信息":
t = 15
case "涉及恶意投稿行为":
t = 16
case "发布非法网站信息":
t = 17
case "发布传播不实信息":
t = 18
case "发布怂恿教唆信息":
t = 19
case "恶意刷屏":
t = 20
case "账号违规":
t = 21
case "恶意抄袭":
t = 22
case "冒充自制原创":
t = 23
case "发布青少年不良内容":
t = 24
case "破坏网络安全":
t = 25
case "发布虚假误导信息":
t = 26
case "仿冒官方认证账号":
t = 27
case "发布不适宜内容":
t = 28
case "违反运营规则":
t = 29
case "恶意创建话题":
t = 30
case "发布违规抽奖":
t = 31
default:
t = 0
}
return
}
// SendSysMsg send sys msg.
func (d *Dao) SendSysMsg(c context.Context, code string, mids []int64, title string, content string, remoteIP string) (err error) {
params := url.Values{}
params.Set("mc", code)
params.Set("title", title)
params.Set("data_type", "4")
params.Set("context", content)
params.Set("mid_list", midsToParam(mids))
var res struct {
Code int `json:"code"`
Data *struct {
Status int8 `json:"status"`
Remark string `json:"remark"`
} `json:"data"`
}
if err = d.httpClient.Post(c, conf.Conf.Property.MSGURL, remoteIP, params, &res); err != nil {
err = errors.WithStack(err)
return
}
if res.Code != 0 {
err = errors.WithStack(ecode.Int(res.Code))
return
}
return
}
func midsToParam(mids []int64) (str string) {
strs := make([]string, 0, len(mids))
for _, mid := range mids {
strs = append(strs, fmt.Sprintf("%d", mid))
}
return strings.Join(strs, ",")
}
// TelInfo tel info.
func (d *Dao) TelInfo(c context.Context, mid int64) (tel string, err error) {
params := url.Values{}
params.Set("mid", fmt.Sprintf("%d", mid))
var resp struct {
Code int `json:"code"`
Data struct {
Mid int64 `json:"mid"`
Tel string `json:"tel"`
JoinIP string `json:"join_ip"`
JoinTime int64 `json:"join_time"`
} `json:"data"`
}
if err = d.httpClient.Get(c, conf.Conf.Property.TelURL, "", params, &resp); err != nil {
err = errors.Wrapf(err, "telinfo : %d", mid)
return
}
if resp.Code != 0 {
err = errors.Errorf("telinfo url(%s) res(%+v) err(%+v)", conf.Conf.Property.TelURL+"?"+params.Encode(), resp, ecode.Int(resp.Code))
return
}
tel = resp.Data.Tel
return
}
// MailInfo .
func (d *Dao) MailInfo(c context.Context, mid int64) (mail string, err error) {
params := url.Values{}
params.Set("mid", fmt.Sprintf("%d", mid))
var resp struct {
Code int `json:"code"`
Data string `json:"data"`
}
if err = d.httpClient.Get(c, conf.Conf.Property.MailURL, "", params, &resp); err != nil {
err = errors.Wrapf(err, "mailinfo : %d", mid)
return
}
if resp.Code != 0 {
err = errors.Errorf("mailinfo url(%s) res(%+v) err(%+v)", conf.Conf.Property.MailURL+"?"+params.Encode(), resp, ecode.Int(resp.Code))
return
}
mail = resp.Data
return
}

View File

@@ -0,0 +1,56 @@
package dao
import (
"context"
"fmt"
"github.com/pkg/errors"
)
func userKey(mid int64) (key string) {
key = fmt.Sprintf("u_%d", mid)
return
}
// UserCache .
// func (d *Dao) UserCache(c context.Context, mid int64) (info *model.MCBlockInfo, err error) {
// var (
// key = userKey(mid)
// conn = d.mc.Get(c)
// item *memcache.Item
// )
// defer conn.Close()
// if item, err = conn.Get(key); err != nil {
// if err == memcache.ErrNotFound {
// err = nil
// return
// }
// err = errors.WithStack(err)
// return
// }
// info = &model.MCBlockInfo{}
// if err = conn.Scan(item, info); err != nil {
// if err == memcache.ErrNotFound {
// info = nil
// err = nil
// return
// }
// err = errors.WithStack(err)
// return
// }
// return
// }
// DeleteUserCache .
func (d *Dao) DeleteUserCache(c context.Context, mid int64) (err error) {
var (
key = userKey(mid)
conn = d.mc.Get(c)
)
defer conn.Close()
if err = conn.Delete(key); err != nil {
err = errors.Wrapf(err, "key : %s", key)
return
}
return
}

View File

@@ -0,0 +1,175 @@
package dao
import (
"context"
"fmt"
"strings"
"go-common/app/admin/main/block/model"
xsql "go-common/library/database/sql"
"github.com/pkg/errors"
)
const (
_user = `SELECT id,mid,status,ctime,mtime FROM block_user WHERE mid=? LIMIT 1`
_users = `SELECT id,mid,status,ctime,mtime FROM block_user WHERE mid IN (%s)`
_upsertUser = `INSERT INTO block_user (mid,status) VALUES (?,?) ON DUPLICATE KEY UPDATE status=?`
_userDetails = `SELECT id,mid,block_count,ctime,mtime FROM block_user_detail WHERE mid IN (%s)`
_addAddBlockCount = `INSERT INTO block_user_detail (mid,block_count) VALUES (?,1) ON DUPLICATE KEY UPDATE block_count=block_count+1`
_history = `SELECT id,mid,admin_id,admin_name,source,area,reason,comment,action,start_time,duration,notify,ctime,mtime FROM block_history_%d WHERE mid=? LIMIT ?,?`
_historyCount = `SELECT count(*) FROM block_history_%d WHERE mid=? LIMIT 1`
_insertHistory = `INSERT INTO block_history_%d (mid,admin_id,admin_name,source,area,reason,comment,action,start_time,duration,notify) VALUES (?,?,?,?,?,?,?,?,?,?,?)`
)
func historyIdx(mid int64) int64 {
return mid % 10
}
// User .
func (d *Dao) User(c context.Context, mid int64) (user *model.DBUser, err error) {
user = &model.DBUser{}
row := d.db.QueryRow(c, _user, mid)
if err = row.Scan(&user.ID, &user.MID, &user.Status, &user.CTime, &user.MTime); err != nil {
err = nil
user = nil
return
}
return
}
// Users .
func (d *Dao) Users(c context.Context, mids []int64) (users []*model.DBUser, err error) {
if len(mids) == 0 {
return
}
var (
sql = fmt.Sprintf(_users, strings.Join(intsToStrs(mids), ","))
rows *xsql.Rows
)
if rows, err = d.db.Query(c, sql); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
user := &model.DBUser{}
if err = rows.Scan(&user.ID, &user.MID, &user.Status, &user.CTime, &user.MTime); err != nil {
err = errors.WithStack(err)
return
}
users = append(users, user)
}
if err = rows.Err(); err != nil {
err = errors.WithStack(err)
}
return
}
// TxUpdateUser .
func (d *Dao) TxUpdateUser(c context.Context, tx *xsql.Tx, mid int64, status model.BlockStatus) (err error) {
if _, err = tx.Exec(_upsertUser, mid, status, status); err != nil {
err = errors.WithStack(err)
return
}
return
}
// UserDetails .
func (d *Dao) UserDetails(c context.Context, mids []int64) (users []*model.DBUserDetail, err error) {
if len(mids) == 0 {
return
}
var (
sql = fmt.Sprintf(_userDetails, strings.Join(intsToStrs(mids), ","))
rows *xsql.Rows
)
if rows, err = d.db.Query(c, sql); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
user := &model.DBUserDetail{}
if err = rows.Scan(&user.ID, &user.MID, &user.BlockCount, &user.CTime, &user.MTime); err != nil {
err = errors.WithStack(err)
return
}
users = append(users, user)
}
if err = rows.Err(); err != nil {
err = errors.WithStack(err)
}
return
}
// UpdateAddBlockCount .
func (d *Dao) UpdateAddBlockCount(c context.Context, mid int64) (err error) {
if _, err = d.db.Exec(c, _addAddBlockCount, mid); err != nil {
err = errors.WithStack(err)
return
}
return
}
// History 获得mid历史封禁记录
func (d *Dao) History(c context.Context, mid int64, start, limit int) (history []*model.DBHistory, err error) {
var (
rows *xsql.Rows
sql = fmt.Sprintf(_history, historyIdx(mid))
)
if rows, err = d.db.Query(c, sql, mid, start, limit); err != nil {
err = errors.WithStack(err)
return
}
defer rows.Close()
for rows.Next() {
h := &model.DBHistory{}
if err = rows.Scan(&h.ID, &h.MID, &h.AdminID, &h.AdminName, &h.Source, &h.Area, &h.Reason, &h.Comment, &h.Action, &h.StartTime, &h.Duration, &h.Notify, &h.CTime, &h.MTime); err != nil {
err = errors.WithStack(err)
return
}
history = append(history, h)
}
if err = rows.Err(); err != nil {
return
}
return
}
// HistoryCount 获得历史记录总长度
func (d *Dao) HistoryCount(c context.Context, mid int64) (total int, err error) {
var (
row *xsql.Row
sql = fmt.Sprintf(_historyCount, historyIdx(mid))
)
row = d.db.QueryRow(c, sql, mid)
if err = row.Scan(&total); err != nil {
err = errors.WithStack(err)
return
}
return
}
// TxInsertHistory .
func (d *Dao) TxInsertHistory(c context.Context, tx *xsql.Tx, h *model.DBHistory) (err error) {
var (
sql = fmt.Sprintf(_insertHistory, historyIdx(h.MID))
)
if _, err = tx.Exec(sql, h.MID, h.AdminID, h.AdminName, h.Source, h.Area, h.Reason, h.Comment, h.Action, h.StartTime, h.Duration, h.Notify); err != nil {
err = errors.WithStack(err)
return
}
return
}
func intsToStrs(ints []int64) (strs []string) {
for _, i := range ints {
strs = append(strs, fmt.Sprintf("%d", i))
}
return
}

View File

@@ -0,0 +1,72 @@
package dao
import (
"context"
mdlaccount "go-common/app/service/main/account/model"
mdlfigure "go-common/app/service/main/figure/model"
mdlspy "go-common/app/service/main/spy/model"
"github.com/pkg/errors"
)
// SpyScore .
func (d *Dao) SpyScore(c context.Context, mid int64) (score int8, err error) {
var (
arg = &mdlspy.ArgUserScore{
Mid: mid,
}
res *mdlspy.UserScore
)
if res, err = d.spyRPC.UserScore(c, arg); err != nil {
err = errors.WithStack(err)
return
}
if res == nil {
return
}
score = res.Score
return
}
// FigureRank .
func (d *Dao) FigureRank(c context.Context, mid int64) (rank int8, err error) {
var (
arg = &mdlfigure.ArgUserFigure{
Mid: mid,
}
res *mdlfigure.FigureWithRank
)
if res, err = d.figureRPC.UserFigure(c, arg); err != nil {
err = errors.WithStack(err)
return
}
if res == nil {
rank = 100
return
}
rank = res.Percentage
return
}
// AccountInfo .
func (d *Dao) AccountInfo(c context.Context, mid int64) (nickname string, tel int32, level int32, regTime int64, err error) {
var (
arg = &mdlaccount.ArgMid{
Mid: mid,
}
res *mdlaccount.Profile
)
if res, err = d.accountRPC.Profile3(c, arg); err != nil {
err = errors.WithStack(err)
return
}
if res == nil {
return
}
nickname = res.Name
tel = res.TelStatus
level = res.Level
regTime = int64(res.JoinTime)
return
}