MerchantConfirmService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. using MySystem.Models;
  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>("MerchantConfirmQueue");
  27. if (!string.IsNullOrEmpty(content))
  28. {
  29. try
  30. {
  31. JsonData jsonObj = JsonMapper.ToObject(content);
  32. int MerchantId = int.Parse(jsonObj["MerchantId"].ToString());
  33. int Kind = 0;
  34. if(content.Contains("\"Kind\""))
  35. {
  36. Kind = int.Parse(jsonObj["Kind"].ToString());
  37. }
  38. StartDo(MerchantId, Kind);
  39. }
  40. catch (Exception ex)
  41. {
  42. LogHelper.Instance.WriteLog(DateTime.Now.ToString() + ":" + ex.ToString(), "public_service");
  43. }
  44. }
  45. else
  46. {
  47. Thread.Sleep(2000);
  48. }
  49. }
  50. }
  51. public void StartDo(int MerchantId, int Kind = 0)
  52. {
  53. WebCMSEntities db = new WebCMSEntities();
  54. MerchantAddInfo AddInfo = db.MerchantAddInfo.FirstOrDefault(m => m.Id == MerchantId);
  55. MerchantInfo merchant = db.MerchantInfo.FirstOrDefault(m => m.Id == MerchantId);
  56. if (AddInfo != null && merchant != null)
  57. {
  58. string BusinessCode = AddInfo.BusinessCode;
  59. if (string.IsNullOrEmpty(BusinessCode))
  60. {
  61. BusinessCode = "LKB" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(5);
  62. AddInfo.BusinessCode = BusinessCode;
  63. }
  64. string pwd = AddInfo.MobilePhone.Substring(5);
  65. merchant.LoginPwd = function.MD532(pwd);
  66. MerchantParamSet query = db.MerchantParamSet.FirstOrDefault(m => m.Id == MerchantId);
  67. if (query == null)
  68. {
  69. query = new MerchantParamSet();
  70. query.IsAll = 1;
  71. db.MerchantParamSet.Add(query);
  72. db.SaveChanges();
  73. }
  74. MerchantForMobile merchantForMobile = db.MerchantForMobile.FirstOrDefault(m => m.Mobile == AddInfo.MobilePhone);
  75. if (merchantForMobile == null)
  76. {
  77. merchantForMobile = db.MerchantForMobile.Add(new MerchantForMobile()
  78. {
  79. Mobile = AddInfo.MobilePhone,
  80. }).Entity;
  81. db.SaveChanges();
  82. }
  83. merchantForMobile.MerchantId = MerchantId;
  84. MerchantLoginInfo info = db.MerchantLoginInfo.FirstOrDefault(m => m.LoginMobile == AddInfo.MobilePhone);
  85. if (info == null)
  86. {
  87. info = db.MerchantLoginInfo.Add(new MerchantLoginInfo()
  88. {
  89. LoginMobile = AddInfo.MobilePhone,
  90. }).Entity;
  91. db.SaveChanges();
  92. }
  93. info.LoginPwd = function.MD532(pwd);
  94. db.SaveChanges();
  95. MerchantAccountLinkInfo infolink = db.MerchantAccountLinkInfo.FirstOrDefault(m => m.LoginId == info.Id && m.MerchantId == MerchantId && m.Kind == 1);
  96. if (infolink == null)
  97. {
  98. infolink = db.MerchantAccountLinkInfo.Add(new MerchantAccountLinkInfo()
  99. {
  100. LoginId = info.Id,
  101. MerchantId = MerchantId,
  102. Kind = 1,
  103. }).Entity;
  104. db.SaveChanges();
  105. }
  106. //微信
  107. if ((Kind == 0 || Kind == 2) && AddInfo.Status <= 0)
  108. {
  109. string result = WeChatFunction.Instance.MerchantCreate(AddInfo, merchant);
  110. if (result.Contains("\"applyment_id\":"))
  111. {
  112. JsonData jsonObj = JsonMapper.ToObject(result);
  113. AddInfo.ApplymentId = jsonObj["applyment_id"].ToString();
  114. AddInfo.WeChatRemark = "";
  115. AddInfo.Status = 0;
  116. merchant.Status = 0;
  117. db.SaveChanges();
  118. List<MerchantSign> signs = RedisDbconn.Instance.GetList<MerchantSign>("MerchantSignList", 1, 10000000);
  119. bool op = signs.Any(m => m.BusinessCode == BusinessCode);
  120. if (!op)
  121. {
  122. RedisDbconn.Instance.AddList("WeChatSignQueue", new MerchantSign()
  123. {
  124. BusinessCode = BusinessCode,
  125. MerchantAddInfoId = MerchantId,
  126. Status = ""
  127. });
  128. }
  129. signs.Clear();
  130. AddInfo.ApplymentId = AddInfo.ApplymentId;
  131. db.SaveChanges();
  132. }
  133. else if (result.Contains("\"message\":"))
  134. {
  135. JsonData jsonObj = JsonMapper.ToObject(result);
  136. AddInfo.Status = -1;
  137. merchant.Status = -1;
  138. AddInfo.WeChatRemark = jsonObj["message"].ToString();
  139. db.SaveChanges();
  140. }
  141. }
  142. //支付宝
  143. if ((Kind == 0 || Kind == 1) && AddInfo.QueryCount <= 0)
  144. {
  145. string content = AlipayFunction.Instance.GetBatchNo(AddInfo.IdCardName, AddInfo.MobilePhone, AddInfo.CreateMan);
  146. JsonData json = JsonMapper.ToObject(content);
  147. if (json["alipay_open_agent_create_response"]["code"].ToString() == "10000")
  148. {
  149. AddInfo.AlipayRemark = "";
  150. AddInfo.QueryCount = 0;
  151. merchant.QueryCount = 0;
  152. db.SaveChanges();
  153. string batch_no = json["alipay_open_agent_create_response"]["batch_no"].ToString();
  154. string Qualifications = AddInfo.Qualifications;
  155. if (!string.IsNullOrEmpty(Qualifications))
  156. {
  157. Qualifications = function.getPath("/" + Qualifications);
  158. }
  159. content = AlipayFunction.Instance.CommonSign(batch_no, AddInfo.UpdateMan, AddInfo.LicenseNumber, AddInfo.LicenseCopy, AddInfo.LicenseCopy, AddInfo.StoreEntrancePic, AddInfo.IndoorPic);
  160. json = JsonMapper.ToObject(content);
  161. if (json["alipay_open_agent_facetoface_sign_response"]["code"].ToString() == "10000")
  162. {
  163. content = AlipayFunction.Instance.AgentConfirm(batch_no);
  164. json = JsonMapper.ToObject(content);
  165. if (json["alipay_open_agent_confirm_response"]["code"].ToString() == "10000")
  166. {
  167. // AddInfo.AlipayAuthToken = json["alipay_open_agent_confirm_response"]["app_auth_token"].ToString();
  168. AddInfo.SeoTitle = batch_no;
  169. db.SaveChanges();
  170. RedisDbconn.Instance.AddList("AlipaySignQueue", new MerchantSign()
  171. {
  172. BusinessCode = batch_no,
  173. MerchantAddInfoId = MerchantId,
  174. Status = ""
  175. });
  176. // content = AlipayFunction.Instance.QuerySignStatus(batch_no);
  177. // content = AlipayFunction.Instance.CommonSignConfirm(batch_no);
  178. // json = JsonMapper.ToObject(content);
  179. // if (json["alipay_open_agent_commonsign_confirm_response"]["code"].ToString() == "10000")
  180. // {
  181. // AddInfo.QueryCount = 0;
  182. // AddInfo.SeoTitle = batch_no;
  183. // db.SaveChanges();
  184. // List<MerchantSign> signs = RedisDbconn.Instance.GetList<MerchantSign>("MerchantSignList", 1, 10000000);
  185. // bool op = signs.Any(m => m.BusinessCode == batch_no);
  186. // if (!op)
  187. // {
  188. // RedisDbconn.Instance.AddList("AlipaySignQueue", new MerchantSign()
  189. // {
  190. // BusinessCode = batch_no,
  191. // MerchantAddInfoId = MerchantId,
  192. // Status = ""
  193. // });
  194. // }
  195. // signs.Clear();
  196. // if (db.MerchantInfo.Any(m => m.Id == merchant.Id) == false)
  197. // {
  198. // RedisDbconn.Instance.AddList("MerchantList:" + merchant.UserId, merchant);
  199. // }
  200. // }
  201. // else
  202. // {
  203. // checkAlipay = false;
  204. // }
  205. }
  206. else
  207. {
  208. AddInfo.AlipayRemark = json["alipay_open_agent_confirm_response"]["sub_msg"].ToString();
  209. AddInfo.QueryCount = -1;
  210. merchant.QueryCount = -1;
  211. db.SaveChanges();
  212. }
  213. }
  214. else
  215. {
  216. AddInfo.AlipayRemark = json["alipay_open_agent_facetoface_sign_response"]["sub_msg"].ToString();
  217. AddInfo.QueryCount = -1;
  218. merchant.QueryCount = -1;
  219. db.SaveChanges();
  220. }
  221. }
  222. else
  223. {
  224. AddInfo.AlipayRemark = json["alipay_open_agent_create_response"]["sub_msg"].ToString();
  225. AddInfo.QueryCount = -1;
  226. merchant.QueryCount = -1;
  227. db.SaveChanges();
  228. }
  229. }
  230. }
  231. db.Dispose();
  232. }
  233. }
  234. }