CheckWeChatSignService.cs 10 KB

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