2f5dd6eefc
后端
- 主键改雪花字符串 ID(pkg/idgen + Base.BeforeCreate),全表外键/JWT/中间件随之调整
- 新增 files 表:/api/upload 按 MD5 去重,返回 {id,url,md5},服务端只收图片
- 头像/记录附图/帖子图改 file_id 关联,读取解析为 URL
- 养护模板(物种×阶段)后台可配 + AI 生成草稿;建档按模板生成任务/计划/提醒
- 社区 AI 运营:虚拟账号池 + 每日定时/手动生成,帖子带 AI 标
- 计划路线图节点带真实日期;首页周历与计划日历同源;新增 day-plan 当日安排
- 体重趋势接口带备注;记录列表分页
小程序
- 公共图片上传 utils/upload.js(仅图片);记录拍照/我的头像/社区发图三处接入
- 首页日历点选查当日任务;记录页体重趋势可点看备注 + 时间轴分页折叠
- 计划页日历点选查当日计划;自定义 tabBar 高度调整
后台(React)
- 新增「养护模板」「社区运营」页;用户管理加机器人筛选
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
134 lines
3.8 KiB
Go
134 lines
3.8 KiB
Go
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"gorm.io/datatypes"
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/sundynix/pets-be/internal/model"
|
|
)
|
|
|
|
// RecordInput 健康记录入参
|
|
type RecordInput struct {
|
|
Type string
|
|
Icon string
|
|
Title string
|
|
Description string
|
|
NumValue float64
|
|
Category string
|
|
ImageURL string
|
|
ImageFileID string
|
|
Extra datatypes.JSON
|
|
OccurredAt *time.Time
|
|
}
|
|
|
|
// ListRecords 列出宠物的健康记录(可按 type 过滤)
|
|
func (s *Service) ListRecords(userID, petID string, recordType string, offset, limit int) ([]model.HealthRecord, int64, error) {
|
|
if _, err := s.ownedPet(userID, petID); err != nil {
|
|
return nil, 0, err
|
|
}
|
|
q := s.db.Model(&model.HealthRecord{}).Where("pet_id = ?", petID)
|
|
if recordType != "" {
|
|
q = q.Where("type = ?", recordType)
|
|
}
|
|
var total int64
|
|
if err := q.Count(&total).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
var records []model.HealthRecord
|
|
err := q.Order("occurred_at desc, id desc").Offset(offset).Limit(limit).Find(&records).Error
|
|
// 用 file_id 回填图片 URL
|
|
ids := make([]string, 0)
|
|
for i := range records {
|
|
if records[i].ImageFileID != "" && records[i].ImageURL == "" {
|
|
ids = append(ids, records[i].ImageFileID)
|
|
}
|
|
}
|
|
if len(ids) > 0 {
|
|
urls := s.fileURLs(ids)
|
|
for i := range records {
|
|
if records[i].ImageURL == "" {
|
|
records[i].ImageURL = urls[records[i].ImageFileID]
|
|
}
|
|
}
|
|
}
|
|
return records, total, err
|
|
}
|
|
|
|
// CreateRecord 新增健康记录;weight 类型同步更新宠物体重
|
|
func (s *Service) CreateRecord(userID, petID string, in RecordInput) (*model.HealthRecord, error) {
|
|
pet, err := s.ownedPet(userID, petID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
occurred := time.Now()
|
|
if in.OccurredAt != nil {
|
|
occurred = *in.OccurredAt
|
|
}
|
|
rec := model.HealthRecord{
|
|
PetID: petID, UserID: userID, Type: in.Type, Icon: in.Icon,
|
|
Title: in.Title, Description: in.Description, NumValue: in.NumValue,
|
|
Category: in.Category, ImageURL: in.ImageURL, ImageFileID: in.ImageFileID, Extra: in.Extra, OccurredAt: occurred,
|
|
}
|
|
// 传了 file_id 未传 url 时,用 file 表的 url 回填 ImageURL 便于直接展示
|
|
if in.ImageFileID != "" && rec.ImageURL == "" {
|
|
rec.ImageURL = s.fileURL(in.ImageFileID)
|
|
}
|
|
if err := s.db.Transaction(func(tx *gorm.DB) error {
|
|
if err := tx.Create(&rec).Error; err != nil {
|
|
return err
|
|
}
|
|
if in.Type == model.RecordWeight && in.NumValue > 0 {
|
|
weight := fmt.Sprintf("%gkg", in.NumValue)
|
|
if err := tx.Model(&model.Pet{}).Where("id = ?", pet.ID).Update("weight", weight).Error; err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}); err != nil {
|
|
return nil, err
|
|
}
|
|
return &rec, nil
|
|
}
|
|
|
|
// DeleteRecord 删除记录
|
|
func (s *Service) DeleteRecord(userID, recordID string) error {
|
|
res := s.db.Where("id = ? AND user_id = ?", recordID, userID).Delete(&model.HealthRecord{})
|
|
if res.Error != nil {
|
|
return res.Error
|
|
}
|
|
if res.RowsAffected == 0 {
|
|
return ErrNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// WeightPoint 体重趋势点
|
|
type WeightPoint struct {
|
|
Value float64 `json:"value"`
|
|
OccurredAt time.Time `json:"occurred_at"`
|
|
Note string `json:"note"` // 记录时填的备注
|
|
}
|
|
|
|
// WeightTrend 最近 N 次体重(升序)
|
|
func (s *Service) WeightTrend(userID, petID string, limit int) ([]WeightPoint, error) {
|
|
if _, err := s.ownedPet(userID, petID); err != nil {
|
|
return nil, err
|
|
}
|
|
if limit <= 0 {
|
|
limit = 7
|
|
}
|
|
var records []model.HealthRecord
|
|
if err := s.db.Where("pet_id = ? AND type = ?", petID, model.RecordWeight).
|
|
Order("occurred_at desc").Limit(limit).Find(&records).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
points := make([]WeightPoint, 0, len(records))
|
|
for i := len(records) - 1; i >= 0; i-- {
|
|
points = append(points, WeightPoint{Value: records[i].NumValue, OccurredAt: records[i].OccurredAt, Note: records[i].Description})
|
|
}
|
|
return points, nil
|
|
}
|