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,70 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"dao.go",
"db_project.go",
"db_sync.go",
"db_task.go",
"db_wechat.go",
"http.go",
"mc.go",
"redis.go",
],
importpath = "go-common/app/admin/ep/saga/dao",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/admin/ep/saga/conf:go_default_library",
"//app/admin/ep/saga/model:go_default_library",
"//library/cache/memcache:go_default_library",
"//library/cache/redis: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",
"//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"],
)
go_test(
name = "go_default_test",
srcs = [
"dao_test.go",
"db_project_test.go",
"db_sync_test.go",
"db_wechat_test.go",
"mc_test.go",
"redis_test.go",
],
embed = [":go_default_library"],
tags = ["automanaged"],
deps = [
"//app/admin/ep/saga/conf:go_default_library",
"//app/admin/ep/saga/model:go_default_library",
"//app/admin/ep/saga/service/utils:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)

View File

@@ -0,0 +1,57 @@
package dao
import (
"context"
"time"
"go-common/app/admin/ep/saga/conf"
"go-common/library/cache/memcache"
"go-common/library/cache/redis"
"go-common/library/database/orm"
bm "go-common/library/net/http/blademaster"
"github.com/jinzhu/gorm"
)
// Dao def
type Dao struct {
// cache
httpClient *bm.Client
db *gorm.DB
mc *memcache.Pool
redis *redis.Pool
mcRecordExpire int32
}
// New create instance of Dao
func New() (d *Dao) {
d = &Dao{
mc: memcache.NewPool(conf.Conf.Memcache.MC),
httpClient: bm.NewClient(conf.Conf.HTTPClient),
db: orm.NewMySQL(conf.Conf.ORM),
redis: redis.NewPool(conf.Conf.Redis),
mcRecordExpire: int32(time.Duration(conf.Conf.Memcache.MCRecordExpire) / time.Second),
}
return
}
// Ping dao.
func (d *Dao) Ping(c context.Context) (err error) {
if err = d.pingRedis(c); err != nil {
return
}
if err = d.pingMC(c); err != nil {
return
}
return d.db.DB().Ping()
}
// Close dao.
func (d *Dao) Close() {
if d.mc != nil {
d.mc.Close()
}
if d.db != nil {
d.db.Close()
}
}

View File

@@ -0,0 +1,24 @@
package dao
import (
"flag"
"os"
"testing"
"go-common/app/admin/ep/saga/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
flag.Set("conf", "../cmd/saga-admin-test.toml")
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New()
defer d.Close()
os.Exit(m.Run())
}

View File

@@ -0,0 +1,138 @@
package dao
import (
"go-common/app/admin/ep/saga/conf"
"go-common/app/admin/ep/saga/model"
"github.com/jinzhu/gorm"
pkgerr "github.com/pkg/errors"
)
// ProjectExist check the project exist or not
func (d *Dao) ProjectExist(projID int) (exist bool, err error) {
var count int
if err = pkgerr.WithStack(d.db.Model(&model.ProjectInfo{}).Where(&model.ProjectInfo{ProjectID: projID}).Count(&count).Error); err != nil {
return
}
if count > 0 {
exist = true
}
return
}
// FavoriteProjects get user's favorite projects
func (d *Dao) FavoriteProjects(userName string) (favorites []*model.ProjectFavorite, err error) {
//err = pkgerr.WithStack(d.db.Where(&model.ProjectFavorite{UserName: userName}).Find(&favorites).Error)
err = pkgerr.WithStack(d.db.Where("user_name = ?", userName).Find(&favorites).Error)
return
}
// AddFavorite add favorite project for user
func (d *Dao) AddFavorite(userName string, projID int) (err error) {
return pkgerr.WithStack(d.db.Create(&model.ProjectFavorite{UserName: userName, ProjID: projID}).Error)
}
// DelFavorite delete favorite project for user
func (d *Dao) DelFavorite(userName string, projID int) (err error) {
return pkgerr.WithStack(d.db.Delete(model.ProjectFavorite{UserName: userName, ProjID: projID}).Error)
}
// AddProjectInfo add ProjectInfo
func (d *Dao) AddProjectInfo(projectInfo *model.ProjectInfo) (err error) {
return pkgerr.WithStack(d.db.Create(projectInfo).Error)
}
// ProjectsInfo all the projects in saga
func (d *Dao) ProjectsInfo() (projects []*model.ProjectInfo, err error) {
err = pkgerr.WithStack(d.db.Find(&projects).Error)
return
}
// ProjectInfoByID query ProjectInfo by ID
func (d *Dao) ProjectInfoByID(projectID int) (projectInfo *model.ProjectInfo, err error) {
projectInfo = &model.ProjectInfo{}
err = pkgerr.WithStack(d.db.Where("project_id = ?", projectID).First(projectInfo).Error)
return
}
// UpdateProjectInfo update
func (d *Dao) UpdateProjectInfo(projectID int, projectInfo *model.ProjectInfo) (err error) {
return pkgerr.WithStack(d.db.Model(&model.ProjectInfo{}).Where("project_id = ?", projectID).Update(projectInfo).Error)
}
// HasProjectInfo is exist project info in database.
func (d *Dao) HasProjectInfo(projectID int) (b bool, err error) {
var size int64
if err = pkgerr.WithStack(d.db.Model(&model.ProjectInfo{}).Where("project_id = ?", projectID).Count(&size).Error); err != nil {
return
}
b = size > 0
return
}
// QueryProjectInfo query Project Info.
func (d *Dao) QueryProjectInfo(ifPage bool, req *model.ProjectInfoRequest) (total int, projectInfo []*model.ProjectInfo, err error) {
var (
projectIDs = conf.Conf.Property.DefaultProject.ProjectIDs
db *gorm.DB
)
gDB := d.db.Model(&model.ProjectInfo{})
if req.Name != "" {
gDB = gDB.Where("name = ?", req.Name)
}
if req.Department != "" {
gDB = gDB.Where("department = ?", req.Department)
}
if req.Business != "" {
gDB = gDB.Where("business = ?", req.Business)
}
if err = pkgerr.WithStack(gDB.Count(&total).Error); err != nil {
return
}
if ifPage {
if req.PageNum == 1 {
db = gDB.Order("name DESC").Where("project_id in (?)", projectIDs).Find(&projectInfo)
} else {
db = gDB.Order("name DESC").Offset((req.PageNum - 2) * req.PageSize).Limit(req.PageSize).Find(&projectInfo)
}
} else {
db = gDB.Find(&projectInfo)
}
if db.Error != nil {
if db.RecordNotFound() {
err = nil
} else {
err = pkgerr.WithStack(db.Error)
}
}
return
}
// QueryConfigInfo query saga and runner Info.
func (d *Dao) QueryConfigInfo(name, department, business, queryObject string) (total int, err error) {
gDB := d.db.Model(&model.ProjectInfo{})
if name != "" {
gDB = gDB.Where("name = ?", name)
}
if department != "" {
gDB = gDB.Where("department = ?", department)
}
if business != "" {
gDB = gDB.Where("business = ?", business)
}
if queryObject == model.ObjectSaga {
gDB = gDB.Where("Saga = ?", true)
} else if queryObject == model.ObjectRunner {
gDB = gDB.Where("Runner = ?", true)
} else {
return
}
err = pkgerr.WithStack(gDB.Count(&total).Error)
return
}

View File

@@ -0,0 +1,203 @@
package dao
import (
"testing"
"go-common/app/admin/ep/saga/model"
"github.com/smartystreets/goconvey/convey"
)
func TestProjectExist(t *testing.T) {
convey.Convey("ProjectExist", t, func(ctx convey.C) {
var (
projectID = int(682)
projectID2 = int(6820)
)
ctx.Convey("When projectID exist", func(ctx convey.C) {
b, err := d.ProjectExist(projectID)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(b, convey.ShouldEqual, true)
})
})
ctx.Convey("When projectID not exist", func(ctx convey.C) {
bb, err := d.ProjectExist(projectID2)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(bb, convey.ShouldEqual, false)
})
})
})
}
func TestFavorite(t *testing.T) {
convey.Convey("Favorite project", t, func(ctx convey.C) {
var (
userName = "zhangsan"
projectID = 333
)
ctx.Convey("add favorite project", func(ctx convey.C) {
err := d.AddFavorite(userName, projectID)
ctx.Convey("The err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("get favorite project", func(ctx convey.C) {
favorites, err := d.FavoriteProjects(userName)
ctx.Convey("The err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(len(favorites), convey.ShouldEqual, 1)
ctx.So(favorites[0].UserName, convey.ShouldEqual, "zhangsan")
ctx.So(favorites[0].ProjID, convey.ShouldEqual, 333)
})
})
ctx.Convey("delete favorite project", func(ctx convey.C) {
err := d.DelFavorite(userName, projectID)
ctx.Convey("delete err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
favorites, err := d.FavoriteProjects(userName)
ctx.Convey("get err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(len(favorites), convey.ShouldEqual, 0)
})
})
})
}
func TestProjectInfoByID(t *testing.T) {
convey.Convey("ProjectInfoByID", t, func(ctx convey.C) {
ctx.Convey("add project info", func(ctx convey.C) {
addInfo := &model.ProjectInfo{
ProjectID: 11111,
Name: "myProject",
Description: "myProject des",
WebURL: "git@git.bilibili.test/test.git",
Repo: "git@git.bilibili.test/test.git",
DefaultBranch: "master",
Saga: true,
Runner: false,
}
err := d.AddProjectInfo(addInfo)
ctx.Convey("Add Project Info. Then err should be nil. ", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
info, err := d.ProjectInfoByID(11111)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(info.Name, convey.ShouldEqual, "myProject")
ctx.So(info.Repo, convey.ShouldEqual, "git@git.bilibili.test/test.git")
ctx.So(info.Saga, convey.ShouldEqual, true)
})
})
ctx.Convey("update project info", func(ctx convey.C) {
updateInfo := &model.ProjectInfo{
ProjectID: 11111,
Name: "lisi",
Description: "lisi des",
WebURL: "git@git.bilibili.test/test.git",
Repo: "git@git.bilibili.test/test.git",
DefaultBranch: "dev",
Saga: true,
Runner: false,
}
err := d.UpdateProjectInfo(11111, updateInfo)
ctx.Convey("update Project Info. Then err should be nil. ", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
info, err := d.ProjectInfoByID(11111)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(info.Name, convey.ShouldEqual, "lisi")
ctx.So(info.Description, convey.ShouldEqual, "lisi des")
ctx.So(info.DefaultBranch, convey.ShouldEqual, "dev")
})
})
ctx.Convey("query not exist project info", func(ctx convey.C) {
_, err := d.ProjectInfoByID(6820)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
})
})
})
}
func TestDaoHasProjectInfo(t *testing.T) {
convey.Convey("HasProjectInfo", t, func(ctx convey.C) {
var (
projectID = int(682)
projectID2 = int(6820)
)
ctx.Convey("When projectID exist", func(ctx convey.C) {
b, err := d.HasProjectInfo(projectID)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(b, convey.ShouldEqual, true)
})
})
ctx.Convey("When projectID not exist", func(ctx convey.C) {
b, err := d.HasProjectInfo(projectID2)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(b, convey.ShouldEqual, false)
})
})
})
}
func TestQueryProjectInfo(t *testing.T) {
convey.Convey("QueryProjectInfo", t, func(ctx convey.C) {
var (
projectName = "andruid"
projectUrl = "git@git-test.bilibili.co:android/andruid.git"
projectInfoRequest = &model.ProjectInfoRequest{
Name: projectName,
}
)
ctx.Convey("query project exist", func(ctx convey.C) {
var url string
total, projectInfo, err := d.QueryProjectInfo(false, projectInfoRequest)
for _, project := range projectInfo {
url = project.Repo
}
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldEqual, 1)
ctx.So(url, convey.ShouldEqual, projectUrl)
})
})
})
}
func TestQueryConfigInfo(t *testing.T) {
convey.Convey("QueryConfigInfo", t, func(ctx convey.C) {
var (
projectName = "Kratos"
projectName2 = "android-blue"
queryObject = "saga"
)
ctx.Convey("query saga config exist", func(ctx convey.C) {
total, err := d.QueryConfigInfo(projectName, "", "", queryObject)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldEqual, 1)
})
})
ctx.Convey("query saga config not exist", func(ctx convey.C) {
total, err := d.QueryConfigInfo(projectName2, "", "", queryObject)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldEqual, 0)
})
})
})
}

