feat: 删除植物和删除养护事项
This commit is contained in:
+74
-10
@@ -111,16 +111,39 @@ func (s *MyPlantService) PlantDetail(id string) (p plant.MyPlant, err error) {
|
||||
}
|
||||
|
||||
func (s *MyPlantService) UpdatePlant(req plantReq.UpdateMyPlant) error {
|
||||
// 以map形式更新 先构建map
|
||||
updateMap := map[string]interface{}{
|
||||
"name": req.Name,
|
||||
"placement": req.Placement,
|
||||
"planting_material": req.PlantingMaterial,
|
||||
"pot_material": req.PotMaterial,
|
||||
"pot_size": req.PotSize,
|
||||
"sunlight": req.Sunlight,
|
||||
}
|
||||
return global.DB.Model(&plant.MyPlant{}).Where("id = ?", req.Id).Updates(updateMap).Error
|
||||
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
// 以map形式更新 先构建map
|
||||
updateMap := map[string]interface{}{
|
||||
"name": req.Name,
|
||||
"placement": req.Placement,
|
||||
"planting_material": req.PlantingMaterial,
|
||||
"pot_material": req.PotMaterial,
|
||||
"pot_size": req.PotSize,
|
||||
"sunlight": req.Sunlight,
|
||||
}
|
||||
//1.修改基本信息
|
||||
err := tx.Model(&plant.MyPlant{}).Where("id = ?", req.Id).Updates(updateMap).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//2.修改计划
|
||||
if len(req.CarePlans) > 0 {
|
||||
//删除已经存在的计划 生成新的计划
|
||||
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,
|
||||
}).Error
|
||||
//3.重新生成任务 使用钩子函数自动执行
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
// TodayTask 今日任务
|
||||
@@ -222,3 +245,44 @@ func (s *MyPlantService) CompleteTask(req plantReq.CompleteTask) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// 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 {
|
||||
return err
|
||||
}
|
||||
// 删除图片 养护计划 养护任务 养护记录 成长记录
|
||||
err := tx.Select("ImgList", "CarePlans", "CareTasks", "CareRecords").Unscoped().Delete(&plants).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// DeletePlans 删除任务
|
||||
func (s *MyPlantService) DeletePlans(req common.IdsReq) error {
|
||||
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
var plans []plant.CarePlan
|
||||
if err := tx.Where("id in ?", req.Ids).Find(&plans).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
var tasks []plant.CareTask
|
||||
if err := tx.Where("plan_id in ?", req.Ids).Find(&tasks).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
//1.删除计划
|
||||
err := tx.Unscoped().Delete(&plans).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//2.删除任务
|
||||
err = tx.Unscoped().Delete(&tasks).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user