package models import ( "fmt" "github.com/beego/beego/v2/client/orm" beego "github.com/beego/beego/v2/server/web" _ "github.com/go-sql-driver/mysql" ) // Orm 全局 ORM 对象,供业务层和控制器使用 var Orm orm.Ormer // Init 初始化模型层资源(数据库连接、模型注册等)。 func Init(_ string) { // 从配置读取数据库连接信息 user, _ := beego.AppConfig.String("mysqluser") pass, _ := beego.AppConfig.String("mysqlpass") urls, _ := beego.AppConfig.String("mysqlurls") dbname, _ := beego.AppConfig.String("mysqldb") if user == "" || urls == "" || dbname == "" { panic("数据库配置(mysqluser/mysqlurls/mysqldb) 未正确设置") } // 组装 DSN:user:pass@tcp(host:port)/dbname?charset=utf8mb4&parseTime=True&loc=Local dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", user, pass, urls, dbname) // 注册默认数据库 if err := orm.RegisterDataBase("default", "mysql", dsn); err != nil { panic("注册数据库失败: " + err.Error()) } // 注册模型 orm.RegisterModel( new(SystemTenant), new(SystemTenantUser), // 租户套餐 / 用户数配额(docs/sql/create_tenant_package.sql) new(SystemTenantPackage), new(SystemTenantPackageModule), new(SystemTenantUserQuotaPackage), new(SystemTenantQuotaOrder), // 产品定价(docs/sql/create_platform_product_pricing.sql) new(PlatformProduct), new(PlatformProductFeature), new(PlatformProductOrder), new(BackendOrganization), new(BackendEmployee), new(BackendPosition), new(BackendErpContact), new(BackendApprovalFlow), new(BackendApprovalRecord), new(BackendReimbursement), new(BackendReimbursementItem), new(BackendReimbursementExpenseType), new(SystemMenu), new(AdminUser), new(AdminRole), new(SystemFile), new(SystemFilesCategory), new(SystemSMSTask), new(SystemOperationLog), new(SystemLoginLog), new(SystemDomainPool), new(SystemTenantDomain), new(SystemModules), new(StorageConfig), new(TenantSiteSetting), new(PlatformSiteSetting), new(ComplaintCategory), new(PlatformComplaint), new(SystemSoftwareUpgrade), new(PlatformCursorEquipment), new(PlatformCursorActivationCode), new(PlatformCursorEquipmentIpLog), new(PlatformAccountPoolKiro), new(PlatformAccountPoolWindsurf), new(PlatformAccountPoolCursor), new(PlatformAccountPoolCodex), new(PlatformNotebook), new(PlatformAgentApi), new(PlatformPasswordStore), new(BackendPasswordStore), new(BackendUserNotifyConfig), new(TenantCrmCustomer), new(TenantCrmSupplier), new(TenantCrmClue), new(TenantCrmBusiness), new(TenantCrmProject), new(TenantCrmProduct), new(TenantCrmProductCategory), new(TenantCrmUnit), new(TenantCrmFollow), new(TenantCrmAttach), new(TenantCrmEntityContact), new(TenantCrmOperateLog), new(TenantCrmContract), new(TenantCrmPayback), new(TenantCrmPaybackRecord), // 招投标管理(docs/sql/create_crm_bidding.sql) new(TenantCrmBiddingTender), new(TenantCrmBiddingTenderFile), new(TenantCrmBiddingTenderAnnounce), new(TenantCrmBiddingTenderSupplier), new(TenantCrmBiddingTenderEvaluation), new(TenantCrmBiddingTenderResult), new(TenantCrmBiddingBid), new(TenantCrmBiddingBidFile), new(TenantCrmBiddingBidApproval), new(TenantCrmBiddingBidTask), new(TenantCrmBiddingBidTemplate), new(TenantCrmBiddingBidDocument), new(TenantCrmBiddingBidQualification), new(TenantCrmBiddingBidQuote), new(TenantCrmBiddingBidCompetitor), new(TenantCrmBiddingBidResult), new(TenantCrmBiddingBidExpense), // 平台更新通知(docs/sql/platform_upgrade.sql) new(PlatformUpgradeNotice), new(ErpAccountSet), new(ErpNormalSetting), new(ErpCompanyContact), new(BackendAiProvider), new(BackendAiChatSession), new(BackendAiChatMessage), new(BackendAiChatPreset), new(BackendMcpTool), new(BackendMcpServer), new(CmsArticleCategory), new(CmsArticle), new(CmsProductCategory), new(CmsProduct), new(CmsSolutionCategory), new(CmsSolution), new(CmsFrontendTemplate), new(CmsFrontendBanner), new(CmsFrontendFriendlink), new(CmsFrontendOnepage), new(BackendMenuFront), // 平台官网管理(docs/sql/create_platform_website.sql) new(PlatformWebsiteCategory), new(PlatformWebsiteArticle), new(PlatformWebsiteTag), new(PlatformWebsiteArticleTag), new(PlatformWebsiteComment), new(PlatformWebsiteNav), new(PlatformWebsiteLink), new(PlatformWebsitePage), // 站点公告(docs/sql/create_platform_website_notice.sql) new(PlatformWebsiteNotice), new(SystemReminderList), new(SystemNormalSetting), new(PlatformNormalSetting), new(BackendNormalSetting), new(PlatformSchedule), new(PlatformScheduleReminder), new(PlatformScheduleReminderSendLog), new(BackendNotebook), new(BackendSchedule), new(BackendScheduleReminder), new(BackendScheduleReminderSendLog), new(OaSchedule), new(OaHoliday), new(OaNotice), new(OaDocCategory), new(OaDoc), new(OaDocLink), new(OaDocShare), new(BackendOaCompensationScheme), new(BackendOaPayroll), new(BackendOaPayrollItem), new(BackendEmployeeFile), new(BackendEmployeeFileRecord), // 平台端支付模块(docs/sql/create_platform_payment.sql) new(PlatformPaymentChannel), new(PlatformPaymentOrder), new(PlatformPaymentOrderLog), new(PlatformPaymentCallbackLog), new(PlatformPaymentChannelBill), new(PlatformPaymentReconcileDiff), new(PlatformPaymentRefund), // 平台端推广佣金(docs/sql/create_platform_commission.sql) new(PlatformCommissionRule), new(PlatformCommissionOrder), // 微信公众号(服务号)绑定与推送(docs/sql/create_platform_wechat_mp.sql) new(WechatMpConfig), new(WechatMpFollower), new(WechatMpVerifyCode), // 统一认证中心(UAC):docs/sql/create_auth_tables.sql,人工执行建表 new(AuthIdentity), new(AuthIdentityThird), new(AuthTenantUser), new(AuthClient), new(AuthSession), new(AuthRefreshToken), new(AuthCode), new(AuthTokenBlacklist), new(AuthTenantIdp), new(AuthTenantAuthConfig), new(AuthLoginLog), ) // 创建全局 Ormer // 数据库约定(重要): // 1. 表结构与全局预置数据变更,一律以 SQL 文件形式放在 docs/sql/ 下由人工执行, // 代码中禁止自动建表 / 补列 / 补全局预置数据(本层不做任何此类操作); // 2. 例外:租户级默认业务数据初始化保留在业务函数中(新租户首次使用时补建,SQL 无法预置), // 搜索「【租户级初始化】」可定位全部位置: // EnsureOaDocDefaultCategories 内置文档分类(项目文档) // crmEnsureDefaultUnits 默认计量单位(套/台/个…) // crmEnsureDefaultProductCategories 默认产品分类(未分类/软件开发/软件产品/硬件产品) // 3. 角色体系:角色数据统一维护在 yz_system_admin_role // (平台角色 cid=1;全局租户角色 cid=2 且 tenant_id=0:租户管理员/租户用户), // 新建与调整走 SQL:docs/sql/update_role_system.sql,代码不做自动补建。 Orm = orm.NewOrm() }