SycnSpMerchantService.cs 18 KB

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