Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

用户金币系统设计 #4

Open
codetalks-new opened this issue Feb 19, 2019 · 2 comments
Open

用户金币系统设计 #4

codetalks-new opened this issue Feb 19, 2019 · 2 comments
Labels
金币系统 关于金币系统的设计演进

Comments

@codetalks-new
Copy link
Owner

codetalks-new commented Feb 19, 2019

1金币等于1万铜币,
1银币等于100铜币。

金币来去

金币来源

暂时不考虑充值。

  1. 每日登录签到随机送金币:+(1~50铜币)
  2. 每日活跃度送金币:+(1~50铜币)
  3. 收到回复感谢:+10铜币
  4. 收到主题感谢: +15 铜币
  5. 创建的主题收到回复: +(5+ 铜币)

金币消耗

  1. 感谢回复: -10 铜币
  2. 感谢创建主题: -15 铜币
  3. 创建主题: - (20 + 25 *字符数/1000 铜币)
  4. 创建回复: -(5+25 * 字符/1000 铜币)
@codetalks-new
Copy link
Owner Author

codetalks-new commented Feb 19, 2019

金币的存储模型

本来作为一个论坛不金币的余额只需要在 User 表中增加一个 balance 字段即可。

但是为了减少跟 User 表直接的耦合。将其放在一个单独的 Wallet 模型中。
此时基本设计如下:

class Wallet(BaseModel,SoftDeleteMixin):
  user = models.OneToOneField(WepostUser,on_delete=models.PROTECT)
  balance = models.DecimalField("余额",max_digits=17, decimal_places=2, default=0)
  freeze = models.DecimalField("冻结",max_digits=17, decimal_places=2, default=0)
  state = models.PositiveSmallIntegerField("状态", choices=WalletState.choices(),default=WalletState.ACTIVE)

基本 state 状态暂时没有特别用处,所以默认的状态是激活状态(active)

@codetalks-new
Copy link
Owner Author

codetalks-new commented Feb 20, 2019

收支明细数据模型

收和支的主要数据模型一致,为了方便查询和记录放在同一个表中。

首先声明一个判断收支方向的枚举

class BillDirection(IntEnum):
  REVENUE = 1
  PAYMENT = 2

  @property
  def label(self):
    return _bill_direction_to_text[self]


_bill_direction_to_text = {
  BillDirection.REVENUE: "收益",
  BillDirection.PAYMENT: "支出"

}

然后定义收支类别:

@unique
class BillCategory(IntEnum):
  # 收入类
  CHECK_IN = 101  # 每日登录签到随机送金币:+(1~50铜币)
  ACTIVE_IN = 102  # 每日活跃度送金币:+(1~50铜币)
  REPLY_IN = 110  # 创建的主题收到回复: +(5+ 铜币)
  LIKE_REPLY_IN = 111  # 收到回复感谢:+10铜币
  LIKE_POST_IN = 112  # 收到主题感谢:+15铜币

  CREATE_POST_COST = 201  # 创建主题: - (20 + 25 *字符数/1000 铜币)
  CREATE_REPLY_COST = 202  # 创建回复: -(5+25 * 字符/1000 铜币)
  LIKE_REPLY_COST = 211  # 发送感谢: -10 铜币
  LIKE_POST_COST = 212  # 发送感谢: -15 铜币

  @property
  def label(self):
    return _bill_category_to_text[self]


_bill_category_to_text = {
  BillCategory.CHECK_IN: "每日登录奖励",
  BillCategory.ACTIVE_IN: "每日活跃度奖励",
  BillCategory.REPLY_IN: "主题回复收益",
  BillCategory.LIKE_IN: "收到谢意",
  BillCategory.CREATE_POST_COST: "创建主题",
  BillCategory.CREATE_REPLY_COST: "创建回复",
  BillCategory.LIKE_COST: "发送谢意",
}

然后定义收支明细的数据模型 Bill

class Bill(BaseModel,SoftDeleteMixin):
  user = models.ForeignKey(WepostUser,on_delete=models.PROTECT,verbose_name="所属用户")
  direction = models.PositiveSmallIntegerField("收支方向",choices=BillDirection.choices())
  # 对于收到谢意类型则记录对应发送用户
  from_user = models.ForeignKey(WepostUser, null=True, on_delete=models.PROTECT,verbose_name="来源用户")
  category = models.PositiveSmallIntegerField("收支类别",choices=BillCategory.choices())
  amount = models.DecimalField("数额", max_digits=17, decimal_places=2)
  current_balance = models.DecimalField("当前余额",max_digits=17, decimal_places=2, default=0)
  current_freeze = models.DecimalField("当前冻结",max_digits=17, decimal_places=2, default=0)

注意,为了方便查询对账,同时需要记录当前的余额和当前冻结。

@codetalks-new codetalks-new added the 金币系统 关于金币系统的设计演进 label Feb 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
金币系统 关于金币系统的设计演进
Projects
None yet
Development

No branches or pull requests

1 participant