base.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. private static void saveClientRequestTotal(string channel, string ip, string oaid)
  163. {
  164. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  165. RedisHelper.IncrBy(cacheKey);
  166. RedisHelper.Expire(cacheKey, 86400);
  167. if (!string.IsNullOrEmpty(oaid))
  168. {
  169. cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  170. RedisHelper.IncrBy(cacheKey);
  171. RedisHelper.Expire(cacheKey, 86400);
  172. }
  173. }
  174. public static int getClientRequestTotalByOAID(TkChannelEnum channel, string oaid)
  175. {
  176. if (string.IsNullOrEmpty(oaid)) return 0;
  177. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  178. return RedisHelper.Get<int>(cacheKey);
  179. }
  180. public static int getClientRequestTotalByIp(TkChannelEnum channel, string ip)
  181. {
  182. if (string.IsNullOrEmpty(ip)) return 0;
  183. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  184. return RedisHelper.Get<int>(cacheKey);
  185. }
  186. public static int getClientRequestTotalByOAID(string channel, string oaid)
  187. {
  188. if (string.IsNullOrEmpty(oaid)) return 0;
  189. string cacheKey = $":cache:{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  190. return RedisHelper.Get<int>(cacheKey);
  191. }
  192. public static int getClientRequestTotalByIp(string channel, string ip)
  193. {
  194. if (string.IsNullOrEmpty(ip)) return 0;
  195. string cacheKey = $":cache:{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  196. return RedisHelper.Get<int>(cacheKey);
  197. }
  198. private static void saveClientRequestTotal(CpsChannelEnum channel, string ip, string oaid)
  199. {
  200. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  201. RedisHelper.IncrBy(cacheKey);
  202. RedisHelper.Expire(cacheKey, 86400);
  203. if (!string.IsNullOrEmpty(oaid))
  204. {
  205. cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  206. RedisHelper.IncrBy(cacheKey);
  207. RedisHelper.Expire(cacheKey, 86400);
  208. }
  209. }
  210. public static int getClientRequestTotalByOAID(CpsChannelEnum channel, string oaid)
  211. {
  212. if (string.IsNullOrEmpty(oaid)) return 0;
  213. string cacheKey = $":cache:cps_{channel}:oaid:{DateTime.Now:yyyyMMdd}:{oaid}";
  214. return RedisHelper.Get<int>(cacheKey);
  215. }
  216. public static int getClientRequestTotalByIp(CpsChannelEnum channel, string ip)
  217. {
  218. if (string.IsNullOrEmpty(ip)) return 0;
  219. string cacheKey = $":cache:cps_{channel}:ip:{DateTime.Now:yyyyMMdd}:{ip}";
  220. return RedisHelper.Get<int>(cacheKey);
  221. }
  222. private static void saveParseCache(string channel, int accountId, string accountName,
  223. bool success, string message, string reason, string deeplink)
  224. {
  225. string dp_flag = deeplink switch
  226. {
  227. "" => "none",
  228. "tbopen://m.taobao.com/tbopen/index.html" or
  229. "pinduoduo://com.xunmeng.pinduoduo/" or
  230. "snssdk1128://feed?refer=web" or
  231. "bdnetdisk://n/action.EXTERNAL_ACTIVITY" or
  232. "openapp.jdmobile://virtual?params=" or "openapp.jdmobile://" => "home",
  233. _ => success ? "success" : "fail",
  234. };
  235. //关于dp的缓存
  236. saveParseAccountCache($"dp_{dp_flag}:all", success, message, reason);
  237. saveParseAccountCache($"dp_{dp_flag}:{channel}", success, message, reason);
  238. saveParseAccountCache($"dp_{dp_flag}:{accountName}", success, message, reason);
  239. if (accountId != 0)
  240. {
  241. saveParseAccountCache($"dp_{dp_flag}:{channel}_{accountId}", success, message, reason);
  242. }
  243. saveParseAccountCache("all", success, message, reason);
  244. saveParseAccountCache($"{channel}", success, message, reason);
  245. saveParseAccountCache($"{accountName}", success, message, reason);
  246. if (accountId != 0)
  247. {
  248. //todo 放着跑两天,要将读取的地方改成读取accountid
  249. saveParseAccountCache($"{channel}_{accountId}", success, message, reason);
  250. }
  251. }
  252. private static void saveParseAccountCache(string accountName, bool success,
  253. string message, string reason)
  254. {
  255. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMM}");
  256. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMdd}");
  257. RedisHelper.IncrBy($":parse_total:{accountName}:{DateTime.Now:yyyyMMddHH}");
  258. string result = success ? "success" : "fail";
  259. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMM}");
  260. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMdd}");
  261. RedisHelper.IncrBy($":parse_total:{accountName}:{result}:{DateTime.Now:yyyyMMddHH}");
  262. if (!string.IsNullOrEmpty(message))
  263. {
  264. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMM}", message);
  265. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMdd}", message);
  266. RedisHelper.SAdd($":parse_total:{accountName}:message:{DateTime.Now:yyyyMMddHH}", message);
  267. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMM}");
  268. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMdd}");
  269. RedisHelper.IncrBy($":parse_total:{accountName}:{message}:{DateTime.Now:yyyyMMddHH}");
  270. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMM}");
  271. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMdd}");
  272. RedisHelper.IncrBy($":parse_total:{accountName}:message:{message}:{DateTime.Now:yyyyMMddHH}");
  273. }
  274. if (!string.IsNullOrEmpty(reason))
  275. {
  276. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMM}", reason);
  277. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMdd}", reason);
  278. RedisHelper.SAdd($":parse_total:{accountName}:reason:{DateTime.Now:yyyyMMddHH}", reason);
  279. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMM}");
  280. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMdd}");
  281. RedisHelper.IncrBy($":parse_total:{accountName}:{reason}:{DateTime.Now:yyyyMMddHH}");
  282. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMM}");
  283. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMdd}");
  284. RedisHelper.IncrBy($":parse_total:{accountName}:reason:{reason}:{DateTime.Now:yyyyMMddHH}");
  285. }
  286. }
  287. public static int GetTotal(string keyname, bool all_node = true)
  288. {
  289. //:coupon_total:tb:20240706
  290. //:coupon_total:tb:success:20240706
  291. //:coupon_total:tb:放弃转链:20240706
  292. var result = EndPointCore.ProcessEndPointNodes<int>(node =>
  293. {
  294. if (!node.is_public_api) return 0;
  295. if (string.IsNullOrEmpty(node.redis_server)) return 0;
  296. if (!all_node)
  297. {
  298. if (CenterHub.IsCenter)
  299. {
  300. if (node.is_coupon_api) { return 0; }
  301. }
  302. else
  303. {
  304. if (!node.is_coupon_api) { return 0; }
  305. }
  306. }
  307. #if DEBUG
  308. switch (node.name)
  309. {
  310. case "bj":
  311. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  312. break;
  313. case "gz":
  314. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  315. break;
  316. case "coupon1":
  317. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  318. break;
  319. default: return 0;
  320. }
  321. #endif
  322. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  323. int count = redis.Get<int>(keyname);
  324. return count;
  325. });
  326. return result.Sum();
  327. }
  328. public static string[] GetTotalKeys(string keyname, bool all_node = true)
  329. {
  330. var result = EndPointCore.ProcessEndPointNodes<string[]>(node =>
  331. {
  332. if (!node.is_public_api) return [];
  333. if (string.IsNullOrEmpty(node.redis_server)) return [];
  334. if (!all_node)
  335. {
  336. if (CenterHub.IsCenter)
  337. {
  338. if (node.is_coupon_api) { return []; }
  339. }
  340. else
  341. {
  342. if (!node.is_coupon_api) { return []; }
  343. }
  344. }
  345. #if DEBUG
  346. switch (node.name)
  347. {
  348. case "bj":
  349. node.redis_server = "101.200.46.46:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  350. break;
  351. case "gz":
  352. node.redis_server = "8.138.110.158:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=webhook";
  353. break;
  354. case "coupon1":
  355. node.redis_server = "c1api.molilian.com:6379,password=pKBiS4ka2IpXayIdcx00,defaultDatabase=0,idleTimeout=20000,preheat=3,tryit=2,ssl=false,prefix=coupon";
  356. break;
  357. default: return [];
  358. }
  359. #endif
  360. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  361. string[] message_keys = redis.SMembers(keyname);
  362. return message_keys;
  363. });
  364. string[] message_keys = [];
  365. foreach (var arr in result)
  366. {
  367. message_keys = message_keys.Union(arr).ToArray();
  368. }
  369. return message_keys;
  370. }
  371. }
  372. }