feat: 徽章处理
This commit is contained in:
@@ -39,7 +39,10 @@ func (s *UserProfileService) UpdateProfile(req plantReq.UpdateProfile, userId st
|
||||
// ProfileDetail 获取用户详情
|
||||
func (s *UserProfileService) ProfileDetail(userId string) (plant.UserProfile, error) {
|
||||
var res plant.UserProfile
|
||||
err := global.DB.Where("user_id = ?", userId).Preload("Avatar").Preload("Level").First(&res).Error
|
||||
err := global.DB.Where("user_id = ?", userId).
|
||||
Preload("Avatar").
|
||||
Preload("Level").
|
||||
First(&res).Error
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
@@ -71,3 +74,14 @@ func (s *UserProfileService) MyStars(req plantReq.StarsPageReq, userId string) (
|
||||
err = db.Limit(limit).Offset(offset).Order("created_at desc").Find(&stars).Error
|
||||
return stars, total, err
|
||||
}
|
||||
|
||||
// MyBadges 我的徽章
|
||||
func (s *UserProfileService) MyBadges(userId string) ([]plant.UserBadge, error) {
|
||||
var badges []plant.UserBadge
|
||||
err := global.DB.Where("user_id = ?", userId).
|
||||
Preload("Badge", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Preload("Icon")
|
||||
}).
|
||||
Find(&badges).Error
|
||||
return badges, err
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package plant
|
||||
import (
|
||||
"errors"
|
||||
"sundynix-go/global"
|
||||
common "sundynix-go/model/commom/request"
|
||||
"sundynix-go/model/plant"
|
||||
plantReq "sundynix-go/model/plant/request"
|
||||
"sundynix-go/model/system"
|
||||
@@ -258,3 +259,56 @@ func (s *WikiService) StarWiki(userId, wikiId, class string) error {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UploadImg 上传图片
|
||||
func (s *WikiService) UploadImg(req common.UploadOss) error {
|
||||
|
||||
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
var wiki plant.Wiki
|
||||
err := tx.Where("id = ?", req.Id).First(&wiki).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var ossList []system.Oss
|
||||
err = tx.Where("id in ?", req.OssIds).Find(&ossList).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var relations []map[string]interface{}
|
||||
for _, oss := range ossList {
|
||||
relations = append(relations, map[string]interface{}{
|
||||
"wiki_id": wiki.Id,
|
||||
"oss_id": oss.Id,
|
||||
})
|
||||
}
|
||||
//3.添加关联关系(添加引用)
|
||||
return global.DB.Table("sundynix_wiki_oss").Create(relations).Error
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteWiki 删除百科
|
||||
func (s *WikiService) DeleteWiki(req common.IdsReq) error {
|
||||
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
var imgIds []string
|
||||
tx.Table("sundynix_wiki_oss").Where("wiki_id IN ?", req.Ids).Pluck("oss_id", &imgIds)
|
||||
// 3. 物理删除图片记录本身
|
||||
if len(imgIds) > 0 {
|
||||
if err := tx.Unscoped().Where("id IN ?", imgIds).Delete(&system.Oss{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// 2. 清理中间表记录 (解开多对多关系)
|
||||
// 使用 Exec 直接操作中间表比循环 Clear 快得多
|
||||
if err := tx.Exec("DELETE FROM sundynix_wiki_oss WHERE wiki_id IN ?", req.Ids).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM sundynix_wiki_class WHERE wiki_id IN ?", req.Ids).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Exec("DELETE FROM sundynix_wiki_related WHERE wiki_id IN ? or related_wiki_id in ?", req.Ids, req.Ids).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
//删除百科本身
|
||||
return tx.Unscoped().Where("id IN ?", req.Ids).Delete(&plant.Wiki{}).Error
|
||||
})
|
||||
}
|
||||
|
||||
@@ -162,11 +162,17 @@ func (userService *UserService) MiniLogin(code string) (result *system.User, err
|
||||
UserId: newUser.Id,
|
||||
MiniOpenId: wxResp.Openid,
|
||||
Nickname: newUser.Name,
|
||||
LevelId: "9bc0cd78-070f-11f1-8e90-1a43c43655d1",
|
||||
CurrentSunlight: 0,
|
||||
TotalSunlight: 0,
|
||||
PlantCount: 0,
|
||||
CareCount: 0,
|
||||
PostCount: 0,
|
||||
WaterCount: 0,
|
||||
FertilizeCount: 0,
|
||||
RepotCount: 0,
|
||||
PruneCount: 0,
|
||||
PhotoCount: 0,
|
||||
}
|
||||
if err := tx.Create(&profile).Error; err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user