3ea50efb7d
## 弹层里塞 24 个表单是走不通的 弹层高度受 .sheet-scroll 的 78vh 限制,字段一多就变成内滚;24 项的选择器 挤在弹层里更难看(用户原话「很丑」)。整体搬成页面。 ## pages/addrecord —— 一个页面吃掉全部 24 种 形态:记录宠物 / 记录时间 / 类型专属字段 / 描述 + 照片 / 底部固定保存键。 关键在「类型专属字段」整段是后端驱动的:前端不认识「体重」「金额」这些业务词, 只认识 number / options / text 三种渲染方式,以及每个字段的值该落到哪儿。 record_types 加了 fields 列,后台用紧凑写法配(一行一个): number:体重:kg 数值 → num_value options:状态:正常|软便|拉稀 单选 → category options:症状:呕吐|拉稀|精神差:- 末尾 - 表示不落 category,只进标题 text:吃了什么 文本 → 只进标题 (空) 只要 时间+描述+照片 解析放服务端不放前端:配错了要在后台保存时就报出来(第几行、错在哪), 不能等用户点开表单才发现渲染不出东西。读取时解析失败只让这一种事项没有额外 字段并打 warn,不让一条烂配置把整个记录页打空。 ## 那个 - 开关不是过度设计 异常观察有两个单选(症状 + 严重程度),而 category 只有一个坑, 周报的高风险数按 category='高' 统计(report.go:46)——必须能指定谁占这个坑。 同理账单按 cost 的 category 分组聚合(report.go:134), 食便相关性排除 poop 的 category='正常'。这三处口径都得严丝合缝对上。 ## 记录页瘦成纯列表 只留分组宫格。健康洞察 / 体重趋势 / 健康时间轴整页搬到 pages/history, 从报告页「记录与趋势」进——报告页才是看数据的地方,而一个「我要记一笔」的 页面上摆三块只读图表,用户每次都得先滚过去才能找到要点的东西。 **没有直接删掉那三块**:时间轴是唯一能看和删历史记录的地方,报告页只有 周报/账单这些聚合。删了用户就再也看不到自己记过什么。搬页面用的是 git 里 改动前的完整版复制,比往 report.js 里合并代码安全。 ## 弹层瘦了一圈 9 个记录分支删掉,连带 17 个方法、SIMPLE_HINT、buildRecord 的 7 个 case 和一批只有它们在用的 data 字段。 js 1089 → 857 行 wxml 480 → 335 行 弹层现在只管「不是记一笔」的事:提醒、海报、档案、发帖、评论、导出、反馈。 buildRecord 只剩 vetSummary 一个分支(它写一条 note 留痕,不是用户填的表单)。 ## 验证(预生产库实跑) 24 种的字段配置逐个核对解析结果,然后照 addrecord 的 buildBody 组装落库: 体重 title='体重:4.6kg' num=4.6 → 宠物档案回写成 4.6kg ✓ 记账 title='记账:128元 医疗' num=128 cat=医疗 → 账单 total=128 分类[(医疗,128)] ✓ 异常 title='异常:呕吐 高' cat=高 → 周报 high_risk_count=1 ✓ 排便 cat=软便 食便相关性口径保住 饮食 title='饮食:幼猫粮 45g 偏少' cat=偏少 喝水 num=180(新类型带数值,老代码没有它的分支也不影响) 洗澡 title='洗澡'(无额外字段) 看病 num=320(两个字段:文本+数值) 体重趋势 1 个点、值 4.6,没被其他 7 条污染。 回填踩过一次坑:fields 列是服务启动时 AutoMigrate 才建的,我在启动前就跑了 回填,Update 报错被忽略、13 行静默没写进去。第二版加了 HasColumn 前置检查 和逐行错误统计才发现。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
117 lines
5.1 KiB
Go
117 lines
5.1 KiB
Go
package database
|
||
|
||
import (
|
||
"golang.org/x/crypto/bcrypt"
|
||
"gorm.io/gorm"
|
||
|
||
"github.com/sundynix/pets-be/internal/config"
|
||
"github.com/sundynix/pets-be/internal/model"
|
||
)
|
||
|
||
// Seed 初始化种子数据:管理员账号 + 记录类型。
|
||
//
|
||
// 文章、养护模板这类**内容**数据不放代码里——正文几百字塞进 Go 源文件,既看不清
|
||
// 也没法在后台改完再同步回来。上线时直接同步数据库。
|
||
//
|
||
// 记录类型是另一码事,它是**结构**:表空着的话记录页一个可记事项都没有,
|
||
// App 直接不能用。所以和管理员账号一样,只在表完全为空时铺一次底,
|
||
// 之后一切以数据库为准,代码里这份不再参与。
|
||
func Seed(db *gorm.DB, cfg *config.Config) error {
|
||
if err := seedAdmin(db, cfg); err != nil {
|
||
return err
|
||
}
|
||
return seedRecordTypes(db)
|
||
}
|
||
|
||
// seedRecordTypes 只在表为空时跑。已经有数据(哪怕只有一条)就完全不碰,
|
||
// 否则后台删掉的类型会在每次重启后自己长回来
|
||
func seedRecordTypes(db *gorm.DB) error {
|
||
var count int64
|
||
if err := db.Model(&model.RecordType{}).Count(&count).Error; err != nil {
|
||
return err
|
||
}
|
||
if count > 0 {
|
||
return nil
|
||
}
|
||
|
||
// locked=true 的这 9 种在 record.go / insight.go / report.go / home.go 里有真实
|
||
// 代码分支(体重回写档案、食便相关性、消费统计、疫苗完成度…),并且被
|
||
// care_template_items.sheet_type 引用,后台不许删,只能停用。
|
||
// form=full 表示 bottom-sheet 里有它专用的 wx:elif 分支。
|
||
// fields 是表单额外字段的配置,一行一个(写法见 model.RecordType.Fields 的注释)。
|
||
// 空 = 只要「时间 + 描述 + 照片」。
|
||
//
|
||
// 原有 9 种的配置必须和现在的统计口径严丝合缝:
|
||
// weight number → num_value,CreateRecord 靠它回写宠物档案
|
||
// cost 金额→num_value + 类别→category,账单是按 category 分组聚合的
|
||
// poop 状态→category,食便相关性分析排除 category='正常'
|
||
// symptom 两个单选:症状不占 category,严重程度占——周报按 category='高' 数高风险
|
||
mk := func(code, label, icon, group string, sort int, locked bool, fields string) model.RecordType {
|
||
form := model.RecordFormSimple
|
||
if locked {
|
||
form = model.RecordFormFull
|
||
}
|
||
return model.RecordType{Code: code, Label: label, Icon: icon, GroupKey: group,
|
||
Sort: sort, Enabled: true, Form: form, Locked: locked, Fields: fields}
|
||
}
|
||
full := func(code, label, icon, group string, sort int, fields string) model.RecordType {
|
||
return mk(code, label, icon, group, sort, true, fields)
|
||
}
|
||
simple := func(code, label, icon, group string, sort int, fields string) model.RecordType {
|
||
return mk(code, label, icon, group, sort, false, fields)
|
||
}
|
||
|
||
d, h, c, cl := model.RecordGroupDaily, model.RecordGroupHealth, model.RecordGroupCare, model.RecordGroupClean
|
||
types := []model.RecordType{
|
||
// 日常
|
||
full("weight", "体重", "weight", d, 10, "number:体重:kg"),
|
||
full("poop", "排便", "poop", d, 20, "options:状态:正常|软便|拉稀"),
|
||
full("food", "饮食", "food", d, 30, "text:吃了什么\noptions:食欲:正常|偏少|不吃"),
|
||
simple("water", "喝水", "water", d, 40, "number:饮水量:ml"),
|
||
full("cost", "记账", "cost", d, 50, "number:金额:元\noptions:类别:医疗|食品|用品|美容|其他"),
|
||
full("photo", "照片", "photo", d, 60, ""),
|
||
// 健康
|
||
full("symptom", "异常", "symptom", h, 10,
|
||
"options:发生了什么:呕吐|拉稀|精神差|皮肤红|其他:-\noptions:严重程度:低|中|高"),
|
||
full("vaccine", "疫苗", "vaccine", h, 20, "text:疫苗名称"),
|
||
full("deworm", "驱虫", "deworm", h, 30, "options:类型:体内|体外|体内外同驱"),
|
||
simple("checkup", "体检", "checkup", h, 40, "text:在哪做的"),
|
||
simple("vet", "看病", "vet", h, 50, "text:诊断\nnumber:花费:元"),
|
||
full("medicine", "给药", "medicine", h, 60, "text:药品名称\ntext:剂量"),
|
||
simple("supplement", "保健品", "supplement", h, 70, "text:名称\ntext:剂量"),
|
||
// 洗护
|
||
simple("bath", "洗澡", "bath", c, 10, ""),
|
||
simple("nail", "剪指甲", "nail", c, 20, ""),
|
||
simple("ear", "洗耳朵", "ear", c, 30, ""),
|
||
simple("tooth", "刷牙", "tooth", c, 40, ""),
|
||
simple("brush", "梳毛", "brush", c, 50, ""),
|
||
simple("groom", "美容", "groom", c, 60, "number:花费:元"),
|
||
// 清洁
|
||
simple("litter", "换猫砂", "litter", cl, 10, ""),
|
||
simple("litterbox", "洗猫砂盆", "litterbox", cl, 20, ""),
|
||
simple("bowl", "洗食盆", "bowl", cl, 30, ""),
|
||
simple("waterbowl", "洗水盆", "waterbowl", cl, 40, ""),
|
||
simple("clean", "消毒", "clean", cl, 50, ""),
|
||
}
|
||
return db.Create(&types).Error
|
||
}
|
||
|
||
func seedAdmin(db *gorm.DB, cfg *config.Config) error {
|
||
var count int64
|
||
if err := db.Model(&model.Admin{}).Where("username = ?", cfg.Admin.Username).Count(&count).Error; err != nil {
|
||
return err
|
||
}
|
||
if count > 0 {
|
||
return nil
|
||
}
|
||
hash, err := bcrypt.GenerateFromPassword([]byte(cfg.Admin.Password), bcrypt.DefaultCost)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
return db.Create(&model.Admin{
|
||
Username: cfg.Admin.Username,
|
||
PasswordHash: string(hash),
|
||
Role: "admin",
|
||
}).Error
|
||
}
|