SycnSpBindService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MySystem.SpModels;
  5. using Library;
  6. using LitJson;
  7. using System.Threading;
  8. namespace MySystem
  9. {
  10. public class SycnSpBindService
  11. {
  12. public readonly static SycnSpBindService Instance = new SycnSpBindService();
  13. private SycnSpBindService()
  14. { }
  15. public void Start()
  16. {
  17. Thread th = new Thread(StartDo);
  18. th.IsBackground = true;
  19. th.Start();
  20. }
  21. public void StartDo()
  22. {
  23. while (true)
  24. {
  25. try
  26. {
  27. WebCMSEntities spdb = new WebCMSEntities();
  28. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  29. DateTime start = DateTime.Now.AddDays(-5);
  30. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  31. var Binds = spdb.BindRecord.Select(m => new { m.Id, m.CreateTime, m.Status, m.MerNo, m.MerSnNo, m.ProductType, m.Remark, m.Field1, m.Field2, m.Field3, m.Field5 }).Where(m => m.Id >= StartId && m.CreateTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  32. foreach (var Bind in Binds)
  33. {
  34. var tran = db.Database.BeginTransaction();
  35. try
  36. {
  37. if(Bind.Field1 == "解绑" && Bind.ProductType == "1")
  38. {
  39. // 删除对应商户,激活机具除外
  40. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == Bind.MerSnNo) ?? new PxcModels.PosMachinesTwo();
  41. if(pos.ActivationState == 0)
  42. {
  43. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  44. if (forMerNo != null)
  45. {
  46. db.MachineForMerNo.Remove(forMerNo);
  47. db.SaveChanges();
  48. }
  49. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  50. if(merchant != null)
  51. {
  52. //备份解绑的商户信息
  53. CustomerSqlConn.op("insert into PosMerchantInfoBak select * from PosMerchantInfo where Id=" + pos.BindMerchantId, MysqlConn.SqlConnStr);
  54. db.SaveChanges();
  55. pos.BindMerchantId = 0;
  56. pos.BindingState = 0;
  57. pos.BindingTime = null;
  58. db.PosMerchantInfo.Remove(merchant);
  59. db.SaveChanges();
  60. }
  61. string IdBrand = pos.BuyUserId + "_" + pos.BrandId;
  62. PxcModels.UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  63. if (userData == null)
  64. {
  65. userData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  66. {
  67. IdBrand = IdBrand,
  68. }).Entity;
  69. db.SaveChanges();
  70. }
  71. userData.BindCount -= 1;
  72. userData.UnBindCount += 1;
  73. db.SaveChanges();
  74. }
  75. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  76. if (edit != null)
  77. {
  78. edit.Status = 2;
  79. spdb.SaveChanges();
  80. }
  81. }
  82. else
  83. {
  84. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  85. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  86. if (pos != null)
  87. {
  88. pos.BindingState = 1;
  89. pos.BindingTime = Bind.CreateTime;
  90. pos.Status = 0;
  91. //广电卡逻辑
  92. if (pos.BrandId == 14 && (Bind.Field2.Contains("副卡") || Bind.Field3.Contains("副卡")))
  93. {
  94. pos.Detail = "1";
  95. }
  96. else
  97. {
  98. if (Bind.Field3.Contains("元"))
  99. {
  100. string[] data = Bind.Field3.Split(new string[] { "元" }, StringSplitOptions.None);
  101. string dataTmp = data[0];
  102. string resultNum = "";
  103. for (int i = dataTmp.Length - 1; i >= 0; i--)
  104. {
  105. string checkStr = dataTmp.Substring(i, 1);
  106. if (function.IsInt(checkStr) || checkStr == ".")
  107. {
  108. resultNum = checkStr + resultNum;
  109. }
  110. else
  111. {
  112. break;
  113. }
  114. }
  115. decimal Deposit = decimal.Parse(function.CheckNum(resultNum));
  116. if (Deposit > 0)
  117. {
  118. pos.SeoKeyword = Deposit.ToString();
  119. }
  120. else
  121. {
  122. pos.Detail = "1";
  123. }
  124. }
  125. else
  126. {
  127. pos.Detail = "1";
  128. }
  129. }
  130. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  131. if (merFor == null)
  132. {
  133. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  134. {
  135. MerNo = Bind.MerNo,
  136. SnId = pos.Id,
  137. }).Entity;
  138. }
  139. else
  140. {
  141. merFor.SnId = pos.Id;
  142. }
  143. db.SaveChanges();
  144. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  145. if (edit != null)
  146. {
  147. edit.Status = 2;
  148. spdb.SaveChanges();
  149. }
  150. string IdBrand = pos.UserId + "_" + pos.BrandId;
  151. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  152. if (MachineData == null)
  153. {
  154. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  155. {
  156. IdBrand = IdBrand,
  157. }).Entity;
  158. db.SaveChanges();
  159. }
  160. MachineData.BindCount += 1;
  161. MachineData.UnBindCount -= 1;
  162. db.SaveChanges();
  163. if(pos.BrandId == 14 && Bind.Field5 == "已激活")
  164. {
  165. SycnSpTradeService.Instance.ActPos(db, pos, 0, 30);
  166. }
  167. if(pos.BuyUserId == 0 && pos.PreUserId > 0)
  168. {
  169. // RedisDbconn.Instance.Set("CheckUserBindPrePos:" + pos.PreUserId, "您的名下有已绑定的预发机 sn:" + pos.PosSn + " 尚未申请,绑定30分钟内申请即可免费自动补录,请前往客小爽产品中点击机具申请继续完成申请");
  170. RedisDbconn.Instance.AddList("CheckUserBindPrePos:" + pos.PreUserId, pos.PosSn);
  171. int ExpireTime = 60 * 25;
  172. RedisDbconn.Instance.SetExpire("CheckUserBindPrePos:" + pos.PreUserId, ExpireTime);
  173. }
  174. }
  175. }
  176. tran.Commit();
  177. }
  178. catch (Exception ex)
  179. {
  180. tran.Rollback();
  181. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  182. }
  183. tran.Dispose();
  184. }
  185. spdb.SaveChanges();
  186. spdb.Dispose();
  187. db.SaveChanges();
  188. db.Dispose();
  189. }
  190. catch (Exception ex)
  191. {
  192. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  193. }
  194. Thread.Sleep(1000);
  195. }
  196. }
  197. }
  198. }