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,66 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"archive_test.go",
"dao_test.go",
"feedback_test.go",
"search_test.go",
"skill_test.go",
"tag_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/model/academy:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"archive.go",
"dao.go",
"feedback.go",
"search.go",
"skill.go",
"tag.go",
],
importpath = "go-common/app/interface/main/creative/dao/academy",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/dao/tool:go_default_library",
"//app/interface/main/creative/model/academy:go_default_library",
"//library/database/elastic:go_default_library",
"//library/database/sql:go_default_library",
"//library/ecode:go_default_library",
"//library/log:go_default_library",
"//library/xstr: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,135 @@
package academy
import (
"context"
"fmt"
"database/sql"
sqlx "go-common/library/database/sql"
"go-common/library/xstr"
"go-common/app/interface/main/creative/dao/tool"
"go-common/app/interface/main/creative/model/academy"
"go-common/library/log"
)
const (
// select
_getArcByOIDAndBusSQL = "SELECT id, oid, business, state, ctime, mtime FROM academy_archive WHERE oid=? AND business=?"
_getSearByBusSQL = "SELECT a.oid, a.business, GROUP_CONCAT(t.tid SEPARATOR ',') AS tidstr FROM academy_archive AS a LEFT JOIN academy_archive_tag as t on t.oid = a.oid"
_getArcCountSQL = "SELECT count(DISTINCT a.oid) FROM (SELECT oid FROM academy_archive WHERE state=0 AND business=?) AS a LEFT JOIN academy_archive_tag as t on t.oid=a.oid"
_getTagByOidsSQL = "SELECT oid, tid FROM academy_archive_tag WHERE state=0 AND oid IN (%s)"
)
//Archive get one achive.
func (d *Dao) Archive(c context.Context, oid int64, bs int) (a *academy.Archive, err error) {
row := d.db.QueryRow(c, _getArcByOIDAndBusSQL, oid, bs)
a = &academy.Archive{}
if err = row.Scan(&a.ID, &a.OID, &a.Business, &a.State, &a.CTime, &a.MTime); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("row.Scan error(%v)", err)
}
return
}
//ArchiveCount get all achive count.
func (d *Dao) ArchiveCount(c context.Context, tids []int64, bs int) (count int, err error) {
sqlStr := _getArcCountSQL
if len(tids) > 0 {
sqlStr += fmt.Sprintf(" WHERE t.tid IN (%s)", xstr.JoinInts(tids))
}
if err = d.db.QueryRow(c, sqlStr, bs).Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("d.db.QueryRow error(%v)", err)
}
return
}
//SearchArchive get all oid & tid.
func (d *Dao) SearchArchive(c context.Context, tidsMap map[int][]int64, bs int) (res []*academy.Archive, err error) {
var (
rows *sqlx.Rows
tids []int64
)
for _, v := range tidsMap {
tids = append(tids, v...)
}
total := len(tids)
sqlStr := _getSearByBusSQL
if total > 0 {
sqlStr += fmt.Sprintf(" WHERE a.state=0 AND a.business=? AND t.tid IN (%s) GROUP BY a.oid ORDER BY a.mtime DESC", xstr.JoinInts(tids))
} else {
sqlStr += " WHERE a.state=0 AND a.business=? GROUP BY a.oid ORDER BY a.mtime DESC"
}
rows, err = d.db.Query(c, sqlStr, bs)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*academy.Archive, 0)
origin := make([]*academy.Archive, 0)
var tidStr string
for rows.Next() {
a := &academy.Archive{}
if err = rows.Scan(&a.OID, &a.Business, &tidStr); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
a.TIDs, _ = xstr.SplitInts(tidStr)
origin = append(origin, a)
}
if total > 0 {
var cts, ots, clts, acts []int64
if v, ok := tidsMap[academy.Course]; ok {
cts = v
}
if v, ok := tidsMap[academy.Operation]; ok {
ots = v
}
if v, ok := tidsMap[academy.Classify]; ok {
clts = v
}
if v, ok := tidsMap[academy.ArticleClass]; ok {
acts = v
}
for _, v := range origin {
log.Info("search tag 课程级别(%+v)|运营标签(%+v)|分类标签(%+v)|专栏分类(%+v)|当前稿件标签(%+v)", cts, ots, clts, acts, v.TIDs)
if tool.ContainAtLeastOne(cts, v.TIDs) &&
tool.ContainAtLeastOne(ots, v.TIDs) &&
tool.ContainAtLeastOne(clts, v.TIDs) &&
tool.ContainAtLeastOne(acts, v.TIDs) {
res = append(res, v)
}
}
} else {
res = origin
}
return
}
//ArchiveTagsByOids get all tids by oids.
func (d *Dao) ArchiveTagsByOids(c context.Context, oids []int64) (res map[int64][]int64, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_getTagByOidsSQL, xstr.JoinInts(oids)))
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make(map[int64][]int64)
for rows.Next() {
a := &academy.ArchiveTag{}
if err = rows.Scan(&a.OID, &a.TID); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
res[a.OID] = append(res[a.OID], a.TID)
}
return
}

