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,57 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = [
"operation_test.go",
"service_test.go",
],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/service:go_default_library",
"//library/ecode:go_default_library",
"//vendor/github.com/smartystreets/goconvey/convey:go_default_library",
],
)
go_library(
name = "go_default_library",
srcs = [
"operation.go",
"service.go",
],
importpath = "go-common/app/interface/main/creative/service/operation",
tags = ["automanaged"],
visibility = ["//visibility:public"],
deps = [
"//app/interface/main/creative/conf:go_default_library",
"//app/interface/main/creative/dao/creative:go_default_library",
"//app/interface/main/creative/model/operation:go_default_library",
"//app/interface/main/creative/service:go_default_library",
"//library/ecode:go_default_library",
"//library/log: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,213 @@
package operation
import (
"context"
"encoding/json"
operMdl "go-common/app/interface/main/creative/model/operation"
"go-common/library/ecode"
"go-common/library/log"
"strconv"
"strings"
"time"
)
// 0 => web+app; 1=> app; 2=> web; 100=>全平台
const (
remarkShowBanner = "show_banner"
)
// Tool get a tool down.
func (s *Service) Tool(c context.Context, ty int8) (ops []*operMdl.Operation, err error) {
var tyStr string
if ty == 0 || ty == 1 { //创作中心页的tool下载
tyStr = "icon"
} else if ty == 2 { //投稿页的tool下载
tyStr = "submit_icon"
}
if op, ok := s.toolCache[tyStr]; ok {
ops = op
}
return
}
// WebOperations get full operations.
func (s *Service) WebOperations(c context.Context) (ops map[string][]*operMdl.Operation, err error) {
if s.operCache == nil {
err = ecode.NothingFound
return
}
ops = make(map[string][]*operMdl.Operation)
for _, v := range s.operCache {
if v.Platform == 0 || v.Platform == 2 {
o := &operMdl.Operation{}
o.ID = v.ID
o.Ty = v.Ty
o.Rank = v.Rank
o.Pic = v.Pic
o.Link = v.Link
o.Content = v.Content
o.Remark = v.Remark
o.Note = v.Note
o.Stime = v.Stime
o.Etime = v.Etime
if o.Ty == "play" || o.Ty == "collect_arc" {
o.Ty = "board"
}
ops[o.Ty] = append(ops[o.Ty], o)
}
}
for _, ty := range operMdl.FullTypes() {
trimTy := strings.Trim(ty, "'")
if _, ok := ops[trimTy]; !ok {
ops[trimTy] = []*operMdl.Operation{}
}
}
return
}
// AppBanner get app index flexslider; filter by platform + business + remark
func (s *Service) AppBanner(c context.Context) (bns []*operMdl.Banner, cbns []*operMdl.BannerCreator, err error) {
bns = make([]*operMdl.Banner, 0, len(s.operCache))
cbns = make([]*operMdl.BannerCreator, 0, len(s.operCache))
for _, v := range s.operCache {
// 平台切除web平台;
// 允许征稿启示也显示在所有APP的Banner上;
// 必须开启remark=="show_banner"的业务校验;
if (v.Ty != "play" && v.Ty != "collect_arc") ||
v.Platform == 2 ||
v.Remark != remarkShowBanner {
continue
}
bn := &operMdl.Banner{}
cbn := &operMdl.BannerCreator{}
bn.Ty = v.Ty
bn.Rank = v.Rank
cbn.Ty = v.Ty
cbn.Rank, _ = strconv.Atoi(v.Rank)
if v.AppPic == "" {
pics := []string{}
if err = json.Unmarshal([]byte(v.Pic), &pics); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", string(v.Pic), err)
}
bn.Pic = pics[len(pics)-1]
cbn.Pic = pics[len(pics)-1]
} else {
bn.Pic = v.AppPic
cbn.Pic = v.AppPic
}
bn.Link = v.Link
bn.Content = v.Content
cbn.Link = v.Link
cbn.Content = v.Content
bns = append(bns, bn)
cbns = append(cbns, cbn)
}
return
}
// CreatorOperationList get operations list.
func (s *Service) CreatorOperationList(c context.Context, pn, ps int) (list *operMdl.BannerList, err error) {
if s.operCache == nil {
err = ecode.NothingFound
return
}
list = &operMdl.BannerList{Pn: pn, Ps: ps}
// notice: s.CreatorRelOperCache["play"] 已经进行合并
play, ok := s.CreatorRelOperCache["play"]
if !ok {
return
}
bcs := make([]*operMdl.BannerCreator, 0)
for _, v := range play {
bc := &operMdl.BannerCreator{}
bc.Ty = v.Ty
bc.Rank, _ = strconv.Atoi(v.Rank)
if v.AppPic == "" {
pics := []string{}
if err = json.Unmarshal([]byte(v.Pic), &pics); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", string(v.Pic), err)
}
bc.Pic = pics[len(pics)-1]
} else {
bc.Pic = v.AppPic
}
bc.Link = v.Link
bc.Content = v.Content
st, _ := time.Parse("2006-01-02 15:04:05", v.Stime)
bc.Stime = st.Unix()
et, _ := time.Parse("2006-01-02 15:04:05", v.Etime)
bc.Etime = et.Unix()
bcs = append(bcs, bc)
}
total := len(bcs)
list.Total = total
start := (pn - 1) * ps
end := pn * ps
if total <= start {
list.BannerCreator = make([]*operMdl.BannerCreator, 0)
} else if total <= end {
list.BannerCreator = bcs[start:total]
} else {
list.BannerCreator = bcs[start:end]
}
return
}
// AppOperationList get operations list.
func (s *Service) AppOperationList(c context.Context, pn, ps int, tp string) (list *operMdl.BannerList, err error) {
tpOK := false
for _, fullTp := range operMdl.FullTypes() {
trimTy := strings.Trim(fullTp, "'")
if trimTy == tp {
tpOK = true
break
}
}
if s.allRelOperCache == nil || !tpOK {
return
}
list = &operMdl.BannerList{Pn: pn, Ps: ps}
vals := make([]*operMdl.Operation, 0)
for _, v := range s.allRelOperCache {
if v.Ty == tp && (v.Platform == 0 || v.Platform == 1) {
vals = append(vals, v)
}
}
if len(vals) == 0 {
return
}
bcs := make([]*operMdl.BannerCreator, 0)
for _, v := range vals {
bc := &operMdl.BannerCreator{}
bc.Ty = v.Ty
bc.Rank, _ = strconv.Atoi(v.Rank)
if v.AppPic == "" {
pics := []string{}
if err = json.Unmarshal([]byte(v.Pic), &pics); err != nil {
log.Error("json.Unmarshal(%v) error(%v)", string(v.Pic), err)
}
bc.Pic = pics[len(pics)-1]
} else {
bc.Pic = v.AppPic
}
bc.Link = v.Link
bc.Content = v.Content
st, _ := time.Parse("2006-01-02 15:04:05", v.Stime)
bc.Stime = st.Unix()
et, _ := time.Parse("2006-01-02 15:04:05", v.Etime)
bc.Etime = et.Unix()
bcs = append(bcs, bc)
}
total := len(bcs)
list.Total = total
start := (pn - 1) * ps
end := pn * ps
if total <= start {
list.BannerCreator = make([]*operMdl.BannerCreator, 0)
} else if total <= end {
list.BannerCreator = bcs[start:total]
} else {
list.BannerCreator = bcs[start:end]
}
return
}

