feat: initialize tenant workbench in shared database
This commit is contained in:
+1
-50
@@ -17,7 +17,6 @@ import (
|
||||
"time"
|
||||
|
||||
"git.118im.com/im_team/im_workbench/internal/config"
|
||||
mysqlDriver "github.com/go-sql-driver/mysql"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"golang.org/x/crypto/xtea"
|
||||
)
|
||||
@@ -106,14 +105,6 @@ func (s *Store) Close() error {
|
||||
return s.db.Close()
|
||||
}
|
||||
|
||||
type tenantDBConfig struct {
|
||||
ID int64 `db:"id"`
|
||||
Code string `db:"code"`
|
||||
DBName string `db:"db_name"`
|
||||
DBDSN string `db:"db_dsn"`
|
||||
State int `db:"state"`
|
||||
}
|
||||
|
||||
// ForTenant returns a tenant-scoped Store over the configured shared database.
|
||||
func (s *Store) ForTenant(ctx context.Context, tenantID int64, tenantCode string) (*Store, error) {
|
||||
if s == nil {
|
||||
@@ -139,46 +130,6 @@ func (s *Store) ForTenant(ctx context.Context, tenantID int64, tenantCode string
|
||||
return st, nil
|
||||
}
|
||||
|
||||
func (s *Store) tenantDBConfig(ctx context.Context, tenantID int64, tenantCode string) (*tenantDBConfig, error) {
|
||||
var cfg tenantDBConfig
|
||||
if tenantID > 0 {
|
||||
err := s.db.GetContext(ctx, &cfg, `SELECT id,code,IFNULL(db_name,'') AS db_name,IFNULL(db_dsn,'') AS db_dsn,state
|
||||
FROM hotgo.im_tenant WHERE id=? LIMIT 1`, tenantID)
|
||||
if err == nil {
|
||||
return &cfg, nil
|
||||
}
|
||||
if !errors.Is(err, sql.ErrNoRows) || strings.TrimSpace(tenantCode) == "" {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
err := s.db.GetContext(ctx, &cfg, `SELECT id,code,IFNULL(db_name,'') AS db_name,IFNULL(db_dsn,'') AS db_dsn,state
|
||||
FROM hotgo.im_tenant WHERE code=? LIMIT 1`, strings.TrimSpace(tenantCode))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &cfg, nil
|
||||
}
|
||||
|
||||
func tenantDSN(baseDSN, dbName, dbDSN string) (string, error) {
|
||||
dbDSN = strings.TrimSpace(dbDSN)
|
||||
if strings.HasPrefix(dbDSN, "mysql:") {
|
||||
dbDSN = strings.TrimPrefix(dbDSN, "mysql:")
|
||||
}
|
||||
if dbDSN != "" {
|
||||
return dbDSN, nil
|
||||
}
|
||||
dbName = strings.TrimSpace(dbName)
|
||||
if dbName == "" {
|
||||
return baseDSN, nil
|
||||
}
|
||||
cfg, err := mysqlDriver.ParseDSN(baseDSN)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cfg.DBName = dbName
|
||||
return cfg.FormatDSN(), nil
|
||||
}
|
||||
|
||||
func (s *Store) WorkbenchModules(ctx context.Context, userID int64) ([]WorkbenchModule, int, error) {
|
||||
var modules []WorkbenchModule
|
||||
err := s.db.SelectContext(ctx, &modules, `SELECT code,name,IFNULL(icon_file_id,'') AS icon_file_id,route,sort_order,0 AS unread_count,enabled,permission
|
||||
@@ -1087,7 +1038,7 @@ func (s *Store) MonthlyReportStats(ctx context.Context, month, departmentID stri
|
||||
|
||||
func (s *Store) MailboxConfig(ctx context.Context) (*MailboxConfig, error) {
|
||||
var cfg MailboxConfig
|
||||
if err := s.db.GetContext(ctx, &cfg, `SELECT enabled,allow_anonymous,required_fields,max_length,attachment_policy,notice_title,notice_content,CAST(COALESCE(UNIX_TIMESTAMP(notice_updated_at),1) AS UNSIGNED) AS version FROM im_mailbox_config WHERE tenant_id=? AND id=1`, s.tenant()); err != nil {
|
||||
if err := s.db.GetContext(ctx, &cfg, `SELECT enabled,allow_anonymous,required_fields,max_length,attachment_policy,notice_title,notice_content,CAST(COALESCE(UNIX_TIMESTAMP(notice_updated_at),1) AS UNSIGNED) AS version FROM im_mailbox_config WHERE tenant_id=? LIMIT 1`, s.tenant()); err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return &MailboxConfig{Enabled: true, AllowAnonymous: true, RequiredFields: `["subject","content"]`, MaxLength: 5000, AttachmentPolicy: `{"max_files":5}`, NoticeTitle: "总经理信箱", NoticeContent: "请如实填写诉求。", Version: 1}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user