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,77 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"academy_test.go",
"dao_test.go",
"material_test.go",
"msg_test.go",
"search_test.go",
"upbfs_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/admin/main/creative/conf:go_default_library",
"//app/admin/main/creative/model/academy: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 = [
"academy.go",
"dao.go",
"material.go",
"msg.go",
"search.go",
"upbfs.go",
],
importpath = "go-common/app/admin/main/creative/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/main/creative/conf:go_default_library",
"//app/admin/main/creative/model/academy:go_default_library",
"//app/admin/main/creative/model/material:go_default_library",
"//app/interface/openplatform/article/model:go_default_library",
"//app/interface/openplatform/article/rpc/client:go_default_library",
"//app/service/main/account/api:go_default_library",
"//app/service/main/archive/api:go_default_library",
"//app/service/main/archive/api/gorpc:go_default_library",
"//app/service/main/archive/model/archive:go_default_library",
"//library/database/elastic:go_default_library",
"//library/database/orm: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/jinzhu/gorm: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,59 @@
package dao
import (
"context"
"strconv"
"go-common/app/interface/openplatform/article/model"
"go-common/app/service/main/archive/api"
"go-common/app/service/main/archive/model/archive"
"go-common/library/ecode"
"go-common/library/log"
"go-common/library/net/metadata"
)
// Archive get archive.
func (d *Dao) Archive(c context.Context, aid int64) (a *api.Arc, err error) {
var ip = metadata.String(c, metadata.RemoteIP)
var arg = &archive.ArgAid2{Aid: aid, RealIP: ip}
if a, err = d.arc.Archive3(c, arg); err != nil {
log.Error("d.arc.Archive3 aid(%d)|error(%v)", aid, err)
err = ecode.CreativeArcServiceErr
}
return
}
// Archives get archive list.
func (d *Dao) Archives(c context.Context, aids []int64) (a map[int64]*api.Arc, err error) {
var ip = metadata.String(c, metadata.RemoteIP)
var arg = &archive.ArgAids2{Aids: aids, RealIP: ip}
if a, err = d.arc.Archives3(c, arg); err != nil {
log.Error("d.arc.Archive3 aids(%v+)|error(%v)", aids, err)
err = ecode.CreativeArcServiceErr
}
return
}
// ArticleMetas batch get articles by aids.
func (d *Dao) ArticleMetas(c context.Context, aids []int64) (res map[int64]*model.Meta, err error) {
var ip = metadata.String(c, metadata.RemoteIP)
arg := &model.ArgAids{Aids: aids, RealIP: ip}
if res, err = d.art.ArticleMetas(c, arg); err != nil {
log.Error("d.art.ArticleMetas aids(%+v)|error(%v)", arg, err)
if _, er := strconv.ParseInt(err.Error(), 10, 64); er != nil {
err = ecode.CreativeArticleRPCErr
}
}
log.Info("d.art.ArticleMetas aids(%v)|res(%+v)", aids, res)
return
}
// Stats get archives stat.
func (d *Dao) Stats(c context.Context, aids []int64, ip string) (a map[int64]*api.Stat, err error) {
var arg = &archive.ArgAids2{Aids: aids, RealIP: ip}
if a, err = d.arc.Stats3(c, arg); err != nil {
log.Error("rpc Stats (%v) error(%v)", aids, err)
err = ecode.CreativeArcServiceErr
}
return
}

View File

