PosMerchantTradeSummaryDbconn.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using Library;
  4. using System.Linq;
  5. using MySystem.MainModels;
  6. namespace MySystem
  7. {
  8. public class PosMerchantTradeSummaryDbconn
  9. {
  10. public readonly static PosMerchantTradeSummaryDbconn Instance = new PosMerchantTradeSummaryDbconn();
  11. public PosMerchantTradeSummaryDbconn()
  12. { }
  13. // TODO:读取很慢,感觉redis没生效
  14. #region 获取单个字段
  15. public decimal GetTrade(int MerchantId)
  16. {
  17. string key = "PosMerchantAmount:" + MerchantId; //总交易
  18. string obj = RedisDbconn.Instance.Get<string>(key);
  19. if (!string.IsNullOrEmpty(obj))
  20. {
  21. return decimal.Parse(function.CheckNum(obj));
  22. }
  23. decimal amt = 0;
  24. WebCMSEntities db = new WebCMSEntities();
  25. bool check = db.PosMerchantTradeSummay.Any(m => m.MerchantId == MerchantId);
  26. if (check)
  27. {
  28. amt = db.PosMerchantTradeSummay.Where(m => m.MerchantId == MerchantId).Sum(m => m.TradeAmount);
  29. }
  30. RedisDbconn.Instance.Set(key, amt.ToString());
  31. RedisDbconn.Instance.SetExpire(key, function.get_Random(3600, 7200));
  32. db.Dispose();
  33. return amt;
  34. }
  35. public decimal GetDateTimeTrade(int MerchantId, string TradeDate)
  36. {
  37. if (DateTime.Now.Day == 1 && TradeDate.Length == 6)
  38. {
  39. TradeDate = DateTime.Now.AddMonths(-1).ToString("yyyyMM");
  40. }
  41. string key = "PosMerchantAmount:" + MerchantId + ":" + TradeDate; //总交易
  42. string obj = RedisDbconn.Instance.Get<string>(key);
  43. if (!string.IsNullOrEmpty(obj))
  44. {
  45. return decimal.Parse(function.CheckNum(obj));
  46. }
  47. decimal amt = 0;
  48. WebCMSEntities db = new WebCMSEntities();
  49. if (TradeDate.Length == 8)
  50. {
  51. bool check = db.PosMerchantTradeSummay.Any(m => m.MerchantId == MerchantId && m.TradeDate == TradeDate);
  52. if (check)
  53. {
  54. amt = db.PosMerchantTradeSummay.Where(m => m.MerchantId == MerchantId && m.TradeDate == TradeDate).Sum(m => m.TradeAmount);
  55. }
  56. RedisDbconn.Instance.Set(key, amt.ToString());
  57. }
  58. else
  59. {
  60. bool check = db.PosMerchantTradeSummay.Any(m => m.MerchantId == MerchantId && m.TradeMonth == TradeDate);
  61. if (check)
  62. {
  63. amt = db.PosMerchantTradeSummay.Where(m => m.MerchantId == MerchantId && m.TradeMonth == TradeDate).Sum(m => m.TradeAmount);
  64. }
  65. RedisDbconn.Instance.Set(key, amt.ToString());
  66. }
  67. RedisDbconn.Instance.SetExpire(key, function.get_Random(3600, 7200));
  68. db.Dispose();
  69. return amt;
  70. }
  71. #endregion
  72. }
  73. }