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

287 lines
7.0 KiB
Go

package models
import (
"fmt"
"time"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
_ "github.com/go-sql-driver/mysql"
)
var Orm orm.Ormer
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 := fmt.Sprintf(
"%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=Local&timeout=10s&readTimeout=30s&writeTimeout=30s",
user, pass, urls, dbname,
)
var dbErr error
for attempt := 1; attempt <= 3; attempt++ {
dbErr = orm.RegisterDataBase("default", "mysql", dsn)
if dbErr == nil {
break
}
fmt.Printf("[db] 第 %d 次注册数据库失败: %v\n", attempt, dbErr)
if attempt < 3 {
time.Sleep(time.Duration(attempt) * 2 * time.Second)
}
}
if dbErr != nil {
panic("注册数据库失败(请检查 mysqlurls 网络连通性): " + dbErr.Error())
}
maxOpen := 20
maxIdle := 5
if v, err := beego.AppConfig.Int("mysqlmaxopen"); err == nil && v > 0 {
maxOpen = v
}
if v, err := beego.AppConfig.Int("mysqlmaxidle"); err == nil && v > 0 {
maxIdle = v
}
if maxIdle > maxOpen {
maxIdle = maxOpen
}
if db, err := orm.GetDB("default"); err == nil && db != nil {
db.SetMaxOpenConns(maxOpen)
db.SetMaxIdleConns(maxIdle)
db.SetConnMaxIdleTime(2 * time.Minute)
db.SetConnMaxLifetime(5 * time.Minute)
}
orm.RegisterModel(
new(SystemTenant),
new(SystemTenantUser),
new(SystemTenantPackage),
new(SystemTenantPackageModule),
new(SystemTenantUserQuotaPackage),
new(SystemTenantQuotaOrder),
new(SystemTenantStoragePackage),
new(SystemTenantStorageOrder),
new(PlatformProduct),
new(PlatformProductFeature),
new(PlatformProductOrder),
new(BackendOrganization),
new(BackendEmployee),
new(BackendPosition),
new(BackendJobRank),
new(BackendErpContact),
new(BackendApprovalFlow),
new(BackendApprovalRecord),
new(TenantApprovalFlow),
new(TenantApprovalNode),
new(PlatformApprovalFlow),
new(PlatformApprovalNode),
new(ApprovalInstance),
new(ApprovalTask),
new(ApprovalBinding),
new(BackendReimbursement),
new(BackendReimbursementItem),
new(BackendReimbursementExpenseType),
new(SystemMenu),
new(UniappUserQuickMenu),
new(AdminUser),
new(AdminRole),
new(SystemFile),
new(SystemFilesCategory),
new(SystemSMSTask),
new(SystemOperationLog),
new(SystemLoginLog),
new(SystemDomainPool),
new(SystemTenantDomain),
new(SystemModules),
new(GuestUser),
new(GuestData),
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(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),
new(PlatformUpgradeNotice),
new(ErpAccountSet),
new(ErpNormalSetting),
new(ErpCompanyContact),
new(ErpProductCategory),
new(ErpProduct),
new(ErpWarehouse),
new(ErpStock),
new(ErpStockLedger),
new(ErpDocument),
new(ErpDocumentItem),
new(ErpPartnerBill),
new(ErpFundBill),
new(ErpFundSettle),
new(BackendAiProvider),
new(PlatformAiProvider),
new(PlatformAiChatSession),
new(PlatformAiChatMessage),
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(PlatformWebsiteCategory),
new(PlatformWebsiteArticle),
new(PlatformWebsiteTag),
new(PlatformWebsiteArticleTag),
new(PlatformWebsiteComment),
new(PlatformWebsiteNav),
new(PlatformWebsiteLink),
new(PlatformWebsitePage),
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(PlatformSchedTpl),
new(PlatformSchedTplDay),
new(OaShift),
new(OaSchedulePlan),
new(OaScheduleEntry),
new(OaScheduleChange),
new(OaAttendance),
new(OaHoliday),
new(OaNotice),
new(OaDailyReport),
new(OaDailyReportComment),
new(OaDailyReportHistory),
new(OaDocCategory),
new(OaDoc),
new(OaDocLink),
new(OaDocShare),
new(BackendOaCompensationScheme),
new(BackendOaPayroll),
new(BackendOaPayrollItem),
new(BackendEmployeeFile),
new(BackendEmployeeFileRecord),
new(PlatformPaymentChannel),
new(PlatformPaymentOrder),
new(PlatformPaymentOrderLog),
new(PlatformPaymentCallbackLog),
new(PlatformPaymentChannelBill),
new(PlatformPaymentReconcileDiff),
new(PlatformPaymentRefund),
new(PlatformCommissionRule),
new(PlatformCommissionOrder),
new(WechatMpConfig),
new(WechatMpFollower),
new(WechatMpVerifyCode),
new(WechatMpLoginToken),
new(WechatMpScanLogin),
new(AuthMiniRegister),
new(AuthRegisterSession),
new(WechatMiniConfig),
new(WechatMiniApp),
new(WechatMiniTemplate),
new(WechatMiniSubscribe),
new(AuthIdentity),
new(AuthIdentityThird),
new(AuthTenantUser),
new(AuthClient),
new(AuthSession),
new(AuthRefreshToken),
new(AuthCode),
new(AuthTokenBlacklist),
new(AuthTenantIdp),
new(AuthTenantAuthConfig),
new(AuthTenantApply),
new(AuthLoginLog),
new(AuthAppLoginTicket),
new(PlatformDict),
new(PlatformDictItem),
new(BackendDict),
new(BackendDictItem),
)
Orm = orm.NewOrm()
}