generator client { provider = "prisma-client-js" } datasource db { provider = "postgresql" url = env("DATABASE_URL") } // ============================================ // 报表定义表 (聚合根1) // 定义各类报表的配置和调度规则 // ============================================ model ReportDefinition { id BigInt @id @default(autoincrement()) @map("definition_id") // === 报表基本信息 === reportType String @map("report_type") @db.VarChar(50) reportName String @map("report_name") @db.VarChar(200) reportCode String @unique @map("report_code") @db.VarChar(50) description String? @map("description") @db.Text // === 报表参数 === parameters Json @map("parameters") // === 调度配置 === scheduleCron String? @map("schedule_cron") @db.VarChar(100) scheduleTimezone String? @map("schedule_timezone") @db.VarChar(50) @default("Asia/Shanghai") scheduleEnabled Boolean @default(false) @map("schedule_enabled") // === 输出格式 === outputFormats String[] @map("output_formats") // === 状态 === isActive Boolean @default(true) @map("is_active") // === 时间戳 === createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime @updatedAt @map("updated_at") lastGeneratedAt DateTime? @map("last_generated_at") @@map("report_definitions") @@index([reportType], name: "idx_def_type") @@index([isActive], name: "idx_def_active") @@index([scheduleEnabled], name: "idx_def_scheduled") } // ============================================ // 报表快照表 (聚合根2 - 读模型) // 存储已生成的报表数据快照 // ============================================ model ReportSnapshot { id BigInt @id @default(autoincrement()) @map("snapshot_id") // === 报表信息 === reportType String @map("report_type") @db.VarChar(50) reportCode String @map("report_code") @db.VarChar(50) reportPeriod String @map("report_period") @db.VarChar(20) periodKey String @map("period_key") @db.VarChar(30) // === 快照数据 === snapshotData Json @map("snapshot_data") summaryData Json? @map("summary_data") // === 数据来源 === dataSources String[] @map("data_sources") dataFreshness Int @default(0) @map("data_freshness") // === 过滤条件 === filterParams Json? @map("filter_params") // === 统计信息 === rowCount Int @default(0) @map("row_count") // === 时间戳 === periodStartAt DateTime @map("period_start_at") periodEndAt DateTime @map("period_end_at") generatedAt DateTime @default(now()) @map("generated_at") expiresAt DateTime? @map("expires_at") // === 关联 === files ReportFile[] @@map("report_snapshots") @@unique([reportCode, periodKey], name: "uk_report_period") @@index([reportType], name: "idx_snapshot_type") @@index([reportCode], name: "idx_snapshot_code") @@index([periodKey], name: "idx_snapshot_period") @@index([generatedAt(sort: Desc)], name: "idx_snapshot_generated") @@index([expiresAt], name: "idx_snapshot_expires") } // ============================================ // 报表文件表 // 存储已导出的报表文件信息 // ============================================ model ReportFile { id BigInt @id @default(autoincrement()) @map("file_id") snapshotId BigInt @map("snapshot_id") // === 文件信息 === fileName String @map("file_name") @db.VarChar(500) filePath String @map("file_path") @db.VarChar(1000) fileUrl String? @map("file_url") @db.VarChar(1000) fileSize BigInt @map("file_size") fileFormat String @map("file_format") @db.VarChar(20) mimeType String @map("mime_type") @db.VarChar(100) // === 访问信息 === downloadCount Int @default(0) @map("download_count") lastDownloadAt DateTime? @map("last_download_at") // === 时间戳 === createdAt DateTime @default(now()) @map("created_at") expiresAt DateTime? @map("expires_at") // === 关联 === snapshot ReportSnapshot @relation(fields: [snapshotId], references: [id], onDelete: Cascade) @@map("report_files") @@index([snapshotId], name: "idx_file_snapshot") @@index([fileFormat], name: "idx_file_format") @@index([createdAt(sort: Desc)], name: "idx_file_created") } // ============================================ // 分析指标表 (聚合数据) // 存储预聚合的分析指标数据 // ============================================ model AnalyticsMetric { id BigInt @id @default(autoincrement()) @map("metric_id") // === 指标信息 === metricType String @map("metric_type") @db.VarChar(50) metricCode String @map("metric_code") @db.VarChar(50) // === 维度 === dimensionTime DateTime? @map("dimension_time") @db.Date dimensionRegion String? @map("dimension_region") @db.VarChar(100) dimensionUserType String? @map("dimension_user_type") @db.VarChar(50) dimensionRightType String? @map("dimension_right_type") @db.VarChar(50) // === 指标值 === metricValue Decimal @map("metric_value") @db.Decimal(20, 8) metricData Json? @map("metric_data") // === 时间戳 === calculatedAt DateTime @default(now()) @map("calculated_at") @@map("analytics_metrics") @@unique([metricCode, dimensionTime, dimensionRegion, dimensionUserType, dimensionRightType], name: "uk_metric_dimensions") @@index([metricType], name: "idx_metric_type") @@index([metricCode], name: "idx_metric_code") @@index([dimensionTime], name: "idx_metric_time") @@index([dimensionRegion], name: "idx_metric_region") } // ============================================ // 认种统计日表 (每日聚合) // ============================================ model PlantingDailyStat { id BigInt @id @default(autoincrement()) @map("stat_id") // === 统计日期 === statDate DateTime @map("stat_date") @db.Date // === 区域维度 === provinceCode String? @map("province_code") @db.VarChar(10) cityCode String? @map("city_code") @db.VarChar(10) // === 统计数据 === orderCount Int @default(0) @map("order_count") treeCount Int @default(0) @map("tree_count") totalAmount Decimal @default(0) @map("total_amount") @db.Decimal(20, 8) newUserCount Int @default(0) @map("new_user_count") activeUserCount Int @default(0) @map("active_user_count") // === 时间戳 === createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime @updatedAt @map("updated_at") @@map("planting_daily_stats") @@unique([statDate, provinceCode, cityCode], name: "uk_daily_stat") @@index([statDate], name: "idx_pds_date") @@index([provinceCode], name: "idx_pds_province") @@index([cityCode], name: "idx_pds_city") } // ============================================ // 社区统计表 // ============================================ model CommunityStat { id BigInt @id @default(autoincrement()) @map("stat_id") // === 社区信息 === communityId BigInt @map("community_id") communityName String @map("community_name") @db.VarChar(200) parentCommunityId BigInt? @map("parent_community_id") // === 统计日期 === statDate DateTime @map("stat_date") @db.Date // === 统计数据 === totalPlanting Int @default(0) @map("total_planting") dailyPlanting Int @default(0) @map("daily_planting") weeklyPlanting Int @default(0) @map("weekly_planting") monthlyPlanting Int @default(0) @map("monthly_planting") memberCount Int @default(0) @map("member_count") // === 时间戳 === createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime @updatedAt @map("updated_at") @@map("community_stats") @@unique([communityId, statDate], name: "uk_community_stat") @@index([communityId], name: "idx_cs_community") @@index([communityName], name: "idx_cs_name") @@index([statDate], name: "idx_cs_date") @@index([parentCommunityId], name: "idx_cs_parent") } // ============================================ // 系统账户月度统计表 // 省公司/市公司账户的月度数据 // ============================================ model SystemAccountMonthlyStat { id BigInt @id @default(autoincrement()) @map("stat_id") // === 账户信息 === accountId BigInt @map("account_id") accountType String @map("account_type") @db.VarChar(30) accountName String @map("account_name") @db.VarChar(200) regionCode String @map("region_code") @db.VarChar(10) // === 统计月份 === statMonth String @map("stat_month") @db.VarChar(7) // === 月度数据 === monthlyHashpower Decimal @default(0) @map("monthly_hashpower") @db.Decimal(20, 8) cumulativeHashpower Decimal @default(0) @map("cumulative_hashpower") @db.Decimal(20, 8) monthlyMining Decimal @default(0) @map("monthly_mining") @db.Decimal(20, 8) cumulativeMining Decimal @default(0) @map("cumulative_mining") @db.Decimal(20, 8) monthlyCommission Decimal @default(0) @map("monthly_commission") @db.Decimal(20, 8) cumulativeCommission Decimal @default(0) @map("cumulative_commission") @db.Decimal(20, 8) monthlyPlantingBonus Decimal @default(0) @map("monthly_planting_bonus") @db.Decimal(20, 8) cumulativePlantingBonus Decimal @default(0) @map("cumulative_planting_bonus") @db.Decimal(20, 8) // === 时间戳 === createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime @updatedAt @map("updated_at") @@map("system_account_monthly_stats") @@unique([accountId, statMonth], name: "uk_account_month") @@index([accountType], name: "idx_sams_type") @@index([statMonth], name: "idx_sams_month") @@index([regionCode], name: "idx_sams_region") } // ============================================ // 系统账户收益流水表 // 记录每笔收益的来源和时间 // ============================================ model SystemAccountIncomeRecord { id BigInt @id @default(autoincrement()) @map("record_id") // === 账户信息 === accountId BigInt @map("account_id") accountType String @map("account_type") @db.VarChar(30) // === 收益信息 === incomeType String @map("income_type") @db.VarChar(50) incomeAmount Decimal @map("income_amount") @db.Decimal(20, 8) currency String @map("currency") @db.VarChar(10) // === 来源信息 === sourceType String @map("source_type") @db.VarChar(50) sourceId String? @map("source_id") @db.VarChar(100) sourceUserId BigInt? @map("source_user_id") sourceAddress String? @map("source_address") @db.VarChar(200) transactionNo String? @map("transaction_no") @db.VarChar(100) // === 备注 === memo String? @map("memo") @db.Text // === 时间戳 === occurredAt DateTime @map("occurred_at") createdAt DateTime @default(now()) @map("created_at") @@map("system_account_income_records") @@index([accountId], name: "idx_sair_account") @@index([accountType], name: "idx_sair_type") @@index([incomeType], name: "idx_sair_income_type") @@index([sourceType], name: "idx_sair_source_type") @@index([sourceAddress], name: "idx_sair_address") @@index([transactionNo], name: "idx_sair_txno") @@index([occurredAt(sort: Desc)], name: "idx_sair_occurred") } // ============================================ // 报表事件表 // ============================================ model ReportEvent { id BigInt @id @default(autoincrement()) @map("event_id") eventType String @map("event_type") @db.VarChar(50) // 聚合根信息 aggregateId String @map("aggregate_id") @db.VarChar(100) aggregateType String @map("aggregate_type") @db.VarChar(50) // 事件数据 eventData Json @map("event_data") // 元数据 userId BigInt? @map("user_id") occurredAt DateTime @default(now()) @map("occurred_at") @db.Timestamp(6) version Int @default(1) @map("version") @@map("report_events") @@index([aggregateType, aggregateId], name: "idx_report_event_aggregate") @@index([eventType], name: "idx_report_event_type") @@index([occurredAt], name: "idx_report_event_occurred") }