SycnSpBindService.cs 26 KB

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