TestHelper.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. }
  25. public void ImportPostDo()
  26. {
  27. while (true)
  28. {
  29. string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:1");
  30. if (!string.IsNullOrEmpty(data))
  31. {
  32. int count = int.Parse(function.CheckInt(data));
  33. if (count > 0)
  34. {
  35. MakerCoupons(1, count);
  36. }
  37. }
  38. else
  39. {
  40. Thread.Sleep(10000);
  41. }
  42. }
  43. }
  44. public void ImportPost2Do()
  45. {
  46. while (true)
  47. {
  48. string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:2");
  49. if (!string.IsNullOrEmpty(data))
  50. {
  51. int count = int.Parse(function.CheckInt(data));
  52. if (count > 0)
  53. {
  54. MakerCoupons(2, count);
  55. }
  56. }
  57. else
  58. {
  59. Thread.Sleep(10000);
  60. }
  61. }
  62. }
  63. //生成券,Kind:1-电签券,2-大机券
  64. public void MakerCoupons(int Kind = 1, int count = 0)
  65. {
  66. try
  67. {
  68. WebCMSEntities db = new WebCMSEntities();
  69. string StartCode = db.PosCoupons.Where(m => m.QueryCount == Kind).OrderByDescending(m => m.Id).FirstOrDefault().ExchangeCode;
  70. int QueryCount = Kind;
  71. Kind += 1;
  72. string title = "0" + Kind.ToString();
  73. int start = int.Parse(function.CheckInt(StartCode.Substring(2).TrimStart('0'))) + 1;
  74. if (start > 0)
  75. {
  76. int end = start + count - 1;
  77. for (int i = start; i <= end; i++)
  78. {
  79. string Code = i.ToString();
  80. for (int j = 0; j < 8 - i.ToString().Length; j++)
  81. {
  82. Code = "0" + Code;
  83. }
  84. Code = title + Code;
  85. bool check = db.PosCoupons.Any(m => m.ExchangeCode == Code);
  86. if (!check)
  87. {
  88. db.PosCoupons.Add(new PosCoupons()
  89. {
  90. ExchangeCode = Code,
  91. QueryCount = QueryCount,
  92. });
  93. db.SaveChanges();
  94. }
  95. }
  96. }
  97. db.Dispose();
  98. }
  99. catch (Exception ex)
  100. {
  101. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "生成兑换券异常");
  102. }
  103. }
  104. }
  105. }