TestHelper.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Threading;
  3. using System.Linq;
  4. using System.Data;
  5. using Library;
  6. using MySystem.Models;
  7. using System.Collections.Generic;
  8. namespace MySystem
  9. {
  10. public class TestHelper
  11. {
  12. public readonly static TestHelper Instance = new TestHelper();
  13. private TestHelper()
  14. {
  15. }
  16. public void Start()//启动
  17. {
  18. Thread thread = new Thread(ImportPostDo);
  19. thread.IsBackground = true;
  20. thread.Start();
  21. Thread thread2 = new Thread(ImportPost2Do);
  22. thread2.IsBackground = true;
  23. thread2.Start();
  24. Thread thread3 = new Thread(ImportPost3Do);
  25. thread3.IsBackground = true;
  26. thread3.Start();
  27. }
  28. public void ImportPostDo()
  29. {
  30. while (true)
  31. {
  32. string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:1");
  33. if (!string.IsNullOrEmpty(data))
  34. {
  35. int count = int.Parse(function.CheckInt(data));
  36. if (count > 0)
  37. {
  38. MakerCoupons(1, count);
  39. }
  40. }
  41. else
  42. {
  43. Thread.Sleep(10000);
  44. }
  45. }
  46. }
  47. public void ImportPost2Do()
  48. {
  49. while (true)
  50. {
  51. string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:2");
  52. if (!string.IsNullOrEmpty(data))
  53. {
  54. int count = int.Parse(function.CheckInt(data));
  55. if (count > 0)
  56. {
  57. MakerCoupons(2, count);
  58. }
  59. }
  60. else
  61. {
  62. Thread.Sleep(10000);
  63. }
  64. }
  65. }
  66. public void ImportPost3Do()
  67. {
  68. while (true)
  69. {
  70. string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:3");
  71. if (!string.IsNullOrEmpty(data))
  72. {
  73. int count = int.Parse(function.CheckInt(data));
  74. if (count > 0)
  75. {
  76. MakerCoupons(3, count);
  77. }
  78. }
  79. else
  80. {
  81. Thread.Sleep(10000);
  82. }
  83. }
  84. }
  85. //生成券,Kind:1-电签券,2-大机券
  86. public void MakerCoupons(int Kind = 1, int count = 0)
  87. {
  88. try
  89. {
  90. WebCMSEntities db = new WebCMSEntities();
  91. string StartCode = "0" + Kind + "00000000";
  92. if(db.PosCoupons.Any(m => m.QueryCount == Kind))
  93. {
  94. StartCode = db.PosCoupons.Where(m => m.QueryCount == Kind).OrderByDescending(m => m.Id).FirstOrDefault().ExchangeCode;
  95. }
  96. int QueryCount = Kind;
  97. Kind += 1;
  98. string title = "0" + Kind.ToString();
  99. int start = int.Parse(function.CheckInt(StartCode.Substring(2).TrimStart('0'))) + 1;
  100. if (start > 0)
  101. {
  102. int end = start + count - 1;
  103. for (int i = start; i <= end; i++)
  104. {
  105. string Code = i.ToString();
  106. for (int j = 0; j < 8 - i.ToString().Length; j++)
  107. {
  108. Code = "0" + Code;
  109. }
  110. Code = title + Code;
  111. bool check = db.PosCoupons.Any(m => m.ExchangeCode == Code);
  112. if (!check)
  113. {
  114. db.PosCoupons.Add(new PosCoupons()
  115. {
  116. ExchangeCode = Code,
  117. QueryCount = QueryCount,
  118. });
  119. db.SaveChanges();
  120. }
  121. }
  122. }
  123. db.Dispose();
  124. }
  125. catch (Exception ex)
  126. {
  127. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "生成兑换券异常");
  128. }
  129. }
  130. }
  131. }