SycnSpBindService.cs 23 KB

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