UserRetainedUtils.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using JiaZhiQuan.Common.Models.VO.CreatorCenter;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace JiaZhiQuan.Common.Utils
  6. {
  7. /// <summary>
  8. /// 用户留存
  9. /// </summary>
  10. public class UserRetainedUtils
  11. {
  12. /// <summary>
  13. /// 获取用户的天留存状态
  14. /// </summary>
  15. /// <param name="userRetainedList">用户的流程记录</param>
  16. /// <param name="day">第几日的留存</param>
  17. /// <param name="userCreateAt">用户的创建时间</param>
  18. /// <returns></returns>
  19. public static string UserDayRetainedStateText(List<UserDayRetainedDTO> userRetainedList, int day, DateTime userCreateAt)
  20. {
  21. int retainedDay = 0;
  22. string userCreateDateStr = userCreateAt.ToString("yyyy-MM-dd"),
  23. nowDateStr = DateTime.Now.Date.ToString("yyyy-MM-dd");
  24. if (userCreateDateStr != nowDateStr)
  25. {
  26. DateTime userCreateDate = DateTime.Parse(userCreateDateStr);
  27. TimeSpan timeDifference = DateTime.Now.Date - userCreateDate;
  28. // 计算创建时间距离今天过去了多少天
  29. retainedDay = (int)Math.Ceiling(timeDifference.TotalDays);
  30. }
  31. // 说明是今天刚创建的
  32. if (retainedDay == 0) return "";
  33. // 如果获取的留存天数>用户创建后过去的天数
  34. if (day > retainedDay) return "";
  35. return userRetainedList.Exists(e => e.day == day) ? "是" : "否";
  36. }
  37. /// <summary>
  38. /// 获取用户的月留存状态
  39. /// </summary>
  40. /// <param name="userRetainedList">用户的流程记录</param>
  41. /// <param name="day">第几日的留存</param>
  42. /// <param name="userCreateAt">用户的创建时间</param>
  43. /// <returns></returns>
  44. public static string UserMonthRetainedStateText(List<UserMonthRetainedDTO> userRetainedList, int month, DateTime userCreateAt)
  45. {
  46. int retainedMonth = 0;
  47. string userCreateDateStr = userCreateAt.ToString("yyyy-MM-01"),
  48. nowDateStr = DateTime.Now.Date.ToString("yyyy-MM-01");
  49. if (userCreateDateStr != nowDateStr)
  50. {
  51. retainedMonth = DateTimeUtils.TwoDateDiffMonths(userCreateDateStr, nowDateStr);
  52. }
  53. // 说明是当前月刚创建的
  54. if (retainedMonth == 0) return "";
  55. // 如果获取的留存月数>用户创建后过去的月数
  56. if (month > retainedMonth) return "";
  57. return userRetainedList.Exists(e => e.month == month) ? "是" : "否";
  58. }
  59. }
  60. public class UserDayRetainedDTO
  61. {
  62. public int id { get; set; }
  63. public long userId { get; set; }
  64. public string appSource { get; set; }
  65. public int day { get; set; }
  66. public string activeDate { get; set; }
  67. }
  68. public class UserMonthRetainedDTO
  69. {
  70. public int id { get; set; }
  71. public DateTime userCreateTime { get; set; }
  72. public long userId { get; set; }
  73. public string appSource { get; set; }
  74. public int month { get; set; }
  75. public string monthFirstDay { get; set; }
  76. }
  77. }