View File

@@ -0,0 +1,398 @@
package dao
import (
"context"
"time"
"go-common/app/admin/ep/saga/model"
pkgerr "github.com/pkg/errors"
)
/*-------------------------------------- commit ----------------------------------------*/
// HasCommit ...
func (d *Dao) HasCommit(projID int, commitID string) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsCommits{}).Where("project_id = ? AND commit_id = ?", projID, commitID).Count(&total).Error)
return
}
// DelCommit ...
func (d *Dao) DelCommit(projID int, commitID string) (err error) {
return pkgerr.WithStack(d.db.Where("project_id = ? AND commit_id = ?", projID, commitID).Delete(&model.StatisticsCommits{}).Error)
}
// UpdateCommit ...
func (d *Dao) UpdateCommit(projID int, commitID string, commit *model.StatisticsCommits) (err error) {
return pkgerr.WithStack(d.db.Model(&model.StatisticsCommits{}).Where("project_id = ? AND commit_id = ?", projID, commitID).Update(commit).Error)
}
// CreateCommit ...
func (d *Dao) CreateCommit(req *model.StatisticsCommits) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}
// QueryCommitByID query commit info by ID
func (d *Dao) QueryCommitByID(projID int, commitID string) (commit *model.StatisticsCommits, err error) {
commit = &model.StatisticsCommits{}
err = pkgerr.WithStack(d.db.Where("project_id = ? AND commit_id = ?", projID, commitID).First(commit).Error)
return
}
// CountCommitByTime ...
func (d *Dao) CountCommitByTime(projID int, since, until string) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsCommits{}).Where("project_id = ? AND created_at > ? AND created_at < ?", projID, since, until).Count(&total).Error)
return
}
// QueryProjectCommits ...
func (d *Dao) QueryProjectCommits(c context.Context, projectID int) (commits []*model.StatisticsCommits, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsCommits{}).Where("project_id = ?", projectID).Find(&commits).Error)
return
}
/*-------------------------------------- issue ----------------------------------------*/
// HasIssue ...
func (d *Dao) HasIssue(projID, issueID int) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsIssues{}).Where("project_id = ? AND issue_id = ?", projID, issueID).Count(&total).Error)
return
}
// UpdateIssue ...
func (d *Dao) UpdateIssue(projID, issueID int, issue *model.StatisticsIssues) (err error) {
return pkgerr.WithStack(d.db.Model(&model.StatisticsIssues{}).Where("project_id = ? AND issue_id = ?", projID, issueID).Update(issue).Error)
}
// CreateIssue ...
func (d *Dao) CreateIssue(req *model.StatisticsIssues) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}
/*-------------------------------------- runner ----------------------------------------*/
// HasRunner ...
func (d *Dao) HasRunner(projID, runnerID int) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsRunners{}).Where("project_id = ? AND runner_id = ?", projID, runnerID).Count(&total).Error)
return
}
// UpdateRunner ...
func (d *Dao) UpdateRunner(projID, runnerID int, runner *model.StatisticsRunners) (err error) {
return pkgerr.WithStack(d.db.Model(&model.StatisticsRunners{}).Where("project_id = ? AND runner_id = ?", projID, runnerID).Update(runner).Error)
}
// CreateRunner ...
func (d *Dao) CreateRunner(req *model.StatisticsRunners) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}
/*-------------------------------------- job ----------------------------------------*/
// HasJob ...
func (d *Dao) HasJob(projID, jobID int) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsJobs{}).Where("project_id = ? AND job_id=?", projID, jobID).Count(&total).Error)
return
}
// UpdateJob ...
func (d *Dao) UpdateJob(projID, jobID int, runner *model.StatisticsJobs) (err error) {
return pkgerr.WithStack(d.db.Model(&model.StatisticsJobs{}).Where("project_id = ? AND job_id = ?", projID, jobID).Update(runner).Error)
}
// CreateJob ...
func (d *Dao) CreateJob(req *model.StatisticsJobs) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}
// QueryJobsByTime ...
func (d *Dao) QueryJobsByTime(projID int, req *model.ProjectJobRequest, since, until string) (total int, jobs []*model.StatisticsJobs, err error) {
gDB := d.db.Model(&model.StatisticsJobs{}).Where("project_id = ? AND created_at > ? AND created_at < ?", projID, since, until)
if req.Branch != "" {
gDB = gDB.Where("ref = ?", req.Branch)
}
if req.User != "" {
gDB = gDB.Where("user_name = ?", req.User)
}
if req.Machine != "" {
gDB = gDB.Where("runner_description = ?", req.Machine)
}
if err = pkgerr.WithStack(gDB.Count(&total).Error); err != nil {
return
}
if err = pkgerr.WithStack(gDB.Find(&jobs).Error); err != nil {
return
}
return
}
/*-------------------------------------- note ----------------------------------------*/
// HasNote ...
func (d *Dao) HasNote(c context.Context, projectID, noteID int) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsNotes{}).Where("project_id = ? AND note_id = ?", projectID, noteID).Count(&total).Error)
return
}
// UpdateNote ...
func (d *Dao) UpdateNote(c context.Context, projectID, noteID int, note *model.StatisticsNotes) (err error) {
return pkgerr.WithStack(d.db.Model(&model.StatisticsNotes{}).Where("project_id = ? AND note_id = ?", projectID, noteID).Update(note).Error)
}
// CreateNote ...
func (d *Dao) CreateNote(c context.Context, note *model.StatisticsNotes) (err error) {
return pkgerr.WithStack(d.db.Create(note).Error)
}
// NoteByMRIID ...
func (d *Dao) NoteByMRIID(c context.Context, projectID, mrIID int) (notes []*model.StatisticsNotes, err error) {
err = pkgerr.WithStack(d.db.Where("project_id = ? AND mr_iid = ? ", projectID, mrIID).Order("note_id desc").Find(&notes).Error)
return
}
// NoteByID ...
func (d *Dao) NoteByID(c context.Context, projectID, mrIID, noteID int) (note *model.StatisticsNotes, err error) {
note = &model.StatisticsNotes{}
err = pkgerr.WithStack(d.db.Where("project_id = ? AND mr_iid = ? AND note_id = ?", projectID, mrIID, noteID).First(note).Error)
return
}
/*-------------------------------------- mr ----------------------------------------*/
// CountMRByTime ...
func (d *Dao) CountMRByTime(projID int, since, until string) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsMrs{}).Where("project_id = ? AND created_at > ? AND created_at < ?", projID, since, until).Count(&total).Error)
return
}
// MRByProjectID query mr by projectID
func (d *Dao) MRByProjectID(c context.Context, projectID int, since *time.Time, until *time.Time) (mrs []*model.StatisticsMrs, err error) {
gDB := d.db.Model(&model.StatisticsMrs{}).Where("project_id = ? ", projectID)
if since != nil || until != nil {
gDB = gDB.Where("updated_at >= ? AND updated_at <= ?", since, until)
}
err = pkgerr.WithStack(gDB.Find(&mrs).Error)
return
}
// HasMR ...
func (d *Dao) HasMR(c context.Context, projectID, mrIID int) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsMrs{}).Where("project_id = ? AND mr_iid = ?", projectID, mrIID).Count(&total).Error)
return
}
// UpdateMR update
func (d *Dao) UpdateMR(c context.Context, projectID, mrIID int, mr *model.StatisticsMrs) (err error) {
return pkgerr.WithStack(d.db.Model(&model.StatisticsMrs{}).Where("project_id = ? AND mr_iid = ?", projectID, mrIID).Update(mr).Error)
}
// CreateMR ...
func (d *Dao) CreateMR(c context.Context, req *model.StatisticsMrs) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}
/*-------------------------------------- aggMR ----------------------------------------*/
// HasAggregateReviewer ...
func (d *Dao) HasAggregateReviewer(c context.Context, projectID, mrIID, reviewerID, reviewID int) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.AggregateMrReviewer{}).Where("project_id = ? and mr_iid = ? and reviewer_id=? and review_id = ? ", projectID, mrIID, reviewerID, reviewID).Count(&total).Error)
return
}
// UpdateAggregateReviewer ...
func (d *Dao) UpdateAggregateReviewer(c context.Context, projectID, mrIID, reviewerID, reviewID int, aggregateReviewer *model.AggregateMrReviewer) (err error) {
return pkgerr.WithStack(d.db.Model(&model.AggregateMrReviewer{}).Where("project_id = ? and mr_iid = ? and reviewer_id=? and review_id = ? ", projectID, mrIID, reviewerID, reviewID).Update(aggregateReviewer).Error)
}
// CreateAggregateReviewer ...
func (d *Dao) CreateAggregateReviewer(c context.Context, req *model.AggregateMrReviewer) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}
/*-------------------------------------- pipeline ----------------------------------------*/
// HasPipeline ...
func (d *Dao) HasPipeline(projID int, pipelineID int) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsPipeline{}).Where("project_id = ? AND pipeline_id = ?", projID, pipelineID).Count(&total).Error)
return
}
// UpdatePipeline ...
func (d *Dao) UpdatePipeline(projID int, pipelineID int, pipeline *model.StatisticsPipeline) (err error) {
return pkgerr.WithStack(d.db.Model(&model.StatisticsPipeline{}).Where("project_id = ? AND pipeline_id = ?", projID, pipelineID).Update(pipeline).Error)
}
// CreatePipeline ...
func (d *Dao) CreatePipeline(req *model.StatisticsPipeline) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}
// QueryPipelinesByTime ...
func (d *Dao) QueryPipelinesByTime(projID int, req *model.PipelineDataReq, since, until string) (total, statNum int, pipelines []*model.StatisticsPipeline, err error) {
gDB := d.db.Model(&model.StatisticsPipeline{}).Where("project_id = ? AND created_at > ? AND created_at < ?", projID, since, until)
if req.Branch != "" {
gDB = gDB.Where("ref = ?", req.Branch)
}
if req.User != "" {
gDB = gDB.Where("user = ?", req.User)
}
if err = pkgerr.WithStack(gDB.Count(&total).Error); err != nil {
return
}
if req.State != "" {
gDB = gDB.Where("status = ?", req.State)
}
if err = pkgerr.WithStack(gDB.Count(&statNum).Error); err != nil {
return
}
if err = pkgerr.WithStack(gDB.Find(&pipelines).Error); err != nil {
return
}
return
}
/*-------------------------------------- member ----------------------------------------*/
// HasMember ...
func (d *Dao) HasMember(c context.Context, projectID, memberID int) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsMembers{}).Where("project_id = ? AND member_id = ?", projectID, memberID).Count(&total).Error)
return
}
// UpdateMember ...
func (d *Dao) UpdateMember(c context.Context, projectID, memberID int, member *model.StatisticsMembers) (err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsMembers{}).Where("project_id = ? AND member_id = ?", projectID, memberID).Update(member).Error)
return
}
// QueryMemberByID ...
func (d *Dao) QueryMemberByID(c context.Context, projectID, memberID int) (member *model.StatisticsMembers, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsMembers{}).Where("project_id = ? AND member_id = ?", projectID, memberID).First(&member).Error)
return
}
// CreateMember ...
func (d *Dao) CreateMember(c context.Context, member *model.StatisticsMembers) (err error) {
return pkgerr.WithStack(d.db.Create(member).Error)
}
/*-------------------------------------- emoji ----------------------------------------*/
// HasMRAwardEmoji ...
func (d *Dao) HasMRAwardEmoji(c context.Context, projectID, mrIID, awardEmojiID int) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsMRAwardEmojis{}).Where("project_id = ? and mr_iid = ? and award_emoji_id=? ", projectID, mrIID, awardEmojiID).Count(&total).Error)
return
}
// UpdateMRAwardEmoji update
func (d *Dao) UpdateMRAwardEmoji(c context.Context, projectID, mrIID, awardEmojiID int, awardEmoji *model.StatisticsMRAwardEmojis) (err error) {
return pkgerr.WithStack(d.db.Model(&model.StatisticsMRAwardEmojis{}).Where("project_id = ? and mr_iid = ? and award_emoji_id=? ", projectID, mrIID, awardEmojiID).Update(awardEmoji).Error)
}
// CreateMRAwardEmoji ...
func (d *Dao) CreateMRAwardEmoji(c context.Context, awardEmoji *model.StatisticsMRAwardEmojis) (err error) {
return pkgerr.WithStack(d.db.Create(awardEmoji).Error)
}
// AwardEmojiByMRIID ...
func (d *Dao) AwardEmojiByMRIID(c context.Context, projectID, mrIID int) (AwardEmojis []*model.StatisticsMRAwardEmojis, err error) {
err = pkgerr.WithStack(d.db.Where("project_id = ? AND mr_iid = ? ", projectID, mrIID).Find(&AwardEmojis).Error)
return
}
/*-------------------------------------- discussion ----------------------------------------*/
// HasDiscussion ...
func (d *Dao) HasDiscussion(c context.Context, projectID, mrIID int, discussionID string) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsDiscussions{}).Where("project_id = ? and mr_iid = ? and discussion_id=? ", projectID, mrIID, discussionID).Count(&total).Error)
return
}
// UpdateDiscussion ...
func (d *Dao) UpdateDiscussion(c context.Context, projectID, mrIID int, discussionID string, discussion *model.StatisticsDiscussions) (err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsDiscussions{}).Where("project_id = ? and mr_iid = ? and discussion_id=? ", projectID, mrIID, discussionID).Update(discussion).Error)
return
}
// CreateDiscussion ...
func (d *Dao) CreateDiscussion(c context.Context, discussion *model.StatisticsDiscussions) (err error) {
return pkgerr.WithStack(d.db.Create(discussion).Error)
}
// DiscussionsByMRIID ...
func (d *Dao) DiscussionsByMRIID(c context.Context, projectID, mrIID int) (discussions []*model.StatisticsDiscussions, err error) {
err = pkgerr.WithStack(d.db.Where("project_id = ? AND mr_iid = ? ", projectID, mrIID).Find(&discussions).Error)
return
}
/*-------------------------------------- Branch ----------------------------------------*/
// DeleteProjectBranch ...
func (d *Dao) DeleteProjectBranch(c context.Context, projectID int) error {
return pkgerr.WithStack(d.db.Model(&model.StatisticsBranches{}).Where("project_id = ?", projectID).Update("is_deleted", model.BranchDeleted).Error)
}
// HasBranch is exist project branch info in database.
func (d *Dao) HasBranch(c context.Context, projectID int, branchName string) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.StatisticsBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).Count(&total).Error)
return
}
// UpdateBranch update
func (d *Dao) UpdateBranch(c context.Context, projectID int, branchName string, branch *model.StatisticsBranches) error {
return pkgerr.WithStack(d.db.Model(&model.StatisticsBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).Save(branch).Error)
}
// CreateBranch query all the records in contact_infos
func (d *Dao) CreateBranch(c context.Context, branch *model.StatisticsBranches) error {
return pkgerr.WithStack(d.db.Create(branch).Error)
}
// QueryProjectBranch ...
func (d *Dao) QueryProjectBranch(c context.Context, projectID int) (b []*model.StatisticsBranches, err error) {
err = pkgerr.WithStack(d.db.Where("project_id = ? and is_deleted != ?", projectID, model.BranchDeleted).Find(&b).Error)
return
}
// QueryFirstBranch ...
func (d *Dao) QueryFirstBranch(c context.Context, projectID int, branchName string) (branch *model.StatisticsBranches, err error) {
branch = &model.StatisticsBranches{}
err = pkgerr.WithStack(d.db.Model(&model.StatisticsBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).First(branch).Error)
return
}
// DeleteAggregateBranch ...
func (d *Dao) DeleteAggregateBranch(c context.Context, projectID int) error {
return pkgerr.WithStack(d.db.Model(&model.AggregateBranches{}).Where("project_id = ?", projectID).Update("is_deleted", model.BranchDeleted).Error)
}
// HasAggregateBranch ...
func (d *Dao) HasAggregateBranch(c context.Context, projectID int, branchName string) (total int, err error) {
err = pkgerr.WithStack(d.db.Model(&model.AggregateBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).Count(&total).Error)
return
}
// UpdateAggregateBranch ...
func (d *Dao) UpdateAggregateBranch(c context.Context, projectID int, branchName string, aggregateBranch *model.AggregateBranches) error {
return pkgerr.WithStack(d.db.Model(&model.AggregateBranches{}).Where("project_id = ? AND branch_name = ?", projectID, branchName).Save(aggregateBranch).Error)
}
// CreateAggregateBranch ...
func (d *Dao) CreateAggregateBranch(c context.Context, aggregateBranch *model.AggregateBranches) error {
return pkgerr.WithStack(d.db.Create(aggregateBranch).Error)
}
// QueryFirstAggregateBranch ...
func (d *Dao) QueryFirstAggregateBranch(c context.Context, projectID int, branchName string) (branch *model.AggregateBranches, err error) {
branch = &model.AggregateBranches{}
err = pkgerr.WithStack(d.db.Model(&model.AggregateBranches{}).Where("project_id = ? AND branch_name = ? ", projectID, branchName).First(branch).Error)
return
}

