feat: rebuild pet app and backend around core care flows
This commit is contained in:
@@ -1,233 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/sundynix/pets-be/internal/model"
|
||||
)
|
||||
|
||||
// 养护方案预置:5 个阶段 × 2 物种,各一套「标准照护」。
|
||||
//
|
||||
// 内容按各阶段的真实生理重点配,不是把同一份东西换个名字:
|
||||
// 幼年期 疫苗序列 + 驱虫 + 社会化,大型犬还要控制生长速度
|
||||
// 青年期 绝育窗口 + 换成年粮
|
||||
// 成年期 维持期,重点是把基线数据记下来(体重趋势、年度体检)
|
||||
// 中年期 年度血检开始有意义,体重和牙结石重点看
|
||||
// 老年期 体检频率加倍,猫看肾和甲状腺、狗看关节和心脏
|
||||
//
|
||||
// 和 record_types / breeds 一样只在表为空时铺一次,之后一切以数据库为准。
|
||||
//
|
||||
// 这些是「结构」不是「内容」:方案表空着的话建档最后一步没东西可挑,
|
||||
// 用户建完档只能自己从零排计划。
|
||||
|
||||
func plan(title, desc, repeat, sheet string) model.CareTemplateItem {
|
||||
return model.CareTemplateItem{Kind: model.CareKindPlan, Repeat: repeat,
|
||||
Title: title, Description: desc, SheetType: sheet}
|
||||
}
|
||||
func weekly(title, desc, weekdays, sheet string) model.CareTemplateItem {
|
||||
it := plan(title, desc, model.RepeatWeekly, sheet)
|
||||
it.Weekdays = weekdays
|
||||
return it
|
||||
}
|
||||
func monthly(title, desc string, day int, sheet string) model.CareTemplateItem {
|
||||
it := plan(title, desc, model.RepeatMonthly, sheet)
|
||||
it.MonthDay = day
|
||||
return it
|
||||
}
|
||||
func once(title, desc string, day int, sheet string) model.CareTemplateItem {
|
||||
it := plan(title, desc, model.RepeatOnce, sheet)
|
||||
it.Day = day
|
||||
return it
|
||||
}
|
||||
func remind(rtype, title string, offset int, freq string) model.CareTemplateItem {
|
||||
return model.CareTemplateItem{Kind: model.CareKindReminder, ReminderType: rtype,
|
||||
Title: title, OffsetDays: offset, Frequency: freq}
|
||||
}
|
||||
func important(it model.CareTemplateItem) model.CareTemplateItem {
|
||||
it.Priority = "重要"
|
||||
return it
|
||||
}
|
||||
|
||||
// TplDef 一套方案的定义(迁移程序要读它)
|
||||
type tplDef struct {
|
||||
Species string
|
||||
Stage string
|
||||
Name string
|
||||
Summary string
|
||||
Items []model.CareTemplateItem
|
||||
}
|
||||
|
||||
// CareTemplateDefs 导出给一次性迁移程序用
|
||||
func CareTemplateDefs() []tplDef { return careTemplateDefs() }
|
||||
|
||||
// SeedCareTemplatesForce 不检查表空,直接铺。只给迁移程序用
|
||||
func SeedCareTemplatesForce(db *gorm.DB) error { return insertCareTemplates(db) }
|
||||
|
||||
func careTemplateDefs() []tplDef {
|
||||
const (
|
||||
J = "幼年期"
|
||||
Y = "青年期"
|
||||
A = "成年期"
|
||||
M = "中年期"
|
||||
S = "老年期"
|
||||
)
|
||||
return []tplDef{
|
||||
// ───────────────── 猫 ─────────────────
|
||||
{model.SpeciesCat, J, "幼猫标准照护", "少量多餐、疫苗序列和社会化,六月龄前把基础打好", []model.CareTemplateItem{
|
||||
important(plan("喂食 3-4 次", "幼猫胃容量小,一天分 3-4 顿,用幼猫粮", model.RepeatDaily, "food")),
|
||||
plan("清理猫砂盆", "每天铲一次,顺手看便便形状和颜色", model.RepeatDaily, "litterbox"),
|
||||
plan("陪玩 15 分钟", "逗猫棒模拟狩猎,消耗精力也是社会化", model.RepeatDaily, ""),
|
||||
weekly("称一次体重", "幼猫每周都在长,体重曲线比单次数值有用", "1", "weight"),
|
||||
weekly("检查耳朵和眼睛", "幼猫耳螨和眼部分泌物高发", "4", "ear"),
|
||||
once("核对疫苗进度", "对照月龄看第几针,三针核心疫苗要在六月龄前打完", 2, "vaccine"),
|
||||
once("首次体内驱虫", "幼猫驱虫比成猫频繁,按体重给药", 5, "deworm"),
|
||||
remind("vaccine", "下一针疫苗", 21, "幼猫期每 3-4 周一针,共三针"),
|
||||
remind("deworm", "体内外驱虫", 30, "幼猫每月一次"),
|
||||
remind("weight", "记录体重", 7, "每周一次"),
|
||||
}},
|
||||
{model.SpeciesCat, Y, "青年猫标准照护", "绝育窗口、换成猫粮,开始把体重控住", []model.CareTemplateItem{
|
||||
plan("喂食 2 次", "六月龄后可以改成早晚两顿", model.RepeatDaily, "food"),
|
||||
plan("清理猫砂盆", "每天铲一次", model.RepeatDaily, "litterbox"),
|
||||
plan("陪玩 15 分钟", "性成熟后精力旺盛,玩够了才不拆家", model.RepeatDaily, ""),
|
||||
weekly("称一次体重", "绝育前后代谢会变,体重要盯紧", "1", "weight"),
|
||||
weekly("梳毛", "换毛期减少毛球", "4", "brush"),
|
||||
once("咨询绝育时间", "多数猫 6-8 月龄可做,术前要空腹和体检", 3, "vet"),
|
||||
once("换成猫粮", "一岁前后用 7 天时间按比例过渡,别一次换完", 14, "food"),
|
||||
remind("vaccine", "年度加强疫苗", 60, "一年一次"),
|
||||
remind("deworm", "体内外驱虫", 60, "每 2-3 个月一次"),
|
||||
}},
|
||||
{model.SpeciesCat, A, "成年猫标准照护", "维持期,重点是把基线数据记下来", []model.CareTemplateItem{
|
||||
plan("喂食和换水", "定时定量,水每天换", model.RepeatDaily, "food"),
|
||||
plan("清理猫砂盆", "每天铲一次,便便异常是最早的信号", model.RepeatDaily, "litterbox"),
|
||||
weekly("梳毛", "长毛猫每天,短毛猫每周两次", "2,5", "brush"),
|
||||
weekly("刷牙", "牙结石是成年猫最常被忽略的问题", "6", "tooth"),
|
||||
monthly("称一次体重", "成年后体重该是平的,涨跌超过 5% 要留意", 1, "weight"),
|
||||
monthly("洗猫砂盆", "整盆倒空清洗一次", 15, "litterbox"),
|
||||
once("安排年度体检", "成年期一年一次,做基础血检建立基线", 7, "checkup"),
|
||||
remind("vaccine", "年度加强疫苗", 90, "一年一次"),
|
||||
remind("deworm", "体内外驱虫", 90, "每 3 个月一次"),
|
||||
remind("monthlyReport", "看看这个月的记录", 30, "每月一次"),
|
||||
}},
|
||||
{model.SpeciesCat, M, "中年猫标准照护", "七岁起代谢变慢,年度血检开始能查出早期问题", []model.CareTemplateItem{
|
||||
plan("喂食和换水", "中年后容易发胖,按体重控制热量", model.RepeatDaily, "food"),
|
||||
plan("清理猫砂盆", "尿量和尿块大小要留意,肾病最早从这里看出来", model.RepeatDaily, "litterbox"),
|
||||
weekly("梳毛", "自我梳理会减少,需要帮忙", "2,5", "brush"),
|
||||
weekly("刷牙", "牙结石到这个年龄已经开始影响进食", "6", "tooth"),
|
||||
monthly("称一次体重", "不明原因掉体重是中年猫要警惕的信号", 1, "weight"),
|
||||
monthly("检查口腔", "看牙龈颜色和有没有结石", 15, ""),
|
||||
once("安排年度体检", "血检 + 尿检,重点看肾功能和甲状腺", 7, "checkup"),
|
||||
remind("vaccine", "年度加强疫苗", 90, "一年一次"),
|
||||
remind("deworm", "体内外驱虫", 90, "每 3 个月一次"),
|
||||
remind("weight", "记录体重", 30, "每月一次"),
|
||||
}},
|
||||
{model.SpeciesCat, S, "老年猫标准照护", "体检半年一次,肾和甲状腺是重点", []model.CareTemplateItem{
|
||||
important(plan("记录饮水量", "多饮多尿是慢性肾病最典型的早期表现", model.RepeatDaily, "water")),
|
||||
plan("喂食", "老年猫可能需要处方粮,少量多次更好消化", model.RepeatDaily, "food"),
|
||||
plan("清理猫砂盆", "尿块变大或变多要立刻记下来", model.RepeatDaily, "litterbox"),
|
||||
weekly("梳毛", "老年猫自己舔不到的地方要帮它梳", "2,5", "brush"),
|
||||
weekly("称一次体重", "老年期体重变化要按周盯,不是按月", "1", "weight"),
|
||||
monthly("检查关节活动", "跳不上原来能跳的地方就是信号", 1, ""),
|
||||
once("安排半年体检", "血检 + 尿检 + 甲状腺,老年期半年一次", 7, "checkup"),
|
||||
remind("vaccine", "年度加强疫苗", 90, "一年一次(可与医生商量减免)"),
|
||||
remind("deworm", "体内外驱虫", 90, "每 3 个月一次"),
|
||||
remind("monthlyReport", "看看这个月的记录", 30, "每月一次"),
|
||||
}},
|
||||
|
||||
// ───────────────── 狗 ─────────────────
|
||||
{model.SpeciesDog, J, "幼犬标准照护", "疫苗序列、社会化,大型犬还要控制生长速度", []model.CareTemplateItem{
|
||||
important(plan("喂食 3-4 次", "幼犬一天分 3-4 顿;大型犬别过量补钙,长太快伤关节", model.RepeatDaily, "food")),
|
||||
plan("外出排便和散步", "疫苗打齐前避免去流浪动物多的地方", model.RepeatDaily, ""),
|
||||
plan("基础训练 10 分钟", "坐、等待、唤回。幼犬期是社会化窗口,错过很难补", model.RepeatDaily, ""),
|
||||
weekly("称一次体重", "幼犬长得快,体重曲线也是喂食量的依据", "1", "weight"),
|
||||
weekly("检查爪垫和耳朵", "散步后看有没有扎伤,垂耳犬要重点看耳道", "4", "ear"),
|
||||
once("核对疫苗进度", "对照月龄看第几针,狂犬疫苗按当地规定单独安排", 2, "vaccine"),
|
||||
once("首次体内驱虫", "按体重给药,幼犬比成犬频繁", 5, "deworm"),
|
||||
remind("vaccine", "下一针疫苗", 21, "幼犬期每 3-4 周一针"),
|
||||
remind("deworm", "体内外驱虫", 30, "幼犬每月一次"),
|
||||
remind("weight", "记录体重", 7, "每周一次"),
|
||||
}},
|
||||
{model.SpeciesDog, Y, "青年犬标准照护", "骨骼刚闭合,可以加运动量了;绝育窗口也在这里", []model.CareTemplateItem{
|
||||
plan("喂食 2 次", "改成早晚两顿", model.RepeatDaily, "food"),
|
||||
plan("散步 30 分钟以上", "骨骼闭合后可以正常运动,之前不宜长距离跑跳", model.RepeatDaily, ""),
|
||||
plan("训练 10 分钟", "把幼犬期学的固化下来", model.RepeatDaily, ""),
|
||||
weekly("称一次体重", "换成犬粮期间容易长胖", "1", "weight"),
|
||||
weekly("梳毛", "换毛期每天,平时每周两次", "2,5", "brush"),
|
||||
once("咨询绝育时间", "大型犬建议等骨骼闭合后再做", 3, "vet"),
|
||||
once("换成犬粮", "用 7 天按比例过渡,别一次换完", 14, "food"),
|
||||
remind("vaccine", "年度加强疫苗", 60, "一年一次"),
|
||||
remind("deworm", "体内外驱虫", 60, "每 2-3 个月一次"),
|
||||
}},
|
||||
{model.SpeciesDog, A, "成年犬标准照护", "维持期,重点是把基线数据记下来", []model.CareTemplateItem{
|
||||
plan("喂食和换水", "定时定量,水每天换", model.RepeatDaily, "food"),
|
||||
plan("散步 30-60 分钟", "运动量不够是成年犬发胖和行为问题的主因", model.RepeatDaily, ""),
|
||||
weekly("梳毛", "顺手摸一遍有没有肿块", "2,5", "brush"),
|
||||
weekly("刷牙", "牙结石是成年犬最常被忽略的问题", "6", "tooth"),
|
||||
monthly("称一次体重", "成年后体重该是平的,涨跌超过 5% 要留意", 1, "weight"),
|
||||
monthly("剪指甲", "听到走路哒哒响就该剪了", 15, "nail"),
|
||||
once("安排年度体检", "一年一次,做基础血检建立基线", 7, "checkup"),
|
||||
remind("vaccine", "年度加强疫苗", 90, "一年一次"),
|
||||
remind("deworm", "体内外驱虫", 90, "每 3 个月一次"),
|
||||
remind("monthlyReport", "看看这个月的记录", 30, "每月一次"),
|
||||
}},
|
||||
{model.SpeciesDog, M, "中年犬标准照护", "体重和关节是重点,年度血检开始有意义", []model.CareTemplateItem{
|
||||
plan("喂食和换水", "代谢变慢,按体重下调热量", model.RepeatDaily, "food"),
|
||||
plan("散步 30 分钟", "保持规律但降低强度,避免急停急转", model.RepeatDaily, ""),
|
||||
weekly("梳毛并摸一遍身体", "中年后肿块发生率上升,每周摸一遍是最便宜的筛查", "2,5", "brush"),
|
||||
weekly("刷牙", "牙结石到这个年龄已经开始影响进食", "6", "tooth"),
|
||||
monthly("称一次体重", "中年犬超重会直接加重关节负担", 1, "weight"),
|
||||
monthly("检查关节活动", "起身变慢、不愿上楼就是信号", 15, ""),
|
||||
once("安排年度体检", "血检 + 关节检查,大型犬加拍髋关节", 7, "checkup"),
|
||||
remind("vaccine", "年度加强疫苗", 90, "一年一次"),
|
||||
remind("deworm", "体内外驱虫", 90, "每 3 个月一次"),
|
||||
remind("weight", "记录体重", 30, "每月一次"),
|
||||
}},
|
||||
{model.SpeciesDog, S, "老年犬标准照护", "体检半年一次,关节和心脏是重点", []model.CareTemplateItem{
|
||||
important(plan("记录精神和食欲", "老年犬变化来得快,每天一句话记下来", model.RepeatDaily, "")),
|
||||
plan("喂食", "老年犬可能需要处方粮,少量多次更好消化", model.RepeatDaily, "food"),
|
||||
plan("散步 15-20 分钟", "短而多次,比一次走久对关节友好", model.RepeatDaily, ""),
|
||||
weekly("梳毛并摸一遍身体", "重点摸有没有新长的肿块", "2,5", "brush"),
|
||||
weekly("称一次体重", "老年期体重变化要按周盯", "1", "weight"),
|
||||
monthly("检查关节和牙齿", "起身、上楼、咀嚼有没有变吃力", 1, ""),
|
||||
once("安排半年体检", "血检 + 心脏听诊 + 关节,老年期半年一次", 7, "checkup"),
|
||||
remind("vaccine", "年度加强疫苗", 90, "一年一次(可与医生商量减免)"),
|
||||
remind("deworm", "体内外驱虫", 90, "每 3 个月一次"),
|
||||
remind("monthlyReport", "看看这个月的记录", 30, "每月一次"),
|
||||
}},
|
||||
}
|
||||
}
|
||||
|
||||
// seedCareTemplates 只在方案表为空时铺一次
|
||||
func seedCareTemplates(db *gorm.DB) error {
|
||||
var count int64
|
||||
if err := db.Model(&model.CareTemplate{}).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
return insertCareTemplates(db)
|
||||
}
|
||||
|
||||
func insertCareTemplates(db *gorm.DB) error {
|
||||
for i, d := range careTemplateDefs() {
|
||||
tpl := model.CareTemplate{
|
||||
Species: d.Species, Stage: d.Stage, Name: d.Name,
|
||||
Summary: d.Summary, Sort: (i + 1) * 10, Enabled: true,
|
||||
}
|
||||
if err := db.Create(&tpl).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
items := make([]model.CareTemplateItem, 0, len(d.Items))
|
||||
for j, it := range d.Items {
|
||||
it.TemplateID = tpl.ID
|
||||
it.Sort = j
|
||||
items = append(items, it)
|
||||
}
|
||||
if len(items) > 0 {
|
||||
if err := db.Create(&items).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"gorm.io/driver/mysql"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
"gorm.io/gorm/schema"
|
||||
@@ -13,40 +13,31 @@ import (
|
||||
"github.com/sundynix/pets-be/internal/model"
|
||||
)
|
||||
|
||||
// New 建立 GORM 连接,所有表名统一加 sundynix_ 前缀
|
||||
func New(cfg *config.Config) (*gorm.DB, error) {
|
||||
func Open(cfg config.PostgresConfig, release bool) (*gorm.DB, error) {
|
||||
logLevel := logger.Info
|
||||
if cfg.Server.Mode == "release" {
|
||||
if release {
|
||||
logLevel = logger.Warn
|
||||
}
|
||||
|
||||
db, err := gorm.Open(mysql.Open(cfg.MySQL.DSN()), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{
|
||||
TablePrefix: "sundynix_",
|
||||
SingularTable: false,
|
||||
},
|
||||
// 关联在 Go 层管,不靠库级外键。迁移期建外键时 MySQL 会为外键列自动
|
||||
// 补一个索引,和字段上的 `index` tag 撞成同名索引,父子表同迁时第二次
|
||||
// 建索引报 1061 duplicate。关掉外键约束创建,索引就只由 index tag 建一次
|
||||
DisableForeignKeyConstraintWhenMigrating: true,
|
||||
Logger: logger.Default.LogMode(logLevel),
|
||||
db, err := gorm.Open(postgres.Open(cfg.DSN()), &gorm.Config{
|
||||
NamingStrategy: schema.NamingStrategy{TablePrefix: "sundynix_", SingularTable: true},
|
||||
Logger: logger.Default.LogMode(logLevel),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("gorm open: %w", err)
|
||||
return nil, fmt.Errorf("open postgres: %w", err)
|
||||
}
|
||||
|
||||
sqlDB, err := db.DB()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sqlDB.SetMaxOpenConns(50)
|
||||
sqlDB.SetMaxOpenConns(25)
|
||||
sqlDB.SetMaxIdleConns(10)
|
||||
sqlDB.SetConnMaxLifetime(time.Hour)
|
||||
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// Migrate 自动建表
|
||||
func Migrate(db *gorm.DB) error {
|
||||
return db.AutoMigrate(model.AllModels()...)
|
||||
if err := db.AutoMigrate(model.All()...); err != nil {
|
||||
return err
|
||||
}
|
||||
return db.Exec("CREATE UNIQUE INDEX IF NOT EXISTS idx_sundynix_walk_one_active_per_user ON sundynix_walk (user_id) WHERE status IN ('active', 'paused')").Error
|
||||
}
|
||||
|
||||
@@ -1,224 +0,0 @@
|
||||
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
|
||||
}
|
||||
if err := seedRecordTypes(db); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := seedBreeds(db); err != nil {
|
||||
return err
|
||||
}
|
||||
return seedCareTemplates(db)
|
||||
}
|
||||
|
||||
// seedBreeds 品种预置。同 record_types:只在表完全为空时铺一次。
|
||||
//
|
||||
// 这也是「结构」不是「内容」——品种列表空着,建档第二步就选不了品种。
|
||||
// 首字母存字段不运行时算:Go 没有标准拼音库,而多音字(藏獒、柴犬)
|
||||
// 自动转常出错,人来定比库来猜靠谱。
|
||||
func seedBreeds(db *gorm.DB) error {
|
||||
var count int64
|
||||
if err := db.Model(&model.Breed{}).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
return nil
|
||||
}
|
||||
breeds := []model.Breed{
|
||||
// 猫 34 种
|
||||
{Species: model.SpeciesCat, Initial: "A", Name: "阿比西尼亚猫", Sort: 10},
|
||||
{Species: model.SpeciesCat, Initial: "A", Name: "奥西猫", Sort: 20},
|
||||
{Species: model.SpeciesCat, Initial: "B", Name: "巴厘猫", Sort: 30},
|
||||
{Species: model.SpeciesCat, Initial: "B", Name: "波斯猫", Sort: 40},
|
||||
{Species: model.SpeciesCat, Initial: "B", Name: "布偶猫", Sort: 50},
|
||||
{Species: model.SpeciesCat, Initial: "B", Name: "伯曼猫", Sort: 60},
|
||||
{Species: model.SpeciesCat, Initial: "C", Name: "长毛折耳猫", Sort: 70},
|
||||
{Species: model.SpeciesCat, Initial: "D", Name: "德文卷毛猫", Sort: 80},
|
||||
{Species: model.SpeciesCat, Initial: "D", Name: "东方短毛猫", Sort: 90},
|
||||
{Species: model.SpeciesCat, Initial: "D", Name: "东奇尼猫", Sort: 100},
|
||||
{Species: model.SpeciesCat, Initial: "E", Name: "俄罗斯蓝猫", Sort: 110},
|
||||
{Species: model.SpeciesCat, Initial: "H", Name: "哈瓦那棕猫", Sort: 120},
|
||||
{Species: model.SpeciesCat, Initial: "K", Name: "科拉特猫", Sort: 130},
|
||||
{Species: model.SpeciesCat, Initial: "L", Name: "拉波猫", Sort: 140},
|
||||
{Species: model.SpeciesCat, Initial: "M", Name: "缅因猫", Sort: 150},
|
||||
{Species: model.SpeciesCat, Initial: "M", Name: "美国短毛猫", Sort: 160},
|
||||
{Species: model.SpeciesCat, Initial: "M", Name: "美国卷耳猫", Sort: 170},
|
||||
{Species: model.SpeciesCat, Initial: "M", Name: "曼赤肯猫", Sort: 180},
|
||||
{Species: model.SpeciesCat, Initial: "M", Name: "孟买猫", Sort: 190},
|
||||
{Species: model.SpeciesCat, Initial: "M", Name: "孟加拉豹猫", Sort: 200},
|
||||
{Species: model.SpeciesCat, Initial: "N", Name: "挪威森林猫", Sort: 210},
|
||||
{Species: model.SpeciesCat, Initial: "R", Name: "日本短尾猫", Sort: 220},
|
||||
{Species: model.SpeciesCat, Initial: "S", Name: "苏格兰折耳猫", Sort: 230},
|
||||
{Species: model.SpeciesCat, Initial: "S", Name: "索马里猫", Sort: 240},
|
||||
{Species: model.SpeciesCat, Initial: "S", Name: "塞尔凯克卷毛猫", Sort: 250},
|
||||
{Species: model.SpeciesCat, Initial: "S", Name: "斯芬克斯无毛猫", Sort: 260},
|
||||
{Species: model.SpeciesCat, Initial: "S", Name: "沙特尔猫", Sort: 270},
|
||||
{Species: model.SpeciesCat, Initial: "T", Name: "土耳其安哥拉猫", Sort: 280},
|
||||
{Species: model.SpeciesCat, Initial: "T", Name: "土耳其梵猫", Sort: 290},
|
||||
{Species: model.SpeciesCat, Initial: "X", Name: "暹罗猫", Sort: 300},
|
||||
{Species: model.SpeciesCat, Initial: "X", Name: "新加坡猫", Sort: 310},
|
||||
{Species: model.SpeciesCat, Initial: "Y", Name: "异国短毛猫", Sort: 320},
|
||||
{Species: model.SpeciesCat, Initial: "Y", Name: "英国短毛猫", Sort: 330},
|
||||
{Species: model.SpeciesCat, Initial: "Z", Name: "中华田园猫", Sort: 340},
|
||||
// 狗 46 种
|
||||
{Species: model.SpeciesDog, Initial: "A", Name: "阿拉斯加雪橇犬", SizeClass: "large", Sort: 10},
|
||||
{Species: model.SpeciesDog, Initial: "A", Name: "阿富汗猎犬", SizeClass: "large", Sort: 20},
|
||||
{Species: model.SpeciesDog, Initial: "A", Name: "澳洲牧羊犬", SizeClass: "medium", Sort: 30},
|
||||
{Species: model.SpeciesDog, Initial: "B", Name: "比熊犬", SizeClass: "small", Sort: 40},
|
||||
{Species: model.SpeciesDog, Initial: "B", Name: "比格犬", SizeClass: "medium", Sort: 50},
|
||||
{Species: model.SpeciesDog, Initial: "B", Name: "边境牧羊犬", SizeClass: "medium", Sort: 60},
|
||||
{Species: model.SpeciesDog, Initial: "B", Name: "巴哥犬", SizeClass: "small", Sort: 70},
|
||||
{Species: model.SpeciesDog, Initial: "B", Name: "博美犬", SizeClass: "small", Sort: 80},
|
||||
{Species: model.SpeciesDog, Initial: "B", Name: "巴吉度猎犬", SizeClass: "medium", Sort: 90},
|
||||
{Species: model.SpeciesDog, Initial: "B", Name: "北京犬", SizeClass: "small", Sort: 100},
|
||||
{Species: model.SpeciesDog, Initial: "C", Name: "柴犬", SizeClass: "medium", Sort: 110},
|
||||
{Species: model.SpeciesDog, Initial: "C", Name: "川东猎犬", SizeClass: "medium", Sort: 120},
|
||||
{Species: model.SpeciesDog, Initial: "D", Name: "德国牧羊犬", SizeClass: "large", Sort: 130},
|
||||
{Species: model.SpeciesDog, Initial: "D", Name: "杜宾犬", SizeClass: "large", Sort: 140},
|
||||
{Species: model.SpeciesDog, Initial: "D", Name: "大丹犬", SizeClass: "giant", Sort: 150},
|
||||
{Species: model.SpeciesDog, Initial: "D", Name: "斗牛犬", SizeClass: "medium", Sort: 160},
|
||||
{Species: model.SpeciesDog, Initial: "F", Name: "法国斗牛犬", SizeClass: "medium", Sort: 170},
|
||||
{Species: model.SpeciesDog, Initial: "G", Name: "贵宾犬(泰迪)", SizeClass: "small", Sort: 180},
|
||||
{Species: model.SpeciesDog, Initial: "G", Name: "古代牧羊犬", SizeClass: "medium", Sort: 190},
|
||||
{Species: model.SpeciesDog, Initial: "H", Name: "哈士奇", SizeClass: "large", Sort: 200},
|
||||
{Species: model.SpeciesDog, Initial: "H", Name: "蝴蝶犬", SizeClass: "small", Sort: 210},
|
||||
{Species: model.SpeciesDog, Initial: "J", Name: "金毛寻回犬", SizeClass: "large", Sort: 220},
|
||||
{Species: model.SpeciesDog, Initial: "J", Name: "吉娃娃", SizeClass: "small", Sort: 230},
|
||||
{Species: model.SpeciesDog, Initial: "K", Name: "柯基犬", SizeClass: "small", Sort: 240},
|
||||
{Species: model.SpeciesDog, Initial: "K", Name: "可卡犬", SizeClass: "medium", Sort: 250},
|
||||
{Species: model.SpeciesDog, Initial: "K", Name: "苏格兰柯利犬", SizeClass: "medium", Sort: 260},
|
||||
{Species: model.SpeciesDog, Initial: "L", Name: "拉布拉多寻回犬", SizeClass: "large", Sort: 270},
|
||||
{Species: model.SpeciesDog, Initial: "L", Name: "罗威纳犬", SizeClass: "large", Sort: 280},
|
||||
{Species: model.SpeciesDog, Initial: "L", Name: "灵缇犬", SizeClass: "large", Sort: 290},
|
||||
{Species: model.SpeciesDog, Initial: "L", Name: "腊肠犬", SizeClass: "small", Sort: 300},
|
||||
{Species: model.SpeciesDog, Initial: "M", Name: "马尔济斯犬", SizeClass: "small", Sort: 310},
|
||||
{Species: model.SpeciesDog, Initial: "M", Name: "迷你雪纳瑞", SizeClass: "small", Sort: 320},
|
||||
{Species: model.SpeciesDog, Initial: "N", Name: "牛头梗", SizeClass: "medium", Sort: 330},
|
||||
{Species: model.SpeciesDog, Initial: "Q", Name: "秋田犬", SizeClass: "large", Sort: 340},
|
||||
{Species: model.SpeciesDog, Initial: "S", Name: "萨摩耶", SizeClass: "large", Sort: 350},
|
||||
{Species: model.SpeciesDog, Initial: "S", Name: "松狮犬", SizeClass: "medium", Sort: 360},
|
||||
{Species: model.SpeciesDog, Initial: "S", Name: "圣伯纳犬", SizeClass: "giant", Sort: 370},
|
||||
{Species: model.SpeciesDog, Initial: "X", Name: "雪纳瑞", SizeClass: "medium", Sort: 390},
|
||||
{Species: model.SpeciesDog, Initial: "X", Name: "西高地白梗", SizeClass: "small", Sort: 400},
|
||||
{Species: model.SpeciesDog, Initial: "X", Name: "喜乐蒂牧羊犬", SizeClass: "small", Sort: 410},
|
||||
{Species: model.SpeciesDog, Initial: "Y", Name: "约克夏梗", SizeClass: "small", Sort: 420},
|
||||
{Species: model.SpeciesDog, Initial: "Y", Name: "银狐犬", SizeClass: "medium", Sort: 430},
|
||||
{Species: model.SpeciesDog, Initial: "Z", Name: "中华田园犬", SizeClass: "medium", Sort: 440},
|
||||
{Species: model.SpeciesDog, Initial: "Z", Name: "中亚牧羊犬", SizeClass: "large", Sort: 450},
|
||||
{Species: model.SpeciesDog, Initial: "Z", Name: "藏獒", SizeClass: "giant", Sort: 460},
|
||||
}
|
||||
for i := range breeds {
|
||||
breeds[i].Enabled = true
|
||||
}
|
||||
return db.Create(&breeds).Error
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in New Issue
Block a user