feat: 个人中心发布
This commit is contained in:
+201
-62
@@ -1,6 +1,8 @@
|
||||
package plant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sundynix-go/global"
|
||||
common "sundynix-go/model/commom/request"
|
||||
"sundynix-go/model/plant"
|
||||
@@ -32,13 +34,14 @@ func (s *MyPlantService) AddPlant(req plantReq.CreateMyPlant, userId string) err
|
||||
var carePlans []*plant.CarePlan
|
||||
for _, v := range req.CarePlans {
|
||||
carePlans = append(carePlans, &plant.CarePlan{
|
||||
UserId: userId,
|
||||
Name: v.Name,
|
||||
Icon: v.Icon,
|
||||
Period: v.Period,
|
||||
UserId: userId,
|
||||
Name: v.Name,
|
||||
Icon: v.Icon,
|
||||
Period: v.Period,
|
||||
TargetAction: v.TargetAction,
|
||||
})
|
||||
}
|
||||
//3.保存数据
|
||||
//3.保存数据 myPlant钩子函数自动处理创建careTask
|
||||
myPlant := plant.MyPlant{
|
||||
UserId: userId,
|
||||
Name: req.Name,
|
||||
@@ -150,9 +153,10 @@ func (s *MyPlantService) UpdatePlant(req plantReq.UpdateMyPlant) error {
|
||||
today := timer.GetZeroTime()
|
||||
for _, plan := range req.CarePlans {
|
||||
err = tx.Model(&plant.CarePlan{}).Where("id = ?", plan.Id).Updates(map[string]interface{}{
|
||||
"icon": plan.Icon,
|
||||
"name": plan.Name,
|
||||
"period": plan.Period,
|
||||
"icon": plan.Icon,
|
||||
"name": plan.Name,
|
||||
"period": plan.Period,
|
||||
"target_action": plan.TargetAction,
|
||||
}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -234,7 +238,8 @@ func (s *MyPlantService) TodayTask(userId string) ([]plantRes.PlantTaskVO, error
|
||||
}
|
||||
|
||||
// CompleteTask 完成任务
|
||||
func (s *MyPlantService) CompleteTask(req plantReq.CompleteTask, userId string) error {
|
||||
func (s *MyPlantService) CompleteTask(req plantReq.CompleteTask, userId string) (*plantRes.TaskCompletionResult, error) {
|
||||
var result plantRes.TaskCompletionResult
|
||||
err := global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
var task plant.CareTask
|
||||
if err := tx.Where("id = ?", req.TaskId).First(&task).Error; err != nil {
|
||||
@@ -257,13 +262,14 @@ func (s *MyPlantService) CompleteTask(req plantReq.CompleteTask, userId string)
|
||||
today := timer.GetZeroTime()
|
||||
nextDueDate := today.AddDate(0, 0, plan.Period)
|
||||
newTask := plant.CareTask{
|
||||
UserId: plan.UserId,
|
||||
PlantId: plan.PlantId,
|
||||
PlanId: plan.Id,
|
||||
Name: plan.Name,
|
||||
Icon: plan.Icon,
|
||||
DueDate: nextDueDate,
|
||||
Status: 1,
|
||||
UserId: plan.UserId,
|
||||
PlantId: plan.PlantId,
|
||||
PlanId: plan.Id,
|
||||
Name: plan.Name,
|
||||
Icon: plan.Icon,
|
||||
TargetAction: plan.TargetAction,
|
||||
DueDate: nextDueDate,
|
||||
Status: 1,
|
||||
}
|
||||
if err := tx.Create(&newTask).Error; err != nil {
|
||||
return err
|
||||
@@ -280,51 +286,134 @@ func (s *MyPlantService) CompleteTask(req plantReq.CompleteTask, userId string)
|
||||
if err := tx.Create(&record).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
//4.等级与阳光计算
|
||||
var profile plant.UserProfile
|
||||
if err := tx.Set("gorm:query_option", "FOR UPDATE").Where("user_id = ?", userId).First(&profile).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
fieldMap := map[string]string{
|
||||
"ACT_WATER": "water_count",
|
||||
"ACT_FERTILIZE": "fertilize_count",
|
||||
"ACT_REPOT": "repot_count",
|
||||
"ACT_PRUNE": "prune_count",
|
||||
}
|
||||
column, ok := fieldMap[task.TargetAction]
|
||||
if !ok {
|
||||
column = "care_count" // 默认备用
|
||||
}
|
||||
const TaskReward = 50
|
||||
newTotalSunlight := profile.TotalSunlight + TaskReward
|
||||
newCurrentSunlight := profile.CurrentSunlight + TaskReward
|
||||
// 5. 等级判定 (根据累计阳光查出当前应有的等级)
|
||||
var latestLevel plant.LevelConfig
|
||||
if err := tx.Where("min_sunlight <= ?", newTotalSunlight).
|
||||
Order("min_sunlight DESC").First(&latestLevel).Error; err != nil {
|
||||
return errors.New("等级配置异常")
|
||||
}
|
||||
result.CurrentLevel = &latestLevel
|
||||
result.IsLevelUp = latestLevel.Id != profile.LevelId
|
||||
// 6. 执行 Profile 更新 (任务阳光 + 计数自增 + 等级同步)
|
||||
profileData := map[string]interface{}{
|
||||
column: gorm.Expr(column + " + 1"),
|
||||
"current_sunlight": newCurrentSunlight,
|
||||
"total_sunlight": newTotalSunlight,
|
||||
"level_id": latestLevel.Id,
|
||||
}
|
||||
if err := tx.Model(&plant.UserProfile{}).Where("user_id = ?", userId).Updates(profileData).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 7. 徽章判定逻辑
|
||||
// 7.1 计算当前动作的最新逻辑数值
|
||||
var currentActionVal int64
|
||||
switch task.TargetAction {
|
||||
case "ACT_WATER":
|
||||
currentActionVal = profile.WaterCount + 1
|
||||
case "ACT_FERTILIZE":
|
||||
currentActionVal = profile.FertilizeCount + 1
|
||||
case "ACT_REPOT":
|
||||
currentActionVal = profile.RepotCount + 1
|
||||
case "ACT_PRUNE":
|
||||
currentActionVal = profile.PruneCount + 1
|
||||
default:
|
||||
currentActionVal = 0
|
||||
}
|
||||
// 7.2 查询已拥有的徽章 ID (用于去重)
|
||||
var ownedBadgeIds []string
|
||||
tx.Model(&plant.UserBadge{}).Where("user_id = ?", userId).Pluck("badge_id", &ownedBadgeIds)
|
||||
ownedMap := make(map[string]bool)
|
||||
for _, id := range ownedBadgeIds {
|
||||
ownedMap[id] = true
|
||||
}
|
||||
// 7.3 筛选当前 Action 下满足条件的最高级徽章
|
||||
var badgeConfigs []plant.BadgeConfig
|
||||
tx.Where("target_action = ?", task.TargetAction).Preload("Icon").Find(&badgeConfigs)
|
||||
|
||||
for i := range badgeConfigs {
|
||||
conf := &badgeConfigs[i]
|
||||
// 如果没拿过且数值达标
|
||||
if !ownedMap[conf.Id] && currentActionVal >= conf.Threshold {
|
||||
// 寻找 Tier 最大的那个
|
||||
if result.NewBadge == nil || conf.Tier > result.NewBadge.Tier {
|
||||
result.NewBadge = conf
|
||||
result.IsGetBadge = true
|
||||
}
|
||||
}
|
||||
}
|
||||
// 8. 奖励入库:如果产生了新徽章
|
||||
if result.NewBadge != nil {
|
||||
// 写入获得记录
|
||||
ub := plant.UserBadge{
|
||||
UserId: userId,
|
||||
BadgeId: result.NewBadge.Id, // 注意这里 Id 是 string
|
||||
}
|
||||
if err := tx.Create(&ub).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 发放徽章额外奖励阳光
|
||||
if err := tx.Model(&plant.UserProfile{}).Where("user_id = ?", userId).
|
||||
Update("current_sunlight", gorm.Expr("current_sunlight + ?", result.NewBadge.RewardSunlight)).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err == nil {
|
||||
go func() {
|
||||
//5.更新用户profile
|
||||
var profile plant.UserProfile
|
||||
if asyncErr := global.DB.Where("user_id = ?", userId).First(&profile).Error; err != nil {
|
||||
global.Logger.Error("完成任务异步操作-----查询用户profile失败", zap.Error(asyncErr))
|
||||
}
|
||||
totalSunlight := profile.TotalSunlight + 50
|
||||
updateData := map[string]interface{}{
|
||||
"care_count": profile.CareCount + 1,
|
||||
"current_sunlight": profile.CurrentSunlight + 50,
|
||||
"total_sunlight": totalSunlight,
|
||||
}
|
||||
//5.1 判断是否到达下个等级
|
||||
var currentLevel plant.LevelConfig
|
||||
levelErr := global.DB.Where("min_sunlight <= ?", totalSunlight).Order("min_sunlight DESC").First(¤tLevel).Error
|
||||
if levelErr != nil {
|
||||
global.Logger.Error("完成任务异步操作-----查询用户等级失败", zap.Error(levelErr))
|
||||
}
|
||||
updateData["level_id"] = currentLevel.Id
|
||||
//5.2 更新用户profile
|
||||
updateProfileErr := global.DB.Model(&plant.UserProfile{}).Where("user_id = ?", userId).Updates(updateData).Error
|
||||
if updateProfileErr != nil {
|
||||
global.Logger.Error("完成任务异步操作-----更新用户profile失败", zap.Error(updateProfileErr))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return err
|
||||
return &result, err
|
||||
}
|
||||
|
||||
// DeletePlants 删除植物
|
||||
func (s *MyPlantService) DeletePlants(req common.IdsReq) error {
|
||||
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
var plants []plant.MyPlant
|
||||
if err := tx.Where("id in ?", req.Ids).Find(&plants).Error; err != nil {
|
||||
var imgIds []string
|
||||
tx.Table("sundynix_my_plant_oss").Where("my_plant_id IN ?", req.Ids).Pluck("oss_id", &imgIds)
|
||||
// 2. 清理中间表记录 (解开多对多关系)
|
||||
// 使用 Exec 直接操作中间表比循环 Clear 快得多
|
||||
if err := tx.Exec("DELETE FROM sundynix_my_plant_oss WHERE my_plant_id IN ?", req.Ids).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除图片 养护计划 养护任务 养护记录 成长记录
|
||||
err := tx.Select("ImgList", "CarePlans", "CareTasks", "CareRecords").Unscoped().Delete(&plants).Error
|
||||
if err != nil {
|
||||
// 3. 物理删除图片记录本身
|
||||
if len(imgIds) > 0 {
|
||||
if err := tx.Unscoped().Where("id IN ?", imgIds).Delete(&system.Oss{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
//4.批量删除养护计划 养护任务 养护记录 成长记录
|
||||
if err := tx.Unscoped().Where("plant_id in ?", req.Ids).Delete(&plant.CarePlan{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Unscoped().Where("plant_id in ?", req.Ids).Delete(&plant.CareTask{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Unscoped().Where("plant_id in ?", req.Ids).Delete(&plant.CareRecord{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Unscoped().Where("plant_id in ?", req.Ids).Delete(&plant.GrowthRecord{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
//5.删除植物本身
|
||||
if err := tx.Unscoped().Where("id in ?", req.Ids).Delete(&plant.MyPlant{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -365,11 +454,12 @@ func (s *MyPlantService) AddCarePlan(req plantReq.AddPlans) error {
|
||||
}
|
||||
//1.新增计划
|
||||
newPlan := plant.CarePlan{
|
||||
UserId: myPlant.UserId,
|
||||
PlantId: myPlant.Id,
|
||||
Name: plan.Name,
|
||||
Icon: plan.Icon,
|
||||
Period: plan.Period,
|
||||
UserId: myPlant.UserId,
|
||||
PlantId: myPlant.Id,
|
||||
Name: plan.Name,
|
||||
Icon: plan.Icon,
|
||||
Period: plan.Period,
|
||||
TargetAction: plan.TargetAction,
|
||||
}
|
||||
err = tx.Create(&newPlan).Error
|
||||
if err != nil {
|
||||
@@ -379,13 +469,14 @@ func (s *MyPlantService) AddCarePlan(req plantReq.AddPlans) error {
|
||||
today := timer.GetZeroTime()
|
||||
dueDate := today.AddDate(0, 0, plan.Period)
|
||||
task := plant.CareTask{
|
||||
UserId: myPlant.UserId,
|
||||
PlantId: myPlant.Id,
|
||||
PlanId: newPlan.Id,
|
||||
Name: plan.Name,
|
||||
Icon: plan.Icon,
|
||||
DueDate: dueDate,
|
||||
Status: 1,
|
||||
UserId: myPlant.UserId,
|
||||
PlantId: myPlant.Id,
|
||||
PlanId: newPlan.Id,
|
||||
Name: plan.Name,
|
||||
Icon: plan.Icon,
|
||||
TargetAction: plan.TargetAction,
|
||||
DueDate: dueDate,
|
||||
Status: 1,
|
||||
}
|
||||
err = tx.Create(&task).Error
|
||||
if err != nil {
|
||||
@@ -459,3 +550,51 @@ func (s *MyPlantService) AddGrowthRecord(req plantReq.CreateGrowthRecord, userId
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// 完成任务后异步执行 更新用户等级 sunlight
|
||||
func (s *MyPlantService) handleCompleteTaskThenUpdateProfile(ctx context.Context, userId string) {
|
||||
//5.更新用户profile
|
||||
var profile plant.UserProfile
|
||||
if asyncErr := global.DB.Where("user_id = ?", userId).First(&profile).Error; asyncErr != nil {
|
||||
global.Logger.Error("完成任务异步操作-----查询用户profile失败", zap.Error(asyncErr))
|
||||
}
|
||||
totalSunlight := profile.TotalSunlight + 50
|
||||
updateData := map[string]interface{}{
|
||||
"care_count": profile.CareCount + 1,
|
||||
"current_sunlight": profile.CurrentSunlight + 50,
|
||||
"total_sunlight": totalSunlight,
|
||||
}
|
||||
//5.1 判断是否到达下个等级
|
||||
var currentLevel plant.LevelConfig
|
||||
levelErr := global.DB.Where("min_sunlight <= ?", totalSunlight).Order("min_sunlight DESC").First(¤tLevel).Error
|
||||
if levelErr != nil {
|
||||
global.Logger.Error("完成任务异步操作-----查询用户等级失败", zap.Error(levelErr))
|
||||
}
|
||||
updateData["level_id"] = currentLevel.Id
|
||||
//5.2 更新用户profile
|
||||
updateProfileErr := global.DB.Model(&plant.UserProfile{}).Where("user_id = ?", userId).Updates(updateData).Error
|
||||
if updateProfileErr != nil {
|
||||
global.Logger.Error("完成任务异步操作-----更新用户profile失败", zap.Error(updateProfileErr))
|
||||
}
|
||||
}
|
||||
|
||||
// todo 完成任务后异步执行 处理徽章
|
||||
func (s *MyPlantService) handleCompleteTaskThenHandleBadge(ctx context.Context, taskId, userId string) {
|
||||
//1.查询任务
|
||||
var task plant.CareTask
|
||||
if asyncErr := global.DB.Where("id = ?", taskId).First(&task).Error; asyncErr != nil {
|
||||
global.Logger.Error("完成任务异步操作-----查询任务失败", zap.Error(asyncErr))
|
||||
}
|
||||
//2.查询徽章
|
||||
var badges []plant.BadgeConfig
|
||||
if asyncErr := global.DB.Where("target_action = ?", task.TargetAction).Order("tier ASC").Find(&badges).Error; asyncErr != nil {
|
||||
global.Logger.Error("完成任务异步操作-----查询徽章失败", zap.Error(asyncErr))
|
||||
}
|
||||
//3.用户的徽章
|
||||
var userBadges []plant.UserBadge
|
||||
if asyncErr := global.DB.Where("user_id = ?", userId).Find(&userBadges).Error; asyncErr != nil {
|
||||
global.Logger.Error("完成任务异步操作-----查询用户徽章失败", zap.Error(asyncErr))
|
||||
}
|
||||
//4.用户个人资料
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user