CheckWeChatSignService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. Thread.Sleep(10000);
  59. }
  60. Thread.Sleep(3600000);
  61. }
  62. }
  63. public string StartDo(QueryMerchantStatus query)
  64. {
  65. try
  66. {
  67. var mId = int.Parse(query.MerchantId);
  68. WebCMSEntities db = new WebCMSEntities();
  69. //查询商户审核状态
  70. var returnInfo = HaoDaHelper.Instance.QueryMerchantStatus(HaoDaHelper.BrhCode, query.MerchantNo);
  71. JsonData dic = JsonMapper.ToObject(returnInfo);
  72. //审核成功(1- 待审核 2 -审核中 3 -审核被拒绝 4 -审核通过 6 -待复核)
  73. if (dic["data"]["code"].ToString() == "4" && !string.IsNullOrEmpty(dic["data"]["storeNo"].ToString()) && !string.IsNullOrEmpty(dic["data"]["outMchtNo"].ToString()))
  74. {
  75. //查询商户是否存在
  76. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == mId);
  77. if (merchantadd != null)
  78. {
  79. // merchantadd.Status = 1;
  80. merchantadd.StoreNo = dic["data"]["storeNo"].ToString();
  81. merchantadd.HdStatus = 1; //0 待审核 1 已通过 2 已开通分账 3 已添加接收方
  82. merchantadd.OutMchtNo = dic["data"]["outMchtNo"].ToString();
  83. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == mId);
  84. if (merchant != null)
  85. {
  86. merchant.Status = 1;
  87. }
  88. //商户开通分账
  89. JsonData Info = JsonMapper.ToObject(HaoDaHelper.Instance.OpenDivideAccounts(merchantadd.OutMchtNo, merchantadd.StoreNo));
  90. //开通失败
  91. if (Info["resultCode"].ToString() == "0")
  92. {
  93. merchantadd.Status = -1;
  94. merchantadd.WeChatRemark = Info["errorDesc"].ToString();
  95. }
  96. //开通成功
  97. if (Info["resultCode"].ToString() == "1")
  98. {
  99. merchantadd.OpenAct = 1;//是否开通分账(1 是 0 否)
  100. merchantadd.HdStatus = 2; //0 待审核 1 已通过 2 已开通分账 3 已添加接收方
  101. //分账接收方签约
  102. JsonData Infos = JsonMapper.ToObject(HaoDaHelper.Instance.DivideAccountsSign(AppConfig.Haoda.AcctNo, merchantadd.OutMchtNo, merchantadd.StoreNo));
  103. //失败
  104. if (Infos["resultCode"].ToString() == "0")
  105. {
  106. merchantadd.WeChatRemark = Infos["errorDesc"].ToString();
  107. }
  108. //成功
  109. else
  110. {
  111. merchantadd.HdStatus = 3; //0 待审核 1 已通过 2 已开通分账 3 已添加接收方
  112. if (!string.IsNullOrEmpty(merchantadd.ToAcctNo))
  113. {
  114. if (!merchantadd.ToAcctNo.Contains("," + AppConfig.Haoda.AcctNo + ","))
  115. {
  116. merchantadd.ToAcctNo += "," + AppConfig.Haoda.AcctNo + ",";
  117. }
  118. }
  119. else
  120. {
  121. merchantadd.ToAcctNo += "," + AppConfig.Haoda.AcctNo + ",";
  122. }
  123. merchantadd.HdPassDate = DateTime.Now;
  124. }
  125. }
  126. db.SaveChanges();
  127. //创客账号
  128. Models.Main1.WebCMSEntities db1 = new Models.Main1.WebCMSEntities();
  129. string pwd = merchantadd.MobilePhone.Substring(5);
  130. merchant.LoginPwd = function.MD532(pwd);
  131. Models.Main1.MerchantLoginInfo info = db1.MerchantLoginInfo.FirstOrDefault(m => m.LoginMobile == merchantadd.MobilePhone);
  132. if (info == null)
  133. {
  134. info = db1.MerchantLoginInfo.Add(new Models.Main1.MerchantLoginInfo()
  135. {
  136. LoginMobile = merchantadd.MobilePhone,
  137. }).Entity;
  138. db1.SaveChanges();
  139. }
  140. info.LoginPwd = function.MD532(pwd);
  141. db1.SaveChanges();
  142. Models.Main1.MerchantAccountLinkInfo infolink = db1.MerchantAccountLinkInfo.FirstOrDefault(m => m.LoginId == info.Id && m.MerchantId == merchantadd.Id && m.Kind == 2);
  143. if (infolink == null)
  144. {
  145. infolink = db1.MerchantAccountLinkInfo.Add(new Models.Main1.MerchantAccountLinkInfo()
  146. {
  147. LoginId = info.Id,
  148. MerchantId = merchantadd.Id,
  149. Kind = 2,
  150. }).Entity;
  151. db1.SaveChanges();
  152. }
  153. db1.Dispose();
  154. //开户意愿申请
  155. RedisDbconn.Instance.AddList("AlipayForHaoDaQueue", mId);
  156. RedisDbconn.Instance.AddList("WeChatForHaoDaQueue", mId);
  157. }
  158. }
  159. //审核被拒绝
  160. else if (dic["data"]["code"].ToString() == "3")
  161. {
  162. MerchantAddInfo merchantadd = db.MerchantAddInfo.FirstOrDefault(m => m.Id == mId);
  163. if (merchantadd != null)
  164. {
  165. string WeChatRemark = dic["data"]["option"].ToString();
  166. if(WeChatRemark.Contains("租赁协议") && WeChatRemark.Contains("水电煤") && WeChatRemark.Contains("门头合影") && WeChatRemark.Contains("下载地址"))
  167. {
  168. WeChatRemark = "门头不一致需补充 “名称不一致说明函”";
  169. }
  170. if(WeChatRemark.Contains("盒伙人在门头合影"))
  171. {
  172. WeChatRemark = "门头不一致需补充 “名称不一致说明函”";
  173. }
  174. if(WeChatRemark.Contains("不支持跨商户进件"))
  175. {
  176. WeChatRemark = "该商户已注册,错误指令【404】";
  177. }
  178. merchantadd.WeChatRemark = WeChatRemark;
  179. merchantadd.Status = -1;
  180. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == mId);
  181. if (merchant != null)
  182. {
  183. merchant.Status = -1;
  184. }
  185. db.SaveChanges();
  186. }
  187. }
  188. else
  189. {
  190. return "wait";
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "好哒商户进件状态查询异常");
  196. }
  197. return "";
  198. }
  199. }
  200. }