SycnSpMerchantService.cs 16 KB

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