SycnSpBindService.cs 26 KB

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