【问题标题】:Updating user field each month under specific set of rules, managing roles and subscriptions根据特定规则集每月更新用户字段,管理角色和订阅
【发布时间】:2017-11-21 07:15:42
【问题描述】:

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

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>LastPaymentDate))
  • @RamazanBinarbasi 错字,我在堆栈上写了这段代码只是为了展示一个例子:)

标签: c# asp.net .net asp.net-mvc c#-4.0


【解决方案1】:

有两种比较日期的方法。 一种是您已经在做的简单检查。

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";
}

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2017-10-04
    • 1970-01-01
    • 2019-11-12
    • 2015-10-02
    相关资源
    最近更新 更多