SycnSpBindService.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 SimDo(RadioCardItem Bind)
  16. {
  17. try
  18. {
  19. WebCMSEntities spdb = new WebCMSEntities();
  20. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  21. DateTime start = DateTime.Now.AddDays(-60);
  22. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.iccId) ?? new PxcModels.MachineForSnNo();
  23. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  24. if (pos != null)
  25. {
  26. string KindName = Bind.productTypeName; //商品类型名称
  27. string Name = Bind.productName; //商品名称
  28. pos.BindingState = 1;
  29. pos.BindingTime = DateTime.Parse(Bind.orderCreateTime);
  30. pos.Status = 0;
  31. pos.IsFirst = 1;
  32. if(Name == "激活后补卡")
  33. {
  34. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.phoneNo);
  35. if(forMerNo != null)
  36. {
  37. PxcModels.PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId);
  38. if(oldpos != null)
  39. {
  40. oldpos.IsFirst = 0;
  41. pos.BindingState = oldpos.BindingState;
  42. pos.BindingTime = oldpos.BindingTime;
  43. pos.ActivationState = oldpos.ActivationState;
  44. pos.ActivationTime = oldpos.ActivationTime;
  45. }
  46. }
  47. }
  48. decimal ActualAmount = Bind.paidInAmount; //实付金额
  49. string ActStatus = Bind.activationStatusName; //激活状态名称
  50. if (KindName == "基础套餐" && Name == "双百套餐" && ActStatus == "已激活" && ActualAmount >= 100)
  51. {
  52. pos.Detail = "0";
  53. }
  54. else
  55. {
  56. pos.Detail = "1";
  57. }
  58. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.phoneNo);
  59. if (merFor == null)
  60. {
  61. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  62. {
  63. MerNo = Bind.phoneNo,
  64. SnId = pos.Id,
  65. }).Entity;
  66. }
  67. else
  68. {
  69. merFor.SnId = pos.Id;
  70. }
  71. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.phoneNo && m.KqSnNo == Bind.iccId);
  72. if (add == null)
  73. {
  74. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  75. {
  76. CreateDate = DateTime.Parse(Bind.orderCreateTime),
  77. KqMerNo = Bind.phoneNo,
  78. KqSnNo = Bind.iccId,
  79. MerchantNo = Bind.phoneNo.Length > 16 ? Bind.phoneNo.Substring(0, 16) : Bind.phoneNo,
  80. }).Entity;
  81. db.SaveChanges();
  82. }
  83. string json = Newtonsoft.Json.JsonConvert.SerializeObject(Bind);
  84. string ProductName = ""; //商品名称
  85. string ActivityName = ""; //促销活动名称
  86. string ProductTypeName = ""; //商品类型名称
  87. string ActStatusName = ""; //激活状态名称
  88. string ActAmount = ""; //实付金额
  89. string EmployeeNo = ""; //员工工号
  90. string EmployeeName = ""; //员工姓名
  91. if(!string.IsNullOrEmpty(json))
  92. {
  93. JsonData jsonObj = JsonMapper.ToObject(json);
  94. ProductName = jsonObj["productName"].ToString();
  95. ProductTypeName = jsonObj["productTypeName"].ToString();
  96. ActAmount = jsonObj["paidInAmount"].ToString();
  97. ActStatusName = jsonObj["activationStatusName"].ToString();
  98. EmployeeNo = jsonObj["jobId"].ToString();
  99. }
  100. PxcModels.SimposMerchantInfo sim = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.phoneNo);
  101. if (sim == null)
  102. {
  103. sim = db.SimposMerchantInfo.Add(new PxcModels.SimposMerchantInfo()
  104. {
  105. Id = add.Id,
  106. CreateDate = DateTime.Parse(Bind.orderCreateTime),
  107. KqMerNo = Bind.phoneNo,
  108. MerchantNo = Bind.phoneNo.Length > 16 ? Bind.phoneNo.Substring(0, 16) : Bind.phoneNo,
  109. }).Entity;
  110. db.SaveChanges();
  111. }
  112. sim.KqSnNo = Bind.iccId;
  113. sim.ProductName = ProductName;
  114. sim.ActivityName = ActivityName;
  115. sim.ProductTypeName = ProductTypeName;
  116. sim.ActStatusName = ActStatusName;
  117. sim.ActAmount = decimal.Parse(ActAmount);
  118. sim.EmployeeNo = EmployeeNo;
  119. sim.EmployeeName = EmployeeName;
  120. pos.BindMerchantId = add.Id;
  121. pos.LastMerchantId = add.Id;
  122. if(pos.CardType < 2)
  123. {
  124. pos.IsPurchase = 1;
  125. }
  126. // sim.UpdateDate = Bind.UpdateTime;
  127. sim.TopUserId = 1;
  128. sim.BrandId = pos.BrandId;
  129. sim.SnStoreId = pos.StoreId;
  130. if(ActStatus == "已激活")
  131. {
  132. sim.MerStandardDate = DateTime.Now;
  133. sim.ActiveStatus = 1;
  134. }
  135. sim.UserId = pos.UserId;
  136. // sim.MgrName = Bind.MerName;
  137. sim.MerStatus = 1;
  138. sim.KqSnNo = pos.PosSn;
  139. // sim.MerRealName = Bind.MerName;
  140. sim.MerchantMobile = Bind.phoneNo;
  141. sim.MerchantName = Bind.phoneNo;
  142. sim.Type = pos.CardType + 1;
  143. db.SaveChanges();
  144. string IdBrand = pos.UserId + "_" + pos.BrandId;
  145. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  146. if (MachineData == null)
  147. {
  148. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  149. {
  150. IdBrand = IdBrand,
  151. }).Entity;
  152. db.SaveChanges();
  153. }
  154. MachineData.BindCount += 1;
  155. MachineData.UnBindCount -= 1;
  156. if(Bind.activationStatusName == "已激活")
  157. {
  158. bool PrizeFlag = pos.CardType > 0;
  159. SycnSpTradeService.Instance.ActPos(db, pos, 0, 30, PrizeFlag, Bind.activationTime);
  160. }
  161. db.SaveChanges();
  162. }
  163. spdb.SaveChanges();
  164. spdb.Dispose();
  165. db.SaveChanges();
  166. db.Dispose();
  167. }
  168. catch (Exception ex)
  169. {
  170. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  171. }
  172. }
  173. }
  174. }