SycnSpMerchantService.cs 17 KB

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