PosPushDataNewHelper.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Library;
  5. using MySystem.SpModels;
  6. namespace MySystem
  7. {
  8. public class PosPushDataNewHelper
  9. {
  10. public readonly static PosPushDataNewHelper Instance = new PosPushDataNewHelper();
  11. private PosPushDataNewHelper()
  12. { }
  13. //绑定数据
  14. public static void Bind(Merchants mer, string rnd = "")
  15. {
  16. try
  17. {
  18. if(mer.ProductType == "22")
  19. {
  20. mer.ProductType = GetLiShuaBrand(mer.SnNo);
  21. }
  22. Dictionary<string, object> dataContent = new Dictionary<string, object>();
  23. dataContent.Add("pos_sn", mer.SnNo); //机具sn
  24. dataContent.Add("mer_no", mer.MerNo); //商户号
  25. dataContent.Add("id_card", mer.MerIdcardNo); //商户身份证
  26. dataContent.Add("mer_name", mer.MerName); //商户姓名
  27. dataContent.Add("mer_mobile", mer.MerMobile); //商户手机号
  28. dataContent.Add("request_id", getRequestId(mer.SnNo + mer.MerNo + rnd, "bind")); //流水号
  29. dataContent.Add("bind_time", mer.CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
  30. dataContent.Add("brand", int.Parse(function.CheckInt(mer.ProductType))); //品牌
  31. if(mer.ProductType == "29")
  32. {
  33. dataContent.Add("user_id", mer.QueryCount); //创客ID
  34. }
  35. Push("bind", dataContent);
  36. }
  37. catch (Exception ex)
  38. {
  39. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送绑定数据异常");
  40. }
  41. }
  42. public static void Bind0(BindRecord mer)
  43. {
  44. try
  45. {
  46. if(mer.ProductType == "22")
  47. {
  48. mer.ProductType = GetLiShuaBrand(mer.MerSnNo);
  49. }
  50. Dictionary<string, object> dataContent = new Dictionary<string, object>();
  51. dataContent.Add("pos_sn", mer.MerSnNo); //机具sn
  52. dataContent.Add("mer_no", mer.MerNo); //商户号
  53. dataContent.Add("id_card", ""); //商户身份证
  54. dataContent.Add("mer_name", mer.MerName); //商户姓名
  55. dataContent.Add("mer_mobile", mer.MerNo); //商户手机号
  56. dataContent.Add("request_id", getRequestId(mer.Id, "bind")); //流水号
  57. dataContent.Add("bind_time", mer.CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
  58. dataContent.Add("brand", int.Parse(function.CheckInt(mer.ProductType))); //品牌
  59. Push("bind", dataContent);
  60. }
  61. catch (Exception ex)
  62. {
  63. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送绑定数据异常");
  64. }
  65. }
  66. public static void Bind2(MerchantRecord mer)
  67. {
  68. try
  69. {
  70. if(mer.ProductType == "22")
  71. {
  72. mer.ProductType = GetLiShuaBrand(mer.MerSnNo);
  73. }
  74. Dictionary<string, object> dataContent = new Dictionary<string, object>();
  75. dataContent.Add("pos_sn", mer.MerSnNo); //机具sn
  76. dataContent.Add("mer_no", mer.MerNo); //商户号
  77. dataContent.Add("id_card", mer.LegalIdCard); //商户身份证
  78. dataContent.Add("mer_name", mer.MerName); //商户姓名
  79. dataContent.Add("mer_mobile", ""); //商户手机号
  80. dataContent.Add("request_id", getRequestId(mer.Id, "merchant")); //流水号
  81. dataContent.Add("bind_time", mer.CreateTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
  82. dataContent.Add("brand", int.Parse(function.CheckInt(mer.ProductType))); //品牌
  83. Push("bind", dataContent);
  84. }
  85. catch (Exception ex)
  86. {
  87. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送绑定数据异常");
  88. }
  89. }
  90. //解绑数据
  91. public static void UnBind(UnBindRecord mer)
  92. {
  93. try
  94. {
  95. Dictionary<string, object> dataContent = new Dictionary<string, object>();
  96. dataContent.Add("pos_sn", mer.MerSnNo); //机具sn
  97. dataContent.Add("mer_no", mer.MerNo); //商户号
  98. dataContent.Add("request_id", getRequestId(mer.Id, "unbind")); //流水号
  99. dataContent.Add("un_bind_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间
  100. dataContent.Add("brand", int.Parse(function.CheckInt(mer.ProductType))); //品牌
  101. Push("un_bind", dataContent);
  102. }
  103. catch (Exception ex)
  104. {
  105. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送解绑数据异常");
  106. }
  107. }
  108. //换绑数据
  109. public static void ChangeBind(ChangeBindRecord mer)
  110. {
  111. try
  112. {
  113. Dictionary<string, object> dataContent = new Dictionary<string, object>();
  114. dataContent.Add("request_id", getRequestId(mer.Id, "changebind")); //流水号
  115. dataContent.Add("mer_no", mer.MerNo); //商户号
  116. dataContent.Add("source_pos_sn", mer.MerSnNo); //来源机具sn
  117. dataContent.Add("source_brand", int.Parse(function.CheckInt(mer.ProductType))); //来源品牌
  118. dataContent.Add("to_pos_sn", mer.MerNewSnNo); //兑换机具sn
  119. dataContent.Add("to_brand", int.Parse(function.CheckInt(mer.ProductType))); //兑换品牌
  120. dataContent.Add("change_bind_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //换绑时间
  121. Push("change_bind", dataContent);
  122. }
  123. catch (Exception ex)
  124. {
  125. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送换绑数据异常");
  126. }
  127. }
  128. //押金数据
  129. public static void Deposit(ActivateRecord act)
  130. {
  131. try
  132. {
  133. List<string> BrandIds = new List<string>();
  134. decimal TradeAmount = decimal.Parse(function.CheckNum(act.SeoTitle));
  135. BrandIds.Add("1");
  136. BrandIds.Add("4");
  137. BrandIds.Add("6");
  138. BrandIds.Add("8");
  139. BrandIds.Add("9");
  140. BrandIds.Add("15");
  141. BrandIds.Add("22");
  142. BrandIds.Add("27");
  143. BrandIds.Add("30");
  144. BrandIds.Add("33");
  145. if(BrandIds.Contains(act.ProductType))
  146. {
  147. TradeAmount = TradeAmount / 100;
  148. }
  149. if(act.ProductType == "22")
  150. {
  151. act.ProductType = GetLiShuaBrand(act.SnNo);
  152. }
  153. Dictionary<string, object> dataContent = new Dictionary<string, object>();
  154. dataContent.Add("pos_sn", act.SnNo); //机具sn
  155. dataContent.Add("mer_no", act.MerNo); //商户号
  156. if(Utils.Instance.IsWifi(int.Parse(act.ProductType)) || Utils.Instance.IsHaoDa(int.Parse(act.ProductType)))
  157. {
  158. dataContent.Add("request_id", getRequestId(act.SnNo + act.MerMobile, "deposit")); //流水号
  159. }
  160. else
  161. {
  162. dataContent.Add("request_id", getRequestId(act.Id, "deposit")); //流水号
  163. }
  164. if(act.ProductType == "14")
  165. {
  166. dataContent.Add("deposit_amount", 0); //押金金额
  167. }
  168. else
  169. {
  170. dataContent.Add("deposit_amount", TradeAmount); //押金金额
  171. }
  172. dataContent.Add("trade_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
  173. dataContent.Add("card_type", 1); //卡类型
  174. dataContent.Add("brand", int.Parse(function.CheckInt(act.ProductType))); //品牌
  175. if(act.ProductType == "29")
  176. {
  177. dataContent.Add("ext_field", act.Field4); //押金活动编码
  178. }
  179. else if(act.ProductType == "36")
  180. {
  181. dataContent.Add("ext_field", act.Field3); //奖励金额
  182. }
  183. else if(act.ProductType == "14" && (function.CheckNull(act.SeoKeyword).Contains("双百套餐") || act.SnNo.StartsWith("CS")))
  184. {
  185. dataContent.Add("fee_amt", TradeAmount); //秒到费
  186. }
  187. Push("deposit", dataContent);
  188. }
  189. catch (Exception ex)
  190. {
  191. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送押金数据异常");
  192. }
  193. }
  194. public static void Deposit(TradeRecord trade)
  195. {
  196. try
  197. {
  198. List<string> BrandIds = new List<string>();
  199. decimal TradeAmount = trade.TradeAmount;
  200. BrandIds.Add("1");
  201. BrandIds.Add("4");
  202. BrandIds.Add("6");
  203. // BrandIds.Add("7");
  204. BrandIds.Add("8");
  205. BrandIds.Add("9");
  206. BrandIds.Add("15");
  207. BrandIds.Add("22");
  208. BrandIds.Add("27");
  209. BrandIds.Add("30");
  210. BrandIds.Add("33");
  211. if(BrandIds.Contains(trade.ProductType))
  212. {
  213. TradeAmount = TradeAmount / 100;
  214. }
  215. if(trade.ProductType == "22")
  216. {
  217. trade.ProductType = GetLiShuaBrand(trade.TradeSnNo);
  218. }
  219. Dictionary<string, object> dataContent = new Dictionary<string, object>();
  220. dataContent.Add("pos_sn", trade.TradeSnNo); //机具sn
  221. dataContent.Add("mer_no", trade.MerNo); //商户号
  222. dataContent.Add("request_id", getRequestId(trade.Id, "deposit1")); //流水号
  223. dataContent.Add("deposit_amount", TradeAmount); //押金金额
  224. dataContent.Add("trade_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
  225. dataContent.Add("card_type", 1); //卡类型
  226. dataContent.Add("brand", int.Parse(function.CheckInt(trade.ProductType))); //品牌
  227. Push("deposit", dataContent);
  228. }
  229. catch (Exception ex)
  230. {
  231. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送押金数据异常");
  232. }
  233. }
  234. //激活数据
  235. public static void Active(ActivateRecord act)
  236. {
  237. try
  238. {
  239. if(act.ProductType == "22")
  240. {
  241. act.ProductType = GetLiShuaBrand(act.SnNo);
  242. }
  243. Dictionary<string, object> dataContent = new Dictionary<string, object>();
  244. dataContent.Add("pos_sn", act.SnNo); //机具sn
  245. dataContent.Add("mobile", act.MerMobile); //商户号
  246. dataContent.Add("request_id", getRequestId(act.Id, "active")); //流水号
  247. dataContent.Add("activation_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); //激活时间
  248. dataContent.Add("brand", int.Parse(function.CheckInt(act.ProductType))); //品牌
  249. Push("active", dataContent);
  250. }
  251. catch (Exception ex)
  252. {
  253. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送激活数据异常");
  254. }
  255. }
  256. //交易数据
  257. public static void Trade(TradeRecord trade)
  258. {
  259. try
  260. {
  261. int QrPayFlag = 0;
  262. int BankCardType = 1;
  263. string FeeRate = function.CheckNum(trade.ErrorCode);
  264. string FeeAmt = function.CheckNum(trade.ErrorMsg);
  265. List<string> BrandIds = new List<string>();
  266. decimal TradeAmount = trade.TradeAmount;
  267. BrandIds.Add("1");
  268. BrandIds.Add("4");
  269. BrandIds.Add("6");
  270. BrandIds.Add("7");
  271. BrandIds.Add("8");
  272. BrandIds.Add("9");
  273. BrandIds.Add("15");
  274. BrandIds.Add("22");
  275. BrandIds.Add("27");
  276. BrandIds.Add("30");
  277. BrandIds.Add("33");
  278. if(BrandIds.Contains(trade.ProductType))
  279. {
  280. TradeAmount = TradeAmount / 100;
  281. }
  282. if(trade.ProductType == "22")
  283. {
  284. trade.ProductType = GetLiShuaBrand(trade.TradeSnNo);
  285. }
  286. if (trade.ProductType == "1" || trade.ProductType == "27")
  287. {
  288. if (trade.TradeType == "02") QrPayFlag = 1;
  289. BankCardType = int.Parse(trade.BankCardType);
  290. decimal feeRateStr = decimal.Parse(FeeRate) * 100;
  291. FeeRate = feeRateStr.ToString().TrimEnd('0');
  292. if(FeeRate != "0") FeeRate = FeeRate.TrimEnd('0');
  293. if(FeeAmt != "0" && !string.IsNullOrEmpty(FeeAmt)) FeeAmt = FeeAmt.TrimEnd('0');
  294. }
  295. else if (trade.ProductType == "2")
  296. {
  297. if (trade.TradeType == "CLOUD_PAY") QrPayFlag = 1;
  298. if (trade.TradeType == "CREDIT_BY_CARD")
  299. {
  300. BankCardType = 1;
  301. }
  302. else
  303. {
  304. BankCardType = 0;
  305. }
  306. FeeRate = FeeRate.Replace("0.00", "0.");
  307. }
  308. else if (trade.ProductType == "4")
  309. {
  310. if (trade.TradeType == "200") QrPayFlag = 1;
  311. if (trade.BankCardType == "100")
  312. {
  313. BankCardType = 1;
  314. }
  315. else if (trade.BankCardType == "200")
  316. {
  317. BankCardType = 0;
  318. }
  319. decimal feeRateStr = decimal.Parse(FeeRate) / 10000;
  320. FeeRate = feeRateStr.ToString().TrimEnd('0');
  321. if(FeeRate != "0") FeeRate = FeeRate.TrimEnd('0');
  322. if(FeeAmt != "0" && !string.IsNullOrEmpty(FeeAmt)) FeeAmt = FeeAmt.TrimEnd('0');
  323. }
  324. else if (trade.ProductType == "6" || trade.ProductType == "22" || trade.ProductType == "33")
  325. {
  326. if (trade.BankCardType == "02" || trade.BankCardType == "12" || trade.BankCardType == "30" || trade.BankCardType == "31" || trade.BankCardType == "M3" || trade.Field2 == "1517")
  327. {
  328. BankCardType = 1;
  329. }
  330. else
  331. {
  332. BankCardType = 0;
  333. }
  334. }
  335. else if (trade.ProductType == "7")
  336. {
  337. if (trade.BankCardType == "OA" || trade.BankCardType == "CC" || trade.BankCardType == "SCC")
  338. {
  339. BankCardType = 1;
  340. }
  341. else
  342. {
  343. BankCardType = 0;
  344. }
  345. }
  346. else if (trade.ProductType == "8" || trade.ProductType == "9")
  347. {
  348. if (trade.TradeType == "200") QrPayFlag = 1;
  349. if (trade.BankCardType == "100")
  350. {
  351. BankCardType = 1;
  352. }
  353. else if (trade.BankCardType == "200")
  354. {
  355. BankCardType = 0;
  356. }
  357. decimal feeRateStr = decimal.Parse(FeeRate) / 10000;
  358. FeeRate = feeRateStr.ToString().TrimEnd('0');
  359. if(FeeRate != "0") FeeRate = FeeRate.TrimEnd('0');
  360. if(FeeAmt != "0" && !string.IsNullOrEmpty(FeeAmt)) FeeAmt = FeeAmt.TrimEnd('0');
  361. }
  362. else if (trade.ProductType == "10" || trade.ProductType == "11")
  363. {
  364. if (trade.TradeType == "F010") QrPayFlag = 1;
  365. if (trade.BankCardType == "01")
  366. {
  367. BankCardType = 1;
  368. }
  369. else if (trade.BankCardType == "00")
  370. {
  371. BankCardType = 0;
  372. }
  373. FeeRate = FeeRate.Replace("0.00", "0.");
  374. }
  375. else if (trade.ProductType == "12") //盒易付
  376. {
  377. if (trade.Field3 == "1") QrPayFlag = 1;
  378. if (trade.BankCardType == "C" || trade.TradeType == "1" || trade.TradeType == "2" || trade.TradeType == "3")
  379. {
  380. BankCardType = 1;
  381. }
  382. else if (trade.BankCardType == "D")
  383. {
  384. BankCardType = 0;
  385. }
  386. }
  387. else if (trade.ProductType == "15")
  388. {
  389. if (trade.BankCardType == "02")
  390. {
  391. BankCardType = 1;
  392. }
  393. else
  394. {
  395. BankCardType = 0;
  396. }
  397. if(FeeAmt != "0") FeeAmt = FeeAmt.TrimEnd('0');
  398. }
  399. else if (trade.ProductType == "30") //拉卡拉
  400. {
  401. if (trade.BankCardType == "01" || trade.BankCardType == "61" || trade.BankCardType == "91" || trade.BankCardType == "92")
  402. {
  403. BankCardType = 1;
  404. }
  405. else
  406. {
  407. BankCardType = 0;
  408. }
  409. }
  410. else if (trade.ProductType == "34") //联动掌中宝
  411. {
  412. if (trade.TradeType == "QuickPass") QrPayFlag = 1;
  413. if (trade.BankCardType == "CC" || trade.TradeType == "AliPay" || trade.TradeType == "Wechat")
  414. {
  415. BankCardType = 1;
  416. }
  417. else
  418. {
  419. BankCardType = 0;
  420. }
  421. }
  422. else if (Utils.Instance.IsWifi(int.Parse(trade.ProductType)))
  423. {
  424. FeeRate = "0.60";
  425. FeeAmt = trade.ErrorMsg;
  426. }
  427. Dictionary<string, string> fee = GetFee(trade.TradeSnNo, FeeRate, FeeAmt);
  428. FeeRate = fee["FeeRate"];
  429. FeeAmt = fee["FeeAmt"];
  430. Dictionary<string, object> dataContent = new Dictionary<string, object>();
  431. dataContent.Add("pos_sn", trade.TradeSnNo); //机具sn
  432. dataContent.Add("mer_no", trade.MerNo); //商户号
  433. dataContent.Add("request_id", trade.TradeSerialNo); //流水号
  434. dataContent.Add("trade_amount", TradeAmount); //交易金额
  435. dataContent.Add("trade_time", trade.CreateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //交易时间
  436. dataContent.Add("brand", int.Parse(function.CheckInt(trade.ProductType))); //品牌
  437. dataContent.Add("card_type", BankCardType); //卡类型
  438. dataContent.Add("qr_pay_flag", QrPayFlag); //云闪付标记
  439. dataContent.Add("fee_rate", FeeRate); //费率
  440. dataContent.Add("fee_amt", FeeAmt); //秒到费
  441. if(trade.ProductType == "14")
  442. {
  443. dataContent.Add("is_first", 1);
  444. }
  445. if(trade.ProductType == "23" || trade.ProductType == "24" || trade.ProductType == "25" || trade.ProductType == "26" || trade.ProductType == "32")
  446. {
  447. dataContent.Add("ext_field", trade.ChannelSerial);
  448. }
  449. Push("trade", dataContent);
  450. }
  451. catch (Exception ex)
  452. {
  453. Utils.WriteLog(DateTime.Now.ToString() + "\n" + ex.ToString() + "\n\n", "推送交易数据异常");
  454. }
  455. }
  456. public static void Push(string dataType, Dictionary<string, object> dataContent)
  457. {
  458. Dictionary<string, object> data = new Dictionary<string, object>();
  459. data.Add("data_type", dataType);
  460. data.Add("data_content", dataContent);
  461. // if(RedisDbconn.Instance.Get<string>("KxsPosDataQueueStatus") == "1")
  462. // {
  463. // RedisDbconn.Instance.AddList("KxsPosDataQueue", Newtonsoft.Json.JsonConvert.SerializeObject(data));
  464. // }
  465. PosPushHelper.Instance.DoSomeThing(Newtonsoft.Json.JsonConvert.SerializeObject(data));
  466. }
  467. public static string getRequestId(int id, string type)
  468. {
  469. if(id > 0)
  470. {
  471. return function.MD5_32(id.ToString() + type);
  472. }
  473. return DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
  474. }
  475. public static string getRequestId(string str, string type)
  476. {
  477. return function.MD5_32(str + type);
  478. }
  479. public static string GetLiShuaBrand(string PosSn)
  480. {
  481. string brand = "22";
  482. JavaProductModels.WebCMSEntities db = new JavaProductModels.WebCMSEntities();
  483. JavaProductModels.KxsMachine pos = db.KxsMachine.FirstOrDefault(m => m.PosSn == PosSn);
  484. if(pos != null)
  485. {
  486. brand = pos.BrandId.ToString();
  487. }
  488. db.Dispose();
  489. return brand;
  490. }
  491. public static Dictionary<string, string> GetFee(string PosSn, string FeeRate, string FeeAmt)
  492. {
  493. if(decimal.Parse(FeeRate) > 0.63M) FeeRate = "0.63";
  494. List<int> BrandIds = new List<int>();
  495. BrandIds.Add(1);
  496. BrandIds.Add(7);
  497. BrandIds.Add(10);
  498. BrandIds.Add(11);
  499. BrandIds.Add(15);
  500. BrandIds.Add(27);
  501. Dictionary<string, string> result = new Dictionary<string, string>();
  502. JavaProductModels.WebCMSEntities db = new JavaProductModels.WebCMSEntities();
  503. JavaProductModels.KxsMachine pos = db.KxsMachine.FirstOrDefault(m => m.PosSn == PosSn && BrandIds.Contains(m.BrandId));
  504. if(pos != null)
  505. {
  506. result.Add("FeeRate", pos.MachineRatio.ToString("f2"));
  507. result.Add("FeeAmt", pos.SecondFee.ToString("f2"));
  508. }
  509. else
  510. {
  511. result.Add("FeeRate", FeeRate);
  512. result.Add("FeeAmt", FeeAmt);
  513. }
  514. db.Dispose();
  515. return result;
  516. }
  517. }
  518. }