OrderTrackingCore.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. var redisServer = EndPointCore.GetRedisServer(node);
  66. if (string.IsNullOrEmpty(redisServer)) return null;
  67. using var scope = RedisClientFactory.CreateScope(redisServer);
  68. var redis = scope.Client;
  69. // 按优先级依次查询不同的缓存key
  70. TkDataDTO result = null;
  71. if (!string.IsNullOrEmpty(mktId))
  72. {
  73. result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:mktId:{mktId}");
  74. if (result != null) return result;
  75. }
  76. if (!string.IsNullOrEmpty(itemId))
  77. {
  78. result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemId}");
  79. if (result != null) return result;
  80. if (!string.IsNullOrEmpty(itemTitle))
  81. {
  82. result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemTitle}");
  83. if (result != null) return result;
  84. }
  85. }
  86. return null;
  87. });
  88. var results = await Task.WhenAll(tasks);
  89. return results.FirstOrDefault(r => r != null);
  90. }
  91. catch (Exception)
  92. {
  93. // TODO: 添加日志记录
  94. return null;
  95. }
  96. }
  97. public static async Task RemoveLinkSummaryAsync(int accountId, string itemId, string mktId, string itemTitle)
  98. {
  99. try
  100. {
  101. var tasks = EndPointCore.List()
  102. .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
  103. .Select(async node =>
  104. {
  105. var redisServer = EndPointCore.GetRedisServer(node);
  106. if (string.IsNullOrEmpty(redisServer)) return;
  107. using var scope = RedisClientFactory.CreateScope(redisServer);
  108. var redis = scope.Client;
  109. var keys = new[]
  110. {
  111. $":cache:order_summary:{accountId}:{itemId}",
  112. $":cache:order_summary:{accountId}:mktId:{mktId}",
  113. $":cache:order_summary:{accountId}:{itemTitle}"
  114. }.Where(k => !string.IsNullOrEmpty(k));
  115. // 批量删除所有相关缓存
  116. await redis.DelAsync(keys.ToArray());
  117. });
  118. await Task.WhenAll(tasks);
  119. }
  120. catch (Exception)
  121. {
  122. // TODO: 添加日志记录
  123. }
  124. }
  125. //public static async void RemoveLinkSummary(int accountId, string itemId, string mktId, string itemTitle)
  126. //{
  127. // await EndPointCore.ProcessEndPointNodesAsync(node =>
  128. // {
  129. // if (!node.is_public_api) return Task.CompletedTask;
  130. // if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  131. // var redis = RedisClientManager.GetRedisClient(node.redis_server);
  132. // string cacheKey = $":cache:order_summary:{accountId}:{itemId}";
  133. // redis.Del(cacheKey);
  134. // cacheKey = $":cache:order_summary:{accountId}:mktId:{mktId}";
  135. // redis.Del(cacheKey);
  136. // cacheKey = $":cache:order_summary:{accountId}:{itemTitle}";
  137. // redis.Del(cacheKey);
  138. // return Task.CompletedTask;
  139. // });
  140. //}
  141. public static async Task<bool> MatchOrder(TkPoolDTO account, TkOrderDetailDTO item)
  142. {
  143. if (item.tbPaidTime < DateTime.Now.AddDays(-1)) return false;
  144. var summary = await GetLinkSummaryAsync(item.accountId, item.itemId, item.mktId, item.itemTitle);
  145. if (summary == null) return false;
  146. var config = TkConfigCore.Get();
  147. Random rand = new();
  148. int click_num = rand.Next(config.fake_click_min, config.fake_click_max + 1);
  149. if (account.fake_click_min != 0 && account.fake_click_max != 0)
  150. click_num = rand.Next(account.fake_click_min, account.fake_click_max + 1);
  151. DateTime paidTime = item.tbPaidTime;
  152. if (paidTime < summary.create_time) return false;
  153. int minutes = paidTime.Minute / 10 * 10;
  154. DateTime rounded = new(paidTime.Year, paidTime.Month, paidTime.Day, paidTime.Hour, minutes, 0);
  155. string batchId = rounded.ToString("yyyyMMddHHmm");
  156. string accountName = item.accountName;
  157. string shortLinkUrl = summary.shortLinkurl;
  158. string deeplinkUrl = summary.deeplink_url;
  159. switch (account.fake_click_link_type)
  160. {
  161. case FakeClickLinkType.Deeplink:
  162. shortLinkUrl = string.Empty;
  163. break;
  164. case FakeClickLinkType.H5:
  165. deeplinkUrl = string.Empty;
  166. break;
  167. }
  168. DateTime expTime = item.tbPaidTime.Hour >= 21 ? item.tbPaidTime.AddHours(3) : item.tbPaidTime.Date.AddDays(1);
  169. var data = new TkOrderTrackingDTO()
  170. {
  171. channel = summary.channel,
  172. accountId = item.accountId,
  173. accountName = accountName,
  174. batchId = batchId,
  175. ip = summary.ip,
  176. oaid = summary.oaid,
  177. mktId = summary.mktId,
  178. itemId = summary.itemId,
  179. itemName = summary.itemName,
  180. taoToken = summary.taoToken,
  181. shortLinkUrl = shortLinkUrl,
  182. deeplinkUrl = deeplinkUrl,
  183. tradeId = item.tradeId,
  184. tradeParentId = item.tradeParentId,
  185. create_time = summary.create_time,
  186. click_time = item.clickTime,
  187. paid_time = item.tbPaidTime,
  188. exp_time = expTime,
  189. click_num = click_num,
  190. };
  191. using var conn = DBContext.GetOpenConnection();
  192. conn.Replace(data);
  193. //RemoveLinkSummary(item.accountId, item.itemId, item.mktId, item.itemTitle);
  194. await RemoveLinkSummaryAsync(item.accountId, item.itemId, item.mktId, item.itemTitle);
  195. return true;
  196. }
  197. }
  198. }