MerchantConfirmService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using MySystem.Models.Main;
  6. using Library;
  7. using LitJson;
  8. using System.Threading;
  9. namespace MySystem
  10. {
  11. public class MerchantConfirmService
  12. {
  13. public readonly static MerchantConfirmService Instance = new MerchantConfirmService();
  14. private MerchantConfirmService()
  15. { }
  16. public void Start()
  17. {
  18. Thread th = new Thread(StartListen);
  19. th.IsBackground = true;
  20. th.Start();
  21. }
  22. public void StartListen()
  23. {
  24. while (true)
  25. {
  26. string content = RedisDbconn.Instance.RPop<string>("MerchantConfirmHdQueue");
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. try
  30. {
  31. JsonData jsonObj = JsonMapper.ToObject(content);
  32. int MerchantId = int.Parse(jsonObj["MerchantId"].ToString());
  33. StartDo(MerchantId);
  34. }
  35. catch (Exception ex)
  36. {
  37. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "好哒商户进件异常");
  38. }
  39. }
  40. else
  41. {
  42. Thread.Sleep(2000);
  43. }
  44. }
  45. }
  46. public void StartDo(int MerchantId)
  47. {
  48. WebCMSEntities db = new WebCMSEntities();
  49. Models.Main1.WebCMSEntities db1 = new Models.Main1.WebCMSEntities();
  50. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  51. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  52. if (AddInfo.HdStatus >= 1)
  53. {
  54. //开户意愿申请
  55. if (AddInfo.Status == -1 && merchant.Status == -1)
  56. {
  57. AddInfo.WeChatSignUrl = "";
  58. AddInfo.WeChatRemark = "";
  59. AddInfo.Status = 0;
  60. RedisDbconn.Instance.AddList("WeChatForHaoDaQueue", AddInfo.Id);
  61. }
  62. if (AddInfo.QueryCount == -1 && merchant.QueryCount == -1)
  63. {
  64. AddInfo.AlipaySignUrl = "";
  65. AddInfo.AlipayRemark = "";
  66. AddInfo.QueryCount = 0;
  67. RedisDbconn.Instance.AddList("AlipayForHaoDaQueue", AddInfo.Id);
  68. }
  69. }
  70. else
  71. {
  72. if (AddInfo != null && merchant != null)
  73. {
  74. string BusinessCode = AddInfo.BusinessCode;
  75. if (string.IsNullOrEmpty(BusinessCode))
  76. {
  77. BusinessCode = "LKB" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(5);
  78. AddInfo.BusinessCode = BusinessCode;
  79. }
  80. string pwd = AddInfo.MobilePhone.Substring(5);
  81. merchant.LoginPwd = function.MD532(pwd);
  82. MerchantParamSet query = db.MerchantParamSet.FirstOrDefault(m => m.Id == MerchantId);
  83. if (query == null)
  84. {
  85. query = new MerchantParamSet();
  86. query.IsAll = 1;
  87. db.MerchantParamSet.Add(query);
  88. db.SaveChanges();
  89. }
  90. MerchantForMobile merchantForMobile = db.MerchantForMobile.FirstOrDefault(m => m.Mobile == AddInfo.MobilePhone);
  91. if (merchantForMobile == null)
  92. {
  93. merchantForMobile = db.MerchantForMobile.Add(new MerchantForMobile()
  94. {
  95. Mobile = AddInfo.MobilePhone,
  96. }).Entity;
  97. db.SaveChanges();
  98. }
  99. merchantForMobile.MerchantId = MerchantId;
  100. db.SaveChanges();
  101. Models.Main1.MerchantLoginInfo info = db1.MerchantLoginInfo.FirstOrDefault(m => m.LoginMobile == AddInfo.MobilePhone);
  102. if (info == null)
  103. {
  104. info = db1.MerchantLoginInfo.Add(new Models.Main1.MerchantLoginInfo()
  105. {
  106. LoginMobile = AddInfo.MobilePhone,
  107. }).Entity;
  108. db1.SaveChanges();
  109. }
  110. info.LoginPwd = function.MD532(pwd);
  111. db1.SaveChanges();
  112. Models.Main1.MerchantAccountLinkInfo infolink = db1.MerchantAccountLinkInfo.FirstOrDefault(m => m.LoginId == info.Id && m.MerchantId == MerchantId && m.Kind == 2);
  113. if (infolink == null)
  114. {
  115. infolink = db1.MerchantAccountLinkInfo.Add(new Models.Main1.MerchantAccountLinkInfo()
  116. {
  117. LoginId = info.Id,
  118. MerchantId = MerchantId,
  119. Kind = 2,
  120. }).Entity;
  121. db1.SaveChanges();
  122. }
  123. if (AddInfo.Status <= 0)
  124. {
  125. //好哒新建商户
  126. string result = HaoDaHelper.Instance.AddNewMerchant(AddNewMerchantSetUtil.SetValue(AddInfo));
  127. JsonData jsonObj = JsonMapper.ToObject(result);
  128. //创建成功
  129. if (jsonObj["resultCode"].ToString() == "1")
  130. {
  131. AddInfo.WeChatRemark = "";
  132. if (string.IsNullOrEmpty(AddInfo.MchtNo))
  133. {
  134. AddInfo.MchtNo = jsonObj["data"]["mchtNo"].ToString();
  135. }
  136. AddInfo.Status = 0;
  137. AddInfo.HdStatus = 0; //0 待审核 1 已通过 2 已开通分账 3 已添加接收方
  138. merchant.Status = 0;
  139. db.SaveChanges();
  140. QueryMerchantStatus queryMerchantStatus = new QueryMerchantStatus();
  141. queryMerchantStatus.MerchantId = MerchantId.ToString();
  142. queryMerchantStatus.MerchantNo = AddInfo.MchtNo;
  143. RedisDbconn.Instance.AddList("WeChatSignHdQueue", queryMerchantStatus);
  144. }
  145. //创建失败
  146. else
  147. {
  148. AddInfo.Status = -1;
  149. merchant.Status = -1;
  150. AddInfo.WeChatRemark = jsonObj["errorDesc"].ToString();
  151. // if (string.IsNullOrEmpty(AddInfo.MchtNo))
  152. // {
  153. // if (jsonObj.ToJson().Contains("mchtNo"))
  154. // {
  155. // AddInfo.MchtNo = jsonObj["data"]["mchtNo"].ToString();
  156. // }
  157. // }
  158. db.SaveChanges();
  159. }
  160. }
  161. }
  162. }
  163. db.Dispose();
  164. db1.Dispose();
  165. }
  166. }
  167. }