WifiTradeHelper.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Linq;
  5. using System.Data;
  6. using MySystem;
  7. using MySystem.PxcModels;
  8. using Library;
  9. using LitJson;
  10. public class WifiTradeHelper
  11. {
  12. public readonly static WifiTradeHelper Instance = new WifiTradeHelper();
  13. private WifiTradeHelper()
  14. {
  15. }
  16. public void Start()
  17. {
  18. Thread th = new Thread(DoWorks);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. private void DoWorks()
  23. {
  24. while (true)
  25. {
  26. try
  27. {
  28. string Month = DateTime.Now.ToString("yyyyMM");
  29. string chk = function.ReadInstance("/wifi/trade" + Month + ".txt");
  30. if(string.IsNullOrEmpty(chk))
  31. {
  32. function.WritePage("/wifi/", "trade" + Month + ".txt", DateTime.Now.ToString());
  33. DoSomething(Month);
  34. }
  35. Thread.Sleep(3600000);
  36. }
  37. catch (Exception ex)
  38. {
  39. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "WIFI发放交易额异常");
  40. }
  41. }
  42. }
  43. public void DoSomething(string Month)
  44. {
  45. WebCMSEntities db = new WebCMSEntities();
  46. List<WifiTradeRecord> trades = db.WifiTradeRecord.Where(m => m.Duration > m.DoMonths && m.LastMonth != Month).ToList();
  47. foreach(WifiTradeRecord trade in trades)
  48. {
  49. if(trade.Unit == "m" && trade.Duration > 0)
  50. {
  51. decimal TradeAmt = trade.TradeAmount / trade.Duration;
  52. DateTime StartMonth = DateTime.Parse(DateTime.Now.ToString("yyyy-MM") + "-01 00:00:00");
  53. PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == trade.SnNo) ?? new PosMachinesTwo();
  54. SycnSpTradeWifiService.Instance.StatTrade(db, pos.BuyUserId, pos.BrandId, StartMonth.ToString("yyyyMM"), TradeAmt);
  55. if (pos.BindingTime < trade.CreateDate.Value.AddMonths(-9))
  56. {
  57. SycnSpTradeWifiService.Instance.StatTradeBefore(db, pos.BuyUserId, pos.BrandId, StartMonth.ToString("yyyyMM"), TradeAmt);
  58. }
  59. else
  60. {
  61. SycnSpTradeWifiService.Instance.StatTradeAfter(db, pos.BuyUserId, pos.BrandId, StartMonth.ToString("yyyyMM"), TradeAmt);
  62. }
  63. WifiTradeRecord edit = db.WifiTradeRecord.FirstOrDefault(m => m.Id == trade.Id);
  64. if(edit != null)
  65. {
  66. edit.DoMonths += 1;
  67. edit.LastMonth = Month;
  68. }
  69. }
  70. db.SaveChanges();
  71. }
  72. db.Dispose();
  73. }
  74. }