2024-10-14 19:27:46 +08:00
|
|
|
|
package model
|
|
|
|
|
|
2024-10-18 10:04:01 +08:00
|
|
|
|
import (
|
|
|
|
|
"catface/app/global/variable"
|
|
|
|
|
"catface/app/utils/data_bind"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-17 22:43:20 +08:00
|
|
|
|
func CreateEncounterFactory(sqlType string) *Encounter {
|
|
|
|
|
return &Encounter{BaseModel: BaseModel{DB: UseDbConn(sqlType)}}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 19:48:43 +08:00
|
|
|
|
type Encounter struct { // Encounter 或者称为 post,指的就是 Human 单次的记录。
|
2024-10-14 19:27:46 +08:00
|
|
|
|
BaseModel
|
2024-10-17 19:48:43 +08:00
|
|
|
|
// TAG 外键关联
|
2024-10-20 17:33:28 +08:00
|
|
|
|
UsersModelId int `gorm:"column:user_id" json:"user_id"`
|
|
|
|
|
UsersModel UsersModel
|
|
|
|
|
AnimalsId string `gorm:"size:20" json:"animals_id"` // TODO 关联对象存在上限
|
2024-10-17 19:48:43 +08:00
|
|
|
|
|
2024-10-20 17:33:28 +08:00
|
|
|
|
Title string `gorm:"size:20;column:title" json:"title"`
|
|
|
|
|
Content string `json:"content"`
|
2024-10-17 19:48:43 +08:00
|
|
|
|
// Time 从 CreatedAt 中解析
|
|
|
|
|
|
|
|
|
|
// TAG Avatar 最好是压缩后的备份图像
|
|
|
|
|
Avatar string `gorm:"type:varchar(1s0)" json:"avatar,omitempty"` // 缩略图 url,为 Go 获取 Photo 之后压缩处理后的图像,单独存储。
|
|
|
|
|
AvatarHeight uint16 `json:"avatar_height,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。
|
|
|
|
|
AvatarWidth uint16 `json:"avatar_width,omitempty"`
|
|
|
|
|
Photos string `gorm:"type:varchar(100)" json:"photos,omitempty"` // 图片数组
|
|
|
|
|
// POI
|
|
|
|
|
Latitude float64 `json:"latitude,omitempty"` // POI 位置相关
|
|
|
|
|
Longitude float64 `json:"longitude,omitempty"`
|
|
|
|
|
// TODO Comment Num 然后去统计?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *Encounter) TableName() string {
|
|
|
|
|
return "encounters"
|
2024-10-14 19:27:46 +08:00
|
|
|
|
}
|
2024-10-18 10:04:01 +08:00
|
|
|
|
|
|
|
|
|
func (e *Encounter) InsertDate(c *gin.Context) bool {
|
|
|
|
|
var tmp Encounter
|
|
|
|
|
if err := data_bind.ShouldBindFormDataToModel(c, &tmp); err == nil {
|
|
|
|
|
if res := e.Create(&tmp); res.Error == nil {
|
|
|
|
|
return true
|
|
|
|
|
} else {
|
|
|
|
|
variable.ZapLog.Error("Encounter 数据新增出错", zap.Error(res.Error))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
variable.ZapLog.Error("Encounter 数据绑定出错", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-10-20 21:01:12 +08:00
|
|
|
|
|
|
|
|
|
func (e *Encounter) Show(num, skip int) (temp []EncounterList) {
|
2024-10-21 00:24:48 +08:00
|
|
|
|
sql := `SELECT e.id, user_id, title, avatar, avatar_height, avatar_width, e.updated_at,
|
2024-10-20 21:01:12 +08:00
|
|
|
|
user_name, user_avatar FROM encounters e JOIN tb_users u ON e.user_id = u.id
|
|
|
|
|
LIMIT ? OFFSET ?
|
|
|
|
|
`
|
|
|
|
|
_ = e.Raw(sql, num, skip).Scan(&temp)
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|