SycnSpBindService.cs 21 KB

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