View File

@@ -0,0 +1,38 @@
package operation
import (
"context"
"go-common/library/ecode"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func Test_WebOperations(t *testing.T) {
Convey("WebOperations", t, WithService(func(s *Service) {
res, err := s.WebOperations(context.TODO())
So(err, ShouldEqual, ecode.NothingFound)
So(res, ShouldBeNil)
}))
Convey("Ping", t, WithService(func(s *Service) {
err := s.Ping(context.TODO())
So(err, ShouldEqual, ecode.NothingFound)
}))
}
func Test_AppBanner(t *testing.T) {
Convey("AppBanner", t, WithService(func(s *Service) {
bns, cbns, err := s.AppBanner(context.TODO())
So(bns, ShouldBeNil)
So(cbns, ShouldBeNil)
So(err, ShouldNotBeNil)
}))
}
func Test_CreatorOperationList(t *testing.T) {
Convey("CreatorOperationList", t, WithService(func(s *Service) {
bns, err := s.CreatorOperationList(context.TODO(), 1, 10)
So(bns, ShouldBeNil)
So(err, ShouldNotBeNil)
}))
}

View File

@@ -0,0 +1,168 @@
package operation
import (
"context"
"go-common/app/interface/main/creative/conf"
"go-common/app/interface/main/creative/dao/creative"
operMdl "go-common/app/interface/main/creative/model/operation"
"go-common/app/interface/main/creative/service"
"go-common/library/log"
"time"
)
//Service struct
type Service struct {
c *conf.Config
creative *creative.Dao
NoticeStr string
CreativeStr string
// cache
operCache []*operMdl.Operation
allRelOperCache []*operMdl.Operation
toolCache map[string][]*operMdl.Operation
WebRelOperCache map[string][]*operMdl.Operation
CreatorRelOperCache map[string][]*operMdl.Operation
}
//New get service
func New(c *conf.Config, rpcdaos *service.RPCDaos) *Service {
s := &Service{
c: c,
creative: creative.New(c),
}
s.loadOper()
s.loadTool()
s.loadRelOper()
go s.loadproc()
return s
}
func (s *Service) loadOper() {
var (
op []*operMdl.Operation
creative *operMdl.Operation
notice *operMdl.Operation
err error
)
op, err = s.creative.Operations(context.TODO(), operMdl.FullTypes())
if err != nil {
log.Error("s.oper.Operations error(%v)", err)
return
}
log.Warn("loadOper fulltypes (%+v)", op)
s.operCache = op
// generate rank 最小的 creative and notice str
for _, v := range op {
if v.Ty == "creative" && (v.Platform == 0 || v.Platform == 1) {
if creative == nil || (v.Rank < creative.Rank) {
creative = v
}
}
if v.Ty == "notice" && (v.Platform == 0 || v.Platform == 1) {
if notice == nil || (v.Rank < notice.Rank) {
notice = v
}
}
}
log.Warn("loadOper CreativeStr(%+v) | NoticeStr(%+v)", creative, notice)
if creative != nil {
s.CreativeStr = creative.Content
} else {
s.CreativeStr = ""
}
if notice != nil {
s.NoticeStr = notice.Content
} else {
s.NoticeStr = ""
}
}
func (s *Service) loadRelOper() {
var (
op []*operMdl.Operation
err error
)
op, err = s.creative.AllOperByTypeSQL(context.TODO(), []string{"'play'", "'notice'", "'collect_arc'"})
if err != nil {
log.Error("s.oper.AllOperByTypeSQL error(%v)", err)
return
}
s.allRelOperCache = op
tmpWebRelOperCache := make(map[string][]*operMdl.Operation)
tmpCreatorRelOperCache := make(map[string][]*operMdl.Operation)
for _, v := range op {
vs := &operMdl.Operation{
ID: v.ID,
Ty: v.Ty,
Rank: v.Rank,
Pic: v.Pic,
Link: v.Link,
Content: v.Content,
Remark: v.Remark,
Note: v.Note,
Stime: v.Stime,
Etime: v.Etime,
AppPic: v.AppPic,
Platform: v.Platform,
}
// 合并 collect_arc + play => play, for Web + Creator
if vs.Ty == "collect_arc" {
vs.Ty = "play"
}
if v.Platform == 0 { //all platform
tmpWebRelOperCache[vs.Ty] = append(tmpWebRelOperCache[vs.Ty], vs)
tmpCreatorRelOperCache[vs.Ty] = append(tmpCreatorRelOperCache[vs.Ty], vs)
} else if v.Platform == 1 { //app
tmpCreatorRelOperCache[vs.Ty] = append(tmpCreatorRelOperCache[vs.Ty], vs)
} else if v.Platform == 2 { //web
tmpWebRelOperCache[vs.Ty] = append(tmpWebRelOperCache[vs.Ty], vs)
}
}
s.WebRelOperCache = tmpWebRelOperCache
s.CreatorRelOperCache = tmpCreatorRelOperCache
}
func (s *Service) loadTool() {
var (
icon []*operMdl.Operation
sicon []*operMdl.Operation
err error
)
icon, err = s.creative.Tool(context.TODO(), "icon")
if err != nil {
log.Error("s.oper.Tool(%s) error(%v)", "icon", err)
return
}
sicon, err = s.creative.Tool(context.TODO(), "submit_icon")
if err != nil {
log.Error("s.oper.Tool(%s) error(%v)", "submit_icon", err)
return
}
var tmp = map[string][]*operMdl.Operation{}
tmp["icon"] = icon
tmp["submit_icon"] = sicon
s.toolCache = tmp
}
// loadproc
func (s *Service) loadproc() {
for {
time.Sleep(1 * time.Minute)
s.loadOper()
s.loadTool()
s.loadRelOper()
}
}
// Ping service
func (s *Service) Ping(c context.Context) (err error) {
if err = s.creative.Ping(c); err != nil {
log.Error("s.operationDao.PingDb err(%v)", err)
}
return
}
// Close dao
func (s *Service) Close() {
s.creative.Close()
}

View File

@@ -0,0 +1,33 @@
package operation
import (
"flag"
"path/filepath"
"time"
"go-common/app/interface/main/creative/conf"
. "github.com/smartystreets/goconvey/convey"
"go-common/app/interface/main/creative/service"
)
var (
s *Service
)
func init() {
dir, _ := filepath.Abs("../../cmd/creative.toml")
flag.Set("conf", dir)
conf.Init()
rpcdaos := service.NewRPCDaos(conf.Conf)
s = New(conf.Conf, rpcdaos)
time.Sleep(time.Second)
}
func WithService(f func(s *Service)) func() {
return func() {
Reset(func() {
})
f(s)
}
}