base.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc.Controllers;
  3. using Microsoft.AspNetCore.Mvc.Filters;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using dodohold.core;
  9. using static dodohold.core.ZTOExpress.CreateOrderArgs;
  10. using System.Net;
  11. using System.Security.Cryptography;
  12. using Spire.Pdf.Exporting.XPS.Schema;
  13. using System.Xml.Linq;
  14. using static QRCoder.PayloadGenerator;
  15. using TencentCloud.Ssl.V20191205.Models;
  16. using CSRedis;
  17. using System.Data;
  18. using TencentCloud.Omics.V20221128.Models;
  19. using TencentCloud.Csip.V20221121.Models;
  20. using COSXML.Network;
  21. using System.Security.Policy;
  22. namespace molilian.core
  23. {
  24. public partial class TkLogCore
  25. {
  26. public static bool save_dailys_log = false;
  27. public static string _test_oaid = "3B191CFA4C6B48F9BA459E915B57743BEC7D424979CC9C51BCAD0C245B1C7BA2";
  28. static TkLogCore()
  29. {
  30. int flag = RedisHelper.Get<int>("turn:save_dailys_log");
  31. if (flag == 1) save_dailys_log = true;
  32. }
  33. public static int BatchInsertLogDB(int limit)
  34. {
  35. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  36. {
  37. if (!node.is_public_api) return 0;
  38. if (CenterHub.IsCenter)
  39. {
  40. if (node.is_coupon_api) return 0;
  41. }
  42. else
  43. {
  44. if (!node.is_coupon_api) return 0;
  45. }
  46. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  47. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  48. return BatchInsertLogDB(limit, redis);
  49. });
  50. return result.Sum();
  51. }
  52. public static int BatchInsertLogDB(int limit, CSRedisClient redis)
  53. {
  54. int total = 0;
  55. using var connection = DBContext.GetOpenConnection();
  56. connection.Open();
  57. using var transaction = connection.BeginTransaction();
  58. try
  59. {
  60. //第三方接口写入日志
  61. total += task_insert_tk_logs(connection, transaction, limit, redis);
  62. total += task_insert_parse_tb_logs(connection, transaction, limit, redis);
  63. total += task_insert_parse_jd_logs(connection, transaction, limit, redis);
  64. total += task_insert_parse_pdd_logs(connection, transaction, limit, redis);
  65. total += task_insert_parse_dy_logs(connection, transaction, limit, redis);
  66. total += task_insert_parse_tool_logs(connection, transaction, limit, redis);
  67. total += task_insert_parse_deeplink_logs(connection, transaction, limit, redis);
  68. total += task_insert_parse_coupon_logs(connection, transaction, limit, redis);
  69. total += task_insert_parse_cps_logs(connection, transaction, limit, redis);
  70. total += task_insert_promotion_img_logs(connection, transaction, limit, redis);
  71. total += task_insert_parse_ks_logs(connection, transaction, limit, redis);
  72. transaction.Commit();
  73. }
  74. catch (Exception ex)
  75. {
  76. transaction.Rollback();
  77. _ = new LoggerLibrary("database_error", "parse_log")
  78. .Info(ex.Message, ex.StackTrace)
  79. .SaveAsync();
  80. NotifyCore.Notify(new NifyMessage
  81. {
  82. message = $"【写入日志异常】\n{ex.Message}\n{ex.StackTrace}",
  83. priority = NifyMessagePriority.high,
  84. tags = ["red_circle"]
  85. });
  86. }
  87. finally
  88. {
  89. connection.Close();
  90. }
  91. return total;
  92. }
  93. private static void saveCache(string channel, int accountId, string accountName, bool success, string message, string reason)
  94. {
  95. saveAccountCache("all", success, message, reason);
  96. saveAccountCache($"{channel}", success, message, reason);
  97. saveAccountCache($"{accountName}", success, message, reason);
  98. if (accountId != 0)
  99. {
  100. //todo 放着跑两天,要将读取的地方改成读取accountid
  101. saveAccountCache($"{channel}_{accountId}", success, message, reason);
  102. }
  103. }
  104. private static void saveAccountCache(string accountName, bool success, string message, string reason)
  105. {
  106. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMM}");
  107. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMdd}");
  108. RedisHelper.IncrBy($":total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  109. string result = success ? "success" : "fail";
  110. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  111. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  112. RedisHelper.IncrBy($":total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  113. if (!string.IsNullOrEmpty(message))
  114. {
  115. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  116. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  117. RedisHelper.SAdd($":total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  118. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  119. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  120. RedisHelper.IncrBy($":total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  121. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  122. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  123. RedisHelper.IncrBy($":total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  124. }
  125. if (!string.IsNullOrEmpty(reason))
  126. {
  127. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  128. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  129. RedisHelper.SAdd($":total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  130. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  131. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  132. RedisHelper.IncrBy($":total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  133. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  134. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  135. RedisHelper.IncrBy($":total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  136. }
  137. }
  138. private static async Task saveUnionCouponParseCacheAsync(TkDataDTO data)
  139. {
  140. string cacheKey = $":cache:parse:{data.ip}_{data.oaid}_{data.itemId}";
  141. await EndPointCore.ProcessEndPointNodesAsync(node =>
  142. {
  143. if (!node.is_coupon_api) return Task.CompletedTask;
  144. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  145. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  146. redis.Set(cacheKey, 1, 2 * 86400);
  147. return Task.CompletedTask;
  148. });
  149. }
  150. private static void saveClientRequestTotal(TkChannelEnum channel, string ip, string oaid)
  151. {
  152. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  153. RedisHelper.IncrBy(cacheKey);
  154. RedisHelper.Expire(cacheKey, 86400);
  155. if (!string.IsNullOrEmpty(oaid))
  156. {
  157. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  158. RedisHelper.IncrBy(cacheKey);
  159. RedisHelper.Expire(cacheKey, 86400);
  160. }
  161. }
  162. public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
  163. {
  164. if (string.IsNullOrEmpty(oaid)) return 0;
  165. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  166. return RedisHelper.Get<int>(cacheKey);
  167. }
  168. public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
  169. {
  170. if (string.IsNullOrEmpty(ip)) return 0;
  171. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  172. return RedisHelper.Get<int>(cacheKey);
  173. }
  174. private static void saveClientRequestTotal(CpsChannelEnum channel, string ip, string oaid)
  175. {
  176. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  177. RedisHelper.IncrBy(cacheKey);
  178. RedisHelper.Expire(cacheKey, 86400);
  179. if (!string.IsNullOrEmpty(oaid))
  180. {
  181. cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  182. RedisHelper.IncrBy(cacheKey);
  183. RedisHelper.Expire(cacheKey, 86400);
  184. }
  185. }
  186. public static int getClientRequestTotalByOAID(CpsChannelEnum channel, string oaid)
  187. {
  188. if (string.IsNullOrEmpty(oaid)) return 0;
  189. string cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  190. return RedisHelper.Get<int>(cacheKey);
  191. }
  192. public static int getClientRequestTotalByIp(CpsChannelEnum channel, string ip)
  193. {
  194. if (string.IsNullOrEmpty(ip)) return 0;
  195. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  196. return RedisHelper.Get<int>(cacheKey);
  197. }
  198. private static void saveParseCache(string channel, int accountId, string accountName,
  199. bool success, string message, string reason, string deeplink)
  200. {
  201. string dp_flag = deeplink switch
  202. {
  203. "" => "none",
  204. "tbopen://m.taobao.com/tbopen/index.html" or
  205. "pinduoduo://com.xunmeng.pinduoduo/" or
  206. "snssdk1128://feed?refer=web" or
  207. "bdnetdisk://n/action.EXTERNAL_ACTIVITY" or
  208. "openapp.jdmobile://virtual?params=" => "home",
  209. _ => success ? "success" : "fail",
  210. };
  211. //关于dp的缓存
  212. saveParseAccountCache($"dp_{dp_flag}:all", success, message, reason);
  213. saveParseAccountCache($"dp_{dp_flag}:{channel}", success, message, reason);
  214. saveParseAccountCache($"dp_{dp_flag}:{accountName}", success, message, reason);
  215. if (accountId != 0)
  216. {
  217. saveParseAccountCache($"dp_{dp_flag}:{channel}_{accountId}", success, message, reason);
  218. }
  219. saveParseAccountCache("all", success, message, reason);
  220. saveParseAccountCache($"{channel}", success, message, reason);
  221. saveParseAccountCache($"{accountName}", success, message, reason);
  222. if (accountId != 0)
  223. {
  224. //todo 放着跑两天,要将读取的地方改成读取accountid
  225. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  226. }
  227. }
  228. private static void saveParseAccountCache(string accountName, bool success,
  229. string message, string reason)
  230. {
  231. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  232. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  233. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  234. string result = success ? "success" : "fail";
  235. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  236. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  237. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  238. if (!string.IsNullOrEmpty(message))
  239. {
  240. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  241. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  242. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  243. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  244. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  245. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  246. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  247. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  248. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  249. }
  250. if (!string.IsNullOrEmpty(reason))
  251. {
  252. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  253. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  254. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  255. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  256. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  257. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  258. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  259. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  260. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  261. }
  262. }
  263. public static int GetTotal(string keyname, bool all_node = true)
  264. {
  265. //:coupon_total:tb:20240706
  266. //:coupon_total:tb:success:20240706
  267. //:coupon_total:tb:放弃转链:20240706
  268. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  269. {
  270. if (!node.is_public_api) return 0;
  271. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  272. if (!all_node)
  273. {
  274. if (CenterHub.IsCenter)
  275. {
  276. if (node.is_coupon_api) { return 0; }
  277. }
  278. else
  279. {
  280. if (!node.is_coupon_api) { return 0; }
  281. }
  282. }
  283. #if DEBUG
  284. switch (node.name)
  285. {
  286. case "bj":
  287. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  288. break;
  289. case "gz":
  290. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  291. break;
  292. case "coupon1":
  293. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  294. break;
  295. default: return 0;
  296. }
  297. #endif
  298. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  299. int count = redis.Get<int>(keyname);
  300. return count;
  301. });
  302. return result.Sum();
  303. }
  304. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  305. {
  306. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  307. {
  308. if (!node.is_public_api) return [];
  309. if (string.IsNullOrEmpty(node.redis_server)) return [];
  310. if (!all_node)
  311. {
  312. if (CenterHub.IsCenter)
  313. {
  314. if (node.is_coupon_api) { return []; }
  315. }
  316. else
  317. {
  318. if (!node.is_coupon_api) { return []; }
  319. }
  320. }
  321. #if DEBUG
  322. switch (node.name)
  323. {
  324. case "bj":
  325. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  326. break;
  327. case "gz":
  328. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  329. break;
  330. case "coupon1":
  331. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  332. break;
  333. default: return [];
  334. }
  335. #endif
  336. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  337. string[] message_keys = redis.SMembers(keyname);
  338. return message_keys;
  339. });
  340. string[] message_keys = [];
  341. foreach (var arr in result)
  342. {
  343. message_keys = message_keys.Union(arr).ToArray();
  344. }
  345. return message_keys;
  346. }
  347. }
  348. }