SycnSpBindService.cs 25 KB

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