|
@@ -244,7 +244,7 @@ BEGIN
|
|
|
RETURN u."objectId";
|
|
RETURN u."objectId";
|
|
|
END $$;
|
|
END $$;
|
|
|
CREATE OR REPLACE FUNCTION xs_complete_appointment(co text,aid text,actor text) RETURNS jsonb LANGUAGE plpgsql AS $$
|
|
CREATE OR REPLACE FUNCTION xs_complete_appointment(co text,aid text,actor text) RETURNS jsonb LANGUAGE plpgsql AS $$
|
|
|
-DECLARE a "CourseAppointment"; c "CommonModel"; u "_User"; existing text; lid text=xs_object_id(); lc text=xs_object_id(); ident bigint; cost jsonb; before_value numeric; tx text; charged boolean=false;
|
|
|
|
|
|
|
+DECLARE a "CourseAppointment"; c "CommonModel"; u "_User"; existing text; lid text=xs_object_id(); lc text=xs_object_id(); ident bigint; cost jsonb; before_value numeric; tx text; charged boolean=false; lesson_time timestamptz=now();
|
|
|
BEGIN
|
|
BEGIN
|
|
|
PERFORM pg_advisory_xact_lock(hashtext('xs-credit:'||co));
|
|
PERFORM pg_advisory_xact_lock(hashtext('xs-credit:'||co));
|
|
|
-- Acquire user before appointment/reservation locks, matching transfer and reservation lock ordering.
|
|
-- Acquire user before appointment/reservation locks, matching transfer and reservation lock ordering.
|
|
@@ -253,15 +253,28 @@ BEGIN
|
|
|
SELECT * INTO u FROM "_User" WHERE "company"=co AND "legacyUserId"::text=a."szyh"::text FOR UPDATE;
|
|
SELECT * INTO u FROM "_User" WHERE "company"=co AND "legacyUserId"::text=a."szyh"::text FOR UPDATE;
|
|
|
IF u."objectId" IS NULL THEN RAISE EXCEPTION '预约学员不存在';END IF;
|
|
IF u."objectId" IS NULL THEN RAISE EXCEPTION '预约学员不存在';END IF;
|
|
|
SELECT "objectId" INTO existing FROM "LessonRecord" WHERE "company"=co AND "yyds"::text=c."generalId"::text LIMIT 1;
|
|
SELECT "objectId" INTO existing FROM "LessonRecord" WHERE "company"=co AND "yyds"::text=c."generalId"::text LIMIT 1;
|
|
|
|
|
+ IF existing IS NOT NULL AND a.dszt='10' THEN
|
|
|
|
|
+ RAISE EXCEPTION '该预约已有上课记录但仍标为上课中,请核对原课时流水和记录';
|
|
|
|
|
+ END IF;
|
|
|
IF existing IS NOT NULL THEN RETURN jsonb_build_object('lessonId',existing,'idempotent',true,'periodDeducted',false);END IF;
|
|
IF existing IS NOT NULL THEN RETURN jsonb_build_object('lessonId',existing,'idempotent',true,'periodDeducted',false);END IF;
|
|
|
SELECT * INTO a FROM "CourseAppointment" WHERE "objectId"=a."objectId" FOR UPDATE;
|
|
SELECT * INTO a FROM "CourseAppointment" WHERE "objectId"=a."objectId" FOR UPDATE;
|
|
|
IF a."dszt"::text NOT IN ('10','11') OR COALESCE(NULLIF(a."pl"::text,''),a."fxpl"::text,'0')='0' THEN RAISE EXCEPTION '预约尚未上课或未分配教师';END IF;
|
|
IF a."dszt"::text NOT IN ('10','11') OR COALESCE(NULLIF(a."pl"::text,''),a."fxpl"::text,'0')='0' THEN RAISE EXCEPTION '预约尚未上课或未分配教师';END IF;
|
|
|
PERFORM xs_validate_appointment(co,aid);
|
|
PERFORM xs_validate_appointment(co,aid);
|
|
|
SELECT EXISTS(SELECT 1 FROM "CourseCreditReservation" WHERE "company"=co AND "appointmentId"=aid AND "state"='consumed') INTO charged;
|
|
SELECT EXISTS(SELECT 1 FROM "CourseCreditReservation" WHERE "company"=co AND "appointmentId"=aid AND "state"='consumed') INTO charged;
|
|
|
|
|
+ IF xs_appointment_day(a.bxrq,a.yysj)<(now() AT TIME ZONE 'Asia/Shanghai')::date THEN
|
|
|
|
|
+ IF charged OR (SELECT count(*) FROM "CourseCreditReservation" WHERE company=co AND "appointmentId"=aid
|
|
|
|
|
+ AND "studentId"::text=a.szyh::text AND state='reserved' AND "creditCosts"::jsonb=a."creditCostsSnapshot"::jsonb)<>1 THEN
|
|
|
|
|
+ RAISE EXCEPTION '跨日补录缺少匹配的未消费预留,请先核对原课时流水';
|
|
|
|
|
+ END IF;
|
|
|
|
|
+ -- Preserve the lesson's business date while recording the debit at its actual posting time.
|
|
|
|
|
+ IF NULLIF(trim(a.kssj),'') IS NULL THEN RAISE EXCEPTION '跨日补录缺少实际上课时间,请先核对上课记录'; END IF;
|
|
|
|
|
+ lesson_time=CASE WHEN a.kssj ~ '(Z|[+-][0-9]{2}(:?[0-9]{2})?)$' THEN a.kssj::timestamptz
|
|
|
|
|
+ ELSE a.kssj::timestamp AT TIME ZONE 'Asia/Shanghai' END;
|
|
|
|
|
+ END IF;
|
|
|
tx='complete:'||aid;
|
|
tx='complete:'||aid;
|
|
|
IF NOT charged THEN
|
|
IF NOT charged THEN
|
|
|
PERFORM set_config('xiaoshu.credit_write','yes',true);
|
|
PERFORM set_config('xiaoshu.credit_write','yes',true);
|
|
|
- UPDATE "CourseCreditReservation" SET "state"='consumed',"consumedAt"=now(),"updatedAt"=now() WHERE "company"=co AND "appointmentId"=aid;
|
|
|
|
|
|
|
+ UPDATE "CourseCreditReservation" SET "state"='consumed',"consumedAt"=now(),"updatedAt"=now() WHERE "company"=co AND "appointmentId"=aid AND state='reserved';
|
|
|
FOR cost IN SELECT value FROM jsonb_array_elements(a."creditCostsSnapshot"::jsonb) LOOP
|
|
FOR cost IN SELECT value FROM jsonb_array_elements(a."creditCostsSnapshot"::jsonb) LOOP
|
|
|
before_value=COALESCE((u."legacyUserData"->>(cost->>'account'))::numeric,0);
|
|
before_value=COALESCE((u."legacyUserData"->>(cost->>'account'))::numeric,0);
|
|
|
u."legacyUserData"=jsonb_set(u."legacyUserData",ARRAY[cost->>'account'],to_jsonb(before_value-(cost->>'amount')::numeric));
|
|
u."legacyUserData"=jsonb_set(u."legacyUserData",ARRAY[cost->>'account'],to_jsonb(before_value-(cost->>'amount')::numeric));
|
|
@@ -270,7 +283,7 @@ BEGIN
|
|
|
UPDATE "_User" SET "legacyUserData"=u."legacyUserData","updatedAt"=now() WHERE "objectId"=u."objectId";
|
|
UPDATE "_User" SET "legacyUserData"=u."legacyUserData","updatedAt"=now() WHERE "objectId"=u."objectId";
|
|
|
END IF;
|
|
END IF;
|
|
|
ident=nextval('xiaoshu_business_id');
|
|
ident=nextval('xiaoshu_business_id');
|
|
|
- PERFORM xs_insert('LessonRecord',jsonb_build_object('objectId',lid,'company',co,'id',ident,'sourceKey',tx,'createdAt',now(),'updatedAt',now(),'xymz',a."szyh",'jsmz',COALESCE(NULLIF(a."pl"::text,''),a."fxpl"::text),'kcid',a."kcid",'kclx',a."dslx",'kcmc',c."subtitle",'yyds',c."generalId"::text,'jffs',a."jffs",'kzsj','[]','lessonAt',now(),'bindingId',a."bindingId",'courseCategoryKey',a."courseCategoryKey",'courseCategoryName',a."courseCategoryName",'teacherPaySnapshot',a."teacherPaySnapshot",'creditCostsSnapshot',a."creditCostsSnapshot"));
|
|
|
|
|
|
|
+ PERFORM xs_insert('LessonRecord',jsonb_build_object('objectId',lid,'company',co,'id',ident,'sourceKey',tx,'createdAt',now(),'updatedAt',now(),'xymz',a."szyh",'jsmz',COALESCE(NULLIF(a."pl"::text,''),a."fxpl"::text),'kcid',a."kcid",'kclx',a."dslx",'kcmc',c."subtitle",'yyds',c."generalId"::text,'jffs',a."jffs",'kzsj','[]','lessonAt',lesson_time,'bindingId',a."bindingId",'courseCategoryKey',a."courseCategoryKey",'courseCategoryName',a."courseCategoryName",'teacherPaySnapshot',a."teacherPaySnapshot",'creditCostsSnapshot',a."creditCostsSnapshot"));
|
|
|
PERFORM xs_insert('CommonModel',jsonb_build_object('objectId',lc,'company',co,'createdAt',now(),'updatedAt',now(),'sourceKey',tx,'generalId',nextval('xiaoshu_business_id'),'itemId',ident,'modelId',59,'nodeId',296,'tableName','ZL_C_skjl','title',c."title",'inputer',actor,'status',99));
|
|
PERFORM xs_insert('CommonModel',jsonb_build_object('objectId',lc,'company',co,'createdAt',now(),'updatedAt',now(),'sourceKey',tx,'generalId',nextval('xiaoshu_business_id'),'itemId',ident,'modelId',59,'nodeId',296,'tableName','ZL_C_skjl','title',c."title",'inputer',actor,'status',99));
|
|
|
UPDATE "CourseAppointment" SET "dszt"='11',"jssj"=COALESCE(NULLIF("jssj",''),now()::text),"updatedAt"=now() WHERE "objectId"=a."objectId";
|
|
UPDATE "CourseAppointment" SET "dszt"='11',"jssj"=COALESCE(NULLIF("jssj",''),now()::text),"updatedAt"=now() WHERE "objectId"=a."objectId";
|
|
|
RETURN jsonb_build_object('lessonId',lid,'idempotent',false,'periodDeducted',NOT charged);
|
|
RETURN jsonb_build_object('lessonId',lid,'idempotent',false,'periodDeducted',NOT charged);
|
|
@@ -364,7 +377,11 @@ BEGIN
|
|
|
UPDATE "CourseCreditReservation" SET "state"='reserved',"updatedAt"=now() WHERE "company"=co AND "appointmentId"=aid;
|
|
UPDATE "CourseCreditReservation" SET "state"='reserved',"updatedAt"=now() WHERE "company"=co AND "appointmentId"=aid;
|
|
|
IF NOT FOUND THEN PERFORM xs_insert('CourseCreditReservation',jsonb_build_object('objectId',xs_object_id(),'company',co,'createdAt',now(),'updatedAt',now(),'sourceKey','cloud:appointment-reservation:'||aid,'appointmentId',aid,'studentId',u."legacyUserId",'creditCosts',a."creditCostsSnapshot",'state','reserved'));END IF;
|
|
IF NOT FOUND THEN PERFORM xs_insert('CourseCreditReservation',jsonb_build_object('objectId',xs_object_id(),'company',co,'createdAt',now(),'updatedAt',now(),'sourceKey','cloud:appointment-reservation:'||aid,'appointmentId',aid,'studentId',u."legacyUserId",'creditCosts',a."creditCostsSnapshot",'state','reserved'));END IF;
|
|
|
ELSE
|
|
ELSE
|
|
|
- UPDATE "CourseCreditReservation" SET "state"='released',"updatedAt"=now() WHERE "company"=co AND "appointmentId"=aid;
|
|
|
|
|
|
|
+ IF a.dszt='10' AND (EXISTS(SELECT 1 FROM "LessonRecord" WHERE company=co AND yyds::text=c."generalId"::text)
|
|
|
|
|
+ OR EXISTS(SELECT 1 FROM "CourseCreditReservation" WHERE company=co AND "appointmentId"=aid AND state='consumed')) THEN
|
|
|
|
|
+ RAISE EXCEPTION '该预约已有上课记录或课时消费,请先核对原流水,不能按误开取消';
|
|
|
|
|
+ END IF;
|
|
|
|
|
+ UPDATE "CourseCreditReservation" SET "state"='released',"updatedAt"=now() WHERE "company"=co AND "appointmentId"=aid AND state='reserved';
|
|
|
UPDATE "CommonModel" SET "status"=-2,"updatedAt"=now() WHERE "objectId"=aid;
|
|
UPDATE "CommonModel" SET "status"=-2,"updatedAt"=now() WHERE "objectId"=aid;
|
|
|
END IF;
|
|
END IF;
|
|
|
PERFORM xs_insert('EnrollmentCommand',jsonb_build_object('objectId',xs_object_id(),'company',co,'createdAt',now(),'updatedAt',now(),'idempotencyKey',xs_object_id(),'request',jsonb_build_object('appointmentId',aid,'restore',restore,'actor',actor,'reason',reason),'result','{}'::jsonb));
|
|
PERFORM xs_insert('EnrollmentCommand',jsonb_build_object('objectId',xs_object_id(),'company',co,'createdAt',now(),'updatedAt',now(),'idempotencyKey',xs_object_id(),'request',jsonb_build_object('appointmentId',aid,'restore',restore,'actor',actor,'reason',reason),'result','{}'::jsonb));
|