2024-10-14 13:49:16 +08:00
|
|
|
|
package model
|
|
|
|
|
|
2024-10-14 19:27:46 +08:00
|
|
|
|
import (
|
|
|
|
|
"catface/app/global/variable"
|
2024-10-30 17:21:09 +08:00
|
|
|
|
"catface/app/utils/data_bind"
|
2024-10-14 19:27:46 +08:00
|
|
|
|
"catface/app/utils/gorm_v2"
|
|
|
|
|
|
2024-10-30 17:21:09 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2024-10-14 19:27:46 +08:00
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func CreateAnimalFactory(sqlType string) *Animal {
|
|
|
|
|
return &Animal{BaseModel: BaseModel{DB: UseDbConn(sqlType)}}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 13:49:16 +08:00
|
|
|
|
type Animal struct {
|
2024-11-06 18:32:50 +08:00
|
|
|
|
// UPDATE 或者这里都应该采取外键连接?
|
2024-10-16 13:51:58 +08:00
|
|
|
|
BaseModel // 假设 BaseModel 中不需要添加 omitempty 标签
|
|
|
|
|
Name string `gorm:"type:varchar(20)" json:"name,omitempty"` // 名称
|
2024-10-30 17:21:09 +08:00
|
|
|
|
Birthday string `gorm:"size:10" json:"birthday,omitempty"` // 生日;就简单存string就好
|
2024-11-09 04:53:28 +08:00
|
|
|
|
Gender uint8 `gorm:"default:1" json:"gender,omitempty"` // 性别
|
|
|
|
|
Breed uint8 `gorm:"default:1" json:"breed,omitempty"` // 品种
|
|
|
|
|
Sterilization uint8 `gorm:"default:1" json:"sterilization,omitempty"` // 1 不明 2 未绝育 3 已绝育
|
|
|
|
|
Vaccination uint8 `gorm:"default:1" json:"vaccination,omitempty"` // 免疫状态
|
|
|
|
|
Deworming uint8 `gorm:"default:1" json:"deworming,omitempty"` // 驱虫状态
|
2024-10-16 14:12:23 +08:00
|
|
|
|
NickNames string `gorm:"type:varchar(31)" json:"nick_names,omitempty"` // 别称,辅助查询;存储上采取 , 间隔符的方式; VARCHAR 会比较合适
|
2024-11-09 04:53:28 +08:00
|
|
|
|
Status uint8 `gorm:"default:1" json:"status,omitempty"` // 状态
|
2024-10-16 13:51:58 +08:00
|
|
|
|
Description string `gorm:"column:description;type:varchar(255)" json:"description,omitempty"` // 简明介绍
|
|
|
|
|
Tags string `json:"tags,omitempty"`
|
|
|
|
|
// TAG imaegs
|
2024-10-30 17:21:09 +08:00
|
|
|
|
Avatar string `gorm:"type:varchar(50)" json:"avatar,omitempty"` // 缩略图 url,为 Go 获取 Photo 之后压缩处理后的图像,单独存储。
|
2024-10-16 13:51:58 +08:00
|
|
|
|
AvatarHeight uint16 `json:"avatar_height,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。 // INFO 暂时没用到
|
|
|
|
|
AvatarWidth uint16 `json:"avatar_width,omitempty"` // 为了方便前端在加载图像前的骨架图 & 瀑布流展示。
|
2024-10-30 17:21:09 +08:00
|
|
|
|
HeadImg string `gorm:"type:varchar(50)" json:"head_img,omitempty"` // Head 默认处理为正方形。
|
2024-10-24 22:08:43 +08:00
|
|
|
|
Photos string `gorm:"type:varchar(255)" json:"photos,omitempty"` // 图片数组
|
2024-10-16 13:51:58 +08:00
|
|
|
|
// TAG POI
|
2024-11-09 04:53:28 +08:00
|
|
|
|
Department uint8 `gorm:"column:department;default:1" json:"department,omitempty"`
|
2024-10-16 13:51:58 +08:00
|
|
|
|
Latitude float64 `json:"latitude,omitempty"` // POI 位置相关
|
|
|
|
|
Longitude float64 `json:"longitude,omitempty"` // POI 位置相关
|
|
|
|
|
ActivityRadius uint64 `json:"activity_radius,omitempty"` // 活动半径
|
|
|
|
|
// CatFace
|
2024-10-24 14:47:52 +08:00
|
|
|
|
FaceModelScore float64 `json:"face_model_score,omitempty" gorm:"defalut:0"` // 评估面部模型得分
|
|
|
|
|
FaceBreeds string `json:"face_breeds,omitempty" gorm:"size:20"`
|
|
|
|
|
FaceBreedProbs string `json:"face_breed_probs,omitempty" gorm:"size:20"`
|
2024-10-24 15:29:09 +08:00
|
|
|
|
// 上传者 ID
|
2024-11-06 18:50:39 +08:00
|
|
|
|
UsersModelId int64 `gorm:"column:user_id" json:"user_id,omitempty"` // 上传者 ID
|
2024-10-24 20:14:24 +08:00
|
|
|
|
UsersModel *UsersModel `json:"users_model,omitempty"`
|
2024-10-14 13:49:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 19:27:46 +08:00
|
|
|
|
func (a *Animal) TableName() string {
|
|
|
|
|
return "animals"
|
2024-10-14 13:49:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 21:28:41 +08:00
|
|
|
|
func (a *Animal) Show(attrs []string, gender []uint8, breed []uint8, sterilization []uint8, status []uint8, department []uint8, notInIds []int64, num int, skip int) (temp []Animal) {
|
2024-10-14 19:27:46 +08:00
|
|
|
|
db := a.DB.Table(a.TableName()).Limit(int(num)).Offset(int(skip)).Select(attrs)
|
2024-10-14 13:49:16 +08:00
|
|
|
|
|
2024-10-14 19:27:46 +08:00
|
|
|
|
// 创建条件映射
|
|
|
|
|
conditions := map[string][]uint8{
|
|
|
|
|
"gender": gender,
|
|
|
|
|
"breed": breed,
|
2024-10-25 05:03:27 +08:00
|
|
|
|
"sterilization": sterilization,
|
2024-10-14 19:27:46 +08:00
|
|
|
|
"status": status,
|
2024-11-06 18:50:39 +08:00
|
|
|
|
"department": department,
|
2024-10-14 19:27:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-06 18:50:39 +08:00
|
|
|
|
db = gorm_v2.BuildWhere(db, conditions) // TIP 这里的 Where 条件连接就很方便了。
|
2024-10-14 13:49:16 +08:00
|
|
|
|
|
2024-11-07 21:28:41 +08:00
|
|
|
|
if len(notInIds) > 0 {
|
|
|
|
|
db = db.Where("id not in (?)", notInIds)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 19:27:46 +08:00
|
|
|
|
err := db.Find(&temp).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
variable.ZapLog.Error("Animal Show Error", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
return
|
2024-10-14 13:49:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 12:32:33 +08:00
|
|
|
|
func (a *Animal) ShowByID(id int64) *Animal {
|
2024-10-14 19:27:46 +08:00
|
|
|
|
var temp Animal
|
|
|
|
|
err := a.DB.Table(a.TableName()).Model(&temp).Where("id = ?", id).Scan(&temp).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
variable.ZapLog.Error("Animal ShowByID Error", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
return &temp
|
2024-10-14 13:49:16 +08:00
|
|
|
|
}
|
2024-10-21 16:16:03 +08:00
|
|
|
|
|
|
|
|
|
func (a *Animal) ShowByIDs(ids []int64, attrs ...string) (temp []Animal) {
|
|
|
|
|
db := a.DB.Table(a.TableName())
|
|
|
|
|
|
|
|
|
|
if len(attrs) > 0 {
|
|
|
|
|
db = db.Select(attrs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := db.Where("id in (?)", ids).Find(&temp).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
variable.ZapLog.Error("Animal ShowByIDs Error", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-10-30 17:21:09 +08:00
|
|
|
|
|
2024-11-08 18:57:08 +08:00
|
|
|
|
/**
|
|
|
|
|
* @description: 采取前后通配符的方式,同时查询 name & nick_names 字段
|
|
|
|
|
* @param {string} name
|
|
|
|
|
* @param {...string} attrs
|
|
|
|
|
* @return {*}
|
|
|
|
|
*/
|
|
|
|
|
func (a *Animal) ShowByName(name string, attrs ...string) (temp []Animal) {
|
|
|
|
|
db := a.DB.Table(a.TableName())
|
|
|
|
|
|
|
|
|
|
if len(attrs) > 0 {
|
|
|
|
|
db = db.Select(attrs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := db.Where("name LIKE ? OR nick_names LIKE ?", "%"+name+"%", "%"+name+"%").Find(&temp).Error
|
|
|
|
|
if err != nil {
|
|
|
|
|
variable.ZapLog.Error("Animal ShowByName Error", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-30 17:21:09 +08:00
|
|
|
|
func (a *Animal) InsertDate(c *gin.Context) (int64, bool) {
|
|
|
|
|
var tmp Animal
|
|
|
|
|
if err := data_bind.ShouldBindFormDataToModel(c, &tmp); err == nil {
|
|
|
|
|
if res := a.Create(&tmp); res.Error == nil {
|
|
|
|
|
// 获取插入的 ID
|
|
|
|
|
insertedID := tmp.Id
|
|
|
|
|
return insertedID, true
|
|
|
|
|
} else {
|
|
|
|
|
variable.ZapLog.Error("Animal 数据新增出错", zap.Error(res.Error))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
variable.ZapLog.Error("Animal 数据绑定出错", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
return 0, false
|
|
|
|
|
}
|