SycnSpBindService.cs 24 KB

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