feat(collections): 自动同步作品交付集验收状态
This commit is contained in:
@@ -40,11 +40,14 @@ CREATE TABLE IF NOT EXISTS collections (
|
||||
project_id BIGINT NOT NULL REFERENCES projects(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
client_description TEXT NOT NULL DEFAULT '',
|
||||
status TEXT NOT NULL DEFAULT 'reviewing' CHECK (status IN ('draft', 'reviewing', 'completed', 'archived')),
|
||||
status TEXT NOT NULL DEFAULT 'draft' CHECK (status IN ('draft', 'reviewing', 'completed', 'archived')),
|
||||
completed_at TIMESTAMPTZ,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (project_id, name)
|
||||
);
|
||||
|
||||
ALTER TABLE collections ADD COLUMN IF NOT EXISTS completed_at TIMESTAMPTZ;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS notes (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
collection_id BIGINT NOT NULL REFERENCES collections(id) ON DELETE CASCADE,
|
||||
@@ -204,4 +207,24 @@ CREATE INDEX IF NOT EXISTS customer_sessions_project_id_idx ON customer_sessions
|
||||
CREATE INDEX IF NOT EXISTS review_events_note_version_idx ON review_events(note_id, version_number);
|
||||
CREATE INDEX IF NOT EXISTS audit_logs_group_id_idx ON audit_logs(group_id);
|
||||
|
||||
-- COLLECTION_STATUS_REPAIR_START
|
||||
UPDATE collections
|
||||
SET status = 'draft', completed_at = NULL
|
||||
WHERE status != 'archived'
|
||||
AND id NOT IN (SELECT collection_id FROM notes WHERE review_status != 'draft');
|
||||
|
||||
UPDATE collections AS c
|
||||
SET status = CASE WHEN s.approved_count = s.work_count THEN 'completed' ELSE 'reviewing' END,
|
||||
completed_at = CASE WHEN s.approved_count = s.work_count THEN COALESCE(c.completed_at, NOW()) ELSE NULL END
|
||||
FROM (
|
||||
SELECT collection_id,
|
||||
COUNT(*) AS work_count,
|
||||
SUM(CASE WHEN review_status = 'approved' THEN 1 ELSE 0 END) AS approved_count
|
||||
FROM notes
|
||||
WHERE review_status != 'draft'
|
||||
GROUP BY collection_id
|
||||
) AS s
|
||||
WHERE c.id = s.collection_id AND c.status != 'archived';
|
||||
-- COLLECTION_STATUS_REPAIR_END
|
||||
|
||||
COMMIT;
|
||||
|
||||
Reference in New Issue
Block a user