正在显示
10 个修改的文件
包含
738 行增加
和
0 行删除
cmd/discuss/doc/dsl/api/message_business.api
0 → 100644
1 | + | ||
2 | +syntax = "v1" | ||
3 | + | ||
4 | +info( | ||
5 | + title: "xx实例" | ||
6 | + desc: "xx实例" | ||
7 | + author: "author" | ||
8 | + email: "email" | ||
9 | + version: "v1" | ||
10 | +) | ||
11 | + | ||
12 | +@server( | ||
13 | + prefix: message_business/v1 | ||
14 | + group: message_business | ||
15 | + jwt: JwtAuth | ||
16 | +) | ||
17 | +service Core { | ||
18 | + @handler getMessageBusiness | ||
19 | + post /message_business/:id (MessageBusinessGetRequest) returns (MessageBusinessGetResponse) | ||
20 | + @handler saveMessageBusiness | ||
21 | + post /message_business (MessageBusinessSaveRequest) returns (MessageBusinessSaveResponse) | ||
22 | + @handler deleteMessageBusiness | ||
23 | + delete /message_business/:id (MessageBusinessDeleteRequest) returns (MessageBusinessDeleteResponse) | ||
24 | + @handler updateMessageBusiness | ||
25 | + put /message_business/:id (MessageBusinessUpdateRequest) returns (MessageBusinessUpdateResponse) | ||
26 | + @handler searchMessageBusiness | ||
27 | + post /message_business/search (MessageBusinessSearchRequest) returns (MessageBusinessSearchResponse) | ||
28 | +} | ||
29 | + | ||
30 | +type ( | ||
31 | + MessageBusinessGetRequest { | ||
32 | + Id int64 `path:"id"` | ||
33 | + } | ||
34 | + MessageBusinessGetResponse struct{ | ||
35 | + MessageBusiness MessageBusinessItem `json:"message_business"` | ||
36 | + } | ||
37 | + | ||
38 | + MessageBusinessSaveRequest struct{ | ||
39 | + MessageBusiness MessageBusinessItem `json:"message_business"` | ||
40 | + } | ||
41 | + MessageBusinessSaveResponse struct{} | ||
42 | + | ||
43 | + MessageBusinessDeleteRequest struct{ | ||
44 | + Id int64 `path:"id"` | ||
45 | + } | ||
46 | + MessageBusinessDeleteResponse struct{} | ||
47 | + | ||
48 | + MessageBusinessUpdateRequest struct{ | ||
49 | + Id int64 `path:"id"` | ||
50 | + MessageBusiness MessageBusinessItem `json:"message_business"` | ||
51 | + } | ||
52 | + MessageBusinessUpdateResponse struct{} | ||
53 | + | ||
54 | + MessageBusinessSearchRequest struct{ | ||
55 | + Page int `json:"page"` | ||
56 | + Size int `json:"size"` | ||
57 | + } | ||
58 | + MessageBusinessSearchResponse{ | ||
59 | + List []MessageBusinessItem `json:"list"` | ||
60 | + Total int64 `json:"total"` | ||
61 | + } | ||
62 | + MessageBusinessItem struct{ | ||
63 | + | ||
64 | + } | ||
65 | +) |
cmd/discuss/doc/dsl/api/message_system.api
0 → 100644
1 | + | ||
2 | +syntax = "v1" | ||
3 | + | ||
4 | +info( | ||
5 | + title: "xx实例" | ||
6 | + desc: "xx实例" | ||
7 | + author: "author" | ||
8 | + email: "email" | ||
9 | + version: "v1" | ||
10 | +) | ||
11 | + | ||
12 | +@server( | ||
13 | + prefix: message_system/v1 | ||
14 | + group: message_system | ||
15 | + jwt: JwtAuth | ||
16 | +) | ||
17 | +service Core { | ||
18 | + @handler getMessageSystem | ||
19 | + post /message_system/:id (MessageSystemGetRequest) returns (MessageSystemGetResponse) | ||
20 | + @handler saveMessageSystem | ||
21 | + post /message_system (MessageSystemSaveRequest) returns (MessageSystemSaveResponse) | ||
22 | + @handler deleteMessageSystem | ||
23 | + delete /message_system/:id (MessageSystemDeleteRequest) returns (MessageSystemDeleteResponse) | ||
24 | + @handler updateMessageSystem | ||
25 | + put /message_system/:id (MessageSystemUpdateRequest) returns (MessageSystemUpdateResponse) | ||
26 | + @handler searchMessageSystem | ||
27 | + post /message_system/search (MessageSystemSearchRequest) returns (MessageSystemSearchResponse) | ||
28 | +} | ||
29 | + | ||
30 | +type ( | ||
31 | + MessageSystemGetRequest { | ||
32 | + Id int64 `path:"id"` | ||
33 | + } | ||
34 | + MessageSystemGetResponse struct{ | ||
35 | + MessageSystem MessageSystemItem `json:"message_system"` | ||
36 | + } | ||
37 | + | ||
38 | + MessageSystemSaveRequest struct{ | ||
39 | + MessageSystem MessageSystemItem `json:"message_system"` | ||
40 | + } | ||
41 | + MessageSystemSaveResponse struct{} | ||
42 | + | ||
43 | + MessageSystemDeleteRequest struct{ | ||
44 | + Id int64 `path:"id"` | ||
45 | + } | ||
46 | + MessageSystemDeleteResponse struct{} | ||
47 | + | ||
48 | + MessageSystemUpdateRequest struct{ | ||
49 | + Id int64 `path:"id"` | ||
50 | + MessageSystem MessageSystemItem `json:"message_system"` | ||
51 | + } | ||
52 | + MessageSystemUpdateResponse struct{} | ||
53 | + | ||
54 | + MessageSystemSearchRequest struct{ | ||
55 | + Page int `json:"page"` | ||
56 | + Size int `json:"size"` | ||
57 | + } | ||
58 | + MessageSystemSearchResponse{ | ||
59 | + List []MessageSystemItem `json:"list"` | ||
60 | + Total int64 `json:"total"` | ||
61 | + } | ||
62 | + MessageSystemItem struct{ | ||
63 | + | ||
64 | + } | ||
65 | +) |
1 | + | ||
2 | +syntax = "proto3"; | ||
3 | + | ||
4 | +option go_package ="./pb"; | ||
5 | + | ||
6 | +package pb; | ||
7 | + | ||
8 | +message MessageBusinessGetReq { | ||
9 | + int64 Id = 1; | ||
10 | +} | ||
11 | +message MessageBusinessGetResp{ | ||
12 | + MessageBusinessItem User = 1; | ||
13 | +} | ||
14 | + | ||
15 | +message MessageBusinessSaveReq { | ||
16 | + | ||
17 | +} | ||
18 | +message MessageBusinessSaveResp{ | ||
19 | + | ||
20 | +} | ||
21 | + | ||
22 | +message MessageBusinessDeleteReq { | ||
23 | + int64 Id = 1; | ||
24 | +} | ||
25 | +message MessageBusinessDeleteResp{ | ||
26 | + | ||
27 | +} | ||
28 | + | ||
29 | +message MessageBusinessUpdateReq { | ||
30 | + int64 Id = 1; | ||
31 | +} | ||
32 | +message MessageBusinessUpdateResp{ | ||
33 | + | ||
34 | +} | ||
35 | + | ||
36 | +message MessageBusinessSearchReq { | ||
37 | + int64 PageNumber = 1; | ||
38 | + int64 PageSize = 2; | ||
39 | +} | ||
40 | +message MessageBusinessSearchResp{ | ||
41 | + repeated MessageBusinessItem List =1; | ||
42 | + int64 Total =2; | ||
43 | +} | ||
44 | +message MessageBusinessItem { | ||
45 | + | ||
46 | +} | ||
47 | + | ||
48 | +service MessageBusinessService { | ||
49 | + rpc MessageBusinessGet(MessageBusinessGetReq) returns(MessageBusinessGetResp); | ||
50 | + rpc MessageBusinessSave(MessageBusinessSaveReq) returns(MessageBusinessSaveResp); | ||
51 | + rpc MessageBusinessDelete(MessageBusinessDeleteReq) returns(MessageBusinessDeleteResp); | ||
52 | + rpc MessageBusinessUpdate(MessageBusinessUpdateReq) returns(MessageBusinessUpdateResp); | ||
53 | + rpc MessageBusinessSearch(MessageBusinessSearchReq) returns(MessageBusinessSearchResp); | ||
54 | +} |
cmd/discuss/doc/dsl/rpc/message_system.proto
0 → 100644
1 | + | ||
2 | +syntax = "proto3"; | ||
3 | + | ||
4 | +option go_package ="./pb"; | ||
5 | + | ||
6 | +package pb; | ||
7 | + | ||
8 | +message MessageSystemGetReq { | ||
9 | + int64 Id = 1; | ||
10 | +} | ||
11 | +message MessageSystemGetResp{ | ||
12 | + MessageSystemItem User = 1; | ||
13 | +} | ||
14 | + | ||
15 | +message MessageSystemSaveReq { | ||
16 | + | ||
17 | +} | ||
18 | +message MessageSystemSaveResp{ | ||
19 | + | ||
20 | +} | ||
21 | + | ||
22 | +message MessageSystemDeleteReq { | ||
23 | + int64 Id = 1; | ||
24 | +} | ||
25 | +message MessageSystemDeleteResp{ | ||
26 | + | ||
27 | +} | ||
28 | + | ||
29 | +message MessageSystemUpdateReq { | ||
30 | + int64 Id = 1; | ||
31 | +} | ||
32 | +message MessageSystemUpdateResp{ | ||
33 | + | ||
34 | +} | ||
35 | + | ||
36 | +message MessageSystemSearchReq { | ||
37 | + int64 PageNumber = 1; | ||
38 | + int64 PageSize = 2; | ||
39 | +} | ||
40 | +message MessageSystemSearchResp{ | ||
41 | + repeated MessageSystemItem List =1; | ||
42 | + int64 Total =2; | ||
43 | +} | ||
44 | +message MessageSystemItem { | ||
45 | + | ||
46 | +} | ||
47 | + | ||
48 | +service MessageSystemService { | ||
49 | + rpc MessageSystemGet(MessageSystemGetReq) returns(MessageSystemGetResp); | ||
50 | + rpc MessageSystemSave(MessageSystemSaveReq) returns(MessageSystemSaveResp); | ||
51 | + rpc MessageSystemDelete(MessageSystemDeleteReq) returns(MessageSystemDeleteResp); | ||
52 | + rpc MessageSystemUpdate(MessageSystemUpdateReq) returns(MessageSystemUpdateResp); | ||
53 | + rpc MessageSystemSearch(MessageSystemSearchReq) returns(MessageSystemSearchResp); | ||
54 | +} |
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" | ||
6 | + "gorm.io/gorm" | ||
7 | + "gorm.io/plugin/soft_delete" | ||
8 | +) | ||
9 | + | ||
10 | +type MessageBusiness struct { | ||
11 | + Id int64 // 唯一标识 | ||
12 | + Type int `json:"type"` // 分类 (1回复 2点赞 3被采纳) | ||
13 | + OptType int `json:"optType"` // 操作类型(1针对文章、1针对评论、2针对圆桌) | ||
14 | + TriggerId int64 `json:"triggerId"` // 触发者ID | ||
15 | + RecipientId int64 `json:"recipientId"` // 接收者ID | ||
16 | + ArticleId int64 `json:"articleId"` // 文章ID | ||
17 | + CommentId int64 `json:"commentId"` // 评论ID | ||
18 | + DiscussionId int64 `json:"discussionId"` // 圆桌ID | ||
19 | + DiscussionOpinionId int64 `json:"discussionOpinionId"` // 观点ID | ||
20 | + Content string `json:"content"` // 消息内容 | ||
21 | + CreatedAt int64 `json:",omitempty"` | ||
22 | + UpdatedAt int64 `json:",omitempty"` | ||
23 | + DeletedAt int64 `json:",omitempty"` | ||
24 | + Version int `json:",omitempty"` | ||
25 | + IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"` | ||
26 | +} | ||
27 | + | ||
28 | +func (m *MessageBusiness) TableName() string { | ||
29 | + return "message_business" | ||
30 | +} | ||
31 | + | ||
32 | +func (m *MessageBusiness) BeforeCreate(tx *gorm.DB) (err error) { | ||
33 | + // m.CreatedAt = time.Now().Unix() | ||
34 | + // m.UpdatedAt = time.Now().Unix() | ||
35 | + return | ||
36 | +} | ||
37 | + | ||
38 | +func (m *MessageBusiness) BeforeUpdate(tx *gorm.DB) (err error) { | ||
39 | + // m.UpdatedAt = time.Now().Unix() | ||
40 | + return | ||
41 | +} | ||
42 | + | ||
43 | +func (m *MessageBusiness) CacheKeyFunc() string { | ||
44 | + if m.Id == 0 { | ||
45 | + return "" | ||
46 | + } | ||
47 | + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id) | ||
48 | +} | ||
49 | + | ||
50 | +func (m *MessageBusiness) CacheKeyFuncByObject(obj interface{}) string { | ||
51 | + if v, ok := obj.(*MessageBusiness); ok { | ||
52 | + return v.CacheKeyFunc() | ||
53 | + } | ||
54 | + return "" | ||
55 | +} | ||
56 | + | ||
57 | +func (m *MessageBusiness) CachePrimaryKeyFunc() string { | ||
58 | + if len("") == 0 { | ||
59 | + return "" | ||
60 | + } | ||
61 | + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key") | ||
62 | +} |
1 | +package models | ||
2 | + | ||
3 | +import ( | ||
4 | + "fmt" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain" | ||
6 | + "gorm.io/gorm" | ||
7 | + "gorm.io/plugin/soft_delete" | ||
8 | +) | ||
9 | + | ||
10 | +type MessageSystem struct { | ||
11 | + Id int64 // 唯一标识 | ||
12 | + RecipientId int64 `json:"recipientId"` // 接收者ID | ||
13 | + Type int `json:"type"` // 系统分类(0待定、1业务正常通知、2业务异常通知) | ||
14 | + Title string `json:"title"` // 标题 | ||
15 | + Content string `json:"content"` // 内容 | ||
16 | + CreatedAt int64 `json:",omitempty"` | ||
17 | + UpdatedAt int64 `json:",omitempty"` | ||
18 | + DeletedAt int64 `json:",omitempty"` | ||
19 | + Version int `json:",omitempty"` | ||
20 | + IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"` | ||
21 | +} | ||
22 | + | ||
23 | +func (m *MessageSystem) TableName() string { | ||
24 | + return "message_system" | ||
25 | +} | ||
26 | + | ||
27 | +func (m *MessageSystem) BeforeCreate(tx *gorm.DB) (err error) { | ||
28 | + // m.CreatedAt = time.Now().Unix() | ||
29 | + // m.UpdatedAt = time.Now().Unix() | ||
30 | + return | ||
31 | +} | ||
32 | + | ||
33 | +func (m *MessageSystem) BeforeUpdate(tx *gorm.DB) (err error) { | ||
34 | + // m.UpdatedAt = time.Now().Unix() | ||
35 | + return | ||
36 | +} | ||
37 | + | ||
38 | +func (m *MessageSystem) CacheKeyFunc() string { | ||
39 | + if m.Id == 0 { | ||
40 | + return "" | ||
41 | + } | ||
42 | + return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id) | ||
43 | +} | ||
44 | + | ||
45 | +func (m *MessageSystem) CacheKeyFuncByObject(obj interface{}) string { | ||
46 | + if v, ok := obj.(*MessageSystem); ok { | ||
47 | + return v.CacheKeyFunc() | ||
48 | + } | ||
49 | + return "" | ||
50 | +} | ||
51 | + | ||
52 | +func (m *MessageSystem) CachePrimaryKeyFunc() string { | ||
53 | + if len("") == 0 { | ||
54 | + return "" | ||
55 | + } | ||
56 | + return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key") | ||
57 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "github.com/jinzhu/copier" | ||
6 | + "github.com/pkg/errors" | ||
7 | + "github.com/tiptok/gocomm/pkg/cache" | ||
8 | + "gorm.io/gorm" | ||
9 | +) | ||
10 | + | ||
11 | +type MessageBusinessRepository struct { | ||
12 | + *cache.CachedRepository | ||
13 | +} | ||
14 | + | ||
15 | +func (repository *MessageBusinessRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.MessageBusiness) (*domain.MessageBusiness, error) { | ||
16 | + var ( | ||
17 | + err error | ||
18 | + m = &models.MessageBusiness{} | ||
19 | + tx = conn.DB() | ||
20 | + ) | ||
21 | + if m, err = repository.DomainModelToModel(dm); err != nil { | ||
22 | + return nil, err | ||
23 | + } | ||
24 | + if tx = tx.Model(m).Save(m); tx.Error != nil { | ||
25 | + return nil, tx.Error | ||
26 | + } | ||
27 | + dm.Id = m.Id | ||
28 | + return repository.ModelToDomainModel(m) | ||
29 | + | ||
30 | +} | ||
31 | + | ||
32 | +func (repository *MessageBusinessRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.MessageBusiness) (*domain.MessageBusiness, error) { | ||
33 | + var ( | ||
34 | + err error | ||
35 | + m *models.MessageBusiness | ||
36 | + tx = conn.DB() | ||
37 | + ) | ||
38 | + if m, err = repository.DomainModelToModel(dm); err != nil { | ||
39 | + return nil, err | ||
40 | + } | ||
41 | + queryFunc := func() (interface{}, error) { | ||
42 | + tx = tx.Model(m).Updates(m) | ||
43 | + return nil, tx.Error | ||
44 | + } | ||
45 | + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { | ||
46 | + return nil, err | ||
47 | + } | ||
48 | + return repository.ModelToDomainModel(m) | ||
49 | +} | ||
50 | + | ||
51 | +func (repository *MessageBusinessRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.MessageBusiness) (*domain.MessageBusiness, error) { | ||
52 | + var ( | ||
53 | + err error | ||
54 | + m *models.MessageBusiness | ||
55 | + tx = transaction.DB() | ||
56 | + ) | ||
57 | + if m, err = repository.DomainModelToModel(dm); err != nil { | ||
58 | + return nil, err | ||
59 | + } | ||
60 | + oldVersion := dm.Version | ||
61 | + m.Version += 1 | ||
62 | + queryFunc := func() (interface{}, error) { | ||
63 | + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m) | ||
64 | + if tx.RowsAffected == 0 { | ||
65 | + return nil, domain.ErrUpdateFail | ||
66 | + } | ||
67 | + return nil, tx.Error | ||
68 | + } | ||
69 | + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { | ||
70 | + return nil, err | ||
71 | + } | ||
72 | + return repository.ModelToDomainModel(m) | ||
73 | +} | ||
74 | + | ||
75 | +func (repository *MessageBusinessRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.MessageBusiness) (*domain.MessageBusiness, error) { | ||
76 | + var ( | ||
77 | + tx = conn.DB() | ||
78 | + m = &models.MessageBusiness{Id: dm.Identify().(int64)} | ||
79 | + ) | ||
80 | + queryFunc := func() (interface{}, error) { | ||
81 | + tx = tx.Where("id = ?", m.Id).Delete(m) | ||
82 | + return m, tx.Error | ||
83 | + } | ||
84 | + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { | ||
85 | + return dm, err | ||
86 | + } | ||
87 | + return repository.ModelToDomainModel(m) | ||
88 | +} | ||
89 | + | ||
90 | +func (repository *MessageBusinessRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.MessageBusiness, error) { | ||
91 | + var ( | ||
92 | + err error | ||
93 | + tx = conn.DB() | ||
94 | + m = new(models.MessageBusiness) | ||
95 | + ) | ||
96 | + queryFunc := func() (interface{}, error) { | ||
97 | + tx = tx.Model(m).Where("id = ?", id).First(m) | ||
98 | + if errors.Is(tx.Error, gorm.ErrRecordNotFound) { | ||
99 | + return nil, domain.ErrNotFound | ||
100 | + } | ||
101 | + return m, tx.Error | ||
102 | + } | ||
103 | + cacheModel := new(models.MessageBusiness) | ||
104 | + cacheModel.Id = id | ||
105 | + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil { | ||
106 | + return nil, err | ||
107 | + } | ||
108 | + return repository.ModelToDomainModel(m) | ||
109 | +} | ||
110 | + | ||
111 | +func (repository *MessageBusinessRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.MessageBusiness, error) { | ||
112 | + var ( | ||
113 | + tx = conn.DB() | ||
114 | + ms []*models.MessageBusiness | ||
115 | + dms = make([]*domain.MessageBusiness, 0) | ||
116 | + total int64 | ||
117 | + ) | ||
118 | + queryFunc := func() (interface{}, error) { | ||
119 | + tx = tx.Model(&ms).Order("id desc") | ||
120 | + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil { | ||
121 | + return dms, tx.Error | ||
122 | + } | ||
123 | + return dms, nil | ||
124 | + } | ||
125 | + | ||
126 | + if _, err := repository.Query(queryFunc); err != nil { | ||
127 | + return 0, nil, err | ||
128 | + } | ||
129 | + | ||
130 | + for _, item := range ms { | ||
131 | + if dm, err := repository.ModelToDomainModel(item); err != nil { | ||
132 | + return 0, dms, err | ||
133 | + } else { | ||
134 | + dms = append(dms, dm) | ||
135 | + } | ||
136 | + } | ||
137 | + return total, dms, nil | ||
138 | +} | ||
139 | + | ||
140 | +func (repository *MessageBusinessRepository) ModelToDomainModel(from *models.MessageBusiness) (*domain.MessageBusiness, error) { | ||
141 | + to := &domain.MessageBusiness{} | ||
142 | + err := copier.Copy(to, from) | ||
143 | + return to, err | ||
144 | +} | ||
145 | + | ||
146 | +func (repository *MessageBusinessRepository) DomainModelToModel(from *domain.MessageBusiness) (*models.MessageBusiness, error) { | ||
147 | + to := &models.MessageBusiness{} | ||
148 | + err := copier.Copy(to, from) | ||
149 | + return to, err | ||
150 | +} | ||
151 | + | ||
152 | +func NewMessageBusinessRepository(cache *cache.CachedRepository) domain.MessageBusinessRepository { | ||
153 | + return &MessageBusinessRepository{CachedRepository: cache} | ||
154 | +} |
1 | +package repository | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "github.com/jinzhu/copier" | ||
6 | + "github.com/pkg/errors" | ||
7 | + "github.com/tiptok/gocomm/pkg/cache" | ||
8 | + "gorm.io/gorm" | ||
9 | +) | ||
10 | + | ||
11 | +type MessageSystemRepository struct { | ||
12 | + *cache.CachedRepository | ||
13 | +} | ||
14 | + | ||
15 | +func (repository *MessageSystemRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.MessageSystem) (*domain.MessageSystem, error) { | ||
16 | + var ( | ||
17 | + err error | ||
18 | + m = &models.MessageSystem{} | ||
19 | + tx = conn.DB() | ||
20 | + ) | ||
21 | + if m, err = repository.DomainModelToModel(dm); err != nil { | ||
22 | + return nil, err | ||
23 | + } | ||
24 | + if tx = tx.Model(m).Save(m); tx.Error != nil { | ||
25 | + return nil, tx.Error | ||
26 | + } | ||
27 | + dm.Id = m.Id | ||
28 | + return repository.ModelToDomainModel(m) | ||
29 | + | ||
30 | +} | ||
31 | + | ||
32 | +func (repository *MessageSystemRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.MessageSystem) (*domain.MessageSystem, error) { | ||
33 | + var ( | ||
34 | + err error | ||
35 | + m *models.MessageSystem | ||
36 | + tx = conn.DB() | ||
37 | + ) | ||
38 | + if m, err = repository.DomainModelToModel(dm); err != nil { | ||
39 | + return nil, err | ||
40 | + } | ||
41 | + queryFunc := func() (interface{}, error) { | ||
42 | + tx = tx.Model(m).Updates(m) | ||
43 | + return nil, tx.Error | ||
44 | + } | ||
45 | + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { | ||
46 | + return nil, err | ||
47 | + } | ||
48 | + return repository.ModelToDomainModel(m) | ||
49 | +} | ||
50 | + | ||
51 | +func (repository *MessageSystemRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.MessageSystem) (*domain.MessageSystem, error) { | ||
52 | + var ( | ||
53 | + err error | ||
54 | + m *models.MessageSystem | ||
55 | + tx = transaction.DB() | ||
56 | + ) | ||
57 | + if m, err = repository.DomainModelToModel(dm); err != nil { | ||
58 | + return nil, err | ||
59 | + } | ||
60 | + oldVersion := dm.Version | ||
61 | + m.Version += 1 | ||
62 | + queryFunc := func() (interface{}, error) { | ||
63 | + tx = tx.Model(m).Select("*").Where("id = ?", m.Id).Where("version = ?", oldVersion).Updates(m) | ||
64 | + if tx.RowsAffected == 0 { | ||
65 | + return nil, domain.ErrUpdateFail | ||
66 | + } | ||
67 | + return nil, tx.Error | ||
68 | + } | ||
69 | + if _, err = repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { | ||
70 | + return nil, err | ||
71 | + } | ||
72 | + return repository.ModelToDomainModel(m) | ||
73 | +} | ||
74 | + | ||
75 | +func (repository *MessageSystemRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.MessageSystem) (*domain.MessageSystem, error) { | ||
76 | + var ( | ||
77 | + tx = conn.DB() | ||
78 | + m = &models.MessageSystem{Id: dm.Identify().(int64)} | ||
79 | + ) | ||
80 | + queryFunc := func() (interface{}, error) { | ||
81 | + tx = tx.Where("id = ?", m.Id).Delete(m) | ||
82 | + return m, tx.Error | ||
83 | + } | ||
84 | + if _, err := repository.Query(queryFunc, m.CacheKeyFunc()); err != nil { | ||
85 | + return dm, err | ||
86 | + } | ||
87 | + return repository.ModelToDomainModel(m) | ||
88 | +} | ||
89 | + | ||
90 | +func (repository *MessageSystemRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.MessageSystem, error) { | ||
91 | + var ( | ||
92 | + err error | ||
93 | + tx = conn.DB() | ||
94 | + m = new(models.MessageSystem) | ||
95 | + ) | ||
96 | + queryFunc := func() (interface{}, error) { | ||
97 | + tx = tx.Model(m).Where("id = ?", id).First(m) | ||
98 | + if errors.Is(tx.Error, gorm.ErrRecordNotFound) { | ||
99 | + return nil, domain.ErrNotFound | ||
100 | + } | ||
101 | + return m, tx.Error | ||
102 | + } | ||
103 | + cacheModel := new(models.MessageSystem) | ||
104 | + cacheModel.Id = id | ||
105 | + if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil { | ||
106 | + return nil, err | ||
107 | + } | ||
108 | + return repository.ModelToDomainModel(m) | ||
109 | +} | ||
110 | + | ||
111 | +func (repository *MessageSystemRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.MessageSystem, error) { | ||
112 | + var ( | ||
113 | + tx = conn.DB() | ||
114 | + ms []*models.MessageSystem | ||
115 | + dms = make([]*domain.MessageSystem, 0) | ||
116 | + total int64 | ||
117 | + ) | ||
118 | + queryFunc := func() (interface{}, error) { | ||
119 | + tx = tx.Model(&ms).Order("id desc") | ||
120 | + if total, tx = transaction.PaginationAndCount(ctx, tx, queryOptions, &ms); tx.Error != nil { | ||
121 | + return dms, tx.Error | ||
122 | + } | ||
123 | + return dms, nil | ||
124 | + } | ||
125 | + | ||
126 | + if _, err := repository.Query(queryFunc); err != nil { | ||
127 | + return 0, nil, err | ||
128 | + } | ||
129 | + | ||
130 | + for _, item := range ms { | ||
131 | + if dm, err := repository.ModelToDomainModel(item); err != nil { | ||
132 | + return 0, dms, err | ||
133 | + } else { | ||
134 | + dms = append(dms, dm) | ||
135 | + } | ||
136 | + } | ||
137 | + return total, dms, nil | ||
138 | +} | ||
139 | + | ||
140 | +func (repository *MessageSystemRepository) ModelToDomainModel(from *models.MessageSystem) (*domain.MessageSystem, error) { | ||
141 | + to := &domain.MessageSystem{} | ||
142 | + err := copier.Copy(to, from) | ||
143 | + return to, err | ||
144 | +} | ||
145 | + | ||
146 | +func (repository *MessageSystemRepository) DomainModelToModel(from *domain.MessageSystem) (*models.MessageSystem, error) { | ||
147 | + to := &models.MessageSystem{} | ||
148 | + err := copier.Copy(to, from) | ||
149 | + return to, err | ||
150 | +} | ||
151 | + | ||
152 | +func NewMessageSystemRepository(cache *cache.CachedRepository) domain.MessageSystemRepository { | ||
153 | + return &MessageSystemRepository{CachedRepository: cache} | ||
154 | +} |
1 | +package domain | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction" | ||
6 | +) | ||
7 | + | ||
8 | +type MessageBusiness struct { | ||
9 | + Id int64 // 唯一标识 | ||
10 | + Type int `json:"type"` // 分类 (1回复 2点赞 3被采纳) | ||
11 | + OptType int `json:"optType"` // 操作类型(1针对文章、1针对评论、2针对圆桌) | ||
12 | + TriggerId int64 `json:"triggerId"` // 触发者ID | ||
13 | + RecipientId int64 `json:"recipientId"` // 接收者ID | ||
14 | + ArticleId int64 `json:"articleId"` // 文章ID | ||
15 | + CommentId int64 `json:"commentId"` // 评论ID | ||
16 | + DiscussionId int64 `json:"discussionId"` // 圆桌ID | ||
17 | + DiscussionOpinionId int64 `json:"discussionOpinionId"` // 观点ID | ||
18 | + Content string `json:"content"` // 消息内容 | ||
19 | + CreatedAt int64 `json:",omitempty"` | ||
20 | + UpdatedAt int64 `json:",omitempty"` | ||
21 | + DeletedAt int64 `json:",omitempty"` | ||
22 | + Version int `json:",omitempty"` | ||
23 | +} | ||
24 | + | ||
25 | +type MessageBusinessRepository interface { | ||
26 | + Insert(ctx context.Context, conn transaction.Conn, dm *MessageBusiness) (*MessageBusiness, error) | ||
27 | + Update(ctx context.Context, conn transaction.Conn, dm *MessageBusiness) (*MessageBusiness, error) | ||
28 | + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *MessageBusiness) (*MessageBusiness, error) | ||
29 | + Delete(ctx context.Context, conn transaction.Conn, dm *MessageBusiness) (*MessageBusiness, error) | ||
30 | + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*MessageBusiness, error) | ||
31 | + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*MessageBusiness, error) | ||
32 | +} | ||
33 | + | ||
34 | +func (m *MessageBusiness) Identify() interface{} { | ||
35 | + if m.Id == 0 { | ||
36 | + return nil | ||
37 | + } | ||
38 | + return m.Id | ||
39 | +} |
1 | +package domain | ||
2 | + | ||
3 | +import ( | ||
4 | + "context" | ||
5 | + "gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction" | ||
6 | +) | ||
7 | + | ||
8 | +type MessageSystem struct { | ||
9 | + Id int64 // 唯一标识 | ||
10 | + RecipientId int64 `json:"recipientId"` // 接收者ID | ||
11 | + Type int `json:"type"` // 系统分类(0待定、1业务正常通知、2业务异常通知) | ||
12 | + Title string `json:"title"` // 标题 | ||
13 | + Content string `json:"content"` // 内容 | ||
14 | + CreatedAt int64 `json:",omitempty"` | ||
15 | + UpdatedAt int64 `json:",omitempty"` | ||
16 | + DeletedAt int64 `json:",omitempty"` | ||
17 | + Version int `json:",omitempty"` | ||
18 | +} | ||
19 | + | ||
20 | +type MessageSystemRepository interface { | ||
21 | + Insert(ctx context.Context, conn transaction.Conn, dm *MessageSystem) (*MessageSystem, error) | ||
22 | + Update(ctx context.Context, conn transaction.Conn, dm *MessageSystem) (*MessageSystem, error) | ||
23 | + UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *MessageSystem) (*MessageSystem, error) | ||
24 | + Delete(ctx context.Context, conn transaction.Conn, dm *MessageSystem) (*MessageSystem, error) | ||
25 | + FindOne(ctx context.Context, conn transaction.Conn, id int64) (*MessageSystem, error) | ||
26 | + Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*MessageSystem, error) | ||
27 | +} | ||
28 | + | ||
29 | +func (m *MessageSystem) Identify() interface{} { | ||
30 | + if m.Id == 0 { | ||
31 | + return nil | ||
32 | + } | ||
33 | + return m.Id | ||
34 | +} |
-
请 注册 或 登录 后发表评论