2024-10-14 19:27:46 +08:00
|
|
|
|
package curd
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"catface/app/model"
|
2024-11-14 04:26:12 +08:00
|
|
|
|
"catface/app/model_es"
|
2024-11-08 18:57:08 +08:00
|
|
|
|
"catface/app/utils/gorm_v2"
|
2024-10-14 19:27:46 +08:00
|
|
|
|
"catface/app/utils/model_handler"
|
|
|
|
|
"catface/app/utils/query_handler"
|
|
|
|
|
"fmt"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
2024-10-17 22:43:20 +08:00
|
|
|
|
func CreateAnimalsCurdFactory() *AnimalsCurd {
|
2024-10-14 19:27:46 +08:00
|
|
|
|
return &AnimalsCurd{model.CreateAnimalFactory("")}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type AnimalsCurd struct {
|
|
|
|
|
animals *model.Animal // INFO 难道数据就是存储到这里?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getSelectAttrs(attrs string) (validSelectedFields []string) {
|
|
|
|
|
if len(attrs) == 0 {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
// 1. 获取空 Field
|
|
|
|
|
fieldMap := model_handler.GetModelField(model.Animal{})
|
|
|
|
|
|
|
|
|
|
// 2. 开始检查请求字段
|
|
|
|
|
attrsArray := query_handler.StringToStringArray(attrs)
|
|
|
|
|
for _, attr := range attrsArray {
|
|
|
|
|
if attr == "*" { // 不需要过滤,直接返回
|
|
|
|
|
return nil
|
|
|
|
|
} else if attr == "avatar" {
|
|
|
|
|
fieldMap["avatar_height"] = true
|
|
|
|
|
fieldMap["avatar_width"] = true
|
|
|
|
|
}
|
|
|
|
|
// 过滤 无效 的请求字段
|
|
|
|
|
if _, ok := fieldMap[attr]; ok {
|
|
|
|
|
fieldMap[attr] = true
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. 装填字段
|
|
|
|
|
for key, value := range fieldMap {
|
|
|
|
|
if value {
|
|
|
|
|
validSelectedFields = append(validSelectedFields, key)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 21:28:41 +08:00
|
|
|
|
func (a *AnimalsCurd) List(attrs string, gender string, breed string, sterilization string, status string, department string, notInIds []int64, num int, skip int, userId int) (temp []model.AnimalWithLikeList) {
|
2024-10-14 19:27:46 +08:00
|
|
|
|
validSelectedFields := getSelectAttrs(attrs)
|
|
|
|
|
genderArray := query_handler.StringToUint8Array(gender)
|
|
|
|
|
breedArray := query_handler.StringToUint8Array(breed)
|
2024-10-25 05:03:27 +08:00
|
|
|
|
sterilizationArray := query_handler.StringToUint8Array(sterilization)
|
2024-10-14 19:27:46 +08:00
|
|
|
|
statusArray := query_handler.StringToUint8Array(status)
|
2024-11-06 18:50:39 +08:00
|
|
|
|
departmentArray := query_handler.StringToUint8Array(department)
|
2024-10-14 19:27:46 +08:00
|
|
|
|
|
|
|
|
|
if num == 0 {
|
|
|
|
|
num = 10
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 21:28:41 +08:00
|
|
|
|
animals := model.CreateAnimalFactory("").Show(validSelectedFields, genderArray, breedArray, sterilizationArray, statusArray, departmentArray, notInIds, num, skip)
|
2024-10-23 12:51:41 +08:00
|
|
|
|
|
2024-11-01 20:07:30 +08:00
|
|
|
|
// 状态记录
|
|
|
|
|
var likeRes []bool
|
|
|
|
|
var err error
|
|
|
|
|
|
2024-10-23 12:57:53 +08:00
|
|
|
|
if userId > 0 {
|
2024-11-01 20:07:30 +08:00
|
|
|
|
// Like:批量查询
|
|
|
|
|
var animalIds []int
|
|
|
|
|
for _, animal := range animals {
|
|
|
|
|
animalIds = append(animalIds, int(animal.Id))
|
|
|
|
|
}
|
|
|
|
|
likeRes, err = model.CreateAnimalLikeFactory("").LikedBatch(userId, animalIds)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err == nil && userId > 0 {
|
2024-10-23 12:57:53 +08:00
|
|
|
|
for i := range animals {
|
|
|
|
|
animalWithLike := model.AnimalWithLikeList{
|
|
|
|
|
Animal: animals[i],
|
2024-11-01 20:07:30 +08:00
|
|
|
|
Like: likeRes[i],
|
2024-10-23 12:57:53 +08:00
|
|
|
|
}
|
|
|
|
|
temp = append(temp, animalWithLike)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for i := range animals {
|
|
|
|
|
animalWithLike := model.AnimalWithLikeList{
|
|
|
|
|
Animal: animals[i],
|
|
|
|
|
}
|
|
|
|
|
temp = append(temp, animalWithLike)
|
2024-10-23 12:51:41 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-23 12:57:53 +08:00
|
|
|
|
|
2024-10-23 12:51:41 +08:00
|
|
|
|
return
|
2024-10-14 19:27:46 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-11-08 18:57:08 +08:00
|
|
|
|
func (a *AnimalsCurd) ShowByName(attrs string, name string) (temp []model.AnimalWithNickNameHit) {
|
|
|
|
|
validSelectedFields := getSelectAttrs(attrs)
|
|
|
|
|
|
|
|
|
|
animals := model.CreateAnimalFactory("").ShowByName(name, validSelectedFields...)
|
|
|
|
|
|
|
|
|
|
for _, animal := range animals {
|
|
|
|
|
animalWithNameHit := model.AnimalWithNickNameHit{
|
|
|
|
|
Animal: animal,
|
2024-11-08 22:20:26 +08:00
|
|
|
|
NickNameHit: !gorm_v2.IsLikePatternMatch(animal.Name, name), // 通过对比 name,然后取反;主要是不想让 SQL 过于复杂,、处理起来也麻烦。
|
2024-11-08 18:57:08 +08:00
|
|
|
|
}
|
|
|
|
|
temp = append(temp, animalWithNameHit)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-14 19:27:46 +08:00
|
|
|
|
func (a *AnimalsCurd) Detail(id string) *model.Animal {
|
|
|
|
|
idInt, err := strconv.Atoi(id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// TODO LOG
|
|
|
|
|
fmt.Println("Detail id error:", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-02 12:32:33 +08:00
|
|
|
|
return model.CreateAnimalFactory("mysql").ShowByID(int64(idInt))
|
2024-10-14 19:27:46 +08:00
|
|
|
|
}
|
2024-11-14 04:26:12 +08:00
|
|
|
|
|
|
|
|
|
func (a *AnimalsCurd) MatchAll(query string, num int) (tmp []model.Animal) {
|
|
|
|
|
// STAGE 1. ES 查询
|
|
|
|
|
animalsFromES, err := model_es.CreateAnimalESFactory(nil).QueryDocumentsMatchAll(query, num)
|
|
|
|
|
if err != nil {
|
|
|
|
|
fmt.Println("ES Query error:", err)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ids []int64
|
|
|
|
|
for _, animal := range animalsFromES {
|
|
|
|
|
ids = append(ids, animal.Id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// STAGE 2. MySQL 补充信息
|
|
|
|
|
animalsFromSQL := model.CreateAnimalFactory("").ShowByIDs(ids, "id", "avatar")
|
|
|
|
|
|
|
|
|
|
// 3. 合并信息
|
|
|
|
|
for _, animalFromES := range animalsFromES {
|
|
|
|
|
for _, animal := range animalsFromSQL {
|
|
|
|
|
if animal.Id == animalFromES.Id {
|
|
|
|
|
animal.NickNamesList = animalFromES.NickNames
|
|
|
|
|
animal.NickNamesHighlight = animalFromES.NickNamesHighlight
|
|
|
|
|
animal.Description = animalFromES.Description
|
|
|
|
|
animal.Name = animalFromES.Name
|
|
|
|
|
tmp = append(tmp, animal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|