SycnSpBindService.cs 20 KB

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