SycnSpBindService.cs 25 KB

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