每月根据特定规则集更新用户字段,管理角色和订阅



我的项目中有一个数据库表(映射为类(,如下所示:

public class Subscriptions
{
public int SubscriptionId {Get;Set;}
public DateTime CreateTime {Get;set;}
public int SubscriptionType{get;set;}//fk 
public bool IsActive {get;set;}
}

现在让我感到困惑的部分是如何重置位于我的用户表中的用户字段:

Public class Users
{
// some properties...
public int HitViews {get;set;}
}

由于我每次登录时都会检查用户的订阅是否处于活动状态,如下所示:

if(_profileStatus == ProfileStatus.ACTIVEPROFILE)
{
   // I also have a LastPaymentDate from API here...
   var lastPaymentDate = _profileStatus.LastPaymentDate.AddMonths(1);
   // this is to ensure we can compare it against the current date
   if(currentDate>LastPaymentDate)
   {
       // Should signal that the field in user table can be reset
   }
}

如果我们想象以下场景,这里的问题是:

  • 如果用户在续订订阅的当天没有登录怎么办,因此当我从我的 API 收到 lastPaymentDate 时,它将是最新的日期,我将无法在第二个 if 语句中完成重置字段......

我想通过将 LastPaymentDate 存储到我的订阅表中来解决此问题,然后像这样对它执行检查:

if(_profileStatus == ProfileStatus.ACTIVEPROFILE)
{
if(currentDate>myDBLastPaymentDate)
{
// now I should check whether the db last payment date and API last payment date are different, if they are, then I simply swipe the DB value with API value ?
}
}

我的问题是,这是有效和正确的方法吗,如果不是,你们有什么建议,以便我可以改进这一点?

有人可以帮助我吗?

有两种方法可以比较日期。一种是简单的检查,因为您已经在这样做了。

if(currentDate>myDBLastPaymentDate)
{
}

另一种方法如下。当您的日期是Datetime格式时,这种方式通常非常有用。

int camparison = DateTime.Compare(currentDate, myDBLastPaymentDate);
string result;
if (camparison < 0)
{
    result= "is earlier than";
}
else if (camparison == 0)
{
    result= "is the same time as";         
}    
else
{
    result= "is later than";
}

希望对您有所帮助。

最新更新