示例#1
0
      public static string GetActiveStatus(DateTime stDate, DateTime edDate)
      {
          ActiveChangeModel activeModel = new ActiveChangeModel();
          var cacheKey    = "IndexActiveStatus:" + stDate.ToString("yyyyMMdd") + edDate.ToString("yyyyMMdd");
          var cacheResult = "";

          try
          {
              //Cache Redis
              cacheResult = RedisHelper.GetKey(cacheKey);
              if (cacheResult != null && cacheResult != "")
              {
                  activeModel = CommonLib.Helper.JsonDeserializeObject <ActiveChangeModel>(cacheResult);
              }
              else
              {
                  activeModel = UserRetentionBLL.GetActiveStatus(stDate, edDate);
                  if (activeModel.dataList != null && activeModel.dataList.Count > 0)
                  {
                      var      cacheExpire = 60 * 60 * 12;
                      var      item        = activeModel.dataList[0];
                      TimeSpan ts          = DateTime.Now.Subtract(Convert.ToDateTime(item.Date));
                      if (ts.TotalDays < 1 && item.NewActive == 0)
                      {
                          cacheExpire = 60 * 60 * 3;
                      }
                      RedisHelper.SetKey(cacheKey, CommonLib.Helper.JsonSerializeObject(activeModel), cacheExpire);
                  }
              }
          }
          catch (Exception ex)
          {
              Logger.Error("活跃列表获取错误", ex);
          }

          return(CommonLib.Helper.JsonSerializeObject(activeModel, "MM-dd"));
      }
        /// <summary>
        /// 返回每日的新增活跃用户和流失活跃用户
        /// </summary>
        /// <param name="stDate"></param>
        /// <param name="edDate"></param>
        /// <returns></returns>
        public ActiveChangeModel GetActiveChangeData(DateTime stDate, DateTime edDate)
        {
            StringBuilder     strSql      = new StringBuilder();
            ActiveChangeModel activeModel = new ActiveChangeModel();

            while (stDate < edDate)
            {
                ActiveChangeUnit singleData = new ActiveChangeUnit();

                singleData.Date = stDate;

                #region 获取已不活跃用户数
                strSql.Clear();
                strSql.Append(" select count(accid) as cnt from [Sys_I200].[dbo].[SysRpt_ShopActive]  " +
                              " where DATEDIFF(DAY,startTime,@now)=0 and active<5 and accid in(");
                strSql.Append(" select accid from [Sys_I200].[dbo].[SysRpt_ShopActive] " +
                              " where DATEDIFF(DAY,updatetime,@before)=0 and active in (5,7))");

                singleData.LostActive = DapperHelper.ExecuteScalar <int>(strSql.ToString(), new { now = stDate, before = stDate.AddDays(-1) });

                strSql.Clear();
                strSql.Append(" select accid from [Sys_I200].[dbo].[SysRpt_ShopActive]  " +
                              " where DATEDIFF(DAY,startTime,@now)=0 and active<5 and accid in(");
                strSql.Append(" select accid from [Sys_I200].[dbo].[SysRpt_ShopActive] " +
                              " where DATEDIFF(DAY,updatetime,@before)=0 and active in (5,7))");

                singleData.LostAccids =
                    CommonLib.Helper.JsonSerializeObject(DapperHelper.Query <int>(strSql.ToString(), new { now = stDate, before = stDate.AddDays(-1) }).ToList());

                #endregion

                #region 获取新增活跃用户数
                strSql.Clear();
                strSql.Append(" select count(accid) as cnt from [Sys_I200].[dbo].[SysRpt_ShopActive]  " +
                              " where DATEDIFF(DAY,startTime,@now)=0 and active=5 and accid in(");
                strSql.Append(" select accid from [Sys_I200].[dbo].[SysRpt_ShopActive] " +
                              " where DATEDIFF(DAY,updatetime,@before)=0 and active<5)");

                singleData.NewActive = DapperHelper.ExecuteScalar <int>(strSql.ToString(), new { now = stDate, before = stDate.AddDays(-1) });

                strSql.Clear();
                strSql.Append(" select accid as cnt from [Sys_I200].[dbo].[SysRpt_ShopActive]  " +
                              " where DATEDIFF(DAY,startTime,@now)=0 and active=5 and accid in(");
                strSql.Append(" select accid from [Sys_I200].[dbo].[SysRpt_ShopActive] " +
                              " where DATEDIFF(DAY,updatetime,@before)=0 and active<5)");

                singleData.NewAccids =
                    CommonLib.Helper.JsonSerializeObject(DapperHelper.Query <int>(strSql.ToString(), new { now = stDate, before = stDate.AddDays(-1) }).ToList());
                #endregion

                singleData.NetValue = singleData.NewActive - singleData.LostActive;
                singleData.Percent  = (singleData.LostActive == 0 ? 0 : (singleData.NetValue * 100 / singleData.LostActive));

                singleData.RowMark = CommonLib.Helper.Md5Hash(singleData.Date.ToLongDateString());
                if (stDate.DayOfWeek.ToString() == "Saturday" || stDate.DayOfWeek.ToString() == "Sunday")
                {
                    singleData.Weekend = "1";
                }
                else
                {
                    singleData.Weekend = "0";
                }

                activeModel.dataList.Add(singleData);
                stDate = stDate.AddDays(1);
            }

            activeModel.dataList.Reverse();
            return(activeModel);
        }