SycnSpBindService.cs 8.4 KB

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