简体   繁体   中英

What is best way of changing between active and upcoming_active records?

I'm using tokens for how many messages a user can send. 1 Message = 1 Token . At the moment I've just got it subtracting the value from an overall value to check if the user has tokens remaining and that's working fine.

I'm trying to change it to work so that it shows which bundle is active, so I'm looking for some help with checking if the user doesn't have enough tokens remaining in the active bundle change to the upcoming_bundle.

Example:

Stored User Data:

Table Name: Tokens

First Record

id: 1
user_id: 5
bundle_type: small
value: 10
value_remaining: 4
state: active_bundle

Second Record

id: 2
user_id: 5
bundle_type: large
value: 100
value_remaining: 100
state: Upcoming_bundle
  • User sends 10 messages (10 tokens)
  • Only 4 remaining tokens in first record. Use 4 remaining tokens and leave 6 tokens
  • Then subtract the 6 tokens from second record which is now active so that will leave 94 remaining tokens.

Should I have a check to database every time the message is sent and update the database to subtract 1 token at a time, then when the remaining_value hits 0 change active_bundle to inactive and upcoming_bundle to active?

I would just like some advice on this, thanks in advanced.

If this is your data model then I would fetch all active & upcoming bundles and then do the logic in php, eg subtract remaining tokens, change status, etc and then update them as a transaction.

If you are flexible on how the data is structured, I would rather have some kind of transaction log, from which I can read each action, ie whether a bundle was added or a token was used with a timestamp. For example like this:

id | user | change | comment             | timestamp
1  | 1    | 10     | bought small bundle | 2016-09-06 09:30:00
2  | 1    | -1     | sent message        | 2016-09-06 10:56:00
3  | 2    | -3     | sent multi-message  | 2016-09-06 10:57:00

Where id is the transaction id, user the user id, change is the number of tokens added (by adding a bundle) or used (by sending one or many messages) and comment a message describing the action. When you want to find out how many tokens there are left you can just do a search for that user and check their SUM(change) instead of weird searches for active/upcoming bundles. Obviously this can be more or less elaborate depending on your needs.

This does not take into account your actual domain! There are more approaches each having their drawbacks. For example my approach might have problems wen the transaction_log-table gets large because of number of users and increased activity, although it is very unlikely (I have seen mysql perform well with a few million records in a similar log table). The important part is: You should figure out what is important to your use case and build a solution around the requirements .

我要做的是,一次将其减去一个,这不仅更安全,而且也容易得多。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM