作者 yangfu
... ... @@ -32,4 +32,3 @@ lastupdate.tmp
public/*
logs/
cmd/discuss/api/api
... ...
... ... @@ -2,11 +2,12 @@ package main
import (
"flag"
"net/http"
"strings"
"github.com/zeromicro/go-zero/core/logx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"net/http"
"strings"
"github.com/golang-jwt/jwt/v4/request"
"github.com/zeromicro/go-zero/core/conf"
... ...
import "core/comment.api"
import "core/message.api"
import "core/article_tag.api"
import "core/user.api"
import "core/company.api"
\ No newline at end of file
... ...
syntax = "v1"
info(
title: "文章内容处理"
desc: "编辑处理文章内容"
author: "author"
email: "email"
version: "v1"
)
// 坐标地点描述
type Location {
Longitude float64 `json:"longitude"` //经度
Latitude float64 `json:"latitude"` //纬度
Descript string `json:"descript"` //地点描述
}
// 人员的简单展示信息
type Author {
Id int64 `json:"id"` // 人员id
Name string `json:"name"` // 人员的名字
Avatar string `json:"avatar"` // 人员头像URL
Group string `json:"group"` // 人员的分组
Position string `json:"position"` // 职位
}
// 创建发布文章
type (
ArticleCreateRequest {
Title string `json:"title"` //标题
Section []string `json:"section"` //文章的文本内容
AuthorId int `json:"authorId"` //发布人id
Images []string `json:"images"` //图片
WhoRead []int `json:"whoRead"` //谁可查看
WhoReview []int `json:"whoReview"` //谁可评论
Location Location `json:"location"` //定位坐标
}
ArticleCreateResponse {
Id int64 `json:"id"`
}
)
// 查看文章的详情
type (
ArticleGetRequest {
Id int64 `json:"id"` //id
}
ArticleGetResponse {
Title string `json:"title"` //标题
AuthorId int `json:"authorId"` //发布人id
Author Author `json:"author"` //发布人
CreatedAt int64 `json:"createdAt"` //文章的发布时间
Section []string `json:"section"` //文章的文本内容
Images []string `json:"images"` //图片
WhoRead []int `json:"whoRead"` //谁可查看
WhoReview []int `json:"whoReview"` //谁可评论
Location Location `json:"location"` //定位坐标
CountLove int `json:"countLove"` // 点赞数量
CountComment int `json:"countComment"` // 评论数量
Show int `json:"showState"` // 评论的展示状态(0显示、1不显示)
}
)
// 小程序接口
@server(
prefix: v1/mini
group: article
jwt: MiniAuth
)
service Core {
@doc "小程序创建发布内容"
@handler CreateArticle
post /article (ArticleCreateRequest) returns (ArticleCreateResponse)
@doc "小程序获取文章内容详情"
@handler GetArticle
get /article/:id (ArticleGetRequest) returns (ArticleGetResponse)
}
\ No newline at end of file
... ...
syntax = "v1"
info(
title: "后台编辑标签"
desc: "编辑处理标签信息"
author: "author"
email: "email"
version: "v1"
)
// 创建标签
type (
TagCreateRequest {
CompanyId int64 `json:"companyId"`
Image string `json:"image"`
Name string `json:"name"` // 标签名称
Group string `json:"group"` // 标签分类
Remark string `json:"remark"` // 备注
}
TagCreateResponse {
Id int64 `json:"id"`
}
)
// 编辑标签
type (
TagEditRequest {
Id int64 `json:"id"`
CompanyId int64 `json:"companyId"`
Image string `json:"image"`
Name string `json:"name"` // 标签名称
Group string `json:"group"` // 标签分类
Remark string `json:"remark"` // 备注
}
TagEditResponse {
Id int64 `json:"id"`
}
)
// 获取标签详情
type (
TagGetRequest {
Id int64 `json:"id"`
}
TagGetResponse {
Id int64 `json:"id"`
Image string `json:"image"`
Name string `json:"name"` // 标签名称
Group string `json:"group"` // 标签分类
Remark string `json:"remark"` // 备注
}
)
//标签列表
type (
TagListRequest {
Page int `json:"page"`
Size int `json:"size"`
}
TagListResponse {
Total int `json:"total"`
List []TagItem `json:"list"`
}
TagItem {
Id int64 `json:"id"`
Image string `json:"image"`
Name string `json:"name"` // 标签名称
Group string `json:"group"` // 标签分类
Remark string `json:"remark"` // 备注
CreatedAt int64 `json:"createdAt"`
}
)
//删除标签
type (
TagDeleteRequest {
Id int64 `json:"id"`
}
TagDeleteResponse {
Id int64 `json:"id"`
}
)
@server(
prefix: v1/mini
group: tags
jwt: MiniAuth
)
service Core {
@doc "后台创建文章标签"
@handler CreateTag
post /article_tag (TagCreateRequest) returns (TagCreateResponse)
@doc "后台编辑文章标签"
@handler EditTag
put /article_tag (TagEditRequest) returns (TagEditResponse)
@doc "后台获取文章标签"
@handler GetTag
get /article_tag/:id (TagGetRequest) returns (TagGetResponse)
@doc "后台删除文章标签"
@handler DeleteTag
delete /article_tag (TagDeleteRequest) returns (TagDeleteResponse)
}
\ No newline at end of file
... ...
... ... @@ -16,7 +16,7 @@ info(
service Core {
@doc "系统消息"
@handler miniSystem
post /mini/message/system (MessageSystemRequest) returns (MessageBusinessResponse)
post /mini/message/system (MessageSystemRequest) returns (MessageSystemResponse)
@doc "业务消息"
@handler miniBusiness
... ... @@ -37,6 +37,7 @@ type (
Type int `json:"type"` // 系统分类
Title string `json:"title"` // 标题
Content string `json:"content"` // 内容
CreatedAt int64 `json:"createdAt"` // 创建时间
}
MessageBusinessRequest struct {
... ...
package message
import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/result"
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
... ... @@ -19,10 +20,11 @@ func MiniSystemHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
l := message.NewMiniSystemLogic(r.Context(), svcCtx)
resp, err := l.MiniSystem(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
result.HttpResult(r, w, resp, err)
//if err != nil {
// httpx.ErrorCtx(r.Context(), w, err)
//} else {
// httpx.OkJsonCtx(r.Context(), w, resp)
//}
}
}
... ...
... ... @@ -7,6 +7,7 @@ import (
comment "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/comment"
company "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/company"
message "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/message"
tags "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/tags"
user "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/handler/user"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
... ... @@ -59,6 +60,33 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
[]rest.Route{
{
Method: http.MethodPost,
Path: "/article_tag",
Handler: tags.CreateTagHandler(serverCtx),
},
{
Method: http.MethodPut,
Path: "/article_tag",
Handler: tags.EditTagHandler(serverCtx),
},
{
Method: http.MethodGet,
Path: "/article_tag/:id",
Handler: tags.GetTagHandler(serverCtx),
},
{
Method: http.MethodDelete,
Path: "/article_tag",
Handler: tags.DeleteTagHandler(serverCtx),
},
},
rest.WithJwt(serverCtx.Config.MiniAuth.AccessSecret),
rest.WithPrefix("/v1/mini"),
)
server.AddRoutes(
[]rest.Route{
{
Method: http.MethodPost,
Path: "/mini/user/apply-join-company",
Handler: user.MiniUserApplyJoinCompanyHandler(serverCtx),
},
... ...
package tags
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/tags"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func CreateTagHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.TagCreateRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := tags.NewCreateTagLogic(r.Context(), svcCtx)
resp, err := l.CreateTag(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package tags
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/tags"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func DeleteTagHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.TagDeleteRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := tags.NewDeleteTagLogic(r.Context(), svcCtx)
resp, err := l.DeleteTag(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package tags
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/tags"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func EditTagHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.TagEditRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := tags.NewEditTagLogic(r.Context(), svcCtx)
resp, err := l.EditTag(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
package tags
import (
"net/http"
"github.com/zeromicro/go-zero/rest/httpx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/logic/tags"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
)
func GetTagHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.TagGetRequest
if err := httpx.Parse(r, &req); err != nil {
httpx.ErrorCtx(r.Context(), w, err)
return
}
l := tags.NewGetTagLogic(r.Context(), svcCtx)
resp, err := l.GetTag(&req)
if err != nil {
httpx.ErrorCtx(r.Context(), w, err)
} else {
httpx.OkJsonCtx(r.Context(), w, resp)
}
}
}
... ...
... ... @@ -2,11 +2,10 @@ package message
import (
"context"
"github.com/zeromicro/go-zero/core/logx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
)
type MiniSystemLogic struct {
... ... @@ -23,8 +22,37 @@ func NewMiniSystemLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniSy
}
}
func (l *MiniSystemLogic) MiniSystem(req *types.MessageSystemRequest) (resp *types.MessageBusinessResponse, err error) {
// todo: add your logic here and delete this line
func (l *MiniSystemLogic) MiniSystem(req *types.MessageSystemRequest) (resp *types.MessageSystemResponse, err error) {
queryOptions := domain.NewQueryOptions().WithOffsetLimit(req.Page, req.Size)
total, list, err := l.svcCtx.MessageSystemRepository.Find(l.ctx, l.svcCtx.DefaultDBConn(), queryOptions)
if err != nil {
return nil, err
}
resp = &types.MessageSystemResponse{}
resp.Total = total
resp.List = make([]types.MessageSystemItem, 0)
for _, item := range list {
to := types.MessageSystemItem{
Id: item.Id,
Type: item.Type,
Title: item.Title,
Content: item.Content,
CreatedAt: item.CreatedAt,
}
resp.List = append(resp.List, to)
}
return resp, nil
}
return
func (l *MiniSystemLogic) CreateMessage(dm *domain.MessageSystem) error {
_, err := l.svcCtx.MessageSystemRepository.Insert(l.ctx, l.svcCtx.DefaultDBConn(), dm)
l.Debugf("")
return err
}
//// CreateSystemMessage 生成一条信息
//func CreateSystemMessage(ctx context.Context, svcCtx *svc.ServiceContext, dm *domain.MessageSystem) error {
// l := NewMiniSystemLogic(ctx, svcCtx)
// err := l.CreateMessage(dm)
// return err
//}
... ...
package tags
import (
"context"
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"
"github.com/zeromicro/go-zero/core/logx"
)
type CreateTagLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewCreateTagLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateTagLogic {
return &CreateTagLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *CreateTagLogic) CreateTag(req *types.TagCreateRequest) (resp *types.TagCreateResponse, err error) {
// todo: add your logic here and delete this line
var conn = l.svcCtx.DefaultDBConn()
//检查重复
cnt, _, err := l.svcCtx.ArticleTagRepository.Find(l.ctx, conn, req.CompanyId, map[string]interface{}{
"name": req.Name,
"group": req.Group,
"countOnly": true,
})
if err != nil {
return nil, xerr.NewErrMsgErr("添加标签失败", err)
}
if cnt > 0 {
return nil, xerr.NewErrMsg(fmt.Sprintf("已存在标签 分类[%s]名称[%s]", req.Group, req.Name))
}
return
}
... ...
package tags
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type DeleteTagLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewDeleteTagLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteTagLogic {
return &DeleteTagLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *DeleteTagLogic) DeleteTag(req *types.TagDeleteRequest) (resp *types.TagDeleteResponse, err error) {
// todo: add your logic here and delete this line
return
}
... ...
package tags
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type EditTagLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewEditTagLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EditTagLogic {
return &EditTagLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *EditTagLogic) EditTag(req *types.TagEditRequest) (resp *types.TagEditResponse, err error) {
// todo: add your logic here and delete this line
return
}
... ...
package tags
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
"github.com/zeromicro/go-zero/core/logx"
)
type GetTagLogic struct {
logx.Logger
ctx context.Context
svcCtx *svc.ServiceContext
}
func NewGetTagLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetTagLogic {
return &GetTagLogic{
Logger: logx.WithContext(ctx),
ctx: ctx,
svcCtx: svcCtx,
}
}
func (l *GetTagLogic) GetTag(req *types.TagGetRequest) (resp *types.TagGetResponse, err error) {
// todo: add your logic here and delete this line
return
}
... ...
... ... @@ -21,6 +21,7 @@ type ServiceContext struct {
ArticleDraftRepository domain.ArticleDraftRepository
ArticleRepository domain.ArticleRepository
ArticleSectionRepository domain.ArticleSectionRepository
ArticleTagRepository domain.ArticleTagRepository
CompanyRepository domain.CompanyRepository
CommentRepository domain.CommentRepository // 待移除
... ... @@ -36,6 +37,7 @@ type ServiceContext struct {
func NewServiceContext(c config.Config) *ServiceContext {
db := database.OpenGormPGDB(c.DB.DataSource, c.Log.Mode)
mlCache := cache.NewMultiLevelCache([]string{c.Redis.Host}, c.Redis.Pass)
redis, _ := redis.NewRedis(redis.RedisConf{Host: c.Redis.Host, Pass: c.Redis.Pass, Type: "node"})
... ... @@ -57,6 +59,7 @@ func NewServiceContext(c config.Config) *ServiceContext {
UserFollowRepository: repository.NewUserFollowRepository(cache.NewCachedRepository(mlCache)),
UserLoveFlagRepository: repository.NewUserLoveFlagRepository(cache.NewCachedRepository(mlCache)),
UserRepository: repository.NewUserRepository(cache.NewCachedRepository(mlCache)),
ArticleTagRepository: repository.NewArticleTagRepository(cache.NewCachedRepository(mlCache)),
}
}
... ...
... ... @@ -22,10 +22,11 @@ type MessageSystemResponse struct {
}
type MessageSystemItem struct {
Id int64 `json:"id"` // ID
Type int `json:"type"` // 系统分类
Title string `json:"title"` // 标题
Content string `json:"content"` // 内容
Id int64 `json:"id"` // ID
Type int `json:"type"` // 系统分类
Title string `json:"title"` // 标题
Content string `json:"content"` // 内容
CreatedAt int64 `json:"createdAt"` // 创建时间
}
type MessageBusinessRequest struct {
... ... @@ -63,6 +64,70 @@ type User struct {
Position string `json:"position,omitempty"` // 职位
}
type TagCreateRequest struct {
CompanyId int64 `json:"companyId"`
Image string `json:"image"`
Name string `json:"name"` // 标签名称
Group string `json:"group"` // 标签分类
Remark string `json:"remark"` // 备注
}
type TagCreateResponse struct {
Id int64 `json:"id"`
}
type TagEditRequest struct {
Id int64 `json:"id"`
CompanyId int64 `json:"companyId"`
Image string `json:"image"`
Name string `json:"name"` // 标签名称
Group string `json:"group"` // 标签分类
Remark string `json:"remark"` // 备注
}
type TagEditResponse struct {
Id int64 `json:"id"`
}
type TagGetRequest struct {
Id int64 `json:"id"`
}
type TagGetResponse struct {
Id int64 `json:"id"`
Image string `json:"image"`
Name string `json:"name"` // 标签名称
Group string `json:"group"` // 标签分类
Remark string `json:"remark"` // 备注
}
type TagListRequest struct {
Page int `json:"page"`
Size int `json:"size"`
}
type TagListResponse struct {
Total int `json:"total"`
List []TagItem `json:"list"`
}
type TagItem struct {
Id int64 `json:"id"`
Image string `json:"image"`
Name string `json:"name"` // 标签名称
Group string `json:"group"` // 标签分类
Remark string `json:"remark"` // 备注
CreatedAt int64 `json:"createdAt"`
}
type TagDeleteRequest struct {
Id int64 `json:"id"`
}
type TagDeleteResponse struct {
Id int64 `json:"id"`
}
type MiniUserLoginRequest struct {
LoginType string `json:"loginType"` // 登录类型 wechat-login whchat-phone-login phone-password-login phone-smscode-login
WechatAuthCode string `json:"wechatAuthcode,optional"` // 微信登录 授权码
... ...
... ... @@ -12,11 +12,14 @@ func Migrate(db *gorm.DB) {
&models.ArticleBackup{},
&models.ArticleDraft{},
&models.ArticleComment{},
&models.ArticleTag{},
&models.UserLoveFlag{},
&models.User{},
&models.Role{},
&models.Company{},
&models.UserFollow{},
&models.MessageSystem{},
&models.MessageBusiness{},
}
db.AutoMigrate(modelsList...)
... ...
... ... @@ -27,7 +27,6 @@ type Article struct {
TargetUser int // 分发方式 0 分发给所有人 1 分发给指定的人
CountLove int // 点赞数量
CountComment int // 评论数量
Tags []int `gorm:"type:jsonb;serializer:json"` // 标签
Show int // 评论的展示状态(0显示、1不显示)
}
... ...
package models
import (
"fmt"
"time"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
"gorm.io/plugin/soft_delete"
)
type ArticleTag struct {
Id int64 `gorm:"primaryKey"` // 唯一标识
CompanyId int64
CreatedAt int64
UpdatedAt int64
DeletedAt int64
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
Version int
Image domain.Image `gorm:"type:jsonb;serializer:json"` // 图片
Name string // 标签名称
Group string // 标签分类
Remark string // 备注
}
func (m *ArticleTag) TableName() string {
return "article_tag"
}
func (m *ArticleTag) BeforeCreate(tx *gorm.DB) (err error) {
m.CreatedAt = time.Now().Unix()
m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleTag) BeforeUpdate(tx *gorm.DB) (err error) {
m.UpdatedAt = time.Now().Unix()
return
}
func (m *ArticleTag) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *ArticleTag) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*ArticleTag); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *ArticleTag) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
... ... @@ -5,16 +5,17 @@ import (
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
"gorm.io/plugin/soft_delete"
"time"
)
// MessageSystem 消息中心-系统消息
type MessageSystem struct {
Id int64 // 唯一标识
CompanyId int64 `json:"companyId"` // 公司ID
RecipientId int64 `json:"recipientId"` // 接收者ID
Type int `json:"type"` // 系统分类(0待定、1业务正常通知、2业务异常通知)
Title string `json:"title"` // 标题
Content string `json:"content"` // 内容
CompanyId int64 `json:"companyId"` // 公司ID
RecipientId int64 `json:"recipientId"` // 接收者ID
Type int `json:"type"` // 系统分类(0待定、1业务正常通知、2业务异常通知)
Title string `json:"title,omitempty"` // 标题
Content string `json:"content,omitempty"` // 内容
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
... ... @@ -27,13 +28,13 @@ func (m *MessageSystem) TableName() string {
}
func (m *MessageSystem) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
m.CreatedAt = time.Now().Unix()
m.UpdatedAt = time.Now().Unix()
return
}
func (m *MessageSystem) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
m.UpdatedAt = time.Now().Unix()
return
}
... ...
... ... @@ -158,7 +158,6 @@ func (repository *ArticleRepository) ModelToDomainModel(from *models.Article) (*
TargetUser: domain.ArticleTarget(from.TargetUser),
CountLove: from.CountLove,
CountComment: from.CountComment,
Tags: from.Tags,
Show: domain.ArticleShow(from.Show),
}
return to, nil
... ... @@ -182,7 +181,6 @@ func (repository *ArticleRepository) DomainModelToModel(from *domain.Article) (*
TargetUser: int(from.TargetUser),
CountLove: from.CountLove,
CountComment: from.CountComment,
Tags: from.Tags,
Show: int(from.Show),
}
// err := copier.Copy(to, from)
... ...
package repository
import (
"context"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
"github.com/tiptok/gocomm/pkg/cache"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/models"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
)
type ArticleTagRepository struct {
*cache.CachedRepository
}
func (repository *ArticleTagRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.ArticleTag) (*domain.ArticleTag, error) {
var (
err error
m = &models.ArticleTag{}
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
if tx = tx.Model(m).Save(m); tx.Error != nil {
return nil, tx.Error
}
dm.Id = m.Id
return repository.ModelToDomainModel(m)
}
func (repository *ArticleTagRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.ArticleTag) (*domain.ArticleTag, error) {
var (
err error
m *models.ArticleTag
tx = conn.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Updates(m)
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleTagRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.ArticleTag) (*domain.ArticleTag, error) {
var (
err error
m *models.ArticleTag
tx = transaction.DB()
)
if m, err = repository.DomainModelToModel(dm); err != nil {
return nil, err
}
oldVersion := dm.Version
m.Version += 1
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m)
if tx.RowsAffected == 0 {
return nil, domain.ErrUpdateFail
}
return nil, tx.Error
}
if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleTagRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.ArticleTag) (*domain.ArticleTag, error) {
var (
tx = conn.DB()
m = &models.ArticleTag{Id: dm.Id}
)
queryFunc := func() (interface{}, error) {
tx = tx.Where("id = ?", m.Id).Delete(m)
return m, tx.Error
}
if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil {
return dm, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleTagRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.ArticleTag, error) {
var (
err error
tx = conn.DB()
m = new(models.ArticleTag)
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(m).Where("id = ?", id).First(m)
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
return nil, domain.ErrNotFound
}
return m, tx.Error
}
cacheModel := new(models.ArticleTag)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *ArticleTagRepository) Find(ctx context.Context, conn transaction.Conn, companyId int64, queryOptions map[string]interface{}) (int64, []*domain.ArticleTag, error) {
var (
tx = conn.DB()
ms []*models.ArticleTag
dms = make([]*domain.ArticleTag, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).
Where("company_id=?", companyId).
Order("id desc")
if v, ok := queryOptions["name"]; ok {
tx.Where("id like ?", v)
}
if v, ok := queryOptions["group"]; ok {
tx.Where("group like ?", v)
}
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
return dms, nil
}
if _, err := repository.Query(queryFunc); err != nil {
return 0, nil, err
}
for _, item := range ms {
if dm, err := repository.ModelToDomainModel(item); err != nil {
return 0, dms, err
} else {
dms = append(dms, dm)
}
}
return total, dms, nil
}
func (repository *ArticleTagRepository) ModelToDomainModel(from *models.ArticleTag) (*domain.ArticleTag, error) {
to := &domain.ArticleTag{}
err := copier.Copy(to, from)
return to, err
}
func (repository *ArticleTagRepository) DomainModelToModel(from *domain.ArticleTag) (*models.ArticleTag, error) {
to := &models.ArticleTag{}
err := copier.Copy(to, from)
return to, err
}
func NewArticleTagRepository(cache *cache.CachedRepository) domain.ArticleTagRepository {
return &ArticleTagRepository{CachedRepository: cache}
}
... ...
... ... @@ -120,7 +120,7 @@ func (repository *MessageSystemRepository) Find(ctx context.Context, conn transa
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
tx = tx.Model(&ms).Order("created_at desc") // 创建时间降序
if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil {
return dms, tx.Error
}
... ...
... ... @@ -24,7 +24,6 @@ type Article struct {
TargetUser ArticleTarget `json:"targetUser"` // 分发方式 0 分发给所有人 1 分发给指定的人
CountLove int `json:"countLove"` // 点赞数量
CountComment int `json:"countComment"` // 评论数量
Tags []int `json:"tags"` // 标签
Show ArticleShow `json:"showState"` // 评论的展示状态(0显示、1不显示)
// ...more
}
... ...
... ... @@ -26,5 +26,5 @@ type ArticleTagRepository interface {
Delete(ctx context.Context, conn transaction.Conn, dm *ArticleTag) (*ArticleTag, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *ArticleTag) (*ArticleTag, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*ArticleTag, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*ArticleTag, error)
Find(ctx context.Context, conn transaction.Conn, companyId int64, queryOptions map[string]interface{}) (int64, []*ArticleTag, error)
}
... ...
... ... @@ -21,8 +21,8 @@ type MessageSystem struct {
type MsgSystemType int
const (
MsgTypeNormal MsgSystemType = 0
MsgTypeAbnormal MsgSystemType = 1
MsgTypeNormal MsgSystemType = 1
MsgTypeAbnormal MsgSystemType = 2
)
type MessageSystemRepository interface {
... ...