feat: 增加客户端采购员归属管理 (#54)

This commit is contained in:
chengma
2026-08-09 16:02:25 +08:00
parent 0181170457
commit e0c0dac7a0
17 changed files with 766 additions and 37 deletions
+37 -2
View File
@@ -264,7 +264,7 @@ var migrations = [][]string{
// 背景见 #20:v1 曾经被原地改写而不是新增版本,导致已经建过库的机器
// (user_version 已经越过 v1)永远不会重跑改写后的语句,程序拿着一个
// 和代码对不上的库静默启动。
const schemaVersion = 6
const schemaVersion = 7
// migrationV4 给 PDD 商品增加店铺名。
//
@@ -326,6 +326,33 @@ var migrationV6 = []string{
`CREATE INDEX idx_web_sessions_expiry ON web_sessions(expires_at);`,
}
// migrationV7 记录客户端当前负责人及完整转交历史,见工单 #54。
// client_id 故意不加 clients 外键:客户端记录可删除后由同一稳定编号重新登记,
// 归属和审计历史不能因此丢失。
var migrationV7 = []string{
`CREATE TABLE client_user_assignments (
assignment_id TEXT PRIMARY KEY,
client_id TEXT NOT NULL,
user_id TEXT NOT NULL,
started_at TEXT NOT NULL,
ended_at TEXT,
assigned_by_user_id TEXT NOT NULL,
ended_by_user_id TEXT,
end_reason TEXT CHECK (end_reason IS NULL OR end_reason IN ('unbind', 'transfer')),
FOREIGN KEY (user_id) REFERENCES users(user_id),
FOREIGN KEY (assigned_by_user_id) REFERENCES users(user_id),
FOREIGN KEY (ended_by_user_id) REFERENCES users(user_id),
CHECK ((ended_at IS NULL AND ended_by_user_id IS NULL AND end_reason IS NULL)
OR (ended_at IS NOT NULL AND ended_by_user_id IS NOT NULL AND end_reason IS NOT NULL))
);`,
`CREATE UNIQUE INDEX idx_client_assignment_current
ON client_user_assignments(client_id) WHERE ended_at IS NULL;`,
`CREATE INDEX idx_client_assignment_user
ON client_user_assignments(user_id, ended_at, client_id);`,
`CREATE INDEX idx_client_assignment_history
ON client_user_assignments(client_id, started_at DESC);`,
}
// Migrate 把数据库升到最新版本。
// 已经是最新的就什么都不做,可以重复调用。
func Migrate(db *sql.DB) error {
@@ -396,6 +423,14 @@ func Migrate(db *sql.DB) error {
if err := runSQLMigration(db, 6, migrationV6); err != nil {
return err
}
reached = 6
}
// v7 是纯追加的客户端归属历史表和索引。
if reached < 7 {
if err := runSQLMigration(db, 7, migrationV7); err != nil {
return err
}
}
return nil
@@ -857,7 +892,7 @@ var requiredTables = []string{
"syb_orders", "sku_mappings", "tasks", "clients",
"idempotency_keys", "task_claims",
"syb_session", "syb_sync_state",
"users", "web_sessions",
"users", "web_sessions", "client_user_assignments",
}
// requiredColumns 只列出不能靠“表存在”发现的关键追加列。