【问题标题】:Can I write logic in a database?我可以在数据库中编写逻辑吗?
【发布时间】:2021-07-21 06:13:08
【问题描述】:

我正在编写一个 Flutter 应用程序,用户可以在上面购买东西,我有一个折扣方案,例如:购买 10 件以上可获得 5% 的折扣,超过 20 件可获得 10% 的折扣等。

这很容易在代码中实现。但我宁愿不要硬编码折扣逻辑,因为我以后可能想更改它。

那么有没有一种方法可以在数据库上创建该逻辑并在结帐时调用它?

我考虑过使用数据库中的变量创建 Switch Case,然后我可以对其进行更改。但是我受到我创建的案例数量的限制。 即:

int discount1 = FirebaseReference; // 5
int limit1 = FirebaseReference; // 10 (so give 5% of if more than 10 is bought)
int discount2 = FirebaseReference; // 6
int limit2 = FirebaseReference; // 20
int discount3 = FirebaseReference; // 8
int limit3 = FirebaseReference; // 30

switch(discount) { 
   case amountBought < limit1: { 
      discount1 
   } 
   break; 
  
   case amountBought >= limit1 && amountBought < limit2: { 
      discount2
   } 
   break; 

   case amountBought >= limit3: { 
      discount3
   } 
   break; 
} 

有没有办法让我在数据库中拥有这种类型的代码?

【问题讨论】:

    标签: database firebase flutter mobile


    【解决方案1】:

    我认为直接将该逻辑编码到数据库中是不可能的(至少在 Firebase 中是这样)。

    也许你的意思是Cloud Functions。这样,您就可以将新的折扣逻辑部署为新功能。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-09
      • 2017-03-16
      • 2020-08-03
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多