SycnSpBindService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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, m.SeoTitle }).Where(m => m.Id >= StartId && m.CreateTime >= start && m.ProductType != "14" && 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. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  92. if (merFor == null)
  93. {
  94. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  95. {
  96. MerNo = Bind.MerNo,
  97. SnId = pos.Id,
  98. }).Entity;
  99. }
  100. else
  101. {
  102. merFor.SnId = pos.Id;
  103. }
  104. db.SaveChanges();
  105. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  106. if (edit != null)
  107. {
  108. edit.Status = 2;
  109. spdb.SaveChanges();
  110. }
  111. string IdBrand = pos.UserId + "_" + pos.BrandId;
  112. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  113. if (MachineData == null)
  114. {
  115. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  116. {
  117. IdBrand = IdBrand,
  118. }).Entity;
  119. db.SaveChanges();
  120. }
  121. MachineData.BindCount += 1;
  122. MachineData.UnBindCount -= 1;
  123. db.SaveChanges();
  124. if(pos.BuyUserId == 0 && pos.PreUserId > 0)
  125. {
  126. // RedisDbconn.Instance.Set("CheckUserBindPrePos:" + pos.PreUserId, "您的名下有已绑定的预发机 sn:" + pos.PosSn + " 尚未申请,绑定30分钟内申请即可免费自动补录,请前往客小爽产品中点击机具申请继续完成申请");
  127. RedisDbconn.Instance.AddList("CheckUserBindPrePos:" + pos.PreUserId, pos.PosSn);
  128. int ExpireTime = 60 * 25;
  129. RedisDbconn.Instance.SetExpire("CheckUserBindPrePos:" + pos.PreUserId, ExpireTime);
  130. }
  131. }
  132. }
  133. tran.Commit();
  134. }
  135. catch (Exception ex)
  136. {
  137. tran.Rollback();
  138. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  139. }
  140. tran.Dispose();
  141. }
  142. spdb.SaveChanges();
  143. spdb.Dispose();
  144. db.SaveChanges();
  145. db.Dispose();
  146. }
  147. catch (Exception ex)
  148. {
  149. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  150. }
  151. Thread.Sleep(1000);
  152. }
  153. }
  154. public void StartSim()
  155. {
  156. Thread th = new Thread(StartSimDo);
  157. th.IsBackground = true;
  158. th.Start();
  159. }
  160. public void StartSimDo()
  161. {
  162. while (true)
  163. {
  164. try
  165. {
  166. WebCMSEntities spdb = new WebCMSEntities();
  167. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  168. DateTime start = DateTime.Now.AddDays(-60);
  169. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  170. 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, m.SeoTitle, m.SeoKeyword }).Where(m => m.Id >= StartId && m.CreateTime >= start && m.ProductType == "14" && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  171. foreach (var Bind in Binds)
  172. {
  173. try
  174. {
  175. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  176. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  177. if (pos != null)
  178. {
  179. pos.BindingState = 1;
  180. pos.BindingTime = Bind.CreateTime;
  181. pos.Status = 0;
  182. string KindName = Bind.Remark; //商品类型名称
  183. string Name = Bind.Field3; //商品名称
  184. decimal ActualAmount = decimal.Parse(function.CheckNum(Bind.SeoTitle)); //实付金额
  185. string ActStatus = Bind.Field5; //激活状态名称
  186. if (KindName == "基础套餐" && Name == "双百套餐" && ActStatus == "已激活" && ActualAmount >= 100)
  187. {
  188. pos.Detail = "0";
  189. }
  190. else
  191. {
  192. pos.Detail = "1";
  193. }
  194. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  195. if (merFor == null)
  196. {
  197. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  198. {
  199. MerNo = Bind.MerNo,
  200. SnId = pos.Id,
  201. }).Entity;
  202. }
  203. else
  204. {
  205. merFor.SnId = pos.Id;
  206. }
  207. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.MerNo);
  208. if (add == null)
  209. {
  210. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  211. {
  212. CreateDate = Bind.CreateTime,
  213. KqMerNo = Bind.MerNo,
  214. MerchantNo = Bind.MerNo.Length > 16 ? Bind.MerNo.Substring(0, 16) : Bind.MerNo,
  215. }).Entity;
  216. db.SaveChanges();
  217. }
  218. pos.BindMerchantId = add.Id;
  219. pos.LastMerchantId = add.Id;
  220. pos.IsFirst = 1;
  221. db.SaveChanges();
  222. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  223. if (edit != null)
  224. {
  225. edit.Status = 2;
  226. spdb.SaveChanges();
  227. }
  228. string IdBrand = pos.UserId + "_" + pos.BrandId;
  229. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  230. if (MachineData == null)
  231. {
  232. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  233. {
  234. IdBrand = IdBrand,
  235. }).Entity;
  236. db.SaveChanges();
  237. }
  238. MachineData.BindCount += 1;
  239. MachineData.UnBindCount -= 1;
  240. if(Bind.Field5 == "已激活")
  241. {
  242. bool PrizeFlag = pos.Detail != "1";
  243. if(pos.IsPurchase == 1)
  244. {
  245. PrizeFlag = false;
  246. }
  247. SycnSpTradeService.Instance.ActPos(db, pos, 0, 30, PrizeFlag, Bind.SeoKeyword);
  248. }
  249. db.SaveChanges();
  250. }
  251. }
  252. catch (Exception ex)
  253. {
  254. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  255. }
  256. }
  257. spdb.SaveChanges();
  258. spdb.Dispose();
  259. db.SaveChanges();
  260. db.Dispose();
  261. }
  262. catch (Exception ex)
  263. {
  264. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  265. }
  266. Thread.Sleep(1000);
  267. }
  268. }
  269. }
  270. }