SycnSpBindService.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. string KindName = Bind.Remark; //商品类型名称
  243. string Name = Bind.Field3; //商品名称
  244. decimal ActualAmount = decimal.Parse(function.CheckNum(Bind.SeoTitle)); //实付金额
  245. if(ActualAmount > 0)
  246. {
  247. PosPushDataNewHelper.Trade(new TradeRecord()
  248. {
  249. TradeSnNo = Bind.MerSnNo,
  250. MerNo = Bind.MerNo,
  251. TradeAmount = ActualAmount >= 10000 ? ActualAmount / 100 : ActualAmount,
  252. TradeSerialNo = function.MD5_16("sim:" + Bind.Id.ToString()),
  253. CreateDate = Bind.CreateTime,
  254. ProductType = Bind.ProductType,
  255. Field1 = Bind.Field3,
  256. Field2 = Bind.Field5,
  257. });
  258. }
  259. PxcModels.MachineForSnNo posFor = db.MachineForSnNo.FirstOrDefault(m => m.SnNo == Bind.MerSnNo) ?? new PxcModels.MachineForSnNo();
  260. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == posFor.SnId);
  261. if (pos != null)
  262. {
  263. pos.BindingState = 1;
  264. pos.BindingTime = Bind.CreateTime;
  265. pos.Status = 0;
  266. pos.IsFirst = 1;
  267. if(Name == "激活后补卡")
  268. {
  269. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  270. if(forMerNo != null)
  271. {
  272. PxcModels.PosMachinesTwo oldpos = db.PosMachinesTwo.FirstOrDefault(m => m.Id == forMerNo.SnId);
  273. if(oldpos != null)
  274. {
  275. oldpos.IsFirst = 0;
  276. pos.BindingState = oldpos.BindingState;
  277. pos.BindingTime = oldpos.BindingTime;
  278. pos.ActivationState = oldpos.ActivationState;
  279. pos.ActivationTime = oldpos.ActivationTime;
  280. }
  281. }
  282. }
  283. string ActStatus = Bind.Field5; //激活状态名称
  284. string CheckCard = function.CheckNull(RedisDbconn.Instance.Get<string>("SimCheckCard"));
  285. if (KindName == "基础套餐" && function.CheckNull(Name).Contains("双百") && ActStatus == "已激活" && ActualAmount >= 100)
  286. {
  287. pos.Detail = "0";
  288. }
  289. else if (CheckCard.Contains("," + Name + ",") && ActStatus == "已激活" && ActualAmount >= 100)
  290. {
  291. pos.Detail = "0";
  292. }
  293. else
  294. {
  295. pos.Detail = "1";
  296. }
  297. PxcModels.MachineForMerNo merFor = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == Bind.MerNo);
  298. if (merFor == null)
  299. {
  300. merFor = db.MachineForMerNo.Add(new PxcModels.MachineForMerNo()
  301. {
  302. MerNo = Bind.MerNo,
  303. SnId = pos.Id,
  304. }).Entity;
  305. }
  306. else
  307. {
  308. merFor.SnId = pos.Id;
  309. }
  310. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.MerNo && m.KqSnNo == Bind.MerSnNo);
  311. if (add == null)
  312. {
  313. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  314. {
  315. CreateDate = Bind.CreateTime,
  316. KqMerNo = Bind.MerNo,
  317. KqSnNo = Bind.MerSnNo,
  318. MerchantNo = Bind.MerNo.Length > 16 ? Bind.MerNo.Substring(0, 16) : Bind.MerNo,
  319. }).Entity;
  320. db.SaveChanges();
  321. }
  322. string json = Bind.SeoDescription;
  323. string ProductName = ""; //商品名称
  324. string ActivityName = ""; //促销活动名称
  325. string ProductTypeName = ""; //商品类型名称
  326. string ActStatusName = ""; //激活状态名称
  327. string ActAmount = ""; //实付金额
  328. string EmployeeNo = ""; //员工工号
  329. string EmployeeName = ""; //员工姓名
  330. if(!string.IsNullOrEmpty(json))
  331. {
  332. JsonData jsonObj = JsonMapper.ToObject(json);
  333. ProductName = jsonObj["ProductName"].ToString();
  334. ActivityName = jsonObj["ActiveName"].ToString();
  335. ProductTypeName = jsonObj["ProductType"].ToString();
  336. ActAmount = jsonObj["ActualAmount"].ToString();
  337. ActStatusName = jsonObj["ActStatus"].ToString();
  338. EmployeeNo = jsonObj["WorkNo"].ToString();
  339. EmployeeName = jsonObj["AccountName"].ToString();
  340. }
  341. PxcModels.SimposMerchantInfo sim = db.SimposMerchantInfo.FirstOrDefault(m => m.KqMerNo == Bind.MerNo);
  342. if (sim == null)
  343. {
  344. sim = db.SimposMerchantInfo.Add(new PxcModels.SimposMerchantInfo()
  345. {
  346. Id = add.Id,
  347. CreateDate = Bind.CreateTime,
  348. KqMerNo = Bind.MerNo,
  349. MerchantNo = Bind.MerNo.Length > 16 ? Bind.MerNo.Substring(0, 16) : Bind.MerNo,
  350. }).Entity;
  351. db.SaveChanges();
  352. }
  353. sim.KqSnNo = Bind.MerSnNo;
  354. sim.ProductName = ProductName;
  355. sim.ActivityName = ActivityName;
  356. sim.ProductTypeName = ProductTypeName;
  357. sim.ActStatusName = ActStatusName;
  358. sim.ActAmount = decimal.Parse(function.CheckNum(ActAmount));
  359. sim.EmployeeNo = EmployeeNo;
  360. sim.EmployeeName = EmployeeName;
  361. pos.BindMerchantId = add.Id;
  362. pos.LastMerchantId = add.Id;
  363. if(pos.CardType < 2)
  364. {
  365. pos.IsPurchase = 1;
  366. }
  367. sim.UpdateDate = Bind.UpdateTime;
  368. sim.TopUserId = 1;
  369. sim.BrandId = pos.BrandId;
  370. sim.SnStoreId = pos.StoreId;
  371. if(ActStatus == "已激活")
  372. {
  373. sim.MerStandardDate = DateTime.Now;
  374. sim.ActiveStatus = 1;
  375. }
  376. sim.UserId = pos.UserId;
  377. sim.MgrName = Bind.MerName;
  378. sim.MerStatus = 1;
  379. sim.KqSnNo = pos.PosSn;
  380. sim.MerRealName = Bind.MerName;
  381. sim.MerchantMobile = Bind.MerNewSnNo;
  382. sim.MerchantName = Bind.MerName;
  383. sim.Type = pos.CardType + 1;
  384. db.SaveChanges();
  385. BindRecord edit = spdb.BindRecord.FirstOrDefault(m => m.Id == Bind.Id);
  386. if (edit != null)
  387. {
  388. edit.Status = 2;
  389. spdb.SaveChanges();
  390. }
  391. string IdBrand = pos.UserId + "_" + pos.BrandId;
  392. PxcModels.UserMachineData MachineData = db.UserMachineData.FirstOrDefault(m => m.IdBrand == IdBrand);
  393. if (MachineData == null)
  394. {
  395. MachineData = db.UserMachineData.Add(new PxcModels.UserMachineData()
  396. {
  397. IdBrand = IdBrand,
  398. }).Entity;
  399. db.SaveChanges();
  400. }
  401. MachineData.BindCount += 1;
  402. MachineData.UnBindCount -= 1;
  403. Utils.Instance.PrizeRecord(1, pos.PosSn, "广电卡,激活状态:" + Bind.Field5);
  404. if(Bind.Field5 == "已激活")
  405. {
  406. bool PrizeFlag = pos.CardType > 0 && ActualAmount >= 100;
  407. //电渠卡判断报备时间超过30天不发奖励
  408. if(pos.PosSn.StartsWith("CS") && !string.IsNullOrEmpty(Bind.SeoDescription))
  409. {
  410. JsonData jsonObj = JsonMapper.ToObject(Bind.SeoDescription);
  411. DateTime reportTime = DateTime.Parse(jsonObj["CreateDate"].ToString());
  412. if(reportTime.AddDays(30) < Bind.CreateDate)
  413. {
  414. PrizeFlag = false;
  415. pos.Detail = "1";
  416. }
  417. }
  418. else
  419. {
  420. PrizeFlag = pos.CardType > 0 && ActualAmount >= 100 && function.CheckNull(Name).Contains("双百");
  421. }
  422. SycnSpTradeService.Instance.ActPos(db, pos, 0, 30, PrizeFlag, Bind.SeoKeyword);
  423. if(PrizeFlag)
  424. {
  425. //发放奖励MQ
  426. PrizeSetHelper.Do("4", pos.PosSn);
  427. }
  428. }
  429. db.SaveChanges();
  430. }
  431. }
  432. catch (Exception ex)
  433. {
  434. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Bind.Id, "同步SP绑定数据到MAIN异常");
  435. }
  436. }
  437. spdb.SaveChanges();
  438. spdb.Dispose();
  439. db.SaveChanges();
  440. db.Dispose();
  441. }
  442. catch (Exception ex)
  443. {
  444. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP绑定数据到MAIN异常");
  445. }
  446. Thread.Sleep(1000);
  447. }
  448. }
  449. }
  450. }