init: init refactor
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
package myPlant
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
"gorm.io/gorm"
|
||||
"sundynix-micro-go/app/plant/api/internal/svc"
|
||||
"sundynix-micro-go/app/plant/api/internal/types"
|
||||
plantModel "sundynix-micro-go/app/plant/model"
|
||||
)
|
||||
|
||||
type GetPlantDetailLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetPlantDetailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetPlantDetailLogic {
|
||||
return &GetPlantDetailLogic{Logger: logx.WithContext(ctx), ctx: ctx, svcCtx: svcCtx}
|
||||
}
|
||||
|
||||
func (l *GetPlantDetailLogic) GetPlantDetail(req *types.IdPathReq) (resp interface{}, err error) {
|
||||
var plant plantModel.SundynixMyPlant
|
||||
if err := l.svcCtx.DB.Where("id = ?", req.Id).First(&plant).Error; err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return nil, fmt.Errorf("植物不存在")
|
||||
}
|
||||
return nil, fmt.Errorf("查询植物失败")
|
||||
}
|
||||
// 查询关联图片ID
|
||||
var ossIds []string
|
||||
l.svcCtx.DB.Model(&plantModel.SundynixMyPlantOss{}).Where("sundynix_my_plant_id = ?", plant.ID).Pluck("sundynix_oss_id", &ossIds)
|
||||
// 查询养护计划
|
||||
var plans []plantModel.SundynixCarePlan
|
||||
l.svcCtx.DB.Where("plant_id = ?", plant.ID).Find(&plans)
|
||||
// 查询成长记录
|
||||
var records []plantModel.SundynixGrowthRecord
|
||||
l.svcCtx.DB.Where("plant_id = ?", plant.ID).Order("created_at DESC").Limit(10).Find(&records)
|
||||
|
||||
return map[string]interface{}{
|
||||
"plant": plant, "imgIds": ossIds, "carePlans": plans, "growthRecords": records,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user