Files
yunzerwebsiteallinone/go/models/init.go
T
2026-09-16 00:22:17 +08:00

180 lines
5.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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),
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),
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(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),
)
// 创建全局 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()
}