diff --git a/app/mix/service/streamer_recomm_service.go b/app/mix/service/streamer_recomm_service.go index b662eebf..12b08acf 100644 --- a/app/mix/service/streamer_recomm_service.go +++ b/app/mix/service/streamer_recomm_service.go @@ -380,7 +380,6 @@ type StreamerScore struct { NewZoneMemberCountInAMonth int64 MomentCountInThreeDays int64 RefundRate float64 - Order int64 // 人工设置的排名,排名值越大,排位越靠前 Score float64 Priorities []float64 // 得分序列,依次为总分、第一优先级字段得分、第二优先级字段得分... @@ -414,7 +413,6 @@ func (s *StreamerScore) CalScore(formula *apollostruct.StreamerScoreFormulaCfg) s.Score += math.Min(100, float64(s.NewZoneMemberCountInThreeDays)/formula.NewZoneMemberCountInThreeDays.Maximum*100) * formula.NewZoneMemberCountInThreeDays.Proportion s.Score += math.Min(100, float64(s.NewZoneMemberCountInAMonth)/formula.NewZoneMemberCountInAMonth.Maximum*100) * formula.NewZoneMemberCountInAMonth.Proportion s.Score += math.Min(100, float64(s.MomentCountInThreeDays)/formula.MomentCountInThreeDays.Maximum*100) * formula.MomentCountInThreeDays.Proportion - s.Score += } type ByScore []*StreamerScore diff --git a/codecreate/codecreate.go b/codecreate/codecreate.go index 589b108d..05e2b7fe 100644 --- a/codecreate/codecreate.go +++ b/codecreate/codecreate.go @@ -29,10 +29,14 @@ func main() { genSource.OutPath = fmt.Sprintf("%v%v%v_mongo.go", consts.RootPath, consts.MongoOutPath, genSource.ModuleName) generator.GenerateModule(genSource) - genSource.InPath = consts.ProtoInPath + genSource.InPath = consts.ProtoOpInPath genSource.OutPath = fmt.Sprintf("%v%v%v/proto/%v_op.go", consts.RootPath, consts.ProtoOutPath, genSource.ModuleName, genSource.ModuleName) generator.GenerateModule(genSource) + genSource.InPath = consts.ProtoApiInPath + genSource.OutPath = fmt.Sprintf("%v%v%v/proto/%v_api.go", consts.RootPath, consts.ProtoOutPath, genSource.ModuleName, genSource.ModuleName) + generator.GenerateModule(genSource) + genSource.InPath = consts.ServiceInPath genSource.OutPath = fmt.Sprintf("%v%v%v.go", consts.RootPath, consts.ServiceOutPath, genSource.ModuleName) generator.GenerateModule(genSource) @@ -45,10 +49,14 @@ func main() { genSource.OutPath = fmt.Sprintf("%v%v%v_errcode.go", consts.RootPath, consts.ErrcodeOutPath, genSource.ModuleName) generator.GenerateModule(genSource) - genSource.InPath = consts.ControllerInPath + genSource.InPath = consts.ControllerOpInPath genSource.OutPath = fmt.Sprintf("%v%v%v_op.go", consts.RootPath, consts.ControllerOutPath, genSource.ModuleName) generator.GenerateModule(genSource) + genSource.InPath = consts.ControllerApiInPath + genSource.OutPath = fmt.Sprintf("%v%v%v_api.go", consts.RootPath, consts.ControllerOutPath, genSource.ModuleName) + generator.GenerateModule(genSource) + genSource.InPath = consts.ControllerCenterInPath genSource.OutPath = fmt.Sprintf("%v%v%v_controller_center.go", consts.RootPath, consts.ControllerCenterOutPath, genSource.ModuleName) generator.GenerateModule(genSource) diff --git a/codecreate/consts/consts.go b/codecreate/consts/consts.go index 77b1ceaf..4ba17caf 100644 --- a/codecreate/consts/consts.go +++ b/codecreate/consts/consts.go @@ -10,7 +10,9 @@ const TemplateRootPath = "codecreate/template/" // in const ControllerCenterInPath = RootPath + TemplateRootPath + "controller_center.txt" -const ControllerInPath = RootPath + TemplateRootPath + "controller.txt" +const ControllerOpInPath = RootPath + TemplateRootPath + "controller_op.txt" + +const ControllerApiInPath = RootPath + TemplateRootPath + "controller_api.txt" const EntityInPath = RootPath + TemplateRootPath + "entity.txt" @@ -20,7 +22,9 @@ const IdGeneratorInPath = RootPath + TemplateRootPath + "idgenerator.txt" const MongoInPath = RootPath + TemplateRootPath + "mongo.txt" -const ProtoInPath = RootPath + TemplateRootPath + "proto.txt" +const ProtoOpInPath = RootPath + TemplateRootPath + "proto_op.txt" + +const ProtoApiInPath = RootPath + TemplateRootPath + "proto_api.txt" const ServiceCenterInPath = RootPath + TemplateRootPath + "service_center.txt" diff --git a/codecreate/resource/EntityDefine.xlsx b/codecreate/resource/EntityDefine.xlsx index 254f9577..ed243f4a 100644 Binary files a/codecreate/resource/EntityDefine.xlsx and b/codecreate/resource/EntityDefine.xlsx differ diff --git a/codecreate/template/controller_api.txt b/codecreate/template/controller_api.txt new file mode 100644 index 00000000..a9c71c4d --- /dev/null +++ b/codecreate/template/controller_api.txt @@ -0,0 +1,71 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "service/api/errcode" + #{moduleName}proto "service/api/proto/#{moduleName}/proto" + "service/app/mix/service" + "service/bizcommon/util" + "service/library/logger" +) + +func ApiCreate#{EntityName}(ctx *gin.Context) { + req := ctx.MustGet("client_req").(*#{moduleName}proto.ApiCreateReq) + ec := service.DefaultService.ApiCreate#{EntityName}(ctx, req) + if ec != errcode.ErrCode#{EntityName}SrvOk { + logger.Error("ApiCreate#{EntityName} fail, req: %v, ec: %v", util.ToJson(req), ec) + ReplyErrorMsg(ctx, "server error") + return + } + + ReplyOk(ctx, nil) +} + +func ApiUpdate#{EntityName}(ctx *gin.Context) { + req := ctx.MustGet("client_req").(*#{moduleName}proto.ApiUpdateReq) + ec := service.DefaultService.ApiUpdate#{EntityName}(ctx, req) + if ec != errcode.ErrCode#{EntityName}SrvOk { + logger.Error("ApiUpdate#{EntityName} fail, req: %v, ec: %v", util.ToJson(req), ec) + ReplyErrCodeMsg(ctx, ec) + return + } + + ReplyOk(ctx, nil) +} + +func ApiDelete#{EntityName}(ctx *gin.Context) { + req := ctx.MustGet("client_req").(*#{moduleName}proto.ApiDeleteReq) + ec := service.DefaultService.ApiDelete#{EntityName}(ctx, req.Id) + if ec != errcode.ErrCode#{EntityName}SrvOk { + logger.Error("ApiDelete#{EntityName} fail, req: %v, ec: %v", util.ToJson(req), ec) + ReplyErrCodeMsg(ctx, ec) + return + } + + ReplyOk(ctx, nil) +} + +func ApiGet#{EntityName}List(ctx *gin.Context) { + req := ctx.MustGet("client_req").(*#{moduleName}proto.ApiListReq) + + //设置默认页长 + if req.Limit == 0 { + req.Limit = consts.DefaultPageSize + } + + list, ec := service.DefaultService.ApiGet#{EntityName}List(ctx, req) + if ec != errcode.ErrCode#{EntityName}SrvOk { + logger.Error("ApiGet#{EntityName}List fail, req: %v, ec: %v", util.ToJson(req), ec) + ReplyErrCodeMsg(ctx, ec) + return + } + + data := &#{moduleName}proto.ApiListData{ + List: list, + Offset: req.Offset + len(list), + } + if len(list) >= req.Limit { + data.More = 1 + } + ReplyOk(ctx, data) +} \ No newline at end of file diff --git a/codecreate/template/controller_center.txt b/codecreate/template/controller_center.txt index ba5873fb..903da55c 100644 --- a/codecreate/template/controller_center.txt +++ b/codecreate/template/controller_center.txt @@ -1,8 +1,15 @@ #{moduleName}proto "service/api/proto/#{moduleName}/proto" // #{EntityCNName} - op#{EntityName}Group := r.Group("/api/#{moduleName}", PrepareToC()) + op#{EntityName}Group := r.Group("/op/#{moduleName}", PrepareOp()) op#{EntityName}Group.POST("create", middleware.JSONParamValidator(#{moduleName}proto.OpCreateReq{}), middleware.JwtAuthenticator(), OpCreate#{EntityName}) op#{EntityName}Group.POST("update", middleware.JSONParamValidator(#{moduleName}proto.OpUpdateReq{}), middleware.JwtAuthenticator(), OpUpdate#{EntityName}) op#{EntityName}Group.POST("delete", middleware.JSONParamValidator(#{moduleName}proto.OpDeleteReq{}), middleware.JwtAuthenticator(), OpDelete#{EntityName}) - op#{EntityName}Group.POST("list", middleware.JSONParamValidator(#{moduleName}proto.OpListReq{}), middleware.JwtAuthenticator(), OpGet#{EntityName}List) \ No newline at end of file + op#{EntityName}Group.POST("list", middleware.JSONParamValidator(#{moduleName}proto.OpListReq{}), middleware.JwtAuthenticator(), OpGet#{EntityName}List) + + // #{EntityCNName} + api#{EntityName}Group := r.Group("/api/#{moduleName}", PrepareToC()) + api#{EntityName}Group.POST("create", middleware.JSONParamValidator(#{moduleName}proto.ApiCreateReq{}), middleware.JwtAuthenticator(), ApiCreate#{EntityName}) + api#{EntityName}Group.POST("update", middleware.JSONParamValidator(#{moduleName}proto.ApiUpdateReq{}), middleware.JwtAuthenticator(), ApiUpdate#{EntityName}) + api#{EntityName}Group.POST("delete", middleware.JSONParamValidator(#{moduleName}proto.ApiDeleteReq{}), middleware.JwtAuthenticator(), ApiDelete#{EntityName}) + api#{EntityName}Group.POST("list", middleware.JSONParamValidator(#{moduleName}proto.ApiListReq{}), middleware.JwtAuthenticator(), ApiGet#{EntityName}List) \ No newline at end of file diff --git a/codecreate/template/controller.txt b/codecreate/template/controller_op.txt similarity index 100% rename from codecreate/template/controller.txt rename to codecreate/template/controller_op.txt diff --git a/codecreate/template/entity.txt b/codecreate/template/entity.txt index ffa9717c..32c1e31c 100644 --- a/codecreate/template/entity.txt +++ b/codecreate/template/entity.txt @@ -3,3 +3,7 @@ package dbstruct type #{EntityName} struct { #{EntityDefination} } + +type #{EntityName}IdSeq struct { + Seq int64 //Id序列号 +} \ No newline at end of file diff --git a/codecreate/template/mongo.txt b/codecreate/template/mongo.txt index 1969c3d9..581159e0 100644 --- a/codecreate/template/mongo.txt +++ b/codecreate/template/mongo.txt @@ -1,12 +1,18 @@ #{moduleName}proto "service/api/proto/#{moduleName}/proto" DB#{EntityName} = "#{moduleName}" COL#{EntityName} = "#{moduleName}" + COL#{EntityName}IdSeq = "#{moduleName}_id_seq" // #{EntityCNName}表 func (m *Mongo) getCol#{EntityName}() *qmgo.Collection { return m.clientMix.Database(DB#{EntityName}).Collection(COL#{EntityName}) } +// #{EntityCNName}IdSeq序列表 +func (m *Mongo) getCol#{EntityCNName}IdSeq() *qmgo.Collection { + return m.clientMix.Database(DB#{EntityCNName}).Collection(COL#{EntityCNName}IdSeq) +} + // #{EntityCNName}相关 func (m *Mongo) Create#{EntityName}(ctx *gin.Context, #{moduleName} *dbstruct.#{EntityName}) error { col := m.getCol#{EntityName}() @@ -49,3 +55,55 @@ func (m *Mongo) Get#{EntityName}List(ctx *gin.Context, req *#{moduleName}proto.O } return list, err } + +func (m *Mongo) Get#{EntityName}ListById(ctx *gin.Context, id int64) (*dbstruct.#{EntityName}, error) { + one := &dbstruct.#{EntityName}{} + col := m.getCol#{EntityName}() + query := qmgo.M{ + "_id": id, + "del_flag": 0, + } + err := col.Find(ctx, query).One(one) + if err == qmgo.ErrNoSuchDocuments { + err = nil + return nil, err + } + return one, err +} + +func (m *Mongo) Get#{EntityName}ListByIds(ctx *gin.Context, ids []int64) ([]*dbstruct.#{EntityName}, error) { + list := make([]*dbstruct.#{EntityName}, 0) + col := m.getCol#{EntityName}() + query := qmgo.M{ + "_id": { + "$in": ids, + } + "del_flag": 0, + } + err := col.Find(ctx, query).Sort("-ct").All(&list) + if err == qmgo.ErrNoSuchDocuments { + err = nil + return list, err + } + return list, err +} + + +// #{moduleName}发号器 +func (m *Mongo) GetAndUpdate#{EntityName}IdSeq(ctx *gin.Context) (idSeq *dbstruct.#{EntityName}IdSeq, err error) { + col := m.getCol#{EntityName}IdSeq() + + change := qmgo.Change{ + Update: qmgo.M{"$inc": qmgo.M{"seq": 1}}, + Upsert: true, + ReturnNew: false, + } + + idSeqInstance := dbstruct.#{EntityName}IdSeq{} + if err = col.Find(ctx, qmgo.M{"_id": "#{moduleName}_seq_id"}).Apply(change, &idSeqInstance); err != nil { + logger.Error("change error : %v", err) + return + } + + return &idSeqInstance, err +} \ No newline at end of file diff --git a/codecreate/template/proto_api.txt b/codecreate/template/proto_api.txt new file mode 100644 index 00000000..cb83a747 --- /dev/null +++ b/codecreate/template/proto_api.txt @@ -0,0 +1,66 @@ +package proto + +import ( + "service/api/base" + "service/dbstruct" +) + +// op 创建 +type ApiCreateReq struct { + base.BaseRequest + *dbstruct.#{EntityName} +} + +type ApiCreateData struct { +} + +type ApiCreateResp struct { + base.BaseResponse + Data *ApiCreateData `json:"data"` +} + +// op 删除 +type ApiDeleteReq struct { + base.BaseRequest + Id int64 `json:"id"` +} + +type ApiDeleteData struct { +} + +type ApiDeleteResp struct { + base.BaseResponse + Data *ApiDeleteData `json:"data"` +} + +// op 更新 +type ApiUpdateReq struct { + base.BaseRequest + *dbstruct.#{EntityName} +} + +type ApiUpdateData struct { +} + +type ApiUpdateResp struct { + base.BaseResponse + Data *ApiUpdateData `json:"data"` +} + +// op 列表 +type ApiListReq struct { + base.BaseRequest + Offset int `json:"offset"` + Limit int `json:"limit"` +} + +type ApiListData struct { + List []*dbstruct.#{EntityName} `json:"list"` + Offset int `json:"offset"` + More int `json:"more"` +} + +type ApiListResp struct { + base.BaseResponse + Data *ApiListData `json:"data"` +} diff --git a/codecreate/template/proto.txt b/codecreate/template/proto_op.txt similarity index 100% rename from codecreate/template/proto.txt rename to codecreate/template/proto_op.txt diff --git a/codecreate/template/service.txt b/codecreate/template/service.txt index 9b04e86e..bc546d02 100644 --- a/codecreate/template/service.txt +++ b/codecreate/template/service.txt @@ -63,10 +63,19 @@ func (p *#{EntityName}) OpList(ctx *gin.Context, req *#{moduleName}proto.OpListR return list, nil } -func (p *#{EntityName}) GetListByIds(ctx *gin.Context, ids []int64) ([]*dbstruct.#{EntityName}, error) { - list, err := p.store.Get#{EntityName}ListByIds(ctx, ids) +func (p *#{EntityName}) GetById(ctx *gin.Context, id int64) (*dbstruct.#{EntityName}, error) { + one, err := p.store.Get#{EntityName}ById(ctx, id) if err != nil { - logger.Error("Get#{EntityName}ListByIds fail, ids: %v, err: %v", ids, err) + logger.Error("Get#{EntityName}ByIds fail, ids: %v, err: %v", ids, err) + return nil, err + } + return one, nil +} + +func (p *#{EntityName}) GetByIds(ctx *gin.Context, ids []int64) ([]*dbstruct.#{EntityName}, error) { + list, err := p.store.Get#{EntityName}ByIds(ctx, ids) + if err != nil { + logger.Error("Get#{EntityName}ByIds fail, ids: %v, err: %v", ids, err) return make([]*dbstruct.#{EntityName}, 0), err } return list, nil diff --git a/codecreate/template/service_center.txt b/codecreate/template/service_center.txt index f93c7214..b1e86ab9 100644 --- a/codecreate/template/service_center.txt +++ b/codecreate/template/service_center.txt @@ -52,4 +52,62 @@ func (s *Service) OpGet#{EntityName}List(ctx *gin.Context, req *#{moduleName}pro return } return +} + +// #{EntityName} +func (s *Service) ApiCreate#{EntityName}(ctx *gin.Context, req *#{moduleName}proto.ApiCreateReq) (ec errcode.ErrCode) { + ec = errcode.ErrCode#{EntityName}SrvOk + err := _Default#{EntityName}.OpCreate(ctx, &#{moduleName}proto.OpCreateReq{ + BaseRequest: req.BaseRequest, + #{EntityName}: req.#{EntityName}, + }) + if err != nil { + logger.Error("OpCreate fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCode#{EntityName}SrvFail + return + } + return +} + +func (s *Service) ApiUpdate#{EntityName}(ctx *gin.Context, req *#{moduleName}proto.ApiUpdateReq) (ec errcode.ErrCode) { + ec = errcode.ErrCode#{EntityName}SrvOk + err := _Default#{EntityName}.OpUpdate(ctx, &#{moduleName}proto.ApiUpdateReq{ + BaseRequest: req.BaseRequest, + #{EntityName}: req.#{EntityName}, + }) + if err == qmgo.ErrNoSuchDocuments { + ec = errcode.ErrCode#{EntityName}NotExist + err = nil + return + } + if err != nil { + logger.Error("OpUpdate fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCode#{EntityName}SrvFail + return + } + return +} + +func (s *Service) ApiDelete#{EntityName}(ctx *gin.Context, id int64) (ec errcode.ErrCode) { + ec = errcode.ErrCode#{EntityName}SrvOk + err := _Default#{EntityName}.OpDelete(ctx, id) + if err != nil { + logger.Error("OpDelete fail, id: %v, err: %v", id, err) + ec = errcode.ErrCode#{EntityName}SrvFail + return + } + return +} + +func (s *Service) ApiGet#{EntityName}List(ctx *gin.Context, req *#{moduleName}proto.ApiListReq) (list []*dbstruct.#{EntityName}, ec errcode.ErrCode) { + ec = errcode.ErrCode#{EntityName}SrvOk + list, err := _Default#{EntityName}.OpList(ctx, &#{moduleName}proto.OpListReq{ + BaseRequest: req.BaseRequest, + }) + if err != nil { + logger.Error("OpGet#{EntityName}List fail, req: %v, err: %v", util.ToJson(req), err) + ec = errcode.ErrCode#{EntityName}SrvFail + return + } + return } \ No newline at end of file diff --git a/dbstruct/hyperlink.go b/dbstruct/hyperlink.go new file mode 100644 index 00000000..a9caab64 --- /dev/null +++ b/dbstruct/hyperlink.go @@ -0,0 +1,6 @@ +package dbstruct + +type Hyperlink struct { + Action string `json:"action" bson:"action"` // 跳转类型 + Url string `json:"url" bson:"url"` // url +}