SycnSpMerchantService.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 SycnSpMerchantService
  11. {
  12. public readonly static SycnSpMerchantService Instance = new SycnSpMerchantService();
  13. private SycnSpMerchantService()
  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. WebCMSEntities spdb = new WebCMSEntities();
  28. PxcModels.WebCMSEntities db = new PxcModels.WebCMSEntities();
  29. DateTime start = DateTime.Now.AddDays(-30);
  30. DateTime end = DateTime.Now.AddMinutes(-2);
  31. int StartId = int.Parse(function.CheckInt(function.ReadInstance("/SycnSp/MerchantsId.txt")));
  32. var Mers = spdb.Merchants.Where(m => m.Id >= StartId && m.CreateTime >= start && m.CreateTime <= end && m.Status == 1).OrderByDescending(m => m.Id).Take(20).ToList();
  33. foreach (var Mer in Mers)
  34. {
  35. var tran = db.Database.BeginTransaction();
  36. try
  37. {
  38. if(Mer.Field2 != "解绑" && Mer.Field2 != "UNBIND" && Mer.ProductType != "12" && Mer.ProductType != "30")
  39. {
  40. if(Utils.Instance.IsWifi(int.Parse(Mer.ProductType)))
  41. {
  42. Mer.MerNo = Mer.SnNo;
  43. }
  44. PosPushDataNewHelper.Bind(Mer);
  45. }
  46. PxcModels.PosMachinesTwo pos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == Mer.SnNo) ?? new PxcModels.PosMachinesTwo();
  47. if (pos.BindingState == 1 && Mer.Field2 != "解绑" && Mer.Field2 != "UNBIND")
  48. {
  49. PxcModels.Users user = db.Users.FirstOrDefault(m => m.Id == pos.UserId) ?? new PxcModels.Users();
  50. int TopUserId = 0;
  51. if (!string.IsNullOrEmpty(user.ParentNav))
  52. {
  53. TopUserId = int.Parse(user.ParentNav.Trim(',').Replace(",,", ",").Split(',')[0]);
  54. }
  55. int BrandId = pos.BrandId;
  56. if (BrandId == 12 || BrandId == 13 || BrandId == 30 || BrandId == 31)
  57. {
  58. MerchantRecord merinfo = spdb.MerchantRecord.FirstOrDefault(m => m.MerNo == Mer.MerNo);
  59. if(merinfo != null)
  60. {
  61. Mer.MerIdcardNo = merinfo.LegalIdCard;
  62. Mer.MerName = merinfo.LegalName;
  63. }
  64. }
  65. PxcModels.PosMerchantInfo add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mer.MerNo);
  66. //防盒易付重推判断
  67. if(pos.BrandId == 12 || pos.BrandId == 13)
  68. {
  69. add = db.PosMerchantInfo.FirstOrDefault(m => m.KqSnNo == Mer.SnNo && m.MerIdcardNo == Mer.MerIdcardNo && m.MerchantName == Mer.MerName);
  70. }
  71. //如果是好哒和WIFI品牌需要和SN一起判断
  72. if(Utils.Instance.IsHaoDa(pos.BrandId))
  73. {
  74. add = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mer.MerNo && m.KqSnNo == Mer.SnNo);
  75. }
  76. if(Utils.Instance.IsWifi(pos.BrandId))
  77. {
  78. add = db.PosMerchantInfo.FirstOrDefault(m => m.KqSnNo == Mer.SnNo);
  79. }
  80. if (add == null)
  81. {
  82. int IsFirst = 1;
  83. if(!string.IsNullOrEmpty(Mer.MerIdcardNo))
  84. {
  85. string startNo = Mer.MerIdcardNo.Substring(0, 6);
  86. string endNo = Mer.MerIdcardNo.Substring(Mer.MerIdcardNo.Length - 4, 4).ToUpper();
  87. string Name = Mer.MerName;
  88. if (Mer.ProductType == "2")
  89. {
  90. if (Name.Contains("-"))
  91. {
  92. Name = Name.Split('-')[1];
  93. }
  94. else if (Name.Contains("_"))
  95. {
  96. Name = Name.Split('_')[1];
  97. }
  98. }
  99. else if (Mer.ProductType == "4" || Mer.ProductType == "8" || Mer.ProductType == "9")
  100. {
  101. Name = Mer.SeoTitle;
  102. }
  103. else if (Mer.ProductType == "10")
  104. {
  105. Name = Name.Replace("*", "");
  106. }
  107. Name = Name.Replace("个体户", "");
  108. Name = Name.Replace("个体商户", "");
  109. Name = Name.Replace("企业户", "");
  110. Name = Name.Replace("企业商户", "");
  111. function.WriteLog(DateTime.Now.ToString() + "-----startNo:" + startNo + ",endNo:" + endNo + ",Name:" + Name, "监控机具是否互斥");
  112. PxcModels.PosMerchantInfo check = db.PosMerchantInfo.FirstOrDefault(m => m.MerIdcardNo.StartsWith(startNo) && m.MerIdcardNo.EndsWith(endNo) && m.MerchantName.Contains(Name));
  113. if(check != null)
  114. {
  115. function.WriteLog("互斥机具---sn:" + check.KqSnNo + ",merno:" + check.KqMerNo + ",name:" + check.MerchantName, "监控机具是否互斥");
  116. IsFirst = 0;
  117. int RecordId = PublicFunction.FeeSetRecord(db, pos, check, 0.63M);
  118. string info = "{\"RecordId\":\"" + RecordId + "\",\"PosId\":\"" + pos.Id + "\",\"Fee\": \"0.63\",\"Kind\": \"2\",\"OpMan\": \""+"系统"+"\"}";
  119. RedisDbconn.Instance.AddList("SetDepositQueue", info);
  120. }
  121. }
  122. string KqMerNo = Mer.MerNo;
  123. string MerchantNo = "";
  124. if(!string.IsNullOrEmpty(KqMerNo))
  125. {
  126. MerchantNo = KqMerNo.Length > 16 ? KqMerNo.Substring(0, 16) : KqMerNo;
  127. }
  128. add = db.PosMerchantInfo.Add(new PxcModels.PosMerchantInfo()
  129. {
  130. CreateDate = Mer.CreateTime,
  131. KqMerNo = KqMerNo,
  132. MerchantNo = MerchantNo,
  133. }).Entity;
  134. //查询商户备份表,如果有记录则继承之前的商户Id
  135. if(Mer.ProductType == "1") //判断是否金控
  136. {
  137. function.WriteLog(DateTime.Now.ToString(), "监控金控换绑机具日志");
  138. function.WriteLog("Mer.MerNo:" + Mer.MerNo, "监控金控换绑机具日志");
  139. PxcModels.PosMerchantInfoBak MerBak = db.PosMerchantInfoBak.FirstOrDefault(m => m.KqMerNo == Mer.MerNo);
  140. if(MerBak != null)
  141. {
  142. function.WriteLog("有备份商户-机具SN:" + MerBak.KqSnNo, "监控金控换绑机具日志");
  143. PxcModels.PosMachinesTwo OldPos = db.PosMachinesTwo.FirstOrDefault(m => m.PosSn == MerBak.KqSnNo);
  144. if(OldPos != null)
  145. {
  146. function.WriteLog("首台机标记:新机具" + pos.IsFirst + ",老机具" + OldPos.IsFirst, "监控金控换绑机具日志");
  147. pos.IsFirst = OldPos.IsFirst;
  148. }
  149. add.Id = MerBak.Id;
  150. db.PosMerchantInfoBak.Remove(MerBak);
  151. }
  152. else
  153. {
  154. function.WriteLog("无备份商户", "监控金控换绑机具日志");
  155. pos.IsFirst = IsFirst;
  156. }
  157. function.WriteLog("\n\n", "监控金控换绑机具日志");
  158. }
  159. else
  160. {
  161. pos.IsFirst = IsFirst;
  162. }
  163. db.SaveChanges();
  164. function.WriteLog("MerNo:" + Mer.MerNo + ",PosSn:" + pos.PosSn + ",IsFirst:" + IsFirst + "\n\n", "监控机具是否互斥");
  165. }
  166. else
  167. {
  168. if(pos.BrandId == 12 || pos.BrandId == 13)
  169. {
  170. add.KqMerNo = Mer.MerNo;
  171. add.MerchantNo = Mer.MerNo.Length > 16 ? Mer.MerNo.Substring(0, 16) : Mer.MerNo;
  172. }
  173. }
  174. if(Utils.Instance.IsWifi(pos.BrandId))
  175. {
  176. add.KqMerNo = Mer.SnNo;
  177. }
  178. add.UpdateDate = Mer.UpdateTime;
  179. add.TopUserId = TopUserId;
  180. add.BrandId = BrandId;
  181. add.SnStoreId = pos.StoreId;
  182. add.SnType = pos.PosSnType;
  183. add.UserId = pos.UserId;
  184. add.MgrName = Mer.AgentName;
  185. add.MerStatus = 1;
  186. add.KqSnNo = Mer.SnNo;
  187. string CertId = function.CheckNull(Mer.MerIdcardNo).ToUpper();
  188. if(BrandId == 15 || BrandId == 16)
  189. {
  190. CertId = CertId.Substring(0, 6) + "********" + CertId.Substring(CertId.Length - 4);
  191. }
  192. add.MerIdcardNo = CertId;
  193. add.MerRealName = Mer.MerRealName;
  194. add.MerchantMobile = Mer.MerMobile;
  195. add.MerchantName = Mer.MerName;
  196. pos.BindMerchantId = add.Id;
  197. pos.LastMerchantId = add.Id;
  198. db.SaveChanges();
  199. PxcModels.Users buser = db.Users.FirstOrDefault(m => m.Id == pos.BuyUserId) ?? new PxcModels.Users();
  200. if(buser.BusinessFlag == 1)
  201. {
  202. bool checkMer = db.BusinessPartnerMerchant.Any(m => m.MerchantId == add.Id);
  203. if(!checkMer)
  204. {
  205. PxcModels.BusinessPartnerPos bpos = db.BusinessPartnerPos.FirstOrDefault(m => m.PosId == pos.Id) ?? new PxcModels.BusinessPartnerPos();
  206. db.BusinessPartnerMerchant.Add(new PxcModels.BusinessPartnerMerchant()
  207. {
  208. CreateDate = DateTime.Now,
  209. MerNo = Mer.MerNo,
  210. MerchantId = add.Id,
  211. PartnerId = bpos.PartnerId,
  212. UserId = pos.BuyUserId,
  213. });
  214. db.SaveChanges();
  215. }
  216. }
  217. //WIFI设备默认设置押金
  218. if(Utils.Instance.IsWifi(pos.BrandId) && pos.ActivationState == 0)
  219. {
  220. if(Mer.Field1 != "v2")
  221. {
  222. if(pos.BrandId == 23)
  223. {
  224. pos.SeoKeyword = "30";
  225. pos.PrizeParams = "30";
  226. }
  227. if(pos.BrandId == 24)
  228. {
  229. pos.SeoKeyword = "120";
  230. pos.PrizeParams = "120";
  231. }
  232. if(pos.BrandId == 25)
  233. {
  234. pos.SeoKeyword = "200";
  235. pos.PrizeParams = "200";
  236. }
  237. if(pos.BrandId == 26)
  238. {
  239. pos.SeoKeyword = "750";
  240. pos.PrizeParams = "750";
  241. }
  242. pos.ActivationState = 1;
  243. pos.ActivationTime = DateTime.Now;
  244. add.ActiveStatus = 1;
  245. add.MerStandardDate = DateTime.Now;
  246. db.SaveChanges();
  247. //检查交易状态,并发奖励
  248. WifiSendPrize(db, pos, add);
  249. //发放奖励MQ
  250. PrizeSetHelper.Do("6", pos.PosSn);
  251. }
  252. //推送激活数据给MQ
  253. Utils.Instance.SendActData(pos);
  254. //wifi预扣款处理
  255. RedisDbconn.Instance.AddList("WifiWithholdBackQueue", "{\"UserId\":\"" + pos.BuyUserId + "\",\"SnNo\":\"" + pos.PosSn + "\"}");
  256. }
  257. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  258. if (edit != null)
  259. {
  260. edit.Status = 2;
  261. spdb.SaveChanges();
  262. }
  263. }
  264. else if (Mer.Field2 == "解绑" && Mer.ProductType == "23")
  265. {
  266. //备份解绑的商户信息
  267. CustomerSqlConn.op("insert into PosMerchantInfoBak select * from PosMerchantInfo where Id=" + pos.BindMerchantId, MysqlConn.SqlConnStr);
  268. PxcModels.PosMerchantInfo merchant = db.PosMerchantInfo.FirstOrDefault(m => m.KqMerNo == Mer.MerNo);
  269. if(merchant != null)
  270. {
  271. PxcModels.MachineForMerNo forMerNo = db.MachineForMerNo.FirstOrDefault(m => m.MerNo == merchant.KqMerNo);
  272. if (forMerNo != null)
  273. {
  274. db.MachineForMerNo.Remove(forMerNo);
  275. }
  276. pos.BindMerchantId = 0;
  277. pos.BindingState = 0;
  278. pos.BindingTime = null;
  279. db.PosMerchantInfo.Remove(merchant);
  280. PxcModels.WifiTradeRecord trade = db.WifiTradeRecord.FirstOrDefault(m => m.SnNo == pos.PosSn);
  281. if(trade != null)
  282. {
  283. trade.DoMonths = trade.Duration;
  284. }
  285. db.SaveChanges();
  286. }
  287. }
  288. else
  289. {
  290. Merchants edit = spdb.Merchants.FirstOrDefault(m => m.Id == Mer.Id);
  291. if (edit != null)
  292. {
  293. edit.Status = 2;
  294. spdb.SaveChanges();
  295. }
  296. }
  297. tran.Commit();
  298. }
  299. catch (Exception ex)
  300. {
  301. tran.Rollback();
  302. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n" + Mer.Id, "同步SP商户数据到MAIN异常");
  303. }
  304. tran.Dispose();
  305. }
  306. spdb.SaveChanges();
  307. spdb.Dispose();
  308. db.SaveChanges();
  309. db.Dispose();
  310. }
  311. catch (Exception ex)
  312. {
  313. function.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString(), "同步SP商户数据到MAIN异常");
  314. }
  315. Thread.Sleep(1000);
  316. }
  317. }
  318. public void WifiSendPrize(PxcModels.WebCMSEntities db, PxcModels.PosMachinesTwo pos, PxcModels.PosMerchantInfo add)
  319. {
  320. PxcModels.WifiTradeRecord trade = db.WifiTradeRecord.FirstOrDefault(m => m.SnNo == pos.PosSn && m.Status == 0);
  321. if (trade != null)
  322. {
  323. trade.Status = 1;
  324. db.SaveChanges();
  325. decimal Prize = trade.TradeAmount * 0.15M;
  326. StatService.Instance.doActiveReward(db, add, pos, pos.BuyUserId, "", 1, Prize);
  327. if (pos.CardType < 100)
  328. {
  329. RedisDbconn.Instance.AddList("OpenRewardQueue", pos.Id.ToString());
  330. }
  331. if (pos.LeaderUserId > 0 && db.Leaders.Any(m => m.Id == pos.LeaderUserId && m.ExpiredDate > DateTime.Now) && pos.CardType < 100)
  332. {
  333. RedisDbconn.Instance.AddList("LeaderPrizeQueue", pos.Id);
  334. }
  335. if (pos.CardType < 100)
  336. {
  337. RedisDbconn.Instance.AddList("OperatePrizeQueue", pos.Id);
  338. }
  339. }
  340. }
  341. }
  342. }