SycnSpMerchantService.cs 18 KB

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