SycnSpChangeBindService.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 SycnSpChangeBindService
  11. {
  12. public readonly static SycnSpChangeBindService Instance = new SycnSpChangeBindService();
  13. private SycnSpChangeBindService()
  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(-30);
  30. List<ChangeBindRecord> Binds = spdb.ChangeBindRecord.Where(m => m.ReBindTime >= start && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  31. foreach (ChangeBindRecord Bind in Binds)
  32. {
  33. var tran = db.Database.BeginTransaction();
  34. try
  35. {
  36. string OldSnNum = Bind.MerSnNo;
  37. string NewSnNum = Bind.MerNewSnNo;
  38. string MerNo = Bind.MerNo;
  39. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == MerNo) ?? new PxcModels.PosMerchantInfo();
  40. PxcModels.MachineForSnNo oldForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == OldSnNum) ?? new PxcModels.MachineForSnNo();
  41. PxcModels.MachineForSnNo newForSnNo = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == NewSnNum) ?? new PxcModels.MachineForSnNo();
  42. PxcModels.PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == oldForSnNo.SnId);
  43. PxcModels.PosMachinesTwo newpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == newForSnNo.SnId);
  44. PxcModels.StoreHouse BackStore = db.StoreHouse.FirstOrDefault(m => m.Id == oldpos.StoreId) ?? new PxcModels.StoreHouse();
  45. PxcModels.StoreHouse OutStore = db.StoreHouse.FirstOrDefault(m => m.Id == newpos.StoreId) ?? new PxcModels.StoreHouse();
  46. string ChangeNo = "BMC" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(3);
  47. PxcModels.MachineChange add = db.MachineChange.Add(new PxcModels.MachineChange()
  48. {
  49. CreateDate = DateTime.Now,
  50. UpdateDate = DateTime.Now,
  51. ChangeNo = ChangeNo, //转换单号
  52. UserId = 0, //创客
  53. ChangeTime = DateTime.Now, //转换时间
  54. BackStoreId = BackStore.Id, //退回仓库
  55. BackStoreName = BackStore.StoreName, //退回仓库名称
  56. Remark = "机具更换", //订单备注
  57. BackStoreUserId = BackStore.UserId, //退回仓库归属人
  58. OutProductType = int.Parse(OutStore.BrandId), //出库产品类型
  59. OutProductName = OutStore.ProductName, //出库产品名称
  60. OutStoreId = OutStore.Id, //出库仓库
  61. OutStoreName = OutStore.StoreName, //出库仓库名称
  62. OutStoreAreas = OutStore.Areas, //出库仓库所在地区
  63. OutStoreAddress = OutStore.Address, //出库仓库地址
  64. OutStoreManager = "", //出库仓库联系人
  65. OutStoreManagerMobile = OutStore.ManageMobile, //出库仓库联系人手机号
  66. }).Entity;
  67. db.SaveChanges();
  68. int BackProductType = 0;
  69. string BackProductName = "";
  70. string ChangeDeviceName = "";
  71. string ChangeSnExpand = "";
  72. PxcModels.KqProducts oldPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == oldpos.BrandId) ?? new PxcModels.KqProducts();
  73. PxcModels.KqProducts newPosBrand = db.KqProducts.FirstOrDefault(m => m.Id == newpos.BrandId) ?? new PxcModels.KqProducts();
  74. newpos.BindMerchantId = merchant.Id;
  75. newpos.BuyUserId = oldpos.BuyUserId;
  76. newpos.UserId = oldpos.UserId;
  77. newpos.RecycEndDate = oldpos.RecycEndDate;
  78. newpos.ScanQrTrade = oldpos.ScanQrTrade;
  79. newpos.DebitCardTrade = oldpos.DebitCardTrade;
  80. newpos.CreditTrade = oldpos.CreditTrade;
  81. newpos.PosSnType = oldpos.PosSnType;
  82. newpos.TransferTime = oldpos.TransferTime;
  83. newpos.IsPurchase = oldpos.IsPurchase;
  84. newpos.BindingState = oldpos.BindingState;
  85. newpos.ActivationState = oldpos.ActivationState;
  86. newpos.BindingTime = oldpos.BindingTime;
  87. newpos.ActivationTime = oldpos.ActivationTime;
  88. newpos.IsFirst = oldpos.IsFirst;
  89. // bool checkActReward = db.ActiveReward.Any(m => m.KqMerNo == merchant.KqMerNo);
  90. // if (!checkActReward)
  91. // {
  92. newpos.SeoKeyword = oldpos.SeoKeyword;
  93. newpos.PrizeParams = oldpos.PrizeParams;
  94. newpos.LeaderUserId= oldpos.LeaderUserId;
  95. // }
  96. // oldpos.BuyUserId = 0;
  97. // oldpos.UserId = 0;
  98. oldpos.Status = -1;
  99. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.KqMerNo);
  100. if (forMerNo != null)
  101. {
  102. forMerNo.SnId = newpos.Id;
  103. }
  104. merchant.KqSnNo = NewSnNum;
  105. db.MachineChangeDetail.Add(new PxcModels.MachineChangeDetail()
  106. {
  107. CreateDate = DateTime.Now,
  108. UpdateDate = DateTime.Now,
  109. ChangeNo = ChangeNo, //订单号
  110. ChangeId = add.Id, //订单Id
  111. BackProductType = oldpos.BrandId, //退回产品类型
  112. BackProductName = oldPosBrand.Name, //退回产品名称
  113. UserId = 0, //创客
  114. BackSnNo = oldpos.PosSn, //设备SN编号
  115. OutProductType = newpos.BrandId, //出库产品类型
  116. OutProductName = newPosBrand.Name, //出库产品名称
  117. OutSnNo = newpos.PosSn, //出库设备SN编号
  118. OutSnType = newpos.PosSnType, //出库SN机具类型
  119. Remark = "机具更换", //备注
  120. BackSnType = oldpos.PosSnType, //退回SN机具类型
  121. });
  122. db.SaveChanges();
  123. BackProductType = oldpos.BrandId;
  124. BackProductName = newPosBrand.Name;
  125. ChangeDeviceName = oldpos.DeviceName;
  126. ChangeSnExpand = oldpos.PosSn;
  127. add.BackProductType = BackProductType; //退回产品类型
  128. add.BackProductName = BackProductName; //退回产品名称
  129. add.ChangeDeviceName = ChangeDeviceName; //转换机具名称
  130. add.ChangeDeviceNum = 1; //转换机具数量
  131. add.ChangeSnExpand = ChangeSnExpand; //机具SN
  132. db.SaveChanges();
  133. ChangeBindRecord edit = spdb.ChangeBindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  134. if (edit != null)
  135. {
  136. edit.Status = 2;
  137. spdb.SaveChanges();
  138. }
  139. tran.Commit();
  140. }
  141. catch (Exception ex)
  142. {
  143. tran.Rollback();
  144. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP换绑数据到MAIN异常");
  145. }
  146. tran.Dispose();
  147. }
  148. spdb.Dispose();
  149. db.Dispose();
  150. }
  151. catch (Exception ex)
  152. {
  153. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP换绑数据到MAIN异常");
  154. }
  155. Thread.Sleep(1000);
  156. }
  157. }
  158. }
  159. }