TestHelper.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. Thread thread4 = new Thread(ImportVirtualCard);
  28. thread4.IsBackground = true;
  29. thread4.Start();
  30. }
  31. public void ImportPostDo()
  32. {
  33. while (true)
  34. {
  35. string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:1");
  36. if (!string.IsNullOrEmpty(data))
  37. {
  38. int count = int.Parse(function.CheckInt(data));
  39. if (count > 0)
  40. {
  41. MakerCoupons(1, count);
  42. }
  43. }
  44. else
  45. {
  46. Thread.Sleep(10000);
  47. }
  48. }
  49. }
  50. public void ImportPost2Do()
  51. {
  52. while (true)
  53. {
  54. string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:2");
  55. if (!string.IsNullOrEmpty(data))
  56. {
  57. int count = int.Parse(function.CheckInt(data));
  58. if (count > 0)
  59. {
  60. MakerCoupons(2, count);
  61. }
  62. }
  63. else
  64. {
  65. Thread.Sleep(10000);
  66. }
  67. }
  68. }
  69. public void ImportPost3Do()
  70. {
  71. while (true)
  72. {
  73. string data = RedisDbconn.Instance.RPop<string>("MakerCouponsQueue:3");
  74. if (!string.IsNullOrEmpty(data))
  75. {
  76. int count = int.Parse(function.CheckInt(data));
  77. if (count > 0)
  78. {
  79. MakerCoupons(3, count);
  80. }
  81. }
  82. else
  83. {
  84. Thread.Sleep(10000);
  85. }
  86. }
  87. }
  88. //生成券,Kind:1-电签券,2-大机券
  89. public void MakerCoupons(int Kind = 1, int count = 0)
  90. {
  91. try
  92. {
  93. WebCMSEntities db = new WebCMSEntities();
  94. string StartCode = "0" + Kind + "00000000";
  95. if(db.PosCoupons.Any(m => m.QueryCount == Kind))
  96. {
  97. StartCode = db.PosCoupons.Where(m => m.QueryCount == Kind).OrderByDescending(m => m.Id).FirstOrDefault().ExchangeCode;
  98. }
  99. int QueryCount = Kind;
  100. Kind += 1;
  101. string title = "0" + Kind.ToString();
  102. int start = int.Parse(function.CheckInt(StartCode.Substring(2).TrimStart('0'))) + 1;
  103. if (start > 0)
  104. {
  105. int end = start + count - 1;
  106. for (int i = start; i <= end; i++)
  107. {
  108. string Code = i.ToString();
  109. for (int j = 0; j < 8 - i.ToString().Length; j++)
  110. {
  111. Code = "0" + Code;
  112. }
  113. Code = title + Code;
  114. bool check = db.PosCoupons.Any(m => m.ExchangeCode == Code);
  115. if (!check)
  116. {
  117. db.PosCoupons.Add(new PosCoupons()
  118. {
  119. ExchangeCode = Code,
  120. QueryCount = QueryCount,
  121. });
  122. db.SaveChanges();
  123. }
  124. }
  125. }
  126. db.Dispose();
  127. }
  128. catch (Exception ex)
  129. {
  130. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "生成兑换券异常");
  131. }
  132. }
  133. //生成虚拟广电卡SN
  134. public void ImportVirtualCard()
  135. {
  136. while (true)
  137. {
  138. string data = RedisDbconn.Instance.RPop<string>("VirtualCardQueue");
  139. if (!string.IsNullOrEmpty(data))
  140. {
  141. int count = int.Parse(function.CheckInt(data));
  142. if (count > 0)
  143. {
  144. MakerVirtualCard(count);
  145. }
  146. }
  147. else
  148. {
  149. Thread.Sleep(10000);
  150. }
  151. }
  152. }
  153. public void MakerVirtualCard(int count = 0)
  154. {
  155. try
  156. {
  157. string BatchNo = DateTime.Now.ToString("yyyyMMddHHmmss");
  158. WebCMSEntities db = new WebCMSEntities();
  159. string StartCode = "CS00000000000001";
  160. if(db.PosMachinesTwo.Any(m => m.BrandId == 14 && m.PosSn.StartsWith("CS")))
  161. {
  162. StartCode = db.PosMachinesTwo.Where(m => m.BrandId == 14 && m.PosSn.StartsWith("CS")).OrderByDescending(m => m.Id).FirstOrDefault().PosSn;
  163. }
  164. string title = "CS";
  165. int start = int.Parse(function.CheckInt(StartCode.Substring(2).TrimStart('0'))) + 1;
  166. if (start > 0)
  167. {
  168. int end = start + count - 1;
  169. for (int i = start; i <= end; i++)
  170. {
  171. string Code = i.ToString();
  172. for (int j = 0; j < 14 - i.ToString().Length; j++)
  173. {
  174. Code = "0" + Code;
  175. }
  176. Code = title + Code;
  177. bool check = db.PosMachinesTwo.Any(m => m.PosSn == Code);
  178. if (!check)
  179. {
  180. PosMachinesTwo pos = db.PosMachinesTwo.Add(new PosMachinesTwo()
  181. {
  182. CreateDate = DateTime.Now,
  183. PosSn = Code, //SN编号
  184. BrandId = 14, //产品类型
  185. StoreId = 8664, //所在仓库
  186. DeviceType = "dianqu", //设备类型
  187. BatchNo = BatchNo,
  188. DeviceName = "电渠",
  189. DeviceKind = "电渠虚拟",
  190. SourceStoreId = 8664, //源仓库
  191. CardType = 2,
  192. OutBatchNo = "线上",
  193. }).Entity;
  194. db.SaveChanges();
  195. }
  196. }
  197. }
  198. db.Dispose();
  199. }
  200. catch (Exception ex)
  201. {
  202. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "生成电渠广电虚拟卡异常");
  203. }
  204. }
  205. }
  206. }