SycnSpMerchantService.cs 16 KB

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