作者 郑周

1.圆桌讨论模型

2.圆桌讨论-观点模型
3.圆桌讨论-采纳模型
4.小组件模型
syntax = "v1"
info(
title: "圆桌讨论"
desc: "圆桌讨论"
author: "zz"
email: "email"
version: "v1"
)
@server(
prefix: discussion/v1
group: discussion
jwt: JwtAuth
)
service Core {
@handler getDiscussion
post /discussion/:id (DiscussionGetRequest) returns (DiscussionGetResponse)
@handler saveDiscussion
post /discussion (DiscussionSaveRequest) returns (DiscussionSaveResponse)
@handler deleteDiscussion
delete /discussion/:id (DiscussionDeleteRequest) returns (DiscussionDeleteResponse)
@handler updateDiscussion
put /discussion/:id (DiscussionUpdateRequest) returns (DiscussionUpdateResponse)
@handler searchDiscussion
post /discussion/search (DiscussionSearchRequest) returns (DiscussionSearchResponse)
}
type (
DiscussionGetRequest {
Id int64 `path:"id"`
}
DiscussionGetResponse struct{
Discussion DiscussionItem `json:"discussion"`
}
DiscussionSaveRequest struct{
Discussion DiscussionItem `json:"discussion"`
}
DiscussionSaveResponse struct{}
DiscussionDeleteRequest struct{
Id int64 `path:"id"`
}
DiscussionDeleteResponse struct{}
DiscussionUpdateRequest struct{
Id int64 `path:"id"`
Discussion DiscussionItem `json:"discussion"`
}
DiscussionUpdateResponse struct{}
DiscussionSearchRequest struct{
Page int `json:"page"`
Size int `json:"size"`
}
DiscussionSearchResponse{
List []DiscussionItem `json:"list"`
Total int64 `json:"total"`
}
DiscussionItem struct{
}
)
... ...
syntax = "v1"
info(
title: "xx实例"
desc: "xx实例"
author: "author"
email: "email"
version: "v1"
)
@server(
prefix: discussion_accept/v1
group: discussion_accept
jwt: JwtAuth
)
service Core {
@handler getDiscussionAccept
post /discussion_accept/:id (DiscussionAcceptGetRequest) returns (DiscussionAcceptGetResponse)
@handler saveDiscussionAccept
post /discussion_accept (DiscussionAcceptSaveRequest) returns (DiscussionAcceptSaveResponse)
@handler deleteDiscussionAccept
delete /discussion_accept/:id (DiscussionAcceptDeleteRequest) returns (DiscussionAcceptDeleteResponse)
@handler updateDiscussionAccept
put /discussion_accept/:id (DiscussionAcceptUpdateRequest) returns (DiscussionAcceptUpdateResponse)
@handler searchDiscussionAccept
post /discussion_accept/search (DiscussionAcceptSearchRequest) returns (DiscussionAcceptSearchResponse)
}
type (
DiscussionAcceptGetRequest {
Id int64 `path:"id"`
}
DiscussionAcceptGetResponse struct{
DiscussionAccept DiscussionAcceptItem `json:"discussion_accept"`
}
DiscussionAcceptSaveRequest struct{
DiscussionAccept DiscussionAcceptItem `json:"discussion_accept"`
}
DiscussionAcceptSaveResponse struct{}
DiscussionAcceptDeleteRequest struct{
Id int64 `path:"id"`
}
DiscussionAcceptDeleteResponse struct{}
DiscussionAcceptUpdateRequest struct{
Id int64 `path:"id"`
DiscussionAccept DiscussionAcceptItem `json:"discussion_accept"`
}
DiscussionAcceptUpdateResponse struct{}
DiscussionAcceptSearchRequest struct{
Page int `json:"page"`
Size int `json:"size"`
}
DiscussionAcceptSearchResponse{
List []DiscussionAcceptItem `json:"list"`
Total int64 `json:"total"`
}
DiscussionAcceptItem struct{
}
)
... ...
syntax = "v1"
info(
title: "xx实例"
desc: "xx实例"
author: "author"
email: "email"
version: "v1"
)
@server(
prefix: discussion_opinion/v1
group: discussion_opinion
jwt: JwtAuth
)
service Core {
@handler getDiscussionOpinion
post /discussion_opinion/:id (DiscussionOpinionGetRequest) returns (DiscussionOpinionGetResponse)
@handler saveDiscussionOpinion
post /discussion_opinion (DiscussionOpinionSaveRequest) returns (DiscussionOpinionSaveResponse)
@handler deleteDiscussionOpinion
delete /discussion_opinion/:id (DiscussionOpinionDeleteRequest) returns (DiscussionOpinionDeleteResponse)
@handler updateDiscussionOpinion
put /discussion_opinion/:id (DiscussionOpinionUpdateRequest) returns (DiscussionOpinionUpdateResponse)
@handler searchDiscussionOpinion
post /discussion_opinion/search (DiscussionOpinionSearchRequest) returns (DiscussionOpinionSearchResponse)
}
type (
DiscussionOpinionGetRequest {
Id int64 `path:"id"`
}
DiscussionOpinionGetResponse struct{
DiscussionOpinion DiscussionOpinionItem `json:"discussion_opinion"`
}
DiscussionOpinionSaveRequest struct{
DiscussionOpinion DiscussionOpinionItem `json:"discussion_opinion"`
}
DiscussionOpinionSaveResponse struct{}
DiscussionOpinionDeleteRequest struct{
Id int64 `path:"id"`
}
DiscussionOpinionDeleteResponse struct{}
DiscussionOpinionUpdateRequest struct{
Id int64 `path:"id"`
DiscussionOpinion DiscussionOpinionItem `json:"discussion_opinion"`
}
DiscussionOpinionUpdateResponse struct{}
DiscussionOpinionSearchRequest struct{
Page int `json:"page"`
Size int `json:"size"`
}
DiscussionOpinionSearchResponse{
List []DiscussionOpinionItem `json:"list"`
Total int64 `json:"total"`
}
DiscussionOpinionItem struct{
}
)
... ...
syntax = "proto3";
option go_package ="./pb";
package pb;
message DiscussionGetReq {
int64 Id = 1;
}
message DiscussionGetResp{
DiscussionItem User = 1;
}
message DiscussionSaveReq {
}
message DiscussionSaveResp{
}
message DiscussionDeleteReq {
int64 Id = 1;
}
message DiscussionDeleteResp{
}
message DiscussionUpdateReq {
int64 Id = 1;
}
message DiscussionUpdateResp{
}
message DiscussionSearchReq {
int64 PageNumber = 1;
int64 PageSize = 2;
}
message DiscussionSearchResp{
repeated DiscussionItem List =1;
int64 Total =2;
}
message DiscussionItem {
}
service DiscussionService {
rpc DiscussionGet(DiscussionGetReq) returns(DiscussionGetResp);
rpc DiscussionSave(DiscussionSaveReq) returns(DiscussionSaveResp);
rpc DiscussionDelete(DiscussionDeleteReq) returns(DiscussionDeleteResp);
rpc DiscussionUpdate(DiscussionUpdateReq) returns(DiscussionUpdateResp);
rpc DiscussionSearch(DiscussionSearchReq) returns(DiscussionSearchResp);
}
... ...
syntax = "proto3";
option go_package ="./pb";
package pb;
message DiscussionAcceptGetReq {
int64 Id = 1;
}
message DiscussionAcceptGetResp{
DiscussionAcceptItem User = 1;
}
message DiscussionAcceptSaveReq {
}
message DiscussionAcceptSaveResp{
}
message DiscussionAcceptDeleteReq {
int64 Id = 1;
}
message DiscussionAcceptDeleteResp{
}
message DiscussionAcceptUpdateReq {
int64 Id = 1;
}
message DiscussionAcceptUpdateResp{
}
message DiscussionAcceptSearchReq {
int64 PageNumber = 1;
int64 PageSize = 2;
}
message DiscussionAcceptSearchResp{
repeated DiscussionAcceptItem List =1;
int64 Total =2;
}
message DiscussionAcceptItem {
}
service DiscussionAcceptService {
rpc DiscussionAcceptGet(DiscussionAcceptGetReq) returns(DiscussionAcceptGetResp);
rpc DiscussionAcceptSave(DiscussionAcceptSaveReq) returns(DiscussionAcceptSaveResp);
rpc DiscussionAcceptDelete(DiscussionAcceptDeleteReq) returns(DiscussionAcceptDeleteResp);
rpc DiscussionAcceptUpdate(DiscussionAcceptUpdateReq) returns(DiscussionAcceptUpdateResp);
rpc DiscussionAcceptSearch(DiscussionAcceptSearchReq) returns(DiscussionAcceptSearchResp);
}
... ...
syntax = "proto3";
option go_package ="./pb";
package pb;
message DiscussionOpinionGetReq {
int64 Id = 1;
}
message DiscussionOpinionGetResp{
DiscussionOpinionItem User = 1;
}
message DiscussionOpinionSaveReq {
}
message DiscussionOpinionSaveResp{
}
message DiscussionOpinionDeleteReq {
int64 Id = 1;
}
message DiscussionOpinionDeleteResp{
}
message DiscussionOpinionUpdateReq {
int64 Id = 1;
}
message DiscussionOpinionUpdateResp{
}
message DiscussionOpinionSearchReq {
int64 PageNumber = 1;
int64 PageSize = 2;
}
message DiscussionOpinionSearchResp{
repeated DiscussionOpinionItem List =1;
int64 Total =2;
}
message DiscussionOpinionItem {
}
service DiscussionOpinionService {
rpc DiscussionOpinionGet(DiscussionOpinionGetReq) returns(DiscussionOpinionGetResp);
rpc DiscussionOpinionSave(DiscussionOpinionSaveReq) returns(DiscussionOpinionSaveResp);
rpc DiscussionOpinionDelete(DiscussionOpinionDeleteReq) returns(DiscussionOpinionDeleteResp);
rpc DiscussionOpinionUpdate(DiscussionOpinionUpdateReq) returns(DiscussionOpinionUpdateResp);
rpc DiscussionOpinionSearch(DiscussionOpinionSearchReq) returns(DiscussionOpinionSearchResp);
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
"gorm.io/plugin/soft_delete"
)
// Discussion 圆桌讨论表
type Discussion struct {
Id int64 // 唯一标识
Title string `json:"title"` // 标题(不超过30个字符)
Content string `json:"content"` // 内容(不超过500个字符)
Images []domain.Image `json:"images"` // 图片(不超过5张)
Widget *domain.Widget `json:"widget"` // 小组件
ProviderId int64 `json:"providerId"` // 发起者ID
ParticipantIds []int64 `json:"participantIds"` // 参与者ID
ArticleId int64 `json:"articleId"` // 文章ID
State int `json:"state"` // 状态(1进行中、2搁置、3中止、4结案)
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
}
func (m *Discussion) TableName() string {
return "discussion"
}
func (m *Discussion) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *Discussion) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *Discussion) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *Discussion) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*Discussion); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *Discussion) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
"gorm.io/plugin/soft_delete"
)
// DiscussionAccept 圆桌讨论采纳表
type DiscussionAccept struct {
Id int64 // 唯一标识
ArticleId int64 `json:"articleId"` // 文章ID
DiscussionId int64 `json:"discussionId"` // 讨论ID
ProviderId int64 `json:"providerId"` // 发起人ID
DoIds []int64 `json:"DoIds"` // 意见采纳-用户观点
OpinionId int64 `json:"opinionId"` // 价值判定-选择观点
SubjectiveTitle string `json:"subjectiveTitle"` // 主观认定-标题
SubjectiveDesc string `json:"subjectiveDesc"` // 主观认定-内容
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
}
func (m *DiscussionAccept) TableName() string {
return "discussion_accept"
}
func (m *DiscussionAccept) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *DiscussionAccept) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *DiscussionAccept) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *DiscussionAccept) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*DiscussionAccept); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *DiscussionAccept) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
package models
import (
"fmt"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
"gorm.io/gorm"
"gorm.io/plugin/soft_delete"
)
// DiscussionOpinion 圆桌讨论观点表
type DiscussionOpinion struct {
Id int64 // 唯一标识
ArticleId int64 `json:"articleId"` // 文章ID
DiscussionId int64 `json:"discussionId"` // 讨论ID
ProviderId int64 `json:"providerId"` // 用户ID
OpinionId int64 `json:"opinionId"` // 选项ID
OpinionDesc string `json:"opinionDesc"` // 选项描述
Reason string `json:"reason"` // 理由
Show int `json:"show"` // 显示状态(1显示、0不显示)
AcceptUserId int64 `json:"acceptUserId"` // 采纳人ID(最终结案的时候唯一)
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
IsDel soft_delete.DeletedAt `gorm:"softDelete:flag,DeletedAtField:DeletedAt"`
}
func (m *DiscussionOpinion) TableName() string {
return "discussion_opinion"
}
func (m *DiscussionOpinion) BeforeCreate(tx *gorm.DB) (err error) {
// m.CreatedAt = time.Now().Unix()
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *DiscussionOpinion) BeforeUpdate(tx *gorm.DB) (err error) {
// m.UpdatedAt = time.Now().Unix()
return
}
func (m *DiscussionOpinion) CacheKeyFunc() string {
if m.Id == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:id:%v", domain.ProjectName, m.TableName(), m.Id)
}
func (m *DiscussionOpinion) CacheKeyFuncByObject(obj interface{}) string {
if v, ok := obj.(*DiscussionOpinion); ok {
return v.CacheKeyFunc()
}
return ""
}
func (m *DiscussionOpinion) CachePrimaryKeyFunc() string {
if len("") == 0 {
return ""
}
return fmt.Sprintf("%v:cache:%v:primarykey:%v", domain.ProjectName, m.TableName(), "key")
}
... ...
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 DiscussionAcceptRepository struct {
*cache.CachedRepository
}
func (repository *DiscussionAcceptRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.DiscussionAccept) (*domain.DiscussionAccept, error) {
var (
err error
m = &models.DiscussionAccept{}
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 *DiscussionAcceptRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.DiscussionAccept) (*domain.DiscussionAccept, error) {
var (
err error
m *models.DiscussionAccept
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 *DiscussionAcceptRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.DiscussionAccept) (*domain.DiscussionAccept, error) {
var (
err error
m *models.DiscussionAccept
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 *DiscussionAcceptRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.DiscussionAccept) (*domain.DiscussionAccept, error) {
var (
tx = conn.DB()
m = &models.DiscussionAccept{Id: dm.Identify().(int64)}
)
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 *DiscussionAcceptRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.DiscussionAccept, error) {
var (
err error
tx = conn.DB()
m = new(models.DiscussionAccept)
)
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.DiscussionAccept)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *DiscussionAcceptRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.DiscussionAccept, error) {
var (
tx = conn.DB()
ms []*models.DiscussionAccept
dms = make([]*domain.DiscussionAccept, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
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 *DiscussionAcceptRepository) ModelToDomainModel(from *models.DiscussionAccept) (*domain.DiscussionAccept, error) {
to := &domain.DiscussionAccept{}
err := copier.Copy(to, from)
return to, err
}
func (repository *DiscussionAcceptRepository) DomainModelToModel(from *domain.DiscussionAccept) (*models.DiscussionAccept, error) {
to := &models.DiscussionAccept{}
err := copier.Copy(to, from)
return to, err
}
func NewDiscussionAcceptRepository(cache *cache.CachedRepository) domain.DiscussionAcceptRepository {
return &DiscussionAcceptRepository{CachedRepository: cache}
}
... ...
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 DiscussionOpinionRepository struct {
*cache.CachedRepository
}
func (repository *DiscussionOpinionRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.DiscussionOpinion) (*domain.DiscussionOpinion, error) {
var (
err error
m = &models.DiscussionOpinion{}
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 *DiscussionOpinionRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.DiscussionOpinion) (*domain.DiscussionOpinion, error) {
var (
err error
m *models.DiscussionOpinion
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 *DiscussionOpinionRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.DiscussionOpinion) (*domain.DiscussionOpinion, error) {
var (
err error
m *models.DiscussionOpinion
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 *DiscussionOpinionRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.DiscussionOpinion) (*domain.DiscussionOpinion, error) {
var (
tx = conn.DB()
m = &models.DiscussionOpinion{Id: dm.Identify().(int64)}
)
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 *DiscussionOpinionRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.DiscussionOpinion, error) {
var (
err error
tx = conn.DB()
m = new(models.DiscussionOpinion)
)
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.DiscussionOpinion)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *DiscussionOpinionRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.DiscussionOpinion, error) {
var (
tx = conn.DB()
ms []*models.DiscussionOpinion
dms = make([]*domain.DiscussionOpinion, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
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 *DiscussionOpinionRepository) ModelToDomainModel(from *models.DiscussionOpinion) (*domain.DiscussionOpinion, error) {
to := &domain.DiscussionOpinion{}
err := copier.Copy(to, from)
return to, err
}
func (repository *DiscussionOpinionRepository) DomainModelToModel(from *domain.DiscussionOpinion) (*models.DiscussionOpinion, error) {
to := &models.DiscussionOpinion{}
err := copier.Copy(to, from)
return to, err
}
func NewDiscussionOpinionRepository(cache *cache.CachedRepository) domain.DiscussionOpinionRepository {
return &DiscussionOpinionRepository{CachedRepository: cache}
}
... ...
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 DiscussionRepository struct {
*cache.CachedRepository
}
func (repository *DiscussionRepository) Insert(ctx context.Context, conn transaction.Conn, dm *domain.Discussion) (*domain.Discussion, error) {
var (
err error
m = &models.Discussion{}
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 *DiscussionRepository) Update(ctx context.Context, conn transaction.Conn, dm *domain.Discussion) (*domain.Discussion, error) {
var (
err error
m *models.Discussion
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 *DiscussionRepository) UpdateWithVersion(ctx context.Context, transaction transaction.Conn, dm *domain.Discussion) (*domain.Discussion, error) {
var (
err error
m *models.Discussion
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 *DiscussionRepository) Delete(ctx context.Context, conn transaction.Conn, dm *domain.Discussion) (*domain.Discussion, error) {
var (
tx = conn.DB()
m = &models.Discussion{Id: dm.Identify().(int64)}
)
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 *DiscussionRepository) FindOne(ctx context.Context, conn transaction.Conn, id int64) (*domain.Discussion, error) {
var (
err error
tx = conn.DB()
m = new(models.Discussion)
)
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.Discussion)
cacheModel.Id = id
if err = repository.QueryCache(cacheModel.CacheKeyFunc, m, queryFunc); err != nil {
return nil, err
}
return repository.ModelToDomainModel(m)
}
func (repository *DiscussionRepository) Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*domain.Discussion, error) {
var (
tx = conn.DB()
ms []*models.Discussion
dms = make([]*domain.Discussion, 0)
total int64
)
queryFunc := func() (interface{}, error) {
tx = tx.Model(&ms).Order("id desc")
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 *DiscussionRepository) ModelToDomainModel(from *models.Discussion) (*domain.Discussion, error) {
to := &domain.Discussion{}
err := copier.Copy(to, from)
return to, err
}
func (repository *DiscussionRepository) DomainModelToModel(from *domain.Discussion) (*models.Discussion, error) {
to := &models.Discussion{}
err := copier.Copy(to, from)
return to, err
}
func NewDiscussionRepository(cache *cache.CachedRepository) domain.DiscussionRepository {
return &DiscussionRepository{CachedRepository: cache}
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
type Discussion struct {
Id int64 // 唯一标识
Title string `json:"title"` // 标题(不超过30个字符)
Content string `json:"content"` // 内容(不超过500个字符)
Images []Image `json:"images"` // 图片(不超过5张)
Widget Widget `json:"widget"` // 小组件
ProviderId int64 `json:"providerId"` // 发起者ID
ParticipantIds []int64 `json:"participantIds"` // 参与者ID
ArticleId int64 `json:"articleId"` // 文章ID
State int `json:"state"` // 状态(1进行中、2搁置、3中止、4结案)
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
}
type DiscussionRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *Discussion) (*Discussion, error)
Update(ctx context.Context, conn transaction.Conn, dm *Discussion) (*Discussion, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *Discussion) (*Discussion, error)
Delete(ctx context.Context, conn transaction.Conn, dm *Discussion) (*Discussion, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*Discussion, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*Discussion, error)
}
func (m *Discussion) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
type DiscussionAccept struct {
Id int64 // 唯一标识
ArticleId int64 `json:"articleId"` // 文章ID
DiscussionId int64 `json:"discussionId"` // 讨论ID
ProviderId int64 `json:"providerId"` // 发起人ID
DoIds []int64 `json:"DoIds"` // 意见采纳-用户观点
OpinionId int64 `json:"opinionId"` // 价值判定-选择观点
SubjectiveTitle string `json:"subjectiveTitle"` // 主观认定-标题
SubjectiveDesc string `json:"subjectiveDesc"` // 主观认定-内容
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
}
type DiscussionAcceptRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *DiscussionAccept) (*DiscussionAccept, error)
Update(ctx context.Context, conn transaction.Conn, dm *DiscussionAccept) (*DiscussionAccept, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *DiscussionAccept) (*DiscussionAccept, error)
Delete(ctx context.Context, conn transaction.Conn, dm *DiscussionAccept) (*DiscussionAccept, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*DiscussionAccept, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*DiscussionAccept, error)
}
func (m *DiscussionAccept) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
... ...
package domain
import (
"context"
"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/db/transaction"
)
type DiscussionOpinion struct {
Id int64 // 唯一标识
ArticleId int64 `json:"articleId"` // 文章ID
DiscussionId int64 `json:"discussionId"` // 讨论ID
ProviderId int64 `json:"providerId"` // 用户ID
OpinionId int64 `json:"opinionId"` // 选项ID
OpinionDesc string `json:"opinionDesc"` // 选项描述
Reason string `json:"reason"` // 理由
Show int `json:"show"` // 显示状态(1显示、0不显示)
AcceptUserId int64 `json:"acceptUserId"` // 采纳人ID(最终结案的时候唯一)
CreatedAt int64 `json:",omitempty"`
UpdatedAt int64 `json:",omitempty"`
DeletedAt int64 `json:",omitempty"`
Version int `json:",omitempty"`
}
type DiscussionOpinionRepository interface {
Insert(ctx context.Context, conn transaction.Conn, dm *DiscussionOpinion) (*DiscussionOpinion, error)
Update(ctx context.Context, conn transaction.Conn, dm *DiscussionOpinion) (*DiscussionOpinion, error)
UpdateWithVersion(ctx context.Context, conn transaction.Conn, dm *DiscussionOpinion) (*DiscussionOpinion, error)
Delete(ctx context.Context, conn transaction.Conn, dm *DiscussionOpinion) (*DiscussionOpinion, error)
FindOne(ctx context.Context, conn transaction.Conn, id int64) (*DiscussionOpinion, error)
Find(ctx context.Context, conn transaction.Conn, queryOptions map[string]interface{}) (int64, []*DiscussionOpinion, error)
}
func (m *DiscussionOpinion) Identify() interface{} {
if m.Id == 0 {
return nil
}
return m.Id
}
... ...
... ... @@ -14,6 +14,20 @@ type Location struct {
Descript string `json:"descript"` //地点描述
}
// Widget 小组件
type Widget struct {
Id int64 `json:"id"` // ID
Type int `json:"type"` // 类型(1投票、2PK)
Title string `json:"title"` // 标题(不超过15个字)
Opinions []Opinion `json:"opinions"` // 选项
}
// Opinion 观点选项
type Opinion struct {
Id int64 `json:"id"` // ID
Desc string `json:"desc"` // 描述(不超过10个字)
}
type UserSimple struct {
Id int64 // 人员id
Name string // 人员的名字
... ...