计算每行的剩余余额,并使用Trigger PostgreSQL插入表中的下一个id行

时间:2018-11-29 10:52:05

标签: postgresql

我有3个表invoice_items,topup和topup_usage。在invoice_items上有2 column个用于触发器amountaccount_id

invoice_items

id             account_id                   type     amount
200  ac96c2bc-d4e3-4d75-9814-0186be8e0163   topup       4
201  ac96c2bc-d4e3-4d75-9814-0186be8e0163   topup       7
202  ac96c2bc-d4e3-4d75-9814-0186be8e0163   topup       2

topup_usage

id    id_topup    balance

表格以及充值数据topup的首字母:

id              account_id              amount_ds    status
322 ac96c2bc-d4e3-4d75-9814-0186be8e0163    4        active
323 ac96c2bc-d4e3-4d75-9814-0186be8e0163    7        active
324 ac96c2bc-d4e3-4d75-9814-0186be8e0163    2        active

有这种情况,当在表invoice_item上插入新数据时,我从列account_idamount中获得2个数据以插入表填充,其中account_id代表account_id和{ amount代表amount_ds。然后,我在系统上执行2笔交易,当状态为invoice_items的表中的“使用”时触发,如下所示:

id             account_id                   type     amount
200  ac96c2bc-d4e3-4d75-9814-0186be8e0163   topup       4
201  ac96c2bc-d4e3-4d75-9814-0186be8e0163   topup       7
202  ac96c2bc-d4e3-4d75-9814-0186be8e0163   topup       2
203  ac96c2bc-d4e3-4d75-9814-0186be8e0163   usage       2
204  ac96c2bc-d4e3-4d75-9814-0186be8e0163   usage       3

要在topup_usage上插入数据,我会尝试这种情况:

id   topup_id  amount
399    322        2
400    322        3

我尝试使topup_id 322总数最大为4,如在表topup(amount_ds)上一样,但是共有5个总数,例如,当topup_id的总使用量超过4时(例如5),它将make generate为这样的下一个topup_id:

 id      topup_id    amount
 399       322        2
 400       322        2
 401       323        1

然后为topup_id 323计算最多7个,但像以前在topup_id 323上一样,最多还有6个可用量。然后如果插入7,就像这样

id      topup_id    amount
399       322         2
400       322         2
401       323         1
402       323         6
403       324         1

我使用这样的触发器:

create or replace function topup_usage() returns trigger
    language plpgsql
as $$
BEGIN
  IF new.type = 'USAGE' THEN
    IF(SELECT notes  = 'true' FROM accounts WHERE accounts.id = new.account_id) THEN

    IF ((SELECT amount_ds FROM topup WHERE topup.account_id = new.account_id AND status = 'active' ORDER BY id ASC LIMIT 1) - new.amount < 0 AND (SELECT COUNT(id) > 1 FROM topup WHERE topup.account_id=new.account_id)) THEN
        INSERT INTO topup_usage (id_topup, balance)
        VALUES((SELECT id FROM topup WHERE topup.account_id = new.account_id and status = 'active' limit 1), (SELECT amount_ds FROM topup WHERE topup.account_id=new.account_id AND status='active' ORDER BY id ASC LIMIT 1) - (SELECT COALESCE(sum(balance), 0) FROM topup_usage WHERE id_topup=(SELECT id FROM topup WHERE topup.account_id=new.account_id ORDER BY id DESC LIMIT 1)) );

         INSERT INTO topup_usage (id_topup, balance)
          VALUES((SELECT id FROM topup WHERE topup.account_id = new.account_id and status = 'active' ORDER BY id ASC limit 1) + 1, new.amount - (SELECT balance FROM topup_usage WHERE id_topup=(SELECT id FROM topup WHERE topup.account_id=new.account_id ORDER BY id ASC LIMIT 1)));

      ELSE
        INSERT INTO topup_usage (id_topup, balance)
        VALUES((SELECT id FROM topup WHERE topup.account_id = new.account_id and status = 'active' limit 1), new.amount);
      END IF;


      IF ((SELECT amount_ds FROM topup WHERE id =(SELECT id FROM topup WHERE status = 'active' ORDER BY id ASC limit 1)) - (SELECT SUM(balance) FROM topup_usage WHERE id_topup=(SELECT id FROM topup WHERE status = 'active' ORDER BY id ASC limit 1))) <= 0 THEN
          UPDATE topup SET status = 'finish' WHERE id = (SELECT id FROM topup WHERE status = 'active' ORDER BY id ASC limit 1);
      END IF;

    END IF;
  END IF;
  return null;
END;
$$;

这个结果发生在我身上

id   topup_id  amount
399    322        2
400    322        3

不是这样的:

 id      topup_id    amount
 399       322         2
 400       322         2
 401       323         1
 402       323         6
 403       324         1

有什么建议吗?

0 个答案:

没有答案