SycnSpBindService.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. List<string> BrandIds = new List<string>();
  28. BrandIds.Add("14");
  29. BrandIds.Add("17");
  30. WebCMSEntities spdb = new WebCMSEntities();
  31. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  32. DateTime start = DateTime.Now.AddDays(-30);
  33. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  34. 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 && !BrandIds.Contains(m.ProductType) && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  35. foreach (var Bind in Binds)
  36. {
  37. var tran = db.Database.BeginTransaction();
  38. try
  39. {
  40. if(Bind.Field1 == "解绑" && Bind.ProductType == "1")
  41. {
  42. // 删除对应商户,激活机具除外
  43. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == Bind.MerSnNo) ?? new PxcModels.PosMachinesTwo();
  44. if(pos.ActivationState == 0)
  45. {
  46. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  47. if (forMerNo != null)
  48. {
  49. db.MachineForMerNo.Remove(forMerNo);
  50. db.SaveChanges();
  51. }
  52. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.Id == pos.BindMerchantId);
  53. if(merchant != null)
  54. {
  55. //备份解绑的商户信息
  56. CustomerSqlConn.op("insert into PosMerchantInfoBak select * from PosMerchantInfo where Id=" + pos.BindMerchantId, MysqlConn.SqlConnStr);
  57. db.SaveChanges();
  58. pos.BindMerchantId = 0;
  59. pos.BindingState = 0;
  60. pos.BindingTime = null;
  61. db.PosMerchantInfo.Remove(merchant);
  62. db.SaveChanges();
  63. }
  64. string IdBrand = pos.BuyUserId + "_" + pos.BrandId;
  65. PxcModels.UserMachineData userData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  66. if (userData == null)
  67. {
  68. userData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  69. {
  70. IdBrand = IdBrand,
  71. }).Entity;
  72. db.SaveChanges();
  73. }
  74. userData.BindCount -= 1;
  75. userData.UnBindCount += 1;
  76. db.SaveChanges();
  77. }
  78. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  79. if (edit != null)
  80. {
  81. edit.Status = 2;
  82. spdb.SaveChanges();
  83. }
  84. }
  85. else
  86. {
  87. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  88. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId && m.BindingState == 0);
  89. if (pos != null)
  90. {
  91. pos.BindingState = 1;
  92. pos.BindingTime = Bind.CreateTime;
  93. pos.Status = 0;
  94. string MerNo = Bind.MerNo;
  95. //wifi设备取sn
  96. if(pos.BrandId >= 23 && pos.BrandId <= 26)
  97. {
  98. MerNo = Bind.MerSnNo;
  99. }
  100. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == MerNo);
  101. if (merFor == null)
  102. {
  103. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  104. {
  105. MerNo = MerNo,
  106. SnId = pos.Id,
  107. }).Entity;
  108. }
  109. else
  110. {
  111. merFor.SnId = pos.Id;
  112. }
  113. db.SaveChanges();
  114. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  115. if (edit != null)
  116. {
  117. edit.Status = 2;
  118. spdb.SaveChanges();
  119. }
  120. string IdBrand = pos.UserId + "_" + pos.BrandId;
  121. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  122. if (MachineData == null)
  123. {
  124. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  125. {
  126. IdBrand = IdBrand,
  127. }).Entity;
  128. db.SaveChanges();
  129. }
  130. MachineData.BindCount += 1;
  131. MachineData.UnBindCount -= 1;
  132. db.SaveChanges();
  133. if(pos.BuyUserId == 0 && pos.PreUserId > 0)
  134. {
  135. // RedisDbconn.Instance.Set("CheckUserBindPrePos:" + pos.PreUserId, "您的名下有已绑定的预发机 sn:" + pos.PosSn + " 尚未申请,绑定30分钟内申请即可免费自动补录,请前往客小爽产品中点击机具申请继续完成申请");
  136. RedisDbconn.Instance.AddList("CheckUserBindPrePos:" + pos.PreUserId, pos.PosSn);
  137. int ExpireTime = 60 * 25;
  138. RedisDbconn.Instance.SetExpire("CheckUserBindPrePos:" + pos.PreUserId, ExpireTime);
  139. }
  140. }
  141. else
  142. {
  143. if(pos.BrandId == 12 || pos.BrandId == 13)
  144. {
  145. pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == Bind.MerSnNo && m.BindingState == 1);
  146. if (pos != null)
  147. {
  148. string MerNo = Bind.MerNo;
  149. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == MerNo);
  150. if (merFor == null)
  151. {
  152. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  153. {
  154. MerNo = MerNo,
  155. SnId = pos.Id,
  156. }).Entity;
  157. }
  158. else
  159. {
  160. merFor.SnId = pos.Id;
  161. }
  162. db.SaveChanges();
  163. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  164. if (edit != null)
  165. {
  166. edit.Status = 2;
  167. spdb.SaveChanges();
  168. }
  169. }
  170. }
  171. else
  172. {
  173. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  174. if (edit != null)
  175. {
  176. edit.Status = 0;
  177. spdb.SaveChanges();
  178. }
  179. }
  180. }
  181. }
  182. tran.Commit();
  183. }
  184. catch (Exception ex)
  185. {
  186. tran.Rollback();
  187. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  188. }
  189. tran.Dispose();
  190. }
  191. spdb.SaveChanges();
  192. spdb.Dispose();
  193. db.SaveChanges();
  194. db.Dispose();
  195. }
  196. catch (Exception ex)
  197. {
  198. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  199. }
  200. Thread.Sleep(1000);
  201. }
  202. }
  203. public void StartSim()
  204. {
  205. Thread th = new Thread(StartSimDo);
  206. th.IsBackground = true;
  207. th.Start();
  208. }
  209. public void StartSimDo()
  210. {
  211. while (true)
  212. {
  213. try
  214. {
  215. WebCMSEntities spdb = new WebCMSEntities();
  216. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  217. DateTime start = DateTime.Now.AddDays(-60);
  218. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/BindRecordId.txt")));
  219. var Binds = spdb.BindRecord.Where(m => m.Id >= StartId && m.CreateTime >= start && m.ProductType == "14" && m.Status == 1).OrderByDescending(m => m.Id).ToList();
  220. foreach (var Bind in Binds)
  221. {
  222. try
  223. {
  224. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  225. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  226. if (pos != null)
  227. {
  228. string KindName = Bind.Remark; //商品类型名称
  229. string Name = Bind.Field3; //商品名称
  230. pos.BindingState = 1;
  231. pos.BindingTime = Bind.CreateTime;
  232. pos.Status = 0;
  233. pos.IsFirst = 1;
  234. if(Name == "激活后补卡")
  235. {
  236. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  237. if(forMerNo != null)
  238. {
  239. PxcModels.PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId);
  240. if(oldpos != null)
  241. {
  242. oldpos.IsFirst = 0;
  243. pos.BindingState = oldpos.BindingState;
  244. pos.BindingTime = oldpos.BindingTime;
  245. pos.ActivationState = oldpos.ActivationState;
  246. pos.ActivationTime = oldpos.ActivationTime;
  247. }
  248. }
  249. }
  250. decimal ActualAmount = decimal.Parse(function.CheckNum(Bind.SeoTitle)); //实付金额
  251. string ActStatus = Bind.Field5; //激活状态名称
  252. if (KindName == "基础套餐" && Name == "双百套餐" && ActStatus == "已激活" && ActualAmount >= 100)
  253. {
  254. pos.Detail = "0";
  255. }
  256. else if ((Name == "升龙卡" || Name == "祥龙卡") && ActStatus == "已激活" && ActualAmount >= 100)
  257. {
  258. pos.Detail = "0";
  259. }
  260. else
  261. {
  262. pos.Detail = "1";
  263. }
  264. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  265. if (merFor == null)
  266. {
  267. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  268. {
  269. MerNo = Bind.MerNo,
  270. SnId = pos.Id,
  271. }).Entity;
  272. }
  273. else
  274. {
  275. merFor.SnId = pos.Id;
  276. }
  277. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.MerNo && m.KqSnNo == Bind.MerSnNo);
  278. if (add == null)
  279. {
  280. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  281. {
  282. CreateDate = Bind.CreateTime,
  283. KqMerNo = Bind.MerNo,
  284. KqSnNo = Bind.MerSnNo,
  285. MerchantNo = Bind.MerNo.Length > 16 ? Bind.MerNo.Substring(0, 16) : Bind.MerNo,
  286. }).Entity;
  287. db.SaveChanges();
  288. }
  289. string json = Bind.SeoDescription;
  290. string ProductName = ""; //商品名称
  291. string ActivityName = ""; //促销活动名称
  292. string ProductTypeName = ""; //商品类型名称
  293. string ActStatusName = ""; //激活状态名称
  294. string ActAmount = ""; //实付金额
  295. string EmployeeNo = ""; //员工工号
  296. string EmployeeName = ""; //员工姓名
  297. if(!string.IsNullOrEmpty(json))
  298. {
  299. JsonData jsonObj = JsonMapper.ToObject(json);
  300. ProductName = jsonObj["ProductName"].ToString();
  301. ActivityName = jsonObj["ActiveName"].ToString();
  302. ProductTypeName = jsonObj["ProductType"].ToString();
  303. ActAmount = jsonObj["ActualAmount"].ToString();
  304. ActStatusName = jsonObj["ActStatus"].ToString();
  305. EmployeeNo = jsonObj["WorkNo"].ToString();
  306. EmployeeName = jsonObj["AccountName"].ToString();
  307. }
  308. PxcModels.SimposMerchantInfo sim = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.MerNo);
  309. if (sim == null)
  310. {
  311. sim = db.SimposMerchantInfo.Add(new PxcModels.SimposMerchantInfo()
  312. {
  313. Id = add.Id,
  314. CreateDate = Bind.CreateTime,
  315. KqMerNo = Bind.MerNo,
  316. MerchantNo = Bind.MerNo.Length > 16 ? Bind.MerNo.Substring(0, 16) : Bind.MerNo,
  317. }).Entity;
  318. db.SaveChanges();
  319. }
  320. sim.KqSnNo = Bind.MerSnNo;
  321. sim.ProductName = ProductName;
  322. sim.ActivityName = ActivityName;
  323. sim.ProductTypeName = ProductTypeName;
  324. sim.ActStatusName = ActStatusName;
  325. sim.ActAmount = decimal.Parse(function.CheckNum(ActAmount));
  326. sim.EmployeeNo = EmployeeNo;
  327. sim.EmployeeName = EmployeeName;
  328. pos.BindMerchantId = add.Id;
  329. pos.LastMerchantId = add.Id;
  330. if(pos.CardType < 2)
  331. {
  332. pos.IsPurchase = 1;
  333. }
  334. sim.UpdateDate = Bind.UpdateTime;
  335. sim.TopUserId = 1;
  336. sim.BrandId = pos.BrandId;
  337. sim.SnStoreId = pos.StoreId;
  338. if(ActStatus == "已激活")
  339. {
  340. sim.MerStandardDate = DateTime.Now;
  341. sim.ActiveStatus = 1;
  342. }
  343. sim.UserId = pos.UserId;
  344. sim.MgrName = Bind.MerName;
  345. sim.MerStatus = 1;
  346. sim.KqSnNo = pos.PosSn;
  347. sim.MerRealName = Bind.MerName;
  348. sim.MerchantMobile = Bind.MerNewSnNo;
  349. sim.MerchantName = Bind.MerName;
  350. sim.Type = pos.CardType + 1;
  351. db.SaveChanges();
  352. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  353. if (edit != null)
  354. {
  355. edit.Status = 2;
  356. spdb.SaveChanges();
  357. }
  358. string IdBrand = pos.UserId + "_" + pos.BrandId;
  359. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  360. if (MachineData == null)
  361. {
  362. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  363. {
  364. IdBrand = IdBrand,
  365. }).Entity;
  366. db.SaveChanges();
  367. }
  368. MachineData.BindCount += 1;
  369. MachineData.UnBindCount -= 1;
  370. Utils.Instance.PrizeRecord(1, pos.PosSn, "广电卡,激活状态:" + Bind.Field5);
  371. if(Bind.Field5 == "已激活")
  372. {
  373. bool PrizeFlag = pos.CardType > 0 && ActualAmount >= 100;
  374. SycnSpTradeService.Instance.ActPos(db, pos, 0, 30, PrizeFlag, Bind.SeoKeyword);
  375. }
  376. db.SaveChanges();
  377. }
  378. }
  379. catch (Exception ex)
  380. {
  381. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  382. }
  383. }
  384. spdb.SaveChanges();
  385. spdb.Dispose();
  386. db.SaveChanges();
  387. db.Dispose();
  388. }
  389. catch (Exception ex)
  390. {
  391. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  392. }
  393. Thread.Sleep(1000);
  394. }
  395. }
  396. }
  397. }