View File

@@ -0,0 +1,75 @@
package academy
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestAcademyArchive(t *testing.T) {
convey.Convey("Archive", t, func(ctx convey.C) {
var (
c = context.Background()
oid = int64(0)
bs = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
a, err := d.Archive(c, oid, bs)
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 TestAcademyArchiveCount(t *testing.T) {
convey.Convey("ArchiveCount", t, func(ctx convey.C) {
var (
c = context.Background()
tids = []int64{}
bs = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
count, err := d.ArchiveCount(c, tids, bs)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}
func TestAcademySearchArchive(t *testing.T) {
convey.Convey("SearchArchive", t, func(ctx convey.C) {
var (
c = context.Background()
tidsMap map[int][]int64
bs = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SearchArchive(c, tidsMap, bs)
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 TestAcademyArchiveTagsByOids(t *testing.T) {
convey.Convey("ArchiveTagsByOids", t, func(ctx convey.C) {
var (
c = context.Background()
oids = []int64{1, 2}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.ArchiveTagsByOids(c, oids)
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,36 @@
package academy
import (
"context"
"go-common/app/interface/main/creative/conf"
"go-common/library/database/elastic"
"go-common/library/database/sql"
)
// Dao define
type Dao struct {
c *conf.Config
db *sql.DB
es *elastic.Elastic
}
// New init dao
func New(c *conf.Config) (d *Dao) {
d = &Dao{
c: c,
db: sql.NewMySQL(c.DB.Creative),
es: elastic.NewElastic(nil),
}
return
}
// Ping db
func (d *Dao) Ping(c context.Context) (err error) {
return d.db.Ping(c)
}
// Close db
func (d *Dao) Close() (err error) {
return d.db.Close()
}

View File

@@ -0,0 +1,36 @@
package academy
import (
"flag"
"os"
"testing"
"go-common/app/interface/main/creative/conf"
)
var (
d *Dao
)
func TestMain(m *testing.M) {
if os.Getenv("DEPLOY_ENV") != "" {
flag.Set("app_id", "main.archive.creative")
flag.Set("conf_token", "96b6a6c10bb311e894c14a552f48fef8")
flag.Set("tree_id", "2305")
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.toml")
}
flag.Parse()
if err := conf.Init(); err != nil {
panic(err)
}
d = New(conf.Conf)
m.Run()
os.Exit(0)
}

View File

@@ -0,0 +1,24 @@
package academy
import (
"context"
"go-common/app/interface/main/creative/model/academy"
"go-common/library/log"
)
const (
// insert
_inFbSQL = "INSERT IGNORE INTO academy_feedback (category, course, suggest, ctime, mtime, mid) VALUES (?,?,?,?,?,?)"
)
// AddFeedBack add academy_feedback.
func (d *Dao) AddFeedBack(c context.Context, f *academy.FeedBack, mid int64) (id int64, err error) {
res, err := d.db.Exec(c, _inFbSQL, f.Category, f.Course, f.Suggest, f.CTime, f.MTime, mid)
if err != nil {
log.Error("d.db.Exec error(%v)", err)
return
}
id, err = res.LastInsertId()
return
}

View File

@@ -0,0 +1,27 @@
package academy
import (
"context"
"testing"
"go-common/app/interface/main/creative/model/academy"
"github.com/smartystreets/goconvey/convey"
)
func TestAcademyAddFeedBack(t *testing.T) {
convey.Convey("AddFeedBack", t, func(ctx convey.C) {
var (
c = context.Background()
f = &academy.FeedBack{}
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
id, err := d.AddFeedBack(c, f, mid)
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,96 @@
package academy
import (
"context"
"time"
"go-common/app/interface/main/creative/model/academy"
"go-common/library/database/elastic"
"go-common/library/ecode"
"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").Index("academy_archive").Fields("oid", "tid", "business")
if aca.Business > 0 {
r.WhereEq("business", aca.Business)
}
r.WhereEq("state", 0).Pn(aca.Pn).Ps(aca.Ps) //state 创作学院稿件状态
r.WhereEq("arc_state", 0) //arc_state 原始稿件状态(视频、专栏)
r.WhereEq("deleted_time", 0) //过滤删除的专栏
if aca.Seed > 0 {
r.OrderRandomSeed(time.Unix(aca.Seed, 0).Format("2006-01-02 15:04:05")) //随机推荐
}
if aca.Keyword != "" {
r.WhereLike([]string{"title", "tid_name"}, []string{aca.Keyword}, true, "low").Highlight(true)
}
if aca.Order != "" {
r.Order(aca.Order, "desc").OrderScoreFirst(false) //order: click (最多点击数), fav(最多收藏数), pubtime(最新发布时间), hot(最热值)
}
if aca.Duration > 0 { //h5 时长筛选 1(1-10分钟 ) 2(10-30分钟) 3(30-60分钟) 4(60分钟+)
switch aca.Duration {
case 1:
r.WhereRange("duration", 1*60, 10*60, elastic.RangeScopeLcRc)
case 2:
r.WhereRange("duration", 10*60, 30*60, elastic.RangeScopeLcRc)
case 3:
r.WhereRange("duration", 30*60, 60*60, elastic.RangeScopeLcRc)
case 4:
r.WhereRange("duration", 60*60, nil, elastic.RangeScopeLcRo)
}
if aca.Order == "" {
r.Order("duration", "desc")
}
}
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)
}
}
res = &academy.SearchResult{}
log.Info("ArchivesWithES r.Scan params(%s)", r.Params())
if err = r.Scan(c, res); err != nil {
log.Error("ArchivesWithES r.Scan|error(%v)", err)
err = ecode.CreativeSearchErr
return
}
return
}
//Keywords get all keywords.
func (d *Dao) Keywords(c context.Context) (res []*academy.SearchKeywords, err error) {
_getKWSQL := "SELECT id, rank, parent_id, state, name, comment FROM academy_search_keywords WHERE state=0 ORDER BY rank ASC"
rows, err := d.db.Query(c, _getKWSQL)
if err != nil {
log.Error("Keywords d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*academy.SearchKeywords, 0)
for rows.Next() {
o := &academy.SearchKeywords{}
if err = rows.Scan(&o.ID, &o.Rank, &o.ParentID, &o.State, &o.Name, &o.Comment); err != nil {
log.Error("Keywords rows.Scan error(%v)", err)
return
}
if o.Name == "" {
continue
}
res = append(res, o)
}
return
}

View File

@@ -0,0 +1,50 @@
package academy
import (
"context"
"testing"
"go-common/app/interface/main/creative/model/academy"
"github.com/smartystreets/goconvey/convey"
)
func TestAcademyArchivesWithES(t *testing.T) {
convey.Convey("ArchivesWithES", t, func(ctx convey.C) {
var (
c = context.Background()
aca = &academy.EsParam{
OID: int64(1),
Tid: []int64{1, 2},
Business: 1,
Pn: 10,
Ps: 20,
Keyword: "string",
Order: "",
IP: "",
}
)
ctx.Convey("When everything goes 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)
})
})
})
}
func TestAcademyKeywords(t *testing.T) {
convey.Convey("Keywords", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.Keywords(c)
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,201 @@
package academy
import (
"context"
"fmt"
"go-common/app/interface/main/creative/model/academy"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
// select
_getOccSQL = "SELECT id, rank, name, `desc`, main_step, main_software, logo FROM academy_occupation ORDER BY rank ASC"
_getSkillSQL = "SELECT id, oid, name, `desc` FROM academy_skill ORDER BY id ASC"
_getSkillArcSQL = "SELECT id, aid, type, pid, skid, sid FROM academy_arc_skill WHERE state=0"
_getSkillArcCntSQL = "SELECT count(*) FROM academy_arc_skill WHERE state=0"
)
//Occupations get all occupation.
func (d *Dao) Occupations(c context.Context) (res []*academy.Occupation, err error) {
rows, err := d.db.Query(c, _getOccSQL)
if err != nil {
log.Error("Occupations d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*academy.Occupation, 0)
for rows.Next() {
o := &academy.Occupation{}
if err = rows.Scan(&o.ID, &o.Rank, &o.Name, &o.Desc, &o.MainStep, &o.MainSoftWare, &o.Logo); err != nil {
log.Error("Occupations rows.Scan error(%v)", err)
return
}
res = append(res, o)
}
return
}
//Skills get all Skill.
func (d *Dao) Skills(c context.Context) (res []*academy.Skill, err error) {
rows, err := d.db.Query(c, _getSkillSQL)
if err != nil {
log.Error("Skills d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*academy.Skill, 0)
for rows.Next() {
o := &academy.Skill{}
if err = rows.Scan(&o.ID, &o.OID, &o.Name, &o.Desc); err != nil {
log.Error("Skills rows.Scan error(%v)", err)
return
}
res = append(res, o)
}
return
}
//SkillArcs get all SkillArc.
func (d *Dao) SkillArcs(c context.Context, pids, skids, sids []int64, offset, limit int) (res []*academy.SkillArc, err error) {
var (
whereStr = _getSkillArcSQL
limiStr = " ORDER BY id ASC LIMIT ?,?"
rows *sql.Rows
)
if len(pids) > 0 {
whereStr += fmt.Sprintf(" AND pid IN (%s)"+limiStr, xstr.JoinInts(pids))
} else if len(skids) > 0 {
whereStr += fmt.Sprintf(" AND skid IN (%s)"+limiStr, xstr.JoinInts(skids))
} else if len(sids) > 0 {
whereStr += fmt.Sprintf(" AND sid IN (%s)"+limiStr, xstr.JoinInts(sids))
} else {
whereStr += limiStr
}
rows, err = d.db.Query(c, whereStr, offset, limit)
if err != nil {
log.Error("SkillArcs d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*academy.SkillArc, 0)
for rows.Next() {
o := &academy.SkillArc{}
if err = rows.Scan(&o.ID, &o.AID, &o.Type, &o.PID, &o.SkID, &o.SID); err != nil {
log.Error("SkillArcs rows.Scan error(%v)", err)
return
}
res = append(res, o)
}
return
}
//SkillArcCount get all skill achive count.
func (d *Dao) SkillArcCount(c context.Context, pids, skids, sids []int64) (count int, err error) {
var (
whereStr = _getSkillArcCntSQL
)
if len(pids) > 0 {
whereStr += fmt.Sprintf(" AND pid IN (%s)", xstr.JoinInts(pids))
} else if len(skids) > 0 {
whereStr += fmt.Sprintf(" AND skid IN (%s)", xstr.JoinInts(skids))
} else if len(sids) > 0 {
whereStr += fmt.Sprintf(" AND sid IN (%s)", xstr.JoinInts(sids))
}
if err = d.db.QueryRow(c, whereStr).Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("d.db.QueryRow error(%v)", err)
}
return
}
// PlayAdd add play archive.
func (d *Dao) PlayAdd(c context.Context, p *academy.Play) (id int64, err error) {
_inPlaySQL := "INSERT INTO academy_playlist (mid, aid, business, watch, ctime, mtime) VALUES (?, ?, ?, ?, ?,?) ON DUPLICATE KEY UPDATE state=0,watch=?,business=?,mtime=?"
res, err := d.db.Exec(c, _inPlaySQL, p.MID, p.AID, p.Business, p.Watch, p.CTime, p.MTime, p.Watch, p.Business, p.MTime)
if err != nil {
log.Error("PlayAdd d.db.Exec error(%v)", err)
return
}
id, err = res.RowsAffected()
return
}
// PlayDel add play archive.
func (d *Dao) PlayDel(c context.Context, p *academy.Play) (id int64, err error) {
_upPlaySQL := "UPDATE academy_playlist SET state=? WHERE mid=? AND aid=? AND business=?"
res, err := d.db.Exec(c, _upPlaySQL, -1, p.MID, p.AID, p.Business) //-1 删除
if err != nil {
log.Error("PlayDel d.db.Exec error(%v)", err)
return
}
id, err = res.RowsAffected()
return
}
//Plays get all play by mid.
func (d *Dao) Plays(c context.Context, mid int64, offset, limit int) (res []*academy.Play, err error) {
_getPlaySSQL := "SELECT mid, aid, business, watch, ctime, mtime FROM academy_playlist WHERE state=0 AND mid=? ORDER BY mtime DESC LIMIT ?,?"
rows, err := d.db.Query(c, _getPlaySSQL, mid, offset, limit)
if err != nil {
log.Error("Plays d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*academy.Play, 0)
for rows.Next() {
o := &academy.Play{}
if err = rows.Scan(&o.MID, &o.AID, &o.Business, &o.Watch, &o.CTime, &o.MTime); err != nil {
log.Error("Plays rows.Scan error(%v)", err)
return
}
res = append(res, o)
}
return
}
//PlayCount get all play achive count.
func (d *Dao) PlayCount(c context.Context, mid int64) (count int, err error) {
_getPlayCntSQL := "SELECT count(*) FROM academy_playlist WHERE state=0 AND mid=?"
if err = d.db.QueryRow(c, _getPlayCntSQL, mid).Scan(&count); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("d.db.QueryRow error(%v)", err)
}
return
}
//Play get play achive info.
func (d *Dao) Play(c context.Context, p *academy.Play) (res *academy.Play, err error) {
_getPlaySQL := "SELECT mid, aid, business, watch, ctime, mtime FROM academy_playlist WHERE mid=? AND aid=? AND business=? "
res = &academy.Play{}
if err = d.db.QueryRow(c, _getPlaySQL, p.MID, p.AID, p.Business).Scan(&res.MID, &res.AID, &res.Business, &res.Watch, &res.CTime, &res.MTime); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("d.db.QueryRow error(%v)", err)
}
return
}

View File

@@ -0,0 +1,127 @@
package academy
import (
"context"
"go-common/app/interface/main/creative/model/academy"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestAcademyOccupations(t *testing.T) {
convey.Convey("Occupations", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.Occupations(c)
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 TestAcademySkills(t *testing.T) {
convey.Convey("Skills", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.Skills(c)
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 TestAcademySkillArcs(t *testing.T) {
convey.Convey("SkillArcs", t, func(ctx convey.C) {
var (
c = context.Background()
pids = []int64{}
skids = []int64{}
sids = []int64{}
offset = int(0)
limit = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.SkillArcs(c, pids, skids, sids, offset, limit)
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 TestAcademySkillArcCount(t *testing.T) {
convey.Convey("SkillArcCount", t, func(ctx convey.C) {
var (
c = context.Background()
pids = []int64{}
skids = []int64{}
sids = []int64{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
count, err := d.SkillArcCount(c, pids, skids, sids)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}
func TestAcademyPlayAdd(t *testing.T) {
convey.Convey("PlayAdd", t, func(ctx convey.C) {
var (
c = context.Background()
p = &academy.Play{}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
id, err := d.PlayAdd(c, p)
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)
})
})
})
}
func TestAcademyPlays(t *testing.T) {
convey.Convey("Plays", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
offset = int(0)
limit = int(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.Plays(c, mid, offset, limit)
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 TestAcademyPlayCount(t *testing.T) {
convey.Convey("PlayCount", t, func(ctx convey.C) {
var (
c = context.Background()
mid = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
count, err := d.PlayCount(c, mid)
ctx.Convey("Then err should be nil.count should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(count, convey.ShouldNotBeNil)
})
})
})
}

View File

@@ -0,0 +1,116 @@
package academy
import (
"context"
"fmt"
"go-common/app/interface/main/creative/model/academy"
"go-common/library/database/sql"
"go-common/library/log"
"go-common/library/xstr"
)
const (
// select
_getTagListSQL = "SELECT id, parent_id, type, business, name, `desc`, state, ctime,mtime FROM academy_tag WHERE state=0 ORDER BY rank asc"
_getTagByIDSQL = "SELECT id, parent_id, type, business, name, `desc`, state, ctime,mtime FROM academy_tag WHERE state=0 AND id=?"
_getTagInIDsSQL = "SELECT id, parent_id, type, business, name, `desc`, state, ctime,mtime FROM academy_tag WHERE state=0 AND id in (%s)"
_getTagLinkSQL = "SELECT id, tid, link_id FROM academy_tag_link WHERE tid in (%s)"
)
// TagList get all tag from academy_tag.
func (d *Dao) TagList(c context.Context) (res map[string][]*academy.Tag, tagMap, parentChildMap map[int64]*academy.Tag, err error) {
rows, err := d.db.Query(c, _getTagListSQL)
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make(map[string][]*academy.Tag) //最终返回标签列表
all := make(map[int8]map[int64]*academy.Tag) //含有分类的一二级标签
top := make(map[int64]*academy.Tag)
tagMap = make(map[int64]*academy.Tag) //不包含二级标签
parentChildMap = make(map[int64]*academy.Tag) //包含一二级标签
allTag := make([]*academy.Tag, 0)
for rows.Next() {
t := &academy.Tag{}
if err = rows.Scan(&t.ID, &t.ParentID, &t.Type, &t.Business, &t.Name, &t.Desc, &t.State, &t.CTime, &t.MTime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
nt := &academy.Tag{}
*nt = *t
tagMap[t.ID] = nt //nt对象 不会被修改
parentChildMap[t.ID] = t //t对象 会被修改
allTag = append(allTag, t)
}
for _, t := range allTag {
if t.ParentID == 0 {
top[t.ID] = t
all[t.Type] = top
tyName := academy.TagClassMap(int(t.Type))
res[tyName] = append(res[tyName], t)
}
}
for _, t := range allTag {
a, ok := all[t.Type][t.ParentID]
if ok && a != nil && a.Type == t.Type {
a.Children = append(a.Children, t)
}
}
return
}
//Tag get one tag.
func (d *Dao) Tag(c context.Context, id int64) (t *academy.Tag, err error) {
row := d.db.QueryRow(c, _getTagByIDSQL, id)
t = &academy.Tag{}
if err = row.Scan(&t.ID, &t.ParentID, &t.Type, &t.Business, &t.Name, &t.Desc, &t.State, &t.CTime, &t.MTime); err != nil {
if err == sql.ErrNoRows {
err = nil
return
}
log.Error("row.Scan error(%v)", err)
}
return
}
//Tags get some tags by ids.
func (d *Dao) Tags(c context.Context, ids []int64) (res []*academy.Tag, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_getTagInIDsSQL, xstr.JoinInts(ids)))
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*academy.Tag, 0)
for rows.Next() {
t := &academy.Tag{}
if err = rows.Scan(&t.ID, &t.ParentID, &t.Type, &t.Business, &t.Name, &t.Desc, &t.State, &t.CTime, &t.MTime); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
res = append(res, t)
}
return
}
//LinkTags get link tags by h5 tids.
func (d *Dao) LinkTags(c context.Context, ids []int64) (res []*academy.LinkTag, err error) {
rows, err := d.db.Query(c, fmt.Sprintf(_getTagLinkSQL, xstr.JoinInts(ids)))
if err != nil {
log.Error("d.db.Query error(%v)", err)
return
}
defer rows.Close()
res = make([]*academy.LinkTag, 0)
for rows.Next() {
t := &academy.LinkTag{}
if err = rows.Scan(&t.ID, &t.TID, &t.LinkID); err != nil {
log.Error("rows.Scan error(%v)", err)
return
}
res = append(res, t)
}
return
}

View File

@@ -0,0 +1,73 @@
package academy
import (
"context"
"testing"
"github.com/smartystreets/goconvey/convey"
)
func TestAcademyTagList(t *testing.T) {
convey.Convey("TagList", t, func(ctx convey.C) {
var (
c = context.Background()
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, tagMap, parentChildMap, err := d.TagList(c)
ctx.Convey("Then err should be nil.res,tagMap,parentChildMap should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(parentChildMap, convey.ShouldNotBeNil)
ctx.So(tagMap, convey.ShouldNotBeNil)
ctx.So(res, convey.ShouldNotBeNil)
})
})
})
}
func TestAcademyTag(t *testing.T) {
convey.Convey("Tag", t, func(ctx convey.C) {
var (
c = context.Background()
id = int64(0)
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
no, err := d.Tag(c, id)
ctx.Convey("Then err should be nil.no should not be nil.", func(ctx convey.C) {
ctx.So(err, convey.ShouldBeNil)
ctx.So(no, convey.ShouldNotBeNil)
})
})
})
}
func TestAcademyTags(t *testing.T) {
convey.Convey("Tags", t, func(ctx convey.C) {
var (
c = context.Background()
ids = []int64{1, 2}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.Tags(c, ids)
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 TestAcademyLinkTags(t *testing.T) {
convey.Convey("LinkTags", t, func(ctx convey.C) {
var (
c = context.Background()
ids = []int64{1}
)
ctx.Convey("When everything goes positive", func(ctx convey.C) {
res, err := d.LinkTags(c, ids)
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)
})
})
})
}