OrderTrackingCore.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 Dataoke;
  10. using Google.Protobuf.WellKnownTypes;
  11. using System.Diagnostics;
  12. using System.Text.Json;
  13. using TencentCloud.Tcm.V20210413.Models;
  14. using System.Security.Cryptography;
  15. using System.Data;
  16. using System.Threading.Channels;
  17. using YunhuiKit;
  18. namespace molilian.core
  19. {
  20. public partial class TkOrderTrackingCore
  21. {
  22. public static void SaveLinkSummary(TkDataDTO item)
  23. {
  24. //清洗数据
  25. item.content = null;
  26. item.rawContent = null;
  27. item.rawContent2 = null;
  28. TkDataDTO newData = new()
  29. {
  30. channel = item.channel,
  31. ip = item.ip,
  32. oaid = item.oaid,
  33. itemId = item.itemId,
  34. itemName = item.itemName,
  35. taoToken = item.taoToken,
  36. shortLinkurl = item.shortLinkurl,
  37. deeplink_url = item.deeplink_url,
  38. create_time = item.create_time,
  39. };
  40. var config = TkConfigCore.Get();
  41. var ttl = config.fake_click_ttl * 3600;
  42. string cacheKey = $":cache:order_summary:{item.accountId}:{item.itemId}";
  43. RedisHelper.Set(cacheKey, newData, ttl);
  44. string mktId = item.mktId;
  45. if (!string.IsNullOrEmpty(mktId) && mktId.Contains('-'))
  46. {
  47. mktId = mktId.Split('-')[^1];
  48. cacheKey = $":cache:order_summary:{item.accountId}:mktId:{mktId}";
  49. RedisHelper.Set(cacheKey, newData, ttl);
  50. }
  51. cacheKey = $":cache:order_summary:{item.accountId}:{item.itemName}";
  52. RedisHelper.Set(cacheKey, newData, ttl);
  53. }
  54. public static async Task<TkDataDTO> GetLinkSummaryAsync(int accountId, string itemId, string mktId, string itemTitle)
  55. {
  56. try
  57. {
  58. mktId = !string.IsNullOrEmpty(mktId) && mktId.Contains('-')
  59. ? mktId.Split('-')[^1]
  60. : mktId;
  61. var tasks = EndPointCore.List()
  62. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  63. .Select(async node =>
  64. {
  65. try
  66. {
  67. var redisServer = EndPointCore.GetRedisServer(node);
  68. if (string.IsNullOrEmpty(redisServer)) return null;
  69. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  70. var redis = scope.Client;
  71. // 按优先级依次查询不同的缓存key
  72. TkDataDTO result = null;
  73. if (!string.IsNullOrEmpty(mktId))
  74. {
  75. result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:mktId:{mktId}");
  76. if (result != null) return result;
  77. }
  78. if (!string.IsNullOrEmpty(itemId))
  79. {
  80. result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemId}");
  81. if (result != null) return result;
  82. if (!string.IsNullOrEmpty(itemTitle))
  83. {
  84. result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemTitle}");
  85. if (result != null) return result;
  86. }
  87. }
  88. }
  89. catch (Exception ex)
  90. {
  91. }
  92. return null;
  93. });
  94. var results = await Task.WhenAll(tasks);
  95. return results.FirstOrDefault(r => r != null);
  96. }
  97. catch (Exception)
  98. {
  99. // TODO: 添加日志记录
  100. return null;
  101. }
  102. }
  103. public static async Task RemoveLinkSummaryAsync(int accountId, string itemId, string mktId, string itemTitle)
  104. {
  105. try
  106. {
  107. var tasks = EndPointCore.List()
  108. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  109. .Select(async node =>
  110. {
  111. try
  112. {
  113. var redisServer = EndPointCore.GetRedisServer(node);
  114. if (string.IsNullOrEmpty(redisServer)) return;
  115. using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
  116. var redis = scope.Client;
  117. var keys = new[]
  118. {
  119. $":cache:order_summary:{accountId}:{itemId}",
  120. $":cache:order_summary:{accountId}:mktId:{mktId}",
  121. $":cache:order_summary:{accountId}:{itemTitle}"
  122. }.Where(k => !string.IsNullOrEmpty(k));
  123. // 批量删除所有相关缓存
  124. await redis.DelAsync(keys.ToArray());
  125. }
  126. catch (Exception ex) { }
  127. });
  128. await Task.WhenAll(tasks);
  129. }
  130. catch (Exception)
  131. {
  132. // TODO: 添加日志记录
  133. }
  134. }
  135. //public static async void RemoveLinkSummary(int accountId, string itemId, string mktId, string itemTitle)
  136. //{
  137. // await EndPointCore.ProcessEndPointNodesAsync(node =>
  138. // {
  139. // if (!node.is_public_api) return Task.CompletedTask;
  140. // if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  141. // var redis = RedisClientManager.GetRedisClient(node.redis_server);
  142. // string cacheKey = $":cache:order_summary:{accountId}:{itemId}";
  143. // redis.Del(cacheKey);
  144. // cacheKey = $":cache:order_summary:{accountId}:mktId:{mktId}";
  145. // redis.Del(cacheKey);
  146. // cacheKey = $":cache:order_summary:{accountId}:{itemTitle}";
  147. // redis.Del(cacheKey);
  148. // return Task.CompletedTask;
  149. // });
  150. //}
  151. public static async Task<bool> MatchOrder(TkPoolDTO account, TkOrderDetailDTO item)
  152. {
  153. if (item.tbPaidTime < DateTime.Now.AddDays(-1)) return false;
  154. var summary = await GetLinkSummaryAsync(item.accountId, item.itemId, item.mktId, item.itemTitle);
  155. if (summary == null) return false;
  156. var config = TkConfigCore.Get();
  157. Random rand = new();
  158. int click_num = rand.Next(config.fake_click_min, config.fake_click_max + 1);
  159. if (account.fake_click_min != 0 && account.fake_click_max != 0)
  160. click_num = rand.Next(account.fake_click_min, account.fake_click_max + 1);
  161. DateTime paidTime = item.tbPaidTime;
  162. if (paidTime < summary.create_time) return false;
  163. int minutes = paidTime.Minute / 10 * 10;
  164. DateTime rounded = new(paidTime.Year, paidTime.Month, paidTime.Day, paidTime.Hour, minutes, 0);
  165. string batchId = rounded.ToString("yyyyMMddHHmm");
  166. string accountName = item.accountName;
  167. string shortLinkUrl = summary.shortLinkurl;
  168. string deeplinkUrl = summary.deeplink_url;
  169. switch (account.fake_click_link_type)
  170. {
  171. case FakeClickLinkType.Deeplink:
  172. shortLinkUrl = string.Empty;
  173. break;
  174. case FakeClickLinkType.H5:
  175. deeplinkUrl = string.Empty;
  176. break;
  177. }
  178. DateTime expTime = item.tbPaidTime.Hour >= 21 ? item.tbPaidTime.AddHours(3) : item.tbPaidTime.Date.AddDays(1);
  179. var data = new TkOrderTrackingDTO()
  180. {
  181. channel = summary.channel,
  182. accountId = item.accountId,
  183. accountName = accountName,
  184. batchId = batchId,
  185. ip = summary.ip,
  186. oaid = summary.oaid,
  187. mktId = summary.mktId,
  188. itemId = summary.itemId,
  189. itemName = summary.itemName,
  190. taoToken = summary.taoToken,
  191. shortLinkUrl = shortLinkUrl,
  192. deeplinkUrl = deeplinkUrl,
  193. tradeId = item.tradeId,
  194. tradeParentId = item.tradeParentId,
  195. create_time = summary.create_time,
  196. click_time = item.clickTime,
  197. paid_time = item.tbPaidTime,
  198. exp_time = expTime,
  199. click_num = click_num,
  200. };
  201. using var conn = DBContext.GetOpenConnection();
  202. conn.Replace(data);
  203. //RemoveLinkSummary(item.accountId, item.itemId, item.mktId, item.itemTitle);
  204. await RemoveLinkSummaryAsync(item.accountId, item.itemId, item.mktId, item.itemTitle);
  205. return true;
  206. }
  207. }
  208. }