SycnSpBindService.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. pos.CardType = 2;
  183. string KindName = Bind.Remark; //商品类型名称
  184. string Name = Bind.Field3; //商品名称
  185. decimal ActualAmount = decimal.Parse(function.CheckNum(Bind.SeoTitle)); //实付金额
  186. string ActStatus = Bind.Field5; //激活状态名称
  187. if (KindName == "基础套餐" && Name == "双百套餐" && ActStatus == "已激活" && ActualAmount >= 100)
  188. {
  189. pos.Detail = "0";
  190. }
  191. else
  192. {
  193. pos.Detail = "1";
  194. }
  195. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  196. if (merFor == null)
  197. {
  198. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  199. {
  200. MerNo = Bind.MerNo,
  201. SnId = pos.Id,
  202. }).Entity;
  203. }
  204. else
  205. {
  206. merFor.SnId = pos.Id;
  207. }
  208. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.MerNo);
  209. if (add == null)
  210. {
  211. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  212. {
  213. CreateDate = Bind.CreateTime,
  214. KqMerNo = Bind.MerNo,
  215. MerchantNo = Bind.MerNo.Length > 16 ? Bind.MerNo.Substring(0, 16) : Bind.MerNo,
  216. }).Entity;
  217. db.SaveChanges();
  218. }
  219. pos.BindMerchantId = add.Id;
  220. pos.LastMerchantId = add.Id;
  221. pos.IsFirst = 1;
  222. if(pos.CardType == 0)
  223. {
  224. pos.IsPurchase = 1;
  225. }
  226. db.SaveChanges();
  227. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  228. if (edit != null)
  229. {
  230. edit.Status = 2;
  231. spdb.SaveChanges();
  232. }
  233. string IdBrand = pos.UserId + "_" + pos.BrandId;
  234. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  235. if (MachineData == null)
  236. {
  237. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  238. {
  239. IdBrand = IdBrand,
  240. }).Entity;
  241. db.SaveChanges();
  242. }
  243. MachineData.BindCount += 1;
  244. MachineData.UnBindCount -= 1;
  245. if(Bind.Field5 == "已激活")
  246. {
  247. bool PrizeFlag = pos.CardType > 1;
  248. SycnSpTradeService.Instance.ActPos(db, pos, 0, 30, PrizeFlag, Bind.SeoKeyword);
  249. }
  250. db.SaveChanges();
  251. }
  252. }
  253. catch (Exception ex)
  254. {
  255. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  256. }
  257. }
  258. spdb.SaveChanges();
  259. spdb.Dispose();
  260. db.SaveChanges();
  261. db.Dispose();
  262. }
  263. catch (Exception ex)
  264. {
  265. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  266. }
  267. Thread.Sleep(1000);
  268. }
  269. }
  270. }
  271. }