Havoc412 75f2b9e59b refactor(animals): 优化动物名称模糊查询逻辑
- 在 AnimalsCurd 的 ShowByName 方法中,保留了原有的名称模糊查询逻辑
- 在 AnimalWithNickNameHit 结构中,添加了注释解释模糊查询的实现方式
- 此修改旨在提高代码的可读性和可维护性,避免使用复杂的 SQL 查询
2024-11-08 22:20:26 +08:00

125 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package curd
import (
"catface/app/model"
"catface/app/utils/gorm_v2"
"catface/app/utils/model_handler"
"catface/app/utils/query_handler"
"fmt"
"strconv"
)
func CreateAnimalsCurdFactory() *AnimalsCurd {
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
}
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) {
validSelectedFields := getSelectAttrs(attrs)
genderArray := query_handler.StringToUint8Array(gender)
breedArray := query_handler.StringToUint8Array(breed)
sterilizationArray := query_handler.StringToUint8Array(sterilization)
statusArray := query_handler.StringToUint8Array(status)
departmentArray := query_handler.StringToUint8Array(department)
if num == 0 {
num = 10
}
animals := model.CreateAnimalFactory("").Show(validSelectedFields, genderArray, breedArray, sterilizationArray, statusArray, departmentArray, notInIds, num, skip)
// 状态记录
var likeRes []bool
var err error
if userId > 0 {
// 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 {
for i := range animals {
animalWithLike := model.AnimalWithLikeList{
Animal: animals[i],
Like: likeRes[i],
}
temp = append(temp, animalWithLike)
}
} else {
for i := range animals {
animalWithLike := model.AnimalWithLikeList{
Animal: animals[i],
}
temp = append(temp, animalWithLike)
}
}
return
}
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,
NickNameHit: !gorm_v2.IsLikePatternMatch(animal.Name, name), // 通过对比 name然后取反主要是不想让 SQL 过于复杂,、处理起来也麻烦。
}
temp = append(temp, animalWithNameHit)
}
return
}
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
}
return model.CreateAnimalFactory("mysql").ShowByID(int64(idInt))
}