This commit introduces several architectural improvements and optimizations across the codebase: - **Performance & Reliability**: - Implemented Redis pipelining in `ConversationCache.CacheMessage` to reduce network round-trips. - Added a circuit breaker to the JPush client to prevent cascading failures. - Introduced batch deletion and batch member addition capabilities in repositories. - Added message idempotency support using `client_msg_id` and a Redis-based cache. - Optimized WebSocket handling with connection limits (total and per-user) and improved error logging. - **Code Refactoring**: - Refactored `Router` to use a `RouterDeps` struct, simplifying the constructor and improving maintainability. - Unified model ID generation logic using new `id_helper.go` (supporting UUID and Snowflake). - Standardized JSON serialization/deserialization in models using `json_helper.go`. - Refactored DTO conversion logic, specifically for `UserResponse` (using functional options) and `Report` responses. - Removed redundant/deprecated DTOs like `PostDetailResponse` and `TradeItemDetailResponse`. - **Cache Improvements**: - Enhanced `LayeredCache` with `SetRaw` to avoid double-encoding when promoting values from Redis to local cache. - Added `DeleteBatch` support to the cache interface. - **Other Changes**: - Cleaned up `config.go` by removing redundant default values and explicit environment variable overrides. - Improved WebSocket registration flow to handle connection limits gracefully.
104 lines
3.7 KiB
Go
104 lines
3.7 KiB
Go
package model
|
|
|
|
import (
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type TradeType string
|
|
|
|
const (
|
|
TradeTypeSell TradeType = "sell"
|
|
TradeTypeBuy TradeType = "buy"
|
|
)
|
|
|
|
type TradeCategory string
|
|
|
|
const (
|
|
TradeCategoryDigital TradeCategory = "digital"
|
|
TradeCategoryBook TradeCategory = "book"
|
|
TradeCategoryClothing TradeCategory = "clothing"
|
|
TradeCategoryDaily TradeCategory = "daily"
|
|
TradeCategoryCosmetics TradeCategory = "cosmetics"
|
|
TradeCategorySport TradeCategory = "sport"
|
|
TradeCategoryTicket TradeCategory = "ticket"
|
|
TradeCategoryOther TradeCategory = "other"
|
|
)
|
|
|
|
type TradeItemStatus string
|
|
|
|
const (
|
|
TradeItemActive TradeItemStatus = "active"
|
|
TradeItemSold TradeItemStatus = "sold"
|
|
TradeItemBought TradeItemStatus = "bought"
|
|
TradeItemOffline TradeItemStatus = "offline"
|
|
)
|
|
|
|
type TradeItem struct {
|
|
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
|
UserID string `json:"user_id" gorm:"type:varchar(36);index;not null"`
|
|
TradeType TradeType `json:"trade_type" gorm:"type:varchar(10);not null;index"`
|
|
Category TradeCategory `json:"category" gorm:"type:varchar(20);not null;index"`
|
|
Title string `json:"title" gorm:"type:varchar(100);not null"`
|
|
Content string `json:"content" gorm:"type:text"`
|
|
Segments MessageSegments `json:"segments,omitempty" gorm:"type:text"`
|
|
User *User `json:"user,omitempty" gorm:"foreignKey:UserID"`
|
|
Images []TradeImage `json:"images" gorm:"foreignKey:TradeItemID"`
|
|
Price *float64 `json:"price,omitempty" gorm:"type:decimal(10,2)"`
|
|
Condition string `json:"condition,omitempty" gorm:"type:varchar(20)"`
|
|
Status TradeItemStatus `json:"status" gorm:"type:varchar(10);not null;default:'active';index"`
|
|
ViewsCount int `json:"views_count" gorm:"default:0"`
|
|
FavoritesCount int `json:"favorites_count" gorm:"default:0"`
|
|
DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime;index"`
|
|
UpdatedAt time.Time `json:"updated_at" gorm:"autoUpdateTime:false"`
|
|
}
|
|
|
|
func (t *TradeItem) BeforeCreate(tx *gorm.DB) error {
|
|
SetUUIDIfEmpty(&t.ID)
|
|
return nil
|
|
}
|
|
|
|
func (TradeItem) TableName() string {
|
|
return "trade_items"
|
|
}
|
|
|
|
type TradeImage struct {
|
|
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
|
TradeItemID string `json:"trade_item_id" gorm:"type:varchar(36);not null;index"`
|
|
URL string `json:"url" gorm:"type:text;not null"`
|
|
ThumbnailURL string `json:"thumbnail_url" gorm:"type:text"`
|
|
PreviewURL string `json:"preview_url" gorm:"type:text"`
|
|
PreviewURLLarge string `json:"preview_url_large" gorm:"type:text"`
|
|
Width int `json:"width" gorm:"default:0"`
|
|
Height int `json:"height" gorm:"default:0"`
|
|
SortOrder int `json:"sort_order" gorm:"default:0"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
|
}
|
|
|
|
func (ti *TradeImage) BeforeCreate(tx *gorm.DB) error {
|
|
SetUUIDIfEmpty(&ti.ID)
|
|
return nil
|
|
}
|
|
|
|
func (TradeImage) TableName() string {
|
|
return "trade_images"
|
|
}
|
|
|
|
type TradeFavorite struct {
|
|
ID string `json:"id" gorm:"type:varchar(36);primaryKey"`
|
|
TradeItemID string `json:"trade_item_id" gorm:"type:varchar(36);not null;index;uniqueIndex:idx_trade_fav_item_user,priority:1"`
|
|
UserID string `json:"user_id" gorm:"type:varchar(36);not null;index;uniqueIndex:idx_trade_fav_item_user,priority:2"`
|
|
CreatedAt time.Time `json:"created_at" gorm:"autoCreateTime"`
|
|
}
|
|
|
|
func (f *TradeFavorite) BeforeCreate(tx *gorm.DB) error {
|
|
SetUUIDIfEmpty(&f.ID)
|
|
return nil
|
|
}
|
|
|
|
func (TradeFavorite) TableName() string {
|
|
return "trade_favorites"
|
|
}
|