|
|
@@ -3,6 +3,7 @@
|
|
|
CREATE OR REPLACE FUNCTION xs_prepare_legacy_appointment(co text,aid text,actor text,seed_rules jsonb) RETURNS jsonb LANGUAGE plpgsql AS $$
|
|
|
DECLARE c "CommonModel";a "CourseAppointment";b "CourseBinding";u "_User";op "_User";
|
|
|
category text;category_name text;course text;day date;minutes numeric;mode text;rule jsonb;costs jsonb;cost jsonb;
|
|
|
+ selected_binding text;
|
|
|
notes_legacy boolean=false;legacy_trial boolean=false;existing "CourseCreditReservation";before_data jsonb;changed boolean=false;
|
|
|
BEGIN
|
|
|
PERFORM pg_advisory_xact_lock(hashtext('xs-credit:'||co));
|
|
|
@@ -33,6 +34,23 @@ BEGIN
|
|
|
category_name=COALESCE(NULLIF(a."courseCategoryName",''),CASE category WHEN 'primary_writing' THEN '小学语法写作课' WHEN 'middle_writing' THEN '中学语法写作课' WHEN 'high_writing' THEN '高中语法写作课' END);
|
|
|
ELSE
|
|
|
PERFORM xs_course_size(co,course);
|
|
|
+ selected_binding=NULLIF(a."bindingId",'');
|
|
|
+ -- Old bookings have no new-system binding ID. Only a confirmed ordinary word
|
|
|
+ -- lesson can prefer one active word enrollment over its imported pending copies.
|
|
|
+ -- Resolve it here so an untouched source booking can still be moved or cancelled by sync.
|
|
|
+ IF selected_binding IS NULL AND a.dslx IN ('1','2') AND (category='word' OR (category IS NULL AND EXISTS(
|
|
|
+ SELECT 1 FROM "CourseTeachingProfile" p WHERE p.company=co AND p."courseId"::text=course AND p.confirmed
|
|
|
+ GROUP BY p."courseId" HAVING bool_and(p."categoryKey" IS NOT DISTINCT FROM 'word')))) THEN
|
|
|
+ SELECT min(cb."objectId") FILTER (WHERE cb."enrollmentStatus"='active') INTO selected_binding
|
|
|
+ FROM "CourseBinding" cb JOIN "CommonModel" bc ON bc.company=cb.company AND bc."modelId"=58 AND bc."itemId"=cb.id
|
|
|
+ WHERE cb.company=co AND cb.yhid::text=a.szyh::text AND cb.kcid::text=course AND bc.status=99
|
|
|
+ AND COALESCE(cb."enrollmentStatus",'pending') IN ('active','pending')
|
|
|
+ HAVING count(*) FILTER (WHERE cb."enrollmentStatus"='active')=1
|
|
|
+ AND bool_and(CASE WHEN cb."enrollmentStatus"='active' THEN cb."categoryKey" IS NOT DISTINCT FROM 'word'
|
|
|
+ ELSE COALESCE(cb."categoryKey",'word')='word' AND
|
|
|
+ (COALESCE(cb."sourceKey",'') ~ '^legacy[:\-]' OR
|
|
|
+ COALESCE(cb."sourceKey",'') ~ '^\[\["(ID|GeneralID)",[0-9]+\]\]$') END);
|
|
|
+ END IF;
|
|
|
-- A textbook may have separate trial and word enrollments for the same student.
|
|
|
-- Respect an explicit binding and resolve an unbound trial from one locked query.
|
|
|
BEGIN
|
|
|
@@ -41,7 +59,7 @@ BEGIN
|
|
|
WHERE cb.company=co AND cb.yhid::text=a.szyh::text AND cb.kcid::text=course AND bc.status=99
|
|
|
AND COALESCE(cb."enrollmentStatus",'pending') IN ('active','pending')
|
|
|
AND (category IS NULL OR cb."categoryKey"=category OR cb."categoryKey" IS NULL OR (legacy_trial AND cb."categoryKey"='word' AND cb."enrollmentStatus"='active'))
|
|
|
- AND (NULLIF(a."bindingId",'') IS NULL OR cb."objectId"=a."bindingId")
|
|
|
+ AND (selected_binding IS NULL OR cb."objectId"=selected_binding)
|
|
|
AND (NOT legacy_trial OR NULLIF(a."bindingId",'') IS NOT NULL OR (cb."categoryKey"='trial' AND cb."enrollmentStatus"='active')
|
|
|
OR NOT EXISTS(SELECT 1 FROM "CourseBinding" trial
|
|
|
JOIN "CommonModel" trial_common ON trial_common.company=trial.company AND trial_common."modelId"=58 AND trial_common."itemId"=trial.id
|