116 lines
4.2 KiB
Go
116 lines
4.2 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// TenantInitializationResult reports how many missing defaults were created.
|
|
// Existing tenant customizations are deliberately left untouched.
|
|
type TenantInitializationResult struct {
|
|
WorkbenchApps int64
|
|
AttendanceRules int64
|
|
AttendanceShifts int64
|
|
MailboxConfigs int64
|
|
FeedbackConfigs int64
|
|
}
|
|
|
|
// InitializeTenantDefaults creates the minimum workbench data required by a tenant.
|
|
// Tenant-scoped unique indexes make this operation safe to retry.
|
|
func (s *Store) InitializeTenantDefaults(ctx context.Context) (*TenantInitializationResult, error) {
|
|
tx, err := s.db.BeginTxx(ctx, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer func() { _ = tx.Rollback() }()
|
|
|
|
now := time.Now().UTC().Round(time.Millisecond)
|
|
tenantID := s.tenant()
|
|
result := &TenantInitializationResult{}
|
|
|
|
apps := []struct {
|
|
code, name, route string
|
|
sort int
|
|
}{
|
|
{code: "attendance", name: "打卡", route: "/attendance", sort: 10},
|
|
{code: "monthly_reports", name: "月报", route: "/reports/monthly", sort: 20},
|
|
{code: "daily_reports", name: "日报", route: "/reports/daily", sort: 30},
|
|
{code: "organization", name: "部门框架", route: "/organization", sort: 40},
|
|
{code: "executive_mailbox", name: "举报信箱", route: "/executive-mailbox", sort: 50},
|
|
{code: "meetings", name: "会议通知", route: "/meetings", sort: 60},
|
|
{code: "meeting_notes", name: "会议笔记", route: "/meeting-notes", sort: 70},
|
|
}
|
|
for _, app := range apps {
|
|
res, execErr := tx.ExecContext(ctx, `INSERT INTO im_workbench_apps
|
|
(tenant_id,code,name,icon_file_id,route,sort_order,unread_key,enabled,permission,created_at,updated_at)
|
|
SELECT ?,?,?,?,?,?,?,?,?,?,?
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM im_workbench_apps WHERE tenant_id=? AND code=?
|
|
)`,
|
|
tenantID, app.code, app.name, "", app.route, app.sort, "", 1, "enabled", now, now,
|
|
tenantID, app.code)
|
|
if execErr != nil {
|
|
return nil, execErr
|
|
}
|
|
rows, _ := res.RowsAffected()
|
|
result.WorkbenchApps += rows
|
|
}
|
|
|
|
res, err := tx.ExecContext(ctx, `INSERT INTO im_attendance_rules
|
|
(tenant_id,rule_id,name,workday,location_required,wifi_required,allow_outside_checkin,appeal_allowed,effective_from,effective_to,state,created_at,updated_at)
|
|
SELECT ?,?,?,?,?,?,?,?,CURDATE(),NULL,'active',?,?
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM im_attendance_rules WHERE tenant_id=? AND rule_id=?
|
|
)`,
|
|
tenantID, "att_rule_default", "默认考勤规则", "1,2,3,4,5", 0, 0, 1, 1, now, now,
|
|
tenantID, "att_rule_default")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result.AttendanceRules, _ = res.RowsAffected()
|
|
|
|
res, err = tx.ExecContext(ctx, `INSERT INTO im_attendance_shifts
|
|
(tenant_id,shift_id,rule_id,name,work_start,work_end,checkin_start,checkout_end,state,sort_order,created_at,updated_at)
|
|
SELECT ?,?,?,?,?,?,?,?,?,?,?,?
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM im_attendance_shifts WHERE tenant_id=? AND shift_id=?
|
|
)`,
|
|
tenantID, "shift_default", "att_rule_default", "默认班次", "09:00:00", "18:00:00", "08:00:00", "19:00:00", "active", 10, now, now,
|
|
tenantID, "shift_default")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result.AttendanceShifts, _ = res.RowsAffected()
|
|
|
|
res, err = tx.ExecContext(ctx, `INSERT INTO im_mailbox_config
|
|
(tenant_id,enabled,allow_anonymous,required_fields,max_length,attachment_policy,notice_title,notice_content,notice_updated_at,created_at,updated_at)
|
|
SELECT ?,1,1,?,5000,?,'总经理信箱','请如实填写诉求。',?,?,?
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM im_mailbox_config WHERE tenant_id=?
|
|
)`,
|
|
tenantID, `["subject","content"]`, `{"max_files":5,"purpose":"executive_mailbox"}`, now, now, now,
|
|
tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result.MailboxConfigs, _ = res.RowsAffected()
|
|
|
|
res, err = tx.ExecContext(ctx, `INSERT INTO im_feedback_config
|
|
(tenant_id,categories,max_length,attachment_policy,allow_contact,diagnostics_notice,created_at,updated_at)
|
|
SELECT ?,?,2000,?,1,'',?,?
|
|
WHERE NOT EXISTS (
|
|
SELECT 1 FROM im_feedback_config WHERE tenant_id=?
|
|
)`,
|
|
tenantID, `["bug","suggestion","complaint","other"]`, `{"max_files":5,"purpose":"feedback"}`, now, now,
|
|
tenantID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result.FeedbackConfigs, _ = res.RowsAffected()
|
|
|
|
if err = tx.Commit(); err != nil {
|
|
return nil, err
|
|
}
|
|
return result, nil
|
|
}
|