CheckWeChatSignService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Data;
  5. using MySystem.Models.Main;
  6. using Library;
  7. using LitJson;
  8. using System.Text.RegularExpressions;
  9. using System.Threading;
  10. namespace MySystem
  11. {
  12. public class CheckWeChatSignService
  13. {
  14. public readonly static CheckWeChatSignService Instance = new CheckWeChatSignService();
  15. private CheckWeChatSignService()
  16. { }
  17. public void Start()
  18. {
  19. Thread th = new Thread(StartListen);
  20. th.IsBackground = true;
  21. th.Start();
  22. }
  23. public void StartListen()
  24. {
  25. while (true)
  26. {
  27. // string content = RedisDbconn.Instance.RPop<string>("WeChatSignHdQueue");
  28. // if (!string.IsNullOrEmpty(content))
  29. // {
  30. // if (content == "sleep")
  31. // {
  32. // RedisDbconn.Instance.AddList("WeChatSignHdQueue", "sleep");
  33. // Thread.Sleep(60000);
  34. // }
  35. // else
  36. // {
  37. // QueryMerchantStatus queryMerchantStatus = Newtonsoft.Json.JsonConvert.DeserializeObject<QueryMerchantStatus>(content);
  38. // string result = StartDo(queryMerchantStatus);
  39. // if (result == "wait")
  40. // {
  41. // Thread.Sleep(500);
  42. // RedisDbconn.Instance.AddList("WeChatSignHdQueue", queryMerchantStatus);
  43. // }
  44. // }
  45. // }
  46. // else
  47. // {
  48. // Thread.Sleep(2000);
  49. // }
  50. DataTable dt = CustomerSqlConn.dtable("select Id,MchtNo from MerchantAddInfo where Id in (select Id from MerchantInfo where Sort=1 and `Status`=0 and QueryCount=0) and MchtNo!='' and MchtNo is not null", AppConfig.Base.SqlConnStr);
  51. foreach(DataRow dr in dt.Rows)
  52. {
  53. StartDo(new QueryMerchantStatus()
  54. {
  55. MerchantId = dr["Id"].ToString(),
  56. MerchantNo = dr["MchtNo"].ToString(),
  57. });
  58. }
  59. Thread.Sleep(120000);
  60. }
  61. }
  62. public string StartDo(QueryMerchantStatus query)
  63. {
  64. try
  65. {
  66. var mId = int.Parse(query.MerchantId);
  67. WebCMSEntities db = new WebCMSEntities();
  68. //查询商户审核状态
  69. var returnInfo = HaoDaHelper.Instance.QueryMerchantStatus(HaoDaHelper.BrhCode, query.MerchantNo);
  70. JsonData dic = JsonMapper.ToObject(returnInfo);
  71. //审核成功(1- 待审核 2 -审核中 3 -审核被拒绝 4 -审核通过 6 -待复核)
  72. if (dic["data"]["code"].ToString() == "4" && !string.IsNullOrEmpty(dic["data"]["storeNo"].ToString()) && !string.IsNullOrEmpty(dic["data"]["outMchtNo"].ToString()))
  73. {
  74. //查询商户是否存在
  75. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == mId);
  76. if (merchantadd != null)
  77. {
  78. // merchantadd.Status = 1;
  79. merchantadd.StoreNo = dic["data"]["storeNo"].ToString();
  80. merchantadd.HdStatus = 1; //0 待审核 1 已通过 2 已开通分账 3 已添加接收方
  81. merchantadd.OutMchtNo = dic["data"]["outMchtNo"].ToString();
  82. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == mId);
  83. if (merchant != null)
  84. {
  85. merchant.Status = 1;
  86. }
  87. //商户开通分账
  88. JsonData Info = JsonMapper.ToObject(HaoDaHelper.Instance.OpenDivideAccounts(merchantadd.OutMchtNo, merchantadd.StoreNo));
  89. //开通失败
  90. if (Info["resultCode"].ToString() == "0")
  91. {
  92. merchantadd.Status = -1;
  93. merchantadd.WeChatRemark = Info["errorDesc"].ToString();
  94. }
  95. //开通成功
  96. if (Info["resultCode"].ToString() == "1")
  97. {
  98. merchantadd.OpenAct = 1;//是否开通分账(1 是 0 否)
  99. merchantadd.HdStatus = 2; //0 待审核 1 已通过 2 已开通分账 3 已添加接收方
  100. //分账接收方签约
  101. JsonData Infos = JsonMapper.ToObject(HaoDaHelper.Instance.DivideAccountsSign(AppConfig.Haoda.AcctNo, merchantadd.OutMchtNo, merchantadd.StoreNo));
  102. //失败
  103. if (Infos["resultCode"].ToString() == "0")
  104. {
  105. merchantadd.WeChatRemark = Infos["errorDesc"].ToString();
  106. }
  107. //成功
  108. else
  109. {
  110. merchantadd.HdStatus = 3; //0 待审核 1 已通过 2 已开通分账 3 已添加接收方
  111. if (!string.IsNullOrEmpty(merchantadd.ToAcctNo))
  112. {
  113. if (!merchantadd.ToAcctNo.Contains("," + AppConfig.Haoda.AcctNo + ","))
  114. {
  115. merchantadd.ToAcctNo += "," + AppConfig.Haoda.AcctNo + ",";
  116. }
  117. }
  118. else
  119. {
  120. merchantadd.ToAcctNo += "," + AppConfig.Haoda.AcctNo + ",";
  121. }
  122. merchantadd.HdPassDate = DateTime.Now;
  123. }
  124. }
  125. db.SaveChanges();
  126. //创客账号
  127. Models.Main1.WebCMSEntities db1 = new Models.Main1.WebCMSEntities();
  128. string pwd = merchantadd.MobilePhone.Substring(5);
  129. merchant.LoginPwd = function.MD532(pwd);
  130. Models.Main1.MerchantLoginInfo info = db1.MerchantLoginInfo.FirstOrDefault(m => m.LoginMobile == merchantadd.MobilePhone);
  131. if (info == null)
  132. {
  133. info = db1.MerchantLoginInfo.Add(new Models.Main1.MerchantLoginInfo()
  134. {
  135. LoginMobile = merchantadd.MobilePhone,
  136. }).Entity;
  137. db1.SaveChanges();
  138. }
  139. info.LoginPwd = function.MD532(pwd);
  140. db1.SaveChanges();
  141. Models.Main1.MerchantAccountLinkInfo infolink = db1.MerchantAccountLinkInfo.FirstOrDefault(m => m.LoginId == info.Id && m.MerchantId == merchantadd.Id && m.Kind == 2);
  142. if (infolink == null)
  143. {
  144. infolink = db1.MerchantAccountLinkInfo.Add(new Models.Main1.MerchantAccountLinkInfo()
  145. {
  146. LoginId = info.Id,
  147. MerchantId = merchantadd.Id,
  148. Kind = 2,
  149. }).Entity;
  150. db1.SaveChanges();
  151. }
  152. db1.Dispose();
  153. //开户意愿申请
  154. RedisDbconn.Instance.AddList("AlipayForHaoDaQueue", mId);
  155. RedisDbconn.Instance.AddList("WeChatForHaoDaQueue", mId);
  156. }
  157. }
  158. //审核被拒绝
  159. else if (dic["data"]["code"].ToString() == "3")
  160. {
  161. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == mId);
  162. if (merchantadd != null)
  163. {
  164. string WeChatRemark = dic["data"]["option"].ToString();
  165. if(WeChatRemark.Contains("租赁协议") && WeChatRemark.Contains("水电煤") && WeChatRemark.Contains("门头合影") && WeChatRemark.Contains("下载地址"))
  166. {
  167. WeChatRemark = "门头不一致需补充 “名称不一致说明函”";
  168. }
  169. if(WeChatRemark.Contains("盒伙人在门头合影"))
  170. {
  171. WeChatRemark = "门头不一致需补充 “名称不一致说明函”";
  172. }
  173. if(WeChatRemark.Contains("不支持跨商户进件"))
  174. {
  175. WeChatRemark = "该商户已注册,错误指令【404】";
  176. }
  177. merchantadd.WeChatRemark = WeChatRemark;
  178. merchantadd.Status = -1;
  179. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == mId);
  180. if (merchant != null)
  181. {
  182. merchant.Status = -1;
  183. }
  184. db.SaveChanges();
  185. }
  186. }
  187. else
  188. {
  189. return "wait";
  190. }
  191. }
  192. catch (Exception ex)
  193. {
  194. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "好哒商户进件状态查询异常");
  195. }
  196. return "";
  197. }
  198. }
  199. }