From ffde5d691f172389e6f1b752a9863163aeabacf2 Mon Sep 17 00:00:00 2001 From: Havoc412 <2993167370@qq.com> Date: Sat, 2 Nov 2024 12:32:33 +0800 Subject: [PATCH] fit use Animal Avatar as encounter Avatar --- .../controller/web/encounter_controller.go | 3 +- app/model/animal.go | 3 +- app/model/encounter.go | 14 ++ app/model/encounter_animal_link.go | 30 ++++- app/model/encounter_result.go | 8 +- app/service/animals/curd/animals_curd.go | 3 +- app/service/encounter/curd/encounter_curd.go | 10 +- test/models_test.go | 122 +++++++++--------- 8 files changed, 116 insertions(+), 77 deletions(-) diff --git a/app/http/controller/web/encounter_controller.go b/app/http/controller/web/encounter_controller.go index 4ea94ef..40a0aea 100644 --- a/app/http/controller/web/encounter_controller.go +++ b/app/http/controller/web/encounter_controller.go @@ -48,8 +48,7 @@ func (e *Encounters) Create(context *gin.Context) { animals_id := data_transfer.GetFloat64Slice(context, "animals_id") // 由于是 Slice 就交给 EAlink 内部遍历时处理。 // Real Insert - 2: EA LINK if encounter_id, ok := model.CreateEncounterFactory("").InsertDate(context); ok && encounter_id > 0 { - if !model.CreateEncounterAnimalLinkFactory("").Insert(int(encounter_id), animals_id) { - // TODO 异常处理。 + if !model.CreateEncounterAnimalLinkFactory("").Insert(int64(encounter_id), animals_id) { response.Fail(context, errcode.ErrEaLinkInstert, errcode.ErrMsg[errcode.ErrEaLinkInstert], "") return } diff --git a/app/model/animal.go b/app/model/animal.go index f99879a..df2411a 100644 --- a/app/model/animal.go +++ b/app/model/animal.go @@ -7,6 +7,7 @@ import ( "github.com/gin-gonic/gin" "go.uber.org/zap" + ) func CreateAnimalFactory(sqlType string) *Animal { @@ -69,7 +70,7 @@ func (a *Animal) Show(attrs []string, gender []uint8, breed []uint8, sterilizati return } -func (a *Animal) ShowByID(id int) *Animal { +func (a *Animal) ShowByID(id int64) *Animal { var temp Animal err := a.DB.Table(a.TableName()).Model(&temp).Where("id = ?", id).Scan(&temp).Error if err != nil { diff --git a/app/model/encounter.go b/app/model/encounter.go index a06cb2e..31adbf3 100644 --- a/app/model/encounter.go +++ b/app/model/encounter.go @@ -9,6 +9,7 @@ import ( "github.com/gin-gonic/gin" "go.uber.org/zap" "gorm.io/gorm" + ) func CreateEncounterFactory(sqlType string) *Encounter { @@ -77,7 +78,20 @@ func formatEncounterList(rows *gorm.DB) (temp []EncounterList, err error) { log.Println("扫描失败:", err) continue } + // STAGE: Set link Status item.Like = ueLikeInt == 1 + // STAGE: Check Url exist + if item.Avatar == "" { + if animal_id, ok := CreateEncounterAnimalLinkFactory("").ShowByEncounterIdFirst(int64(item.Id)); ok { + if animal := CreateAnimalFactory("").ShowByID(animal_id); animal != nil { + item.Avatar = animal.Avatar + item.AvatarHeight = int(animal.AvatarHeight) + item.AvatarWidth = int(animal.AvatarWidth) + item.UseAnimalAvatar = true + } + } + } + // append temp = append(temp, item) } diff --git a/app/model/encounter_animal_link.go b/app/model/encounter_animal_link.go index 17ad86a..bc88a32 100644 --- a/app/model/encounter_animal_link.go +++ b/app/model/encounter_animal_link.go @@ -12,19 +12,41 @@ func CreateEncounterAnimalLinkFactory(sqlType string) *EncounterAnimalLink { */ type EncounterAnimalLink struct { *gorm.DB `gorm:"-" json:"-"` - EncounterId int `gorm:"column:encounter_id;index" json:"encounter_id"` + EncounterId int64 `gorm:"column:encounter_id;index" json:"encounter_id"` Encounter Encounter - AnimalId int `gorm:"column:animal_id;index" json:"animal_id"` + AnimalId int64 `gorm:"column:animal_id;index" json:"animal_id"` Animal Animal } -func (e *EncounterAnimalLink) Insert(encounterId int, animalId []float64) bool { +func (e *EncounterAnimalLink) Insert(encounterId int64, animalId []float64) bool { // Build Slice var results []EncounterAnimalLink for _, id := range animalId { - results = append(results, EncounterAnimalLink{EncounterId: encounterId, AnimalId: int(id)}) + results = append(results, EncounterAnimalLink{EncounterId: encounterId, AnimalId: int64(id)}) } // 调用批处理插入方法 return e.Create(&results).Error == nil } + +func (e *EncounterAnimalLink) ShowByEncounterId(encounterId int64) ([]int64, bool) { + var results []EncounterAnimalLink + if err := e.Where("encounter_id = ?", encounterId).Find(&results).Error; err != nil { + // 处理错误情况,例如日志记录或返回错误信息 + return nil, false + } + intSlice := make([]int64, len(results)) + for i, result := range results { + intSlice[i] = result.AnimalId // 假设 AnimalId 是你需要提取的字段 + } + return intSlice, true +} + +func (e *EncounterAnimalLink) ShowByEncounterIdFirst(encounterId int64) (int64, bool) { + var results EncounterAnimalLink + if err := e.Where("encounter_id = ?", encounterId).First(&results).Error; err != nil { + // 处理错误情况,例如日志记录或返回错误信息 + return 0, false + } + return results.AnimalId, true +} diff --git a/app/model/encounter_result.go b/app/model/encounter_result.go index 7fde547..f789105 100644 --- a/app/model/encounter_result.go +++ b/app/model/encounter_result.go @@ -11,10 +11,12 @@ type EncounterList struct { AvatarHeight int `form:"avatar_height" json:"height"` AvatarWidth int `form:"avatar_width" json:"width"` UpdatedAt *time.Time `form:"updated_at" json:"time"` // TIP 设为 *time.Time,omitempty 和 autoUpdated 就都可以生效 - Like bool `form:"ue_like" json:"like"` - UserName string `form:"user_name" json:"userName"` - UserAvatar string `form:"user_avatar" json:"userAvatar"` + Like bool `form:"ue_like" json:"like"` + UseAnimalAvatar bool `form:"use_animal_avatar" json:"use_animal_avatar"` + + UserName string `form:"user_name" json:"user_name"` + UserAvatar string `form:"user_avatar" json:"user_avatar"` } type EncounterDetail struct { diff --git a/app/service/animals/curd/animals_curd.go b/app/service/animals/curd/animals_curd.go index 7b99920..fb24a6f 100644 --- a/app/service/animals/curd/animals_curd.go +++ b/app/service/animals/curd/animals_curd.go @@ -6,6 +6,7 @@ import ( "catface/app/utils/query_handler" "fmt" "strconv" + ) func CreateAnimalsCurdFactory() *AnimalsCurd { @@ -102,5 +103,5 @@ func (a *AnimalsCurd) Detail(id string) *model.Animal { return nil } - return model.CreateAnimalFactory("mysql").ShowByID(idInt) + return model.CreateAnimalFactory("mysql").ShowByID(int64(idInt)) } diff --git a/app/service/encounter/curd/encounter_curd.go b/app/service/encounter/curd/encounter_curd.go index 79d6fad..a59eff9 100644 --- a/app/service/encounter/curd/encounter_curd.go +++ b/app/service/encounter/curd/encounter_curd.go @@ -4,6 +4,7 @@ import ( "catface/app/model" "catface/app/utils/query_handler" "strconv" + ) func CreateEncounterCurdFactory() *EncounterCurd { @@ -52,14 +53,15 @@ func (e *EncounterCurd) Detail(id string) *model.EncounterDetail { } // 3. animals data - // animals_id := query_handler.StringToint64Array(encounter.AnimalsId) - // animals := model.CreateAnimalFactory("").ShowByIDs(animals_id, "avatar", "name", "id") - // _ = animals + var animals []model.Animal + if animals_id, ok := model.CreateEncounterAnimalLinkFactory("").ShowByEncounterId(encounter.Id); ok { + animals = model.CreateAnimalFactory("").ShowByIDs(animals_id, "avatar", "name", "id") + } // 4. 合并 return &model.EncounterDetail{ Encounter: *encounter, UsersModel: *user, - // Animals: animals, + Animals: animals, } } diff --git a/test/models_test.go b/test/models_test.go index 1183235..97f7b0c 100644 --- a/test/models_test.go +++ b/test/models_test.go @@ -4,10 +4,8 @@ package test import ( "catface/app/model" "fmt" - "reflect" - "strconv" - "strings" "testing" + ) func TestUsers(t *testing.T) { @@ -117,67 +115,67 @@ func TestEaLink(t *testing.T) { } } -// 测试函数 -func TestInsertEncounterAnimalLinks(t *testing.T) { - Init() +// // 测试函数 +// func TestInsertEncounterAnimalLinks(t *testing.T) { +// Init() - // 定义要插入的数据 - data := []struct { - EncounterId int - AnimalIds string - }{ - {10, "4"}, - {11, "2,3"}, - {13, "4"}, - {14, "4"}, - {15, "4"}, - {16, "4"}, - {17, "4"}, - {18, "4"}, - {19, "4"}, - {20, "4"}, - } +// // 定义要插入的数据 +// data := []struct { +// EncounterId int +// AnimalIds string +// }{ +// {10, "4"}, +// {11, "2,3"}, +// {13, "4"}, +// {14, "4"}, +// {15, "4"}, +// {16, "4"}, +// {17, "4"}, +// {18, "4"}, +// {19, "4"}, +// {20, "4"}, +// } - // 插入数据 - for _, item := range data { - animalIds := strings.Split(item.AnimalIds, ",") - for _, animalIdStr := range animalIds { - animalId, err := strconv.Atoi(animalIdStr) - if err != nil { - t.Errorf("Failed to convert animal Id: %v", err) - continue - } - link := model.EncounterAnimalLink{ - EncounterId: item.EncounterId, - AnimalId: animalId, - } - if err := DB.Create(&link).Error; err != nil { - t.Errorf("Failed to insert link: %v", err) - } - } - } +// // 插入数据 +// for _, item := range data { +// animalIds := strings.Split(item.AnimalIds, ",") +// for _, animalIdStr := range animalIds { +// animalId, err := strconv.Atoi(animalIdStr) +// if err != nil { +// t.Errorf("Failed to convert animal Id: %v", err) +// continue +// } +// link := model.EncounterAnimalLink{ +// EncounterId: item.EncounterId, +// AnimalId: animalId, +// } +// if err := DB.Create(&link).Error; err != nil { +// t.Errorf("Failed to insert link: %v", err) +// } +// } +// } - // 验证数据是否正确插入 - var links []model.EncounterAnimalLink - if err := DB.Find(&links).Error; err != nil { - t.Errorf("Failed to fetch links: %v", err) - } +// // 验证数据是否正确插入 +// var links []model.EncounterAnimalLink +// if err := DB.Find(&links).Error; err != nil { +// t.Errorf("Failed to fetch links: %v", err) +// } - expectedLinks := []model.EncounterAnimalLink{ - {EncounterId: 10, AnimalId: 4}, - {EncounterId: 11, AnimalId: 2}, - {EncounterId: 11, AnimalId: 3}, - {EncounterId: 13, AnimalId: 4}, - {EncounterId: 14, AnimalId: 4}, - {EncounterId: 15, AnimalId: 4}, - {EncounterId: 16, AnimalId: 4}, - {EncounterId: 17, AnimalId: 4}, - {EncounterId: 18, AnimalId: 4}, - {EncounterId: 19, AnimalId: 4}, - {EncounterId: 20, AnimalId: 4}, - } +// expectedLinks := []model.EncounterAnimalLink{ +// {EncounterId: 10, AnimalId: 4}, +// {EncounterId: 11, AnimalId: 2}, +// {EncounterId: 11, AnimalId: 3}, +// {EncounterId: 13, AnimalId: 4}, +// {EncounterId: 14, AnimalId: 4}, +// {EncounterId: 15, AnimalId: 4}, +// {EncounterId: 16, AnimalId: 4}, +// {EncounterId: 17, AnimalId: 4}, +// {EncounterId: 18, AnimalId: 4}, +// {EncounterId: 19, AnimalId: 4}, +// {EncounterId: 20, AnimalId: 4}, +// } - if !reflect.DeepEqual(links, expectedLinks) { - t.Errorf("Expected links: %v, but got: %v", expectedLinks, links) - } -} +// if !reflect.DeepEqual(links, expectedLinks) { +// t.Errorf("Expected links: %v, but got: %v", expectedLinks, links) +// } +// }