@@ -0,0 +1,73 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoArchive(t *testing.T) {
convey.Convey("Archive", t, func(ctx convey.C) {
var (
c = context.Background()
aid = int64(10110788)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
a, err := d.Archive(c, aid)
ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoArchives(t *testing.T) {
convey.Convey("Archives", t, func(ctx convey.C) {
var (
c = context.Background()
aids = []int64{10110788}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
a, err := d.Archives(c, aids)
ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(a, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoArticleMetas(t *testing.T) {
convey.Convey("ArticleMetas", t, func(ctx convey.C) {
var (
c = context.Background()
aids = []int64{10110788}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.ArticleMetas(c, aids)
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 TestDaoStats(t *testing.T) {
convey.Convey("Stats", t, func(ctx convey.C) {
var (
c = context.Background()
aids = []int64{10110788}
ip = ""
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
a, err := d.Stats(c, aids, ip)
ctx.Convey("Then err should be nil.a should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(a, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,93 @@
package dao
import (
"context"
"go-common/app/admin/main/creative/conf"
article "go-common/app/interface/openplatform/article/rpc/client"
accapi "go-common/app/service/main/account/api"
archive "go-common/app/service/main/archive/api/gorpc"
"go-common/library/database/elastic"
"go-common/library/database/orm"
"go-common/library/ecode"
"go-common/library/log"
bm "go-common/library/net/http/blademaster"
"github.com/jinzhu/gorm"
)
const (
_msgURL = "/api/notify/send.user.notify.do"
)
// Dao dao.
type Dao struct {
c *conf.Config
DB *gorm.DB
DBArchive *gorm.DB
acc accapi.AccountClient
arc *archive.Service2
art *article.Service
es *elastic.Elastic
msgURL string
// http
client *bm.Client
}
// New new a dao and return.
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
DB: orm.NewMySQL(c.ORM),
DBArchive: orm.NewMySQL(c.ORMArchive),
arc: archive.New2(c.ArchiveRPC),
art: article.New(c.ArticleRPC),
es: elastic.NewElastic(nil),
// http client
client: bm.NewClient(c.HTTPClient),
}
d.msgURL = c.Host.Msg + _msgURL
d.initORM()
var err error
if d.acc, err = accapi.NewClient(c.AccClient); err != nil {
panic(err)
}
return
}
func (d *Dao) initORM() {
d.DB.LogMode(true)
d.DBArchive.LogMode(true)
d.DB.SingularTable(true)
}
// Ping check connection of db , mc.
func (d *Dao) Ping(c context.Context) (err error) {
if d.DB != nil {
err = d.DB.DB().PingContext(c)
}
if d.DBArchive != nil {
err = d.DBArchive.DB().PingContext(c)
}
return
}
// Close close connection of db , mc.
func (d *Dao) Close() {
if d.DB != nil {
d.DB.Close()
}
if d.DBArchive != nil {
d.DBArchive.Close()
}
}
// ProfileStat get account.
func (d *Dao) ProfileStat(c context.Context, mid int64) (res *accapi.ProfileStatReply, err error) {
arg := &accapi.MidReq{Mid: mid}
if res, err = d.acc.ProfileWithStat3(c, arg); err != nil {
log.Error("d.acc.ProfileWithStat3() error(%v)", err)
err = ecode.CreativeAccServiceErr
}
return
}

View File

@@ -0,0 +1,44 @@
package dao
import (
"flag"
"go-common/app/admin/main/creative/conf"
"os"
"strings"
"testing"
gock "gopkg.in/h2non/gock.v1"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative-admin")
flag.Set("conf_token", "fea489c730517fcd0980908ca8310779")
flag.Set("tree_id", "5684")
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/creative-admin.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)
d.client.SetTransport(gock.DefaultTransport)
return r
}

View File

@@ -0,0 +1,56 @@
package dao
import (
"context"
"github.com/jinzhu/gorm"
"go-common/app/admin/main/creative/model/material"
"go-common/library/log"
)
// CategoryByID .
func (d *Dao) CategoryByID(c context.Context, id int64) (cate *material.Category, err error) {
cate = &material.Category{}
if err = d.DB.Where("id=?", id).First(&cate).Error; err != nil {
log.Error("d.CategoryByID.Find error(%v)", err)
return
}
return
}
// BindWithCategory .
func (d *Dao) BindWithCategory(c context.Context, MaterialID, CategoryID, index int64) (id int64, err error) {
var state int
cate := &material.WithCategory{}
if err = d.DB.Where("material_id=?", MaterialID).First(&cate).Error; err != nil && err != gorm.ErrRecordNotFound {
log.Error("d.BindWithCategory.Find error(%v)", err)
return
}
cate.CategoryID = CategoryID
cate.MaterialID = MaterialID
cate.Index = index
if err != nil && err == gorm.ErrRecordNotFound {
//添加关联
if CategoryID == 0 {
return
}
if err = d.DB.Create(cate).Error; err != nil {
log.Error("BindWithCategory Create error(%+v)", err)
return
}
} else {
if CategoryID == 0 {
//删除关联
state = material.StateOff
} else {
state = material.StateOn
}
if err = d.DB.Model(&material.WithCategory{}).Where("id=?", cate.ID).Update(cate).Update(map[string]int{"state": state}).Error; err != nil {
log.Error("dao BindWithCategory error(%v)", err)
return
}
}
id = cate.ID
err = nil
return
}

View File

@@ -0,0 +1,42 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoCategoryByID(t *testing.T) {
convey.Convey("CategoryByID", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
cate, err := d.CategoryByID(c, id)
ctx.Convey("Then err should be nil.cate should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(cate, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoBindWithCategory(t *testing.T) {
convey.Convey("BindWithCategory", t, func(ctx convey.C) {
var (
c = context.Background()
MaterialID = int64(2)
CategoryID = int64(1)
index = int64(1)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
id, err := d.BindWithCategory(c, MaterialID, CategoryID, index)
ctx.Convey("Then err should be nil.id should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(id, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,65 @@
package dao
import (
"context"
"net/url"
"go-common/library/ecode"
"go-common/library/net/metadata"
"go-common/library/xstr"
"go-common/library/log"
"github.com/pkg/errors"
)
// MutliSendSysMsg Mutli send sys msg.
func (d *Dao) MutliSendSysMsg(c context.Context, allMids []int64, title string, context string) (err error) {
var times int
ulen := len(allMids)
if ulen%100 == 0 {
times = ulen / 100
} else {
times = ulen/100 + 1
}
var mids []int64
for i := 0; i < times; i++ {
if i == times-1 {
mids = allMids[i*100:]
} else {
mids = allMids[i*100 : (i+1)*100]
}
if err = d.SendSysMsg(c, mids, title, context); err != nil {
err = errors.Wrapf(err, "d.SendSysMsg(%+v,%s,%s)", mids, title, context)
continue
}
}
return
}
// SendSysMsg send sys msg.
func (d *Dao) SendSysMsg(c context.Context, mids []int64, title string, context string) (err error) {
var ip = metadata.String(c, metadata.RemoteIP)
params := url.Values{}
params.Set("mc", "1_14_6")
params.Set("title", title)
params.Set("data_type", "4")
params.Set("context", context)
params.Set("mid_list", xstr.JoinInts(mids))
var res struct {
Code int `json:"code"`
Data *struct {
Status int8 `json:"status"`
Remark string `json:"remark"`
} `json:"data"`
}
if err = d.client.Post(c, d.msgURL, ip, params, &res); err != nil {
err = errors.Wrapf(err, "SendSysMsg d.client.Post(%s)", d.msgURL+"?"+params.Encode())
return
}
log.Info("dao.SendSysMsg res (%+v) ", res)
if res.Code != 0 {
err = errors.Wrapf(ecode.Int(res.Code), "SendSysMsg d.client.Post(%s,%d)", d.msgURL+"?"+params.Encode(), res.Code)
}
return
}

View File

@@ -0,0 +1,47 @@
package dao
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
gock "gopkg.in/h2non/gock.v1"
)
func TestDaoMutliSendSysMsg(t *testing.T) {
convey.Convey("MutliSendSysMsg", t, func(ctx convey.C) {
var (
c = context.Background()
allMids = []int64{27515256}
title = "title"
context = "context"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.msgURL).Reply(200).JSON(`{"code":20007}`)
err := d.MutliSendSysMsg(c, allMids, title, context)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoSendSysMsg(t *testing.T) {
convey.Convey("SendSysMsg", t, func(ctx convey.C) {
var (
c = context.Background()
mids = []int64{27515256}
title = "title"
context = "context"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
defer gock.OffAll()
httpMock("GET", d.msgURL).Reply(200).JSON(`{"code":20007}`)
err := d.SendSysMsg(c, mids, title, context)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,57 @@
package dao
import (
"context"
"go-common/app/admin/main/creative/model/academy"
"go-common/library/database/elastic"
"go-common/library/log"
)
// ArchivesWithES search archives by es.
func (d *Dao) ArchivesWithES(c context.Context, aca *academy.EsParam) (res *academy.SearchResult, err error) {
r := d.es.NewRequest("academy_archive").Fields("oid", "tid")
r.Index("academy_archive").WhereEq("state", academy.StateNormal).WhereEq("business", aca.Business).Pn(aca.Pn).Ps(aca.Ps).Order("id", "desc")
if aca.Business == academy.BusinessForArchvie && aca.State != academy.DefaultState { //arc_state 稿件原始状态 state 创作学院稿件状态
r.WhereEq("arc_state", aca.State)
}
if aca.Business == academy.BusinessForArticle { //只筛选开放浏览的专栏
r.WhereEq("arc_state", 0).WhereEq("deleted_time", 0)
}
if aca.Keyword != "" {
r.WhereLike([]string{"title", "tid_name"}, []string{aca.Keyword}, true, "low")
}
if aca.Uname != "" {
r.WhereLike([]string{"uname"}, []string{aca.Uname}, true, "low")
}
if aca.OID > 0 {
r.WhereEq("oid", aca.OID)
}
if len(aca.TidsMap) > 0 {
for _, v := range aca.TidsMap {
cmb := &elastic.Combo{}
tids := make([]interface{}, 0, len(v))
for _, tid := range v {
tids = append(tids, tid)
}
cmb.ComboIn([]map[string][]interface{}{
{"tid": tids},
}).MinIn(1).MinAll(1)
r.WhereCombo(cmb)
}
}
if aca.Business == academy.BusinessForArchvie {
if aca.Copyright != 3 {
r.WhereEq("copyright", aca.Copyright) //投稿类型
} else {
r.WhereIn("copyright", []int8{0, 1, 2})
}
}
res = &academy.SearchResult{}
if err = r.Scan(c, res); err != nil {
log.Error("ArchivesWithES r.Scan params(%s)|error(%v)", r.Params(), err)
}
log.Info("ArchivesWithES params(%s)", r.Params())
return
}

View File

@@ -0,0 +1,25 @@
package dao
import (
"context"
"go-common/app/admin/main/creative/model/academy"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoArchivesWithES(t *testing.T) {
convey.Convey("ArchivesWithES", t, func(ctx convey.C) {
var (
c = context.Background()
aca = &academy.EsParam{}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
res, err := d.ArchivesWithES(c, aca)
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,87 @@
package dao
import (
"bytes"
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"fmt"
"hash"
"net/http"
"strconv"
"time"
"go-common/app/admin/main/creative/conf"
"go-common/library/ecode"
"go-common/library/log"
)
// bfs info
const (
_uploadURL = "/bfs/%s/%s"
_template = "%s\n%s\n%s\n%d\n"
_method = "PUT"
_bucket = "creative"
)
// Upload upload picture or log file to bfs
func (d *Dao) Upload(c context.Context, fileName string, fileType string, timing int64, data []byte, bfs *conf.Bfs) (location string, err error) {
var (
req *http.Request
resp *http.Response
code int
client = &http.Client{Timeout: time.Duration(bfs.Timeout) * time.Millisecond}
url = fmt.Sprintf(bfs.Host+_uploadURL, _bucket, fileName)
)
// prepare the data of the file and init the request
buf := new(bytes.Buffer)
_, err = buf.Write(data)
if err != nil {
log.Error("Upload.buf.Write.error(%v)", err)
err = ecode.RequestErr
return
}
if req, err = http.NewRequest(_method, url, buf); err != nil {
log.Error("http.NewRequest() Upload(%v) error(%v)", url, err)
return
}
// request setting
authorization := authorize(bfs.Key, bfs.Secret, _method, _bucket, fileName, timing)
req.Header.Set("Date", fmt.Sprint(timing))
req.Header.Set("Authorization", authorization)
req.Header.Set("Content-Type", fileType)
resp, err = client.Do(req)
// response treatment
if err != nil {
log.Error("Bfs client.Do(%s) error(%v)", url, err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("Bfs status code error:%v", resp.StatusCode)
return
}
code, err = strconv.Atoi(resp.Header.Get("code"))
if err != nil || code != 200 {
err = fmt.Errorf("Bfs response code error:%v", code)
return
}
location = resp.Header.Get("Location")
return
}
// authorize returns authorization for upload file to bfs
func authorize(key, secret, method, bucket, file string, expire int64) (authorization string) {
var (
content string
mac hash.Hash
signature string
)
content = fmt.Sprintf(_template, method, bucket, file, expire)
mac = hmac.New(sha1.New, []byte(secret))
mac.Write([]byte(content))
signature = base64.StdEncoding.EncodeToString(mac.Sum(nil))
authorization = fmt.Sprintf("%s:%s:%d", key, signature, expire)
return
}

View File

@@ -0,0 +1,49 @@
package dao
import (
"context"
"go-common/app/admin/main/creative/conf"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoUpload(t *testing.T) {
convey.Convey("Upload", t, func(ctx convey.C) {
var (
c = context.Background()
fileName = "filename"
fileType = "png"
timing = int64(1545382342)
data = []byte("iamdata")
bfs = &conf.Bfs{}
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
bfs = d.c.Bfs
location, err := d.Upload(c, fileName, fileType, timing, data, bfs)
ctx.Convey("Then err should be nil.location should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil) // http code 401
ctx.So(location, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoauthorize(t *testing.T) {
convey.Convey("authorize", t, func(ctx convey.C) {
var (
key = "8d4e593ba7555502"
secret = "0bdbd4c7caeeddf587c3c4daec0475"
method = "PUT"
bucket = "archive"
file = ""
expire = int64(100)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
authorization := authorize(key, secret, method, bucket, file, expire)
ctx.Convey("Then authorization should not be nil.", func(ctx convey.C) {
ctx.So(authorization, convey.ShouldNotBeNil)
})
})
})
}