package models import ( "database/sql/driver" "encoding/json" "errors" "time" ) // JSONObject 自定义 JSONB 类型 type JSONObject map[string]any func (j JSONObject) Value() (driver.Value, error) { if j == nil { return nil, nil } return json.Marshal(j) } func (j *JSONObject) Scan(src any) error { if src == nil { *j = nil return nil } var data []byte switch v := src.(type) { case []byte: data = v case string: data = []byte(v) default: return errors.New("JSONObject: 不支持的数据源") } // 尝试解析为对象 if err := json.Unmarshal(data, j); err == nil { return nil } // 兼容 JSON 数组(存到 "_" 字段) var arr []any if err := json.Unmarshal(data, &arr); err == nil { *j = JSONObject{"_": arr} return nil } return errors.New("JSONObject: 解析失败") } // Project 项目 type Project struct { ID uint64 `gorm:"primaryKey" json:"id"` Name string `gorm:"size:120;not null" json:"name"` Slug string `gorm:"size:120;uniqueIndex" json:"slug"` Stage string `gorm:"size:32;not null;default:recruit" json:"stage"` Direction string `gorm:"size:64;not null" json:"direction"` TeamType string `gorm:"size:32;not null;default:oneperson" json:"team_type"` Founder string `gorm:"size:80;not null" json:"founder"` Phone string `gorm:"size:32" json:"phone"` Email string `gorm:"size:120" json:"email"` Intro string `gorm:"type:text" json:"intro"` Progress int `gorm:"not null;default:0" json:"progress"` SpaceID *uint64 `gorm:"column:space_id" json:"space_id"` CohortID *uint64 `gorm:"column:cohort_id" json:"cohort_id"` Package string `gorm:"size:32;not null;default:basic" json:"package"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` Space *Space `gorm:"foreignKey:SpaceID" json:"space,omitempty"` Cohort *Cohort `gorm:"foreignKey:CohortID" json:"cohort,omitempty"` Members []TeamMember `gorm:"foreignKey:ProjectID" json:"members,omitempty"` Products []Product `gorm:"foreignKey:ProjectID" json:"products,omitempty"` } // TableName overrides func (Project) TableName() string { return "projects" } type TeamMember struct { ID uint64 `gorm:"primaryKey" json:"id"` ProjectID uint64 `gorm:"not null" json:"project_id"` Name string `gorm:"size:64;not null" json:"name"` Role string `gorm:"size:64;not null" json:"role"` Bio string `gorm:"type:text" json:"bio"` CreatedAt time.Time `json:"created_at"` } func (TeamMember) TableName() string { return "team_members" } type Space struct { ID uint64 `gorm:"primaryKey" json:"id"` Name string `gorm:"size:120;not null" json:"name"` Location string `gorm:"size:120;not null" json:"location"` Capacity int `gorm:"not null;default:0" json:"capacity"` Occupied int `gorm:"not null;default:0" json:"occupied"` Type string `gorm:"size:32;not null;default:offline" json:"type"` Features string `gorm:"type:text" json:"features"` Status string `gorm:"size:32;not null;default:open" json:"status"` CreatedAt time.Time `json:"created_at"` } func (Space) TableName() string { return "spaces" } type DataResource struct { ID uint64 `gorm:"primaryKey" json:"id"` Title string `gorm:"size:160;not null" json:"title"` Category string `gorm:"size:64;not null" json:"category"` Domain string `gorm:"size:64;not null" json:"domain"` SizeGB float64 `gorm:"column:size_gb" json:"size_gb"` RowsCount int64 `gorm:"column:rows_count" json:"rows_count"` License string `gorm:"size:64;not null;default:授权使用" json:"license"` Description string `gorm:"type:text" json:"description"` ApplyCount int `gorm:"not null;default:0" json:"apply_count"` Available bool `gorm:"not null;default:true" json:"available"` CreatedAt time.Time `json:"created_at"` } func (DataResource) TableName() string { return "data_resources" } type Tool struct { ID uint64 `gorm:"primaryKey" json:"id"` Name string `gorm:"size:120;not null" json:"name"` Category string `gorm:"size:64;not null" json:"category"` Description string `gorm:"type:text" json:"description"` Version string `gorm:"size:32" json:"version"` Homepage string `gorm:"size:255" json:"homepage"` FreeQuota string `gorm:"size:120;column:free_quota" json:"free_quota"` Status string `gorm:"size:32;not null;default:available" json:"status"` Domain string `gorm:"size:32;not null;default:platform" json:"domain"` Template JSONObject `gorm:"type:jsonb" json:"template"` CreatedAt time.Time `json:"created_at"` } func (Tool) TableName() string { return "tools" } // ToolRun 工具运行记录 type ToolRun struct { ID uint64 `gorm:"primaryKey" json:"id"` ToolID uint64 `gorm:"column:tool_id;not null" json:"tool_id"` UserName string `gorm:"size:80;column:user_name;not null;default:demo@opc" json:"user_name"` Input JSONObject `gorm:"type:jsonb" json:"input"` Output JSONObject `gorm:"type:jsonb" json:"output"` Score *float64 `gorm:"type:numeric(5,2)" json:"score"` DurationMs int `gorm:"column:duration_ms" json:"duration_ms"` CreatedAt time.Time `json:"created_at"` } func (ToolRun) TableName() string { return "tool_runs" } type Cohort struct { ID uint64 `gorm:"primaryKey" json:"id"` Name string `gorm:"size:120;not null" json:"name"` Theme string `gorm:"size:120" json:"theme"` StartDate *time.Time `gorm:"column:start_date" json:"start_date"` EndDate *time.Time `gorm:"column:end_date" json:"end_date"` Capacity int `gorm:"not null;default:30" json:"capacity"` Enrolled int `gorm:"not null;default:0" json:"enrolled"` Status string `gorm:"size:32;not null;default:planning" json:"status"` Description string `gorm:"type:text" json:"description"` CreatedAt time.Time `json:"created_at"` } func (Cohort) TableName() string { return "cohorts" } type Training struct { ID uint64 `gorm:"primaryKey" json:"id"` CohortID *uint64 `gorm:"column:cohort_id" json:"cohort_id"` Title string `gorm:"size:160;not null" json:"title"` Topic string `gorm:"size:64;not null" json:"topic"` Speaker string `gorm:"size:120" json:"speaker"` SpeakerOrg string `gorm:"size:120;column:speaker_org" json:"speaker_org"` TrainDate *time.Time `gorm:"column:train_date" json:"train_date"` Location string `gorm:"size:120" json:"location"` Attendees int `gorm:"not null;default:0" json:"attendees"` Format string `gorm:"size:32;not null;default:offline" json:"format"` Summary string `gorm:"type:text" json:"summary"` CreatedAt time.Time `json:"created_at"` } func (Training) TableName() string { return "trainings" } type Coaching struct { ID uint64 `gorm:"primaryKey" json:"id"` ProjectID uint64 `gorm:"not null;column:project_id" json:"project_id"` MentorName string `gorm:"size:80;not null;column:mentor_name" json:"mentor_name"` MentorOrg string `gorm:"size:120;column:mentor_org" json:"mentor_org"` Topic string `gorm:"size:64;not null" json:"topic"` ScheduledAt *time.Time `gorm:"column:scheduled_at" json:"scheduled_at"` DurationMin int `gorm:"column:duration_min;default:60" json:"duration_min"` Status string `gorm:"size:32;not null;default:scheduled" json:"status"` Notes string `gorm:"type:text" json:"notes"` CreatedAt time.Time `json:"created_at"` } func (Coaching) TableName() string { return "coachings" } type Scenario struct { ID uint64 `gorm:"primaryKey" json:"id"` Name string `gorm:"size:160;not null" json:"name"` Domain string `gorm:"size:64;not null" json:"domain"` Publisher string `gorm:"size:120" json:"publisher"` Intro string `gorm:"type:text" json:"intro"` PartnerOrg string `gorm:"size:120;column:partner_org" json:"partner_org"` OpenedAt *time.Time `gorm:"column:opened_at" json:"opened_at"` Status string `gorm:"size:32;not null;default:open" json:"status"` CreatedAt time.Time `json:"created_at"` } func (Scenario) TableName() string { return "scenarios" } type ScenarioMapping struct { ID uint64 `gorm:"primaryKey" json:"id"` ProjectID uint64 `gorm:"not null;column:project_id" json:"project_id"` ScenarioID uint64 `gorm:"not null;column:scenario_id" json:"scenario_id"` Status string `gorm:"size:32;not null;default:pending" json:"status"` StartedAt *time.Time `gorm:"column:started_at" json:"started_at"` ClosedAt *time.Time `gorm:"column:closed_at" json:"closed_at"` Feedback string `gorm:"type:text" json:"feedback"` CreatedAt time.Time `json:"created_at"` } func (ScenarioMapping) TableName() string { return "scenario_mappings" } type Product struct { ID uint64 `gorm:"primaryKey" json:"id"` ProjectID uint64 `gorm:"not null;column:project_id" json:"project_id"` Name string `gorm:"size:160;not null" json:"name"` Tagline string `gorm:"size:255" json:"tagline"` Category string `gorm:"size:64" json:"category"` CoverURL string `gorm:"size:255;column:cover_url" json:"cover_url"` DemoURL string `gorm:"size:255;column:demo_url" json:"demo_url"` Status string `gorm:"size:32;not null;default:prototyping" json:"status"` Compliance string `gorm:"size:32;not null;default:pending" json:"compliance"` CreatedAt time.Time `json:"created_at"` } func (Product) TableName() string { return "products" } type Event struct { ID uint64 `gorm:"primaryKey" json:"id"` Title string `gorm:"size:160;not null" json:"title"` Type string `gorm:"size:32;not null" json:"type"` Host string `gorm:"size:120" json:"host"` EventDate *time.Time `gorm:"column:event_date" json:"event_date"` Location string `gorm:"size:120" json:"location"` Projects int `gorm:"not null;default:0" json:"projects"` Leads int `gorm:"not null;default:0" json:"leads"` Deals int `gorm:"not null;default:0" json:"deals"` Summary string `gorm:"type:text" json:"summary"` CreatedAt time.Time `json:"created_at"` } func (Event) TableName() string { return "events" } type Policy struct { ID uint64 `gorm:"primaryKey" json:"id"` Title string `gorm:"size:160;not null" json:"title"` Region string `gorm:"size:64;not null" json:"region"` Category string `gorm:"size:64;not null" json:"category"` AmountMax float64 `gorm:"column:amount_max" json:"amount_max"` Intro string `gorm:"type:text" json:"intro"` ApplyURL string `gorm:"size:255;column:apply_url" json:"apply_url"` Status string `gorm:"size:32;not null;default:open" json:"status"` CreatedAt time.Time `json:"created_at"` } func (Policy) TableName() string { return "policies" } type PolicyApplication struct { ID uint64 `gorm:"primaryKey" json:"id"` ProjectID uint64 `gorm:"not null;column:project_id" json:"project_id"` PolicyID uint64 `gorm:"not null;column:policy_id" json:"policy_id"` Status string `gorm:"size:32;not null;default:submitted" json:"status"` SubmittedAt *time.Time `gorm:"column:submitted_at" json:"submitted_at"` GrantedAt *time.Time `gorm:"column:granted_at" json:"granted_at"` Amount *float64 `gorm:"column:amount" json:"amount"` Notes string `gorm:"type:text" json:"notes"` CreatedAt time.Time `json:"created_at"` } func (PolicyApplication) TableName() string { return "policy_applications" } type Milestone struct { ID uint64 `gorm:"primaryKey" json:"id"` ProjectID uint64 `gorm:"not null;column:project_id" json:"project_id"` Title string `gorm:"size:160;not null" json:"title"` Stage string `gorm:"size:32;not null" json:"stage"` HappenedAt *time.Time `gorm:"column:happened_at" json:"happened_at"` Evidence string `gorm:"type:text" json:"evidence"` CreatedAt time.Time `json:"created_at"` } func (Milestone) TableName() string { return "milestones" } type ComplianceLog struct { ID uint64 `gorm:"primaryKey" json:"id"` ProjectID *uint64 `gorm:"column:project_id" json:"project_id"` ResourceID *uint64 `gorm:"column:resource_id" json:"resource_id"` Action string `gorm:"size:64;not null" json:"action"` Operator string `gorm:"size:80;not null" json:"operator"` Result string `gorm:"size:32;not null" json:"result"` Notes string `gorm:"type:text" json:"notes"` CreatedAt time.Time `json:"created_at"` } func (ComplianceLog) TableName() string { return "compliance_logs" } type News struct { ID uint64 `gorm:"primaryKey" json:"id"` Title string `gorm:"size:255;not null" json:"title"` Category string `gorm:"size:64;not null" json:"category"` Summary string `gorm:"type:text" json:"summary"` CoverURL string `gorm:"size:255;column:cover_url" json:"cover_url"` Author string `gorm:"size:80" json:"author"` PublishedAt time.Time `gorm:"column:published_at" json:"published_at"` Views int `gorm:"not null;default:0" json:"views"` } func (News) TableName() string { return "news" } type Mentor struct { ID uint64 `gorm:"primaryKey" json:"id"` Name string `gorm:"size:80;not null" json:"name"` Title string `gorm:"size:120" json:"title"` Org string `gorm:"size:120" json:"org"` Domain string `gorm:"size:64;not null" json:"domain"` YearsExp int `gorm:"column:years_exp;default:0" json:"years_exp"` Bio string `gorm:"type:text" json:"bio"` Avatar string `gorm:"size:255" json:"avatar"` Rating float64 `gorm:"type:numeric(3,1);default:5.0" json:"rating"` Cases int `gorm:"not null;default:0" json:"cases"` CreatedAt time.Time `json:"created_at"` } func (Mentor) TableName() string { return "mentors" } type KPISnapshot struct { ID uint64 `gorm:"primaryKey" json:"id"` MetricKey string `gorm:"size:64;column:metric_key;not null" json:"metric_key"` MetricName string `gorm:"size:120;column:metric_name;not null" json:"metric_name"` Value float64 `gorm:"not null" json:"value"` Target *float64 `json:"target"` Unit string `gorm:"size:32" json:"unit"` SnapDate time.Time `gorm:"column:snap_date;not null" json:"snap_date"` Category string `gorm:"size:32;not null" json:"category"` CreatedAt time.Time `json:"created_at"` } func (KPISnapshot) TableName() string { return "kpi_snapshots" } type Announcement struct { ID uint64 `gorm:"primaryKey" json:"id"` Title string `gorm:"size:255;not null" json:"title"` Content string `gorm:"type:text" json:"content"` Pinned bool `gorm:"not null;default:false" json:"pinned"` PublishedAt time.Time `gorm:"column:published_at" json:"published_at"` CreatedAt time.Time `json:"created_at"` } func (Announcement) TableName() string { return "announcements" } // Partner 合作伙伴 type Partner struct { ID uint64 `gorm:"primaryKey" json:"id"` Name string `gorm:"size:120;not null" json:"name"` ShortName string `gorm:"size:64;column:short_name" json:"short_name"` Type string `gorm:"size:32;not null" json:"type"` Logo string `gorm:"size:255" json:"logo"` Homepage string `gorm:"size:255" json:"homepage"` Description string `gorm:"type:text" json:"description"` Capabilities JSONObject `gorm:"type:jsonb" json:"capabilities"` Scenarios JSONObject `gorm:"type:jsonb" json:"scenarios"` Featured bool `gorm:"not null;default:false" json:"featured"` Contact string `gorm:"size:255" json:"contact"` Status string `gorm:"size:32;not null;default:active" json:"status"` CreatedAt time.Time `json:"created_at"` } func (Partner) TableName() string { return "partners" }