View File

@@ -0,0 +1,280 @@
package dao
import (
"context"
"strconv"
"testing"
"go-common/app/admin/ep/saga/model"
"go-common/app/admin/ep/saga/service/utils"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoCommit(t *testing.T) {
convey.Convey("test dao commit", t, func(ctx convey.C) {
var (
commit *model.StatisticsCommits
err error
total int
)
ctx.Convey("When project not exist", func(ctx convey.C) {
total, err = d.HasCommit(55555, "55555")
ctx.Convey("HasCommit err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldEqual, 0)
})
err = d.DelCommit(55555, "55555")
ctx.Convey("DelCommit err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("When project exist", func(ctx convey.C) {
var (
commitID = "11111"
PorjID = 11111
)
ctx.Convey("create commit", func(ctx convey.C) {
info := &model.StatisticsCommits{
CommitID: commitID,
ProjectID: PorjID,
Title: "test",
}
err = d.CreateCommit(info)
ctx.Convey("CreateCommit err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("has commit after create", func(ctx convey.C) {
total, err = d.HasCommit(PorjID, commitID)
ctx.Convey("HasCommit err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldEqual, 1)
})
})
ctx.Convey("query commit", func(ctx convey.C) {
commit, err = d.QueryCommitByID(PorjID, commitID)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(commit.Title, convey.ShouldEqual, "test")
})
})
ctx.Convey("create second temp commit", func(ctx convey.C) {
temp := &model.StatisticsCommits{
CommitID: "33333",
ProjectID: 33333,
Title: "temp",
}
err = d.CreateCommit(temp)
ctx.Convey("CreateCommit again err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("has commit when exist two rows", func(ctx convey.C) {
total, err = d.HasCommit(PorjID, commitID)
ctx.Convey("HasCommit err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldEqual, 1)
})
})
ctx.Convey("update commit", func(ctx convey.C) {
updateInfo := &model.StatisticsCommits{
CommitID: commitID,
ProjectID: PorjID,
Title: "update",
}
err = d.UpdateCommit(PorjID, commitID, updateInfo)
ctx.Convey("UpdateCommit err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("query commit after update", func(ctx convey.C) {
commit, err = d.QueryCommitByID(PorjID, commitID)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(commit.Title, convey.ShouldEqual, "update")
})
})
ctx.Convey("update temp commit", func(ctx convey.C) {
updateInfo := &model.StatisticsCommits{
CommitID: "33333",
ProjectID: 33333,
Title: "update temp",
}
err = d.UpdateCommit(33333, "33333", updateInfo)
ctx.Convey("UpdateCommit temp err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("query temp commit after update", func(ctx convey.C) {
commit, err = d.QueryCommitByID(33333, "33333")
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(commit.Title, convey.ShouldEqual, "update temp")
})
})
ctx.Convey("delete commit", func(ctx convey.C) {
err = d.DelCommit(PorjID, commitID)
ctx.Convey("DelCommit commit exist.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("has commit after delete", func(ctx convey.C) {
total, err = d.HasCommit(PorjID, commitID)
ctx.Convey("HasCommit after delete .", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldEqual, 0)
})
})
ctx.Convey("has temp commit when not delete", func(ctx convey.C) {
total, err = d.HasCommit(33333, "33333")
ctx.Convey("HasCommit after delete .", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldEqual, 1)
})
})
ctx.Convey("delete temp commit", func(ctx convey.C) {
err = d.DelCommit(33333, "33333")
ctx.Convey("DelCommit commit exist.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("has tem commit after delete", func(ctx convey.C) {
total, err = d.HasCommit(33333, "33333")
ctx.Convey("HasCommit after delete .", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldEqual, 0)
})
})
})
})
}
func TestDaoCommitSpe(t *testing.T) {
convey.Convey("test dao commit include special char", t, func(ctx convey.C) {
var err error
commitSpe := &model.StatisticsCommits{
CommitID: "22222",
ProjectID: 22222,
Title: "🇭相关",
}
ctx.Convey("create commit", func(ctx convey.C) {
err = d.CreateCommit(commitSpe)
ctx.Convey("CreateCommit err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldNotBeNil)
ctx.So(err.Error(), convey.ShouldContainSubstring, "Incorrect string value")
})
})
ctx.Convey("create commit after to ascii", func(ctx convey.C) {
commitSpe.Title = strconv.QuoteToASCII(commitSpe.Title)
err = d.CreateCommit(commitSpe)
ctx.Convey("CreateCommit err should be nil after to ascii.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("delete commit", func(ctx convey.C) {
err = d.DelCommit(22222, "22222")
ctx.Convey("DelCommit err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestQueryPipelinesByTime(t *testing.T) {
convey.Convey("QueryPipelinesByTime", t, func(ctx convey.C) {
var (
req = &model.PipelineDataReq{
Branch: "master",
State: "success",
User: "wangweizhen",
}
since = `2018-12-01 00:00:00`
until = `2018-12-02 00:00:00`
)
ctx.Convey("query pipelines info", func(ctx convey.C) {
total, statNum, _, err := d.QueryPipelinesByTime(682, req, since, until)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldBeGreaterThan, 1)
ctx.So(statNum, convey.ShouldBeGreaterThan, 1)
})
})
})
}
func TestMRByProjectID(t *testing.T) {
convey.Convey("QueryMRByProjectID", t, func(ctx convey.C) {
since, until := utils.CalSyncTime()
ctx.Convey("query MRByProjectID info", func(ctx convey.C) {
mrs, err := d.MRByProjectID(context.TODO(), 682, since, until)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(len(mrs), convey.ShouldBeGreaterThan, 0)
})
})
})
}
func TestDaoBranchSpe(t *testing.T) {
convey.Convey("test dao branch include special char", t, func(ctx convey.C) {
var (
branch = &model.StatisticsBranches{
ProjectID: 666,
ProjectName: "六六大顺",
CommitID: "6661",
BranchName: "666",
Protected: false,
Merged: false,
DevelopersCanPush: false,
DevelopersCanMerge: false,
IsDeleted: true,
}
total int
err error
)
ctx.Convey("create branch", func(ctx convey.C) {
err = d.CreateBranch(context.TODO(), branch)
ctx.Convey("CreateBranch err should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("has branch", func(ctx convey.C) {
total, err = d.HasBranch(context.TODO(), 666, "666")
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldBeGreaterThan, 0)
})
})
ctx.Convey("update branch", func(ctx convey.C) {
err = d.UpdateBranch(context.TODO(), 666, "666", branch)
ctx.Convey("Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}

View File

@@ -0,0 +1,13 @@
package dao
import (
"go-common/app/admin/ep/saga/model"
pkgerr "github.com/pkg/errors"
)
// Tasks get all the tasks for the specified project
func (d *Dao) Tasks(projID, status int) (tasks []*model.Task, err error) {
err = pkgerr.WithStack(d.db.Where(&model.Task{ProjID: projID, Status: status}).Find(&tasks).Error)
return
}

View File

@@ -0,0 +1,208 @@
package dao
import (
"database/sql"
"fmt"
"strconv"
"strings"
"go-common/app/admin/ep/saga/model"
"go-common/library/log"
"github.com/pkg/errors"
pkgerr "github.com/pkg/errors"
)
const (
_where = "WHERE"
_and = "AND"
_wildcards = "%"
_contactRightJoinLogSQL = "SELECT m.id,m.name,ml.username,ml.operation_type,ml.operation_result,ml.ctime FROM machines AS m INNER JOIN machine_logs AS ml ON m.id = ml.machine_id"
_contactRightCountSQL = "SELECT count(m.id) FROM machines AS m INNER JOIN machine_logs AS ml ON m.id = ml.machine_id"
)
//var (
// regUserID = regexp.MustCompile(`^\d+$`)
//)
// QueryUserByUserName query user by user name
func (d *Dao) QueryUserByUserName(userName string) (contactInfo *model.ContactInfo, err error) {
contactInfo = &model.ContactInfo{}
err = pkgerr.WithStack(d.db.Where(&model.ContactInfo{UserName: userName}).First(contactInfo).Error)
return
}
// QueryUserByID query user by user ID
func (d *Dao) QueryUserByID(userID string) (contactInfo *model.ContactInfo, err error) {
contactInfo = &model.ContactInfo{}
err = pkgerr.WithStack(d.db.Where(&model.ContactInfo{UserID: userID}).First(contactInfo).Error)
return
}
// UserIds query user ids for the user names 从数据库表ContactInfo查询员工编号
func (d *Dao) UserIds(userNames []string) (userIds string, err error) {
var (
userName string
ids []string
contactInfo *model.ContactInfo
)
if len(userNames) == 0 {
err = errors.Errorf("UserIds: userNames is empty!")
return
}
for _, userName = range userNames {
if contactInfo, err = d.QueryUserByUserName(userName); err != nil {
err = errors.Wrapf(err, "UserIds: no such user (%s) in db, err (%s)", userName, err.Error())
return
}
log.Info("UserIds: username (%s), userid (%s)", userName, contactInfo.UserID)
if contactInfo.UserID != "" {
ids = append(ids, contactInfo.UserID)
}
}
if len(ids) <= 0 {
err = errors.Wrapf(err, "UserIds: failed to find all the users in db, what a pity!")
return
}
userIds = strings.Join(ids, "|")
return
}
// ContactInfos query all the records in contact_infos
func (d *Dao) ContactInfos() (contactInfos []*model.ContactInfo, err error) {
err = pkgerr.WithStack(d.db.Find(&contactInfos).Error)
return
}
// FindContacts find application record by auditor.
func (d *Dao) FindContacts(pn, ps int) (total int64, ars []*model.ContactInfo, err error) {
cdb := d.db.Model(&model.ContactInfo{}).Order("ID desc").Offset((pn - 1) * ps).Limit(ps)
if err = pkgerr.WithStack(cdb.Find(&ars).Error); err != nil {
return
}
if err = pkgerr.WithStack(d.db.Model(&model.ContactInfo{}).Count(&total).Error); err != nil {
return
}
return
}
// CreateContact create contact info record
func (d *Dao) CreateContact(contact *model.ContactInfo) (err error) {
return pkgerr.WithStack(d.db.Create(contact).Error)
}
// DelContact delete the contact info with the specified UserID
func (d *Dao) DelContact(contact *model.ContactInfo) (err error) {
return pkgerr.WithStack(d.db.Delete(contact).Error)
}
// UptContact update the contact information
func (d *Dao) UptContact(contact *model.ContactInfo) (err error) {
return pkgerr.WithStack(d.db.Model(&model.ContactInfo{}).Where(&model.ContactInfo{UserID: contact.UserID}).Updates(*contact).Error)
}
// InsertContactLog insert machine log.
func (d *Dao) InsertContactLog(contactlog *model.ContactLog) (err error) {
return pkgerr.WithStack(d.db.Create(contactlog).Error)
}
// FindMachineLogs Find Machine Logs.
func (d *Dao) FindMachineLogs(queryRequest *model.QueryContactLogRequest) (total int64, machineLogs []*model.AboundContactLog, err error) {
var (
qSQL = _contactRightJoinLogSQL
cSQL = _contactRightCountSQL
rows *sql.Rows
)
if queryRequest.UserID > 0 || queryRequest.UserName != "" || queryRequest.OperateType != "" || queryRequest.OperateUser != "" {
var (
strSQL = ""
logicalWord = _where
)
if queryRequest.UserID > 0 {
strSQL = fmt.Sprintf("%s %s ml.machine_id = %s", strSQL, logicalWord, strconv.FormatInt(queryRequest.UserID, 10))
logicalWord = _and
}
if queryRequest.UserName != "" {
strSQL = fmt.Sprintf("%s %s m.name like '%s'", strSQL, logicalWord, _wildcards+queryRequest.UserName+_wildcards)
logicalWord = _and
}
if queryRequest.OperateType != "" {
strSQL = fmt.Sprintf("%s %s ml.operation_type like '%s'", strSQL, logicalWord, _wildcards+queryRequest.OperateType+_wildcards)
logicalWord = _and
}
if queryRequest.OperateUser != "" {
strSQL = fmt.Sprintf("%s %s ml.username like '%s'", strSQL, logicalWord, _wildcards+queryRequest.OperateUser+_wildcards)
logicalWord = _and
}
qSQL = _contactRightJoinLogSQL + " " + strSQL
cSQL = _contactRightCountSQL + " " + strSQL
}
cDB := d.db.Raw(cSQL)
if err = pkgerr.WithStack(cDB.Count(&total).Error); err != nil {
return
}
gDB := d.db.Raw(qSQL)
if rows, err = gDB.Order("ml.ctime DESC").Offset((queryRequest.PageNum - 1) * queryRequest.PageSize).Limit(queryRequest.PageSize).Rows(); err != nil {
return
}
defer rows.Close()
for rows.Next() {
ml := &model.AboundContactLog{}
if err = rows.Scan(&ml.MachineID, &ml.Name, &ml.Username, &ml.OperateType, &ml.OperateResult, &ml.OperateTime); err != nil {
return
}
machineLogs = append(machineLogs, ml)
}
return
}
// AddWechatCreateLog ...
func (d *Dao) AddWechatCreateLog(req *model.WechatCreateLog) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}
// QueryWechatCreateLog ...
func (d *Dao) QueryWechatCreateLog(ifpage bool, req *model.Pagination, wechatCreateInfo *model.WechatCreateLog) (wechatCreateInfos []*model.WechatCreateLog, total int, err error) {
gDB := d.db.Table("wechat_create_logs").Where(wechatCreateInfo).Model(&model.WechatCreateLog{})
if err = errors.WithStack(gDB.Count(&total).Error); err != nil {
return
}
if ifpage {
gDB = gDB.Order("id DESC").Offset((req.PageNum - 1) * req.PageSize).Limit(req.PageSize).Find(&wechatCreateInfos)
} else {
gDB = gDB.Find(&wechatCreateInfos)
}
if gDB.Error != nil {
if gDB.RecordNotFound() {
err = nil
} else {
err = errors.WithStack(gDB.Error)
}
}
return
}
// CreateChatLog ...
func (d *Dao) CreateChatLog(req *model.WechatChatLog) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}
// CreateMessageLog ...
func (d *Dao) CreateMessageLog(req *model.WechatMessageLog) (err error) {
return pkgerr.WithStack(d.db.Create(req).Error)
}

View File

@@ -0,0 +1,122 @@
package dao
import (
"testing"
"go-common/app/admin/ep/saga/model"
"github.com/smartystreets/goconvey/convey"
)
func TestQueryUserByUserName(t *testing.T) {
convey.Convey("Test QueryUserByUserName", t, func(ctx convey.C) {
contactInfo, err := d.QueryUserByUserName("zhanglin")
ctx.Convey("The err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(contactInfo.NickName, convey.ShouldEqual, "木木哥")
ctx.So(contactInfo.UserID, convey.ShouldEqual, "003207")
})
})
}
func TestQueryUserByID(t *testing.T) {
convey.Convey("Test QueryUserByID", t, func(ctx convey.C) {
contactInfo, err := d.QueryUserByID("003207")
ctx.Convey("The err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(contactInfo.UserName, convey.ShouldEqual, "zhanglin")
ctx.So(contactInfo.NickName, convey.ShouldEqual, "木木哥")
})
})
}
func TestUserIds(t *testing.T) {
convey.Convey("Test UserIds", t, func(ctx convey.C) {
var (
userNames = []string{"zhanglin", "wuwei"}
)
userIds, err := d.UserIds(userNames)
ctx.Convey("The err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(userIds, convey.ShouldEqual, "003207|001396")
})
})
}
func TestWechatContact(t *testing.T) {
convey.Convey("test wechat contact", t, func(ctx convey.C) {
var (
ID = "111"
)
contact := &model.ContactInfo{
ID: ID,
UserName: "lisi",
UserID: "222",
NickName: "sansan",
VisibleSaga: true,
}
contactUpdate := &model.ContactInfo{
ID: ID,
UserName: "zhan",
UserID: "333",
NickName: "sansan",
VisibleSaga: true,
}
ctx.Convey("set contact", func(ctx convey.C) {
err := d.CreateContact(contact)
ctx.Convey("set err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("update contact", func(ctx convey.C) {
err := d.UptContact(contactUpdate)
ctx.Convey("get err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("delete contact", func(ctx convey.C) {
err := d.DelContact(contactUpdate)
ctx.Convey("get err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestWechatCreateLog(t *testing.T) {
convey.Convey("test wechat create log", t, func(ctx convey.C) {
info := &model.WechatCreateLog{
Name: "lisi",
Owner: "zhanglin",
ChatID: "333",
Cuser: "333",
}
info1 := &model.WechatCreateLog{
Name: "lisi",
}
ctx.Convey("set wechat log", func(ctx convey.C) {
err := d.AddWechatCreateLog(info)
ctx.Convey("set err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("query wechat log", func(ctx convey.C) {
infos, total, err := d.QueryWechatCreateLog(false, nil, info1)
ctx.Convey("query err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(total, convey.ShouldBeGreaterThanOrEqualTo, 1)
ctx.So(infos[0].Name, convey.ShouldEqual, "lisi")
})
})
})
}

View File

@@ -0,0 +1,279 @@
package dao
import (
"bytes"
"context"
"encoding/json"
"net/http"
xhttp "net/http"
"net/url"
"strconv"
"strings"
"go-common/app/admin/ep/saga/model"
"go-common/library/ecode"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
qyWechatURL = "https://qyapi.weixin.qq.com"
corpID = "wx0833ac9926284fa5" // 企业微信Bilibili的企业ID
departmentID = "12" // 公司统一用部门ID
//corpID = "wwa24b497e5efdd78c" // 香满庭
//departmentID = "2" // 香满庭
_ajSessionID = "_AJSESSIONID"
)
// WechatPushMsg wechat push text message to specified user 发送企业微信信息
func (d *Dao) WechatPushMsg(c context.Context, token string, txtMsg *model.TxtNotification) (invalidUser string, err error) {
var (
u string
params = url.Values{}
res struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
InvalidUser string `json:"invaliduser"`
InvalidParty string `json:"invalidparty"`
InvalidTag string `json:"invalidtag"`
}
)
u = qyWechatURL + "/cgi-bin/message/send"
params.Set("access_token", token)
if err = d.PostJSON(c, u, "", params, &res, txtMsg); err != nil {
log.Info("WechatPushMsg PostJSON err (%+v)", err)
return
}
if res.ErrCode != 0 || res.InvalidUser != "" || res.InvalidParty != "" || res.InvalidTag != "" {
invalidUser = res.InvalidUser
err = errors.Errorf("WechatPushMsg: errcode: %d, errmsg: %s, invalidUser: %s, invalidParty: %s, invalidTag: %s",
res.ErrCode, res.ErrMsg, res.InvalidUser, res.InvalidParty, res.InvalidTag)
log.Info("WechatPushMsg err (%+v)", err)
return
}
return
}
// PostJSON post http request with json params.
func (d *Dao) PostJSON(c context.Context, uri, ip string, params url.Values, res interface{}, v interface{}) (err error) {
var (
body = &bytes.Buffer{}
req *xhttp.Request
url string
en string
)
if err = json.NewEncoder(body).Encode(v); err != nil {
return
}
url = uri
if en = params.Encode(); en != "" {
url += "?" + en
}
if req, err = xhttp.NewRequest(xhttp.MethodPost, url, body); err != nil {
return
}
if err = d.httpClient.Do(c, req, &res); err != nil {
return
}
return
}
// WechatAccessToken query access token with the specified secret 企业微信api获取公司token
func (d *Dao) WechatAccessToken(c context.Context, secret string) (token string, expire int32, err error) {
var (
u string
params = url.Values{}
res struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
AccessToken string `json:"access_token"`
ExpiresIn int32 `json:"expires_in"`
}
)
u = qyWechatURL + "/cgi-bin/gettoken"
params.Set("corpid", corpID)
params.Set("corpsecret", secret)
if err = d.httpClient.Get(c, u, "", params, &res); err != nil {
return
}
if res.ErrCode != 0 {
err = errors.Errorf("WechatAccessToken: errcode: %d, errmsg: %s", res.ErrCode, res.ErrMsg)
return
}
token = res.AccessToken
expire = res.ExpiresIn
return
}
// WechatContacts query all the contacts
func (d *Dao) WechatContacts(c context.Context, accessToken string) (contacts []*model.ContactInfo, err error) {
var (
u string
params = url.Values{}
res struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
UserList []*model.ContactInfo `json:"userlist"`
}
)
u = qyWechatURL + "/cgi-bin/user/list"
params.Set("access_token", accessToken)
params.Set("department_id", departmentID)
params.Set("fetch_child", "1")
if err = d.httpClient.Get(c, u, "", params, &res); err != nil {
return
}
if res.ErrCode != 0 {
err = errors.Errorf("WechatContacts: errcode: %d, errmsg: %s", res.ErrCode, res.ErrMsg)
return
}
contacts = res.UserList
return
}
// WechatSagaVisible get all the user ids who can visiable saga 获取用户ID列表
func (d *Dao) WechatSagaVisible(c context.Context, accessToken string, agentID int) (users []*model.UserInfo, err error) {
var (
u string
params = url.Values{}
res struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
VisibleUsers model.AllowUserInfo `json:"allow_userinfos"`
}
)
u = qyWechatURL + "/cgi-bin/agent/get"
params.Set("access_token", accessToken)
params.Set("agentid", strconv.Itoa(agentID))
if err = d.httpClient.Get(c, u, "", params, &res); err != nil {
return
}
if res.ErrCode != 0 {
err = errors.Errorf("WechatSagaVisible: errcode: %d, errmsg: %s", res.ErrCode, res.ErrMsg)
return
}
users = res.VisibleUsers.Users
return
}
// WechatParams ...
func (d *Dao) WechatParams(c context.Context, u string, params url.Values, resp interface{}) (err error) {
var (
req *xhttp.Request
en string
)
if en = params.Encode(); en != "" {
u += "?" + en
}
if req, err = xhttp.NewRequest(xhttp.MethodGet, u, nil); err != nil {
return
}
return d.httpClient.Do(c, req, &resp)
}
// NewRequest ...
func (d *Dao) NewRequest(method, url string, v interface{}) (req *http.Request, err error) {
body := &bytes.Buffer{}
if method != http.MethodGet {
if err = json.NewEncoder(body).Encode(v); err != nil {
log.Error("json encode value(%s), error(%v) ", v, err)
return
}
}
if req, err = http.NewRequest(method, url, body); err != nil {
log.Error("http new request url(%s), error(%v)", url, err)
}
return
}
// QueryAllConfigFile ...
func (d *Dao) QueryAllConfigFile(c context.Context, sessionID, url string) (resp *model.ConfigData, err error) {
var (
req *http.Request
respValue = &model.SvenResp{}
)
log.Info("QueryAllConfigFile: sessionID: %s, url: %s", sessionID, url)
if req, err = d.NewRequest(http.MethodGet, url, nil); err != nil {
return
}
req.Header.Set("Cookie", _ajSessionID+"="+sessionID)
if err = d.httpClient.Do(c, req, &respValue); err != nil {
return
}
if respValue.Code != ecode.OK.Code() {
err = errors.Wrapf(ecode.Int(respValue.Code), "QueryAllConfigFile failed, sessionID(%s), url(%s)", sessionID, url)
return
}
resp = respValue.Data
return
}
// QueryConfigFileContent ...
func (d *Dao) QueryConfigFileContent(c context.Context, sessionID, url string) (content string, err error) {
var (
req *http.Request
respValue = &model.ConfigValueResp{}
)
if req, err = d.NewRequest(http.MethodGet, url, nil); err != nil {
return
}
req.Header.Set("Cookie", _ajSessionID+"="+sessionID)
if err = d.httpClient.Do(c, req, respValue); err != nil {
return
}
if respValue.Code != ecode.OK.Code() {
err = errors.Wrapf(ecode.Int(respValue.Code), "QueryConfigFileContent failed, sessionID(%s), url(%s)", sessionID, url)
return
}
if respValue.Data != nil {
content = respValue.Data.Comment
}
return
}
// RequestConfig ...
func (d *Dao) RequestConfig(c context.Context, sessionID, reqUrl string, params url.Values) (resp *model.CommonResp, err error) {
var req *http.Request
if req, err = http.NewRequest("POST", reqUrl, strings.NewReader(params.Encode())); err != nil {
log.Error("http.NewRequest error(%v) | uri(%s) params(%s)", err, reqUrl, params.Encode())
return
}
log.Info("RequestConfig url: %v", req.URL)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Cookie", _ajSessionID+"="+sessionID)
if err = d.httpClient.Do(c, req, &resp); err != nil {
log.Error("RequestConfig err%+v", err)
return
}
if resp.Code != ecode.OK.Code() {
err = errors.Wrapf(ecode.Int(resp.Code), "RequestConfig failed, sessionID(%s), url(%s)", sessionID, reqUrl)
return
}
return
}

120
app/admin/ep/saga/dao/mc.go Normal file
View File

@@ -0,0 +1,120 @@
package dao
import (
"context"
"go-common/app/admin/ep/saga/model"
"go-common/library/cache/memcache"
"github.com/pkg/errors"
)
func (d *Dao) pingMC(c context.Context) (err error) {
conn := d.mc.Get(c)
defer conn.Close()
if err = conn.Set(&memcache.Item{Key: "ping", Value: []byte{1}, Expiration: 0}); err != nil {
err = errors.Wrap(err, "conn.Store(set,ping,1)")
}
return
}
// SetData set data info to memcache
func (d *Dao) SetData(c context.Context, key string, dataMap map[string]*model.TeamDataResp) (err error) {
var (
conn = d.mc.Get(c)
item *memcache.Item
//dataMap = make(map[string]*model.TeamDataResp)
)
defer conn.Close()
item = &memcache.Item{Key: key, Object: dataMap, Expiration: 0, Flags: memcache.FlagJSON}
if err = conn.Set(item); err != nil {
err = errors.Wrapf(err, "conn.Set(%s,%v)", key, dataMap)
return
}
return
}
// GetData get data info from memcache
func (d *Dao) GetData(c context.Context, key string, dataMap *map[string]*model.TeamDataResp) (err error) {
var (
conn = d.mc.Get(c)
reply *memcache.Item
)
defer conn.Close()
reply, err = conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
return
}
err = errors.Wrapf(err, "conn.Get(%s)", key)
return
}
if err = conn.Scan(reply, dataMap); err != nil {
err = errors.Wrapf(err, "reply.Scan(%s)", string(reply.Value))
return
}
return
}
// DeleteData delete data info in memcache
func (d *Dao) DeleteData(c context.Context, key string) (err error) {
var (
conn = d.mc.Get(c)
)
defer conn.Close()
err = conn.Delete(key)
if err != nil {
if err == memcache.ErrNotFound {
err = nil
return
}
err = errors.Wrapf(err, "conn.Delete(%s)", key)
}
return
}
// SetPipeline set pipeline info info to memcache
func (d *Dao) SetPipeline(c context.Context, key string, pipeline *model.PipelineDataResp) (err error) {
var (
conn = d.mc.Get(c)
item *memcache.Item
)
defer conn.Close()
item = &memcache.Item{Key: key, Object: pipeline, Expiration: 0, Flags: memcache.FlagJSON}
if err = conn.Set(item); err != nil {
err = errors.Wrapf(err, "conn.Set(%s,%v)", key, pipeline)
return
}
return
}
// GetPipeline get pipeline info from memcache
func (d *Dao) GetPipeline(c context.Context, key string) (pipeline *model.PipelineDataResp, err error) {
var (
conn = d.mc.Get(c)
reply *memcache.Item
)
defer conn.Close()
reply, err = conn.Get(key)
if err != nil {
if err == memcache.ErrNotFound {
return
}
err = errors.Wrapf(err, "conn.Get(%s,%v)", key, pipeline)
return
}
pipeline = new(model.PipelineDataResp)
if err = conn.Scan(reply, pipeline); err != nil {
err = errors.Wrapf(err, "reply.Scan(%s)", string(reply.Value))
}
return
}

View File

@@ -0,0 +1,100 @@
package dao
import (
"context"
"testing"
"go-common/app/admin/ep/saga/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoPingMC(t *testing.T) {
convey.Convey("pingMC", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.pingMC(c)
ctx.Convey("The err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestMcData(t *testing.T) {
convey.Convey("test mc data", t, func(ctx convey.C) {
var (
c = context.Background()
key = "111"
dataSet = make(map[string]*model.TeamDataResp)
dataGet = make(map[string]*model.TeamDataResp)
)
teamData := &model.TeamDataResp{
Department: "live",
Business: "ios",
QueryDes: "description",
Total: 10,
}
dataSet["zhangsan"] = teamData
ctx.Convey("set data", func(ctx convey.C) {
err := d.SetData(c, key, dataSet)
ctx.Convey("set err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("get data", func(ctx convey.C) {
err := d.GetData(c, key, &dataGet)
_, ok := dataGet["zhangsan"]
ctx.Convey("get err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldEqual, true)
ctx.So(dataGet["zhangsan"].Department, convey.ShouldEqual, "live")
ctx.So(dataGet["zhangsan"].Total, convey.ShouldEqual, 10)
})
})
ctx.Convey("delete data", func(ctx convey.C) {
err := d.DeleteData(c, key)
_, ok := dataGet["zhangsan"]
ctx.Convey("get err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(ok, convey.ShouldEqual, false)
})
})
})
}
func TestMcPipeline(t *testing.T) {
convey.Convey("test mc Pipeline", t, func(ctx convey.C) {
var (
c = context.Background()
key = "111"
)
pipelineData := &model.PipelineDataResp{
Department: "openplatform",
Business: "android",
QueryDes: "description",
Total: 11,
}
ctx.Convey("set pipeline data", func(ctx convey.C) {
err := d.SetPipeline(c, key, pipelineData)
ctx.Convey("set err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
ctx.Convey("get pipeline data", func(ctx convey.C) {
pipeline, err := d.GetPipeline(c, key)
ctx.Convey("get err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(pipeline.Business, convey.ShouldEqual, "android")
ctx.So(pipeline.Total, convey.ShouldEqual, 11)
})
})
})
}

View File

@@ -0,0 +1,195 @@
package dao
import (
"context"
"encoding/json"
"fmt"
"go-common/app/admin/ep/saga/model"
"go-common/library/cache/redis"
"go-common/library/log"
"github.com/pkg/errors"
)
const (
requiredViableUsersKeyRedis = "saga_wechat_require_visible_users_key"
)
func (d *Dao) pingRedis(c context.Context) (err error) {
conn := d.redis.Get(c)
_, err = conn.Do("SET", "PING", "PONG")
defer conn.Close()
return
}
func weixinTokenKeyRedis(key string) string {
return fmt.Sprintf("saga_weixin_token_%s", key)
}
// AccessTokenRedis get access token from redis
func (d *Dao) AccessTokenRedis(c context.Context, key string) (token string, err error) {
var (
wkey = weixinTokenKeyRedis(key)
conn = d.redis.Get(c)
value []byte
)
defer conn.Close()
if value, err = redis.Bytes(conn.Do("GET", wkey)); err != nil {
if err == redis.ErrNil {
err = nil
}
return
}
if err = json.Unmarshal(value, &token); err != nil {
err = errors.WithStack(err)
return
}
return
}
// SetAccessTokenRedis set the access token to redis
func (d *Dao) SetAccessTokenRedis(c context.Context, key, token string, expire int32) (err error) {
var (
wkey = weixinTokenKeyRedis(key)
conn = d.redis.Get(c)
item []byte
)
defer conn.Close()
if item, err = json.Marshal(token); err != nil {
err = errors.WithStack(err)
return
}
if err = conn.Send("SET", wkey, item, "EX", expire); err != nil {
return
}
if err = conn.Flush(); err != nil {
return
}
if _, err = conn.Receive(); err != nil {
return
}
return
}
// RequireVisibleUsersRedis get wechat require visible users from redis
func (d *Dao) RequireVisibleUsersRedis(c context.Context, userMap *map[string]model.RequireVisibleUser) (err error) {
var (
conn = d.redis.Get(c)
reply []byte
)
defer conn.Close()
if reply, err = redis.Bytes(conn.Do("GET", requiredViableUsersKeyRedis)); err != nil {
if err == redis.ErrNil {
log.Info("no such key (%s) in cache, err (%s)", requiredViableUsersKeyRedis, err.Error())
err = nil
}
return
}
if err = json.Unmarshal(reply, &userMap); err != nil {
err = errors.WithStack(err)
return
}
return
}
// SetRequireVisibleUsersRedis set wechat require visible users to redis
func (d *Dao) SetRequireVisibleUsersRedis(c context.Context, contactInfo *model.ContactInfo) (err error) {
var (
conn = d.redis.Get(c)
item []byte
userMap = make(map[string]model.RequireVisibleUser)
)
defer conn.Close()
if err = d.RequireVisibleUsersRedis(c, &userMap); err != nil {
log.Error("get require visible user error(%v)", err)
return
}
user := model.RequireVisibleUser{
UserName: contactInfo.UserName,
NickName: contactInfo.NickName,
}
userMap[contactInfo.UserID] = user
if item, err = json.Marshal(userMap); err != nil {
err = errors.WithStack(err)
return
}
if err = conn.Send("SET", requiredViableUsersKeyRedis, item); err != nil {
return
}
if err = conn.Flush(); err != nil {
return
}
if _, err = conn.Receive(); err != nil {
err = errors.Wrapf(err, "conn.Set(%s,%v)", requiredViableUsersKeyRedis, userMap)
return
}
return
}
// DeleteRequireVisibleUsersRedis delete the wechat require visible key in redis
func (d *Dao) DeleteRequireVisibleUsersRedis(c context.Context) (err error) {
var (
conn = d.redis.Get(c)
)
defer conn.Close()
if err = conn.Send("DEL", requiredViableUsersKeyRedis); err != nil {
return
}
if err = conn.Flush(); err != nil {
return
}
if _, err = conn.Receive(); err != nil {
err = errors.Wrapf(err, "conn.Delete(%s)", requiredViableUsersKeyRedis)
return
}
return
}
// SetItemRedis ...
func (d *Dao) SetItemRedis(c context.Context, key string, value interface{}, ttl int) (err error) {
var (
conn = d.redis.Get(c)
bs []byte
)
defer conn.Close()
if bs, err = json.Marshal(value); err != nil {
return errors.WithStack(err)
}
if ttl == 0 {
if _, err = conn.Do("SET", key, bs); err != nil {
return errors.Wrapf(err, "conn.Do(SET %s, %s) error(%v)", key, value, err)
}
} else {
if _, err = conn.Do("SET", key, bs, "EX", ttl); err != nil {
return errors.Wrapf(err, "conn.Do(SET %s, %s) error(%v)", key, value, err)
}
}
return
}
// ItemRedis ...
func (d *Dao) ItemRedis(c context.Context, key string, value interface{}) (err error) {
var (
conn = d.redis.Get(c)
bs []byte
)
defer conn.Close()
if bs, err = redis.Bytes(conn.Do("GET", key)); err != nil {
if err == redis.ErrNil {
return
}
return errors.Wrapf(err, "conn.Get(%s)", key)
}
if err = json.Unmarshal(bs, &value); err != nil {
return errors.WithStack(err)
}
return
}

View File

@@ -0,0 +1,116 @@
package dao
import (
"context"
"testing"
"go-common/app/admin/ep/saga/model"
"github.com/smartystreets/goconvey/convey"
)
func TestDaoWeixinTokenKeyRedis(t *testing.T) {
convey.Convey("weixinTokenKeyRedis", t, func(ctx convey.C) {
var (
key = "111"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
p1 := weixinTokenKeyRedis(key)
ctx.Convey("Then p1 should not be nil.", func(ctx convey.C) {
ctx.So(p1, convey.ShouldEqual, "saga_weixin_token__111")
})
})
})
}
func TestDaoAccessTokenRedis(t *testing.T) {
convey.Convey("AccessTokenRedis", t, func(ctx convey.C) {
var (
c = context.Background()
key = "111"
token = "sdfgsdgfdg"
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.SetAccessTokenRedis(c, key, token, -1)
ctx.Convey("Set Access Token. Then err should be nil. ", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
token, err := d.AccessTokenRedis(c, key)
ctx.Convey("get access token. Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(token, convey.ShouldEqual, token)
})
})
})
}
func TestDaoRequireVisibleUsersRedis(t *testing.T) {
convey.Convey("RequireVisibleUsersRedis", t, func(ctx convey.C) {
var (
c = context.Background()
userID = "222"
contactInfo = &model.ContactInfo{
ID: "111",
UserName: "zhanglin",
UserID: userID,
NickName: "mumuge",
VisibleSaga: true,
}
userMap = make(map[string]model.RequireVisibleUser)
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.SetRequireVisibleUsersRedis(c, contactInfo)
ctx.Convey("Set Visible Users. Then err should be nil. ", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
err = d.RequireVisibleUsersRedis(c, &userMap)
ctx.Convey("get Visible Users. Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(userMap[userID].UserName, convey.ShouldEqual, "zhanglin")
ctx.So(userMap[userID].NickName, convey.ShouldEqual, "mumuge")
})
err = d.DeleteRequireVisibleUsersRedis(c)
ctx.Convey("delete Visible Users. Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
})
})
}
func TestDaoGetItemRedis(t *testing.T) {
convey.Convey("GetItemRedis", t, func(ctx convey.C) {
var (
c = context.Background()
key = "333"
contactInfo = &model.ContactInfo{
ID: "111",
UserName: "zhanglin",
UserID: "333",
NickName: "mumuge",
VisibleSaga: true,
}
getContactInfo *model.ContactInfo
)
ctx.Convey("When everything gose positive", func(ctx convey.C) {
err := d.SetItemRedis(c, key, contactInfo, 0)
ctx.Convey("Set Item. Then err should be nil. ", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
})
err = d.ItemRedis(c, key, &getContactInfo)
ctx.Convey("get Item. Then err should be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(getContactInfo.UserName, convey.ShouldEqual, "zhanglin")
ctx.So(getContactInfo.UserID, convey.ShouldEqual, "333")
ctx.So(getContactInfo.NickName, convey.ShouldEqual, "mumuge")
ctx.So(getContactInfo.VisibleSaga, convey.ShouldEqual, true)
})
})
})
}