SycnSpBindService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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.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. string KindName = Bind.Remark; //商品类型名称
  180. string Name = Bind.Field3; //商品名称
  181. pos.BindingState = 1;
  182. pos.BindingTime = Bind.CreateTime;
  183. pos.Status = 0;
  184. pos.IsFirst = 1;
  185. if(Name == "激活后补卡")
  186. {
  187. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  188. if(forMerNo != null)
  189. {
  190. PxcModels.PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId);
  191. if(oldpos != null)
  192. {
  193. oldpos.IsFirst = 0;
  194. pos.BindingState = oldpos.BindingState;
  195. pos.BindingTime = oldpos.BindingTime;
  196. pos.ActivationState = oldpos.ActivationState;
  197. pos.ActivationTime = oldpos.ActivationTime;
  198. }
  199. }
  200. }
  201. decimal ActualAmount = decimal.Parse(function.CheckNum(Bind.SeoTitle)); //实付金额
  202. string ActStatus = Bind.Field5; //激活状态名称
  203. if (KindName == "基础套餐" && Name == "双百套餐" && ActStatus == "已激活" && ActualAmount >= 100)
  204. {
  205. pos.Detail = "0";
  206. }
  207. else
  208. {
  209. pos.Detail = "1";
  210. }
  211. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  212. if (merFor == null)
  213. {
  214. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  215. {
  216. MerNo = Bind.MerNo,
  217. SnId = pos.Id,
  218. }).Entity;
  219. }
  220. else
  221. {
  222. merFor.SnId = pos.Id;
  223. }
  224. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.MerNo && m.KqSnNo == Bind.MerSnNo);
  225. if (add == null)
  226. {
  227. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  228. {
  229. CreateDate = Bind.CreateTime,
  230. KqMerNo = Bind.MerNo,
  231. KqSnNo = Bind.MerSnNo,
  232. MerchantNo = Bind.MerNo.Length > 16 ? Bind.MerNo.Substring(0, 16) : Bind.MerNo,
  233. }).Entity;
  234. db.SaveChanges();
  235. }
  236. PxcModels.SimposMerchantInfo sim = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.MerNo && m.KqSnNo == Bind.MerSnNo);
  237. if (sim == null)
  238. {
  239. string json = Bind.SeoDescription;
  240. string ProductName = ""; //商品名称
  241. string ActivityName = ""; //促销活动名称
  242. string ProductTypeName = ""; //商品类型名称
  243. string ActStatusName = ""; //激活状态名称
  244. string ActAmount = ""; //实付金额
  245. string EmployeeNo = ""; //员工工号
  246. string EmployeeName = ""; //员工姓名
  247. if(!string.IsNullOrEmpty(json))
  248. {
  249. JsonData jsonObj = JsonMapper.ToObject(json);
  250. ProductName = jsonObj["ProductName"].ToString();
  251. ActivityName = jsonObj["ActiveName"].ToString();
  252. ProductTypeName = jsonObj["ProductType"].ToString();
  253. ActAmount = jsonObj["ActualAmount"].ToString();
  254. ActStatusName = jsonObj["ActStatus"].ToString();
  255. EmployeeNo = jsonObj["WorkNo"].ToString();
  256. EmployeeName = jsonObj["AccountName"].ToString();
  257. }
  258. sim = db.SimposMerchantInfo.Add(new PxcModels.SimposMerchantInfo()
  259. {
  260. Id = add.Id,
  261. CreateDate = Bind.CreateTime,
  262. KqMerNo = Bind.MerNo,
  263. KqSnNo = Bind.MerSnNo,
  264. MerchantNo = Bind.MerNo.Length > 16 ? Bind.MerNo.Substring(0, 16) : Bind.MerNo,
  265. ProductName = ProductName,
  266. ActivityName = ActivityName,
  267. ProductTypeName = ProductTypeName,
  268. ActStatusName = ActStatusName,
  269. ActAmount = decimal.Parse(ActAmount),
  270. EmployeeNo = EmployeeNo,
  271. EmployeeName = EmployeeName,
  272. }).Entity;
  273. db.SaveChanges();
  274. }
  275. pos.BindMerchantId = add.Id;
  276. pos.LastMerchantId = add.Id;
  277. if(pos.CardType < 2)
  278. {
  279. pos.IsPurchase = 1;
  280. }
  281. sim.UpdateDate = Bind.UpdateTime;
  282. sim.TopUserId = 1;
  283. sim.BrandId = pos.BrandId;
  284. sim.SnStoreId = pos.StoreId;
  285. if(ActStatus == "已激活")
  286. {
  287. sim.MerStandardDate = DateTime.Now;
  288. sim.ActiveStatus = 1;
  289. }
  290. sim.UserId = pos.UserId;
  291. sim.MgrName = Bind.MerName;
  292. sim.MerStatus = 1;
  293. sim.KqSnNo = pos.PosSn;
  294. sim.MerRealName = Bind.MerName;
  295. sim.MerchantMobile = Bind.MerNewSnNo;
  296. sim.MerchantName = Bind.MerName;
  297. sim.Type = pos.CardType + 1;
  298. db.SaveChanges();
  299. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  300. if (edit != null)
  301. {
  302. edit.Status = 2;
  303. spdb.SaveChanges();
  304. }
  305. string IdBrand = pos.UserId + "_" + pos.BrandId;
  306. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  307. if (MachineData == null)
  308. {
  309. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  310. {
  311. IdBrand = IdBrand,
  312. }).Entity;
  313. db.SaveChanges();
  314. }
  315. MachineData.BindCount += 1;
  316. MachineData.UnBindCount -= 1;
  317. if(Bind.Field5 == "已激活")
  318. {
  319. bool PrizeFlag = pos.CardType > 0;
  320. SycnSpTradeService.Instance.ActPos(db, pos, 0, 30, PrizeFlag, Bind.SeoKeyword);
  321. }
  322. db.SaveChanges();
  323. }
  324. }
  325. catch (Exception ex)
  326. {
  327. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  328. }
  329. }
  330. spdb.SaveChanges();
  331. spdb.Dispose();
  332. db.SaveChanges();
  333. db.Dispose();
  334. }
  335. catch (Exception ex)
  336. {
  337. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  338. }
  339. Thread.Sleep(1000);
  340. }
  341. }
  342. }
  343. }