init: initial commit
This commit is contained in:
@@ -0,0 +1,224 @@
|
||||
package plant
|
||||
|
||||
import (
|
||||
"sundynix-go/global"
|
||||
common "sundynix-go/model/commom/request"
|
||||
"sundynix-go/model/plant"
|
||||
plantReq "sundynix-go/model/plant/request"
|
||||
plantRes "sundynix-go/model/plant/response"
|
||||
"sundynix-go/model/system"
|
||||
"sundynix-go/utils/timer"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type MyPlantService struct{}
|
||||
|
||||
var MyPlantServiceApp = new(MyPlantService)
|
||||
|
||||
// AddPlant 添加植物
|
||||
func (s *MyPlantService) AddPlant(req plantReq.CreateMyPlant, id string) error {
|
||||
err := global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
//1. 验证oss是否存在
|
||||
var ossList []*system.Oss
|
||||
err := tx.Where("id in ?", req.OssIds).Find(&ossList).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bgTime := timer.GetZeroTime()
|
||||
//2. 处理养护计划
|
||||
var carePlans []*plant.CarePlan
|
||||
for _, v := range req.CarePlans {
|
||||
carePlans = append(carePlans, &plant.CarePlan{
|
||||
UserId: id,
|
||||
Name: v.Name,
|
||||
Icon: v.Icon,
|
||||
Period: v.Period,
|
||||
})
|
||||
}
|
||||
//3.保存数据
|
||||
myPlant := plant.MyPlant{
|
||||
UserId: id,
|
||||
Name: req.Name,
|
||||
PlantTime: bgTime,
|
||||
Status: 1,
|
||||
Placement: req.Placement,
|
||||
PotMaterial: req.PotMaterial,
|
||||
PotSize: req.PotSize,
|
||||
Sunlight: req.Sunlight,
|
||||
PlantingMaterial: req.PlantingMaterial,
|
||||
CarePlans: carePlans,
|
||||
}
|
||||
err = tx.Create(&myPlant).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//4.处理图片关系
|
||||
if len(ossList) > 0 {
|
||||
var relations []map[string]interface{}
|
||||
for _, oss := range ossList {
|
||||
relations = append(relations, map[string]interface{}{
|
||||
"my_plant_id": myPlant.Id,
|
||||
"oss_id": oss.Id,
|
||||
})
|
||||
}
|
||||
err = tx.Table("sundynix_my_plant_oss").Create(relations).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return err
|
||||
|
||||
}
|
||||
|
||||
// PlantPage 植物列表
|
||||
func (s *MyPlantService) PlantPage(req common.PageInfo, userId string) (list interface{}, total int64, err error) {
|
||||
limit := req.PageSize
|
||||
offset := req.PageSize * (req.Current - 1)
|
||||
db := global.DB.Model(&plant.MyPlant{}).Preload("ImgList", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc")
|
||||
})
|
||||
var myPlants []*plant.MyPlant
|
||||
db = db.Where("user_id = ?", userId)
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
err = db.Limit(limit).Offset(offset).Order("created_at desc").Find(&myPlants).Error
|
||||
return myPlants, total, err
|
||||
}
|
||||
|
||||
// PlantDetail 植物详情
|
||||
func (s *MyPlantService) PlantDetail(id string) (p plant.MyPlant, err error) {
|
||||
var res plant.MyPlant
|
||||
err = global.DB.Where("id = ?", id).
|
||||
Preload("ImgList", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc")
|
||||
}).
|
||||
Preload("CarePlans").
|
||||
Preload("CareRecords").
|
||||
First(&res).Error
|
||||
|
||||
//不存在的时候不要返回错误,而是返回nil
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// TodayTask 今日任务
|
||||
func (s *MyPlantService) TodayTask(userId string) ([]plantRes.PlantTaskVO, error) {
|
||||
today := timer.GetZeroTime()
|
||||
endOfToday := today.Add(24 * time.Hour)
|
||||
|
||||
var tasks []*plant.CareTask
|
||||
// 查询条件:1.未完成的任务(包含今天和以前逾期的) 2.今天已经完成的任务
|
||||
err := global.DB.Where("user_id = ? AND ("+
|
||||
"(status = 1 AND due_date < ?) OR "+
|
||||
"(status = 2 AND completed_at >= ? AND completed_at < ?)"+
|
||||
")", userId, endOfToday, today, endOfToday).
|
||||
Order("due_date ASC").
|
||||
Find(&tasks).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 2.内存处理:聚合数据
|
||||
plantTaskMap := make(map[string][]*plant.CareTask)
|
||||
expiredMap := make(map[string]bool)
|
||||
var plantIds []string
|
||||
for _, t := range tasks {
|
||||
plantIds = append(plantIds, t.PlantId)
|
||||
plantTaskMap[t.PlantId] = append(plantTaskMap[t.PlantId], t)
|
||||
// 判定右上角红标:状态为待办 且 应做时间早于今天
|
||||
if t.Status == 1 && t.DueDate.Before(today) {
|
||||
expiredMap[t.PlantId] = true
|
||||
}
|
||||
}
|
||||
//3.批量查询植物
|
||||
var myPlants []*plant.MyPlant
|
||||
err = global.DB.Where("id in ?", plantIds).Preload("ImgList", func(db *gorm.DB) *gorm.DB {
|
||||
return db.Order("created_at desc")
|
||||
}).Find(&myPlants).Error
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
//4.组装结果
|
||||
var res []plantRes.PlantTaskVO
|
||||
for _, p := range myPlants {
|
||||
plantTaskVO := plantRes.PlantTaskVO{
|
||||
MyPlant: p,
|
||||
HasExpired: expiredMap[p.Id],
|
||||
Tasks: plantTaskMap[p.Id],
|
||||
}
|
||||
res = append(res, plantTaskVO)
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// CompleteTask 完成任务
|
||||
func (s *MyPlantService) CompleteTask(req plantReq.CompleteTask) error {
|
||||
return global.DB.Transaction(func(tx *gorm.DB) error {
|
||||
var task plant.CareTask
|
||||
if err := tx.Where("id = ?", req.TaskId).First(&task).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
//1.更新当前任务为完成
|
||||
updateData := map[string]interface{}{
|
||||
"status": 2,
|
||||
"completed_at": time.Now(),
|
||||
}
|
||||
if err := tx.Model(&task).Where("id = ?", req.TaskId).Updates(updateData).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
//2.获取计划模版
|
||||
var plan plant.CarePlan
|
||||
if err := tx.Where("id = ?", task.PlanId).First(&plan).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
//3.生成下个周期的任务
|
||||
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,
|
||||
}
|
||||
if err := tx.Create(&newTask).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
//4.保存养护记录
|
||||
record := plant.CareRecord{
|
||||
UserId: plan.UserId,
|
||||
PlantId: plan.PlantId,
|
||||
PlanId: plan.Id,
|
||||
Name: plan.Name,
|
||||
Icon: plan.Icon,
|
||||
Remark: req.Remark,
|
||||
}
|
||||
if err := tx.Create(&record).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user