RepositoryExtension.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using JiaZhiQuan.Common.Config;
  2. using Newtonsoft.Json;
  3. using Newtonsoft.Json.Linq;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Dynamic;
  7. using Wicture.DbRESTFul;
  8. using Wicture.DbRESTFul.Infrastructure;
  9. using Wicture.DbRESTFul.Infrastructure.Repository;
  10. namespace JiaZhiQuan.Common
  11. {
  12. public static partial class RepositoryExtension
  13. {
  14. private static bool IsPropertyExist(dynamic data, string propertyName)
  15. {
  16. if (data is ExpandoObject)
  17. return ((IDictionary<string, object>)data).ContainsKey(propertyName);
  18. return data.GetType().GetProperty(propertyName) != null;
  19. }
  20. /// <summary>
  21. /// 判断是否使用审核数据
  22. /// </summary>
  23. /// <param name="repository"></param>
  24. /// <returns></returns>
  25. public static bool IsUsingPreviewData(this DbRESTFulRepository repository)
  26. {
  27. var httpContext = repository.Context as HttpApiExecutionContext;
  28. var headers = httpContext.HttpContext.Request.Headers;
  29. var deviceInfo = httpContext.CustomFields["deviceInfo"];
  30. var channel = httpContext.CustomFields["channel"];
  31. try
  32. {
  33. if (string.IsNullOrEmpty(deviceInfo)) return false;
  34. var json = JsonConvert.DeserializeObject<JObject>(deviceInfo);
  35. var appVersion = channel + "," + json.Value<string>("soft_version");
  36. return ConfigFromDb.AppPreviewTypeList.Contains(appVersion);
  37. }
  38. catch (Exception ex)
  39. {
  40. LoggerManager.Logger.Error(ex, "读取os_version进出错");
  41. return false;
  42. }
  43. }
  44. /// <summary>
  45. /// 获取当前时间应该获取统计数据的起止日期,startDate <= DATE >= endDate
  46. /// </summary>
  47. public static DateRange GetQueryDateRange(this DbRESTFulRepository repository, int days)
  48. {
  49. // 如果是早上10点后才展示昨日的数据
  50. var now = DateTime.Now;
  51. string endDate;
  52. string startDate;
  53. if (days == 0) days = 1;
  54. if (now.Hour < 10)
  55. {
  56. endDate = now.AddDays(-2).ToString("yyyy-MM-dd");
  57. startDate = now.AddDays(-2 - days + 1).ToString("yyyy-MM-dd");
  58. }
  59. else
  60. {
  61. endDate = now.AddDays(-1).ToString("yyyy-MM-dd");
  62. startDate = now.AddDays(-1 - days + 1).ToString("yyyy-MM-dd");
  63. }
  64. return new DateRange
  65. {
  66. startDate = startDate,
  67. endDate = endDate,
  68. };
  69. }
  70. }
  71. }