OrderTrackingCore.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. namespace molilian.core
  18. {
  19. public partial class TkOrderTrackingCore
  20. {
  21. public static void SaveLinkSummary(TkDataDTO item)
  22. {
  23. //清洗数据
  24. item.content = null;
  25. item.rawContent = null;
  26. item.rawContent2 = null;
  27. TkDataDTO newData = new()
  28. {
  29. channel = item.channel,
  30. ip = item.ip,
  31. oaid = item.oaid,
  32. itemId = item.itemId,
  33. itemName = item.itemName,
  34. taoToken = item.taoToken,
  35. shortLinkurl = item.shortLinkurl,
  36. deeplink_url = item.deeplink_url,
  37. create_time = item.create_time,
  38. };
  39. var config = TkConfigCore.Get();
  40. var ttl = config.fake_click_ttl * 3600;
  41. string cacheKey = $":cache:order_summary:{item.accountId}:{item.itemId}";
  42. RedisHelper.Set(cacheKey, newData, ttl);
  43. string mktId = item.mktId;
  44. if (!string.IsNullOrEmpty(mktId) && mktId.Contains('-'))
  45. {
  46. mktId = mktId.Split('-')[^1];
  47. cacheKey = $":cache:order_summary:{item.accountId}:mktId:{mktId}";
  48. RedisHelper.Set(cacheKey, newData, ttl);
  49. }
  50. cacheKey = $":cache:order_summary:{item.accountId}:{item.itemName}";
  51. RedisHelper.Set(cacheKey, newData, ttl);
  52. }
  53. public static TkDataDTO GetLinkSummary(int accountId, string itemId, string mktId, string itemTitle)
  54. {
  55. if (!string.IsNullOrEmpty(mktId) && mktId.Contains('-'))
  56. {
  57. mktId = mktId.Split('-')[^1];
  58. }
  59. var result = EndPointCore.ProcessEndPointNodes<TkDataDTO>(node =>
  60. {
  61. if (!node.is_public_api) return null;
  62. if (string.IsNullOrEmpty(node.redis_server)) return null;
  63. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  64. if (!string.IsNullOrEmpty(mktId))
  65. {
  66. string cacheKey = $":cache:order_summary:{accountId}:mktId:{mktId}";
  67. var result = redis.Get<TkDataDTO>(cacheKey);
  68. if (result != null) return result;
  69. }
  70. if (!string.IsNullOrEmpty(itemId))
  71. {
  72. string cacheKey = $":cache:order_summary:{accountId}:{itemId}";
  73. var result = redis.Get<TkDataDTO>(cacheKey);
  74. if (result != null) return result;
  75. }
  76. if (!string.IsNullOrEmpty(itemId))
  77. {
  78. string cacheKey = $":cache:order_summary:{accountId}:{itemTitle}";
  79. var result = redis.Get<TkDataDTO>(cacheKey);
  80. if (result != null) return result;
  81. }
  82. return null;
  83. });
  84. return result.Count > 0 ? result[0] : null;
  85. }
  86. public static async void RemoveLinkSummary(int accountId, string itemId, string mktId, string itemTitle)
  87. {
  88. await EndPointCore.ProcessEndPointNodesAsync(node =>
  89. {
  90. if (!node.is_public_api) return Task.CompletedTask;
  91. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  92. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  93. string cacheKey = $":cache:order_summary:{accountId}:{itemId}";
  94. redis.Del(cacheKey);
  95. cacheKey = $":cache:order_summary:{accountId}:mktId:{mktId}";
  96. redis.Del(cacheKey);
  97. cacheKey = $":cache:order_summary:{accountId}:{itemTitle}";
  98. redis.Del(cacheKey);
  99. return Task.CompletedTask;
  100. });
  101. }
  102. public static bool MatchOrder(TkPoolDTO account, TkOrderDetailDTO item, IDbConnection conn)
  103. {
  104. if (item.tbPaidTime < DateTime.Now.AddDays(-1)) return false;
  105. var summary = GetLinkSummary(item.accountId, item.itemId, item.mktId, item.itemTitle);
  106. if (summary == null) return false;
  107. var config = TkConfigCore.Get();
  108. Random rand = new();
  109. int click_num = rand.Next(config.fake_click_min, config.fake_click_max + 1);
  110. if (account.fake_click_min != 0 && account.fake_click_max != 0)
  111. click_num = rand.Next(account.fake_click_min, account.fake_click_max + 1);
  112. DateTime paidTime = item.tbPaidTime;
  113. if (paidTime < summary.create_time) return false;
  114. int minutes = paidTime.Minute / 10 * 10;
  115. DateTime rounded = new(paidTime.Year, paidTime.Month, paidTime.Day, paidTime.Hour, minutes, 0);
  116. string batchId = rounded.ToString("yyyyMMddHHmm");
  117. string accountName = item.accountName;
  118. string shortLinkUrl = summary.shortLinkurl;
  119. string deeplinkUrl = summary.deeplink_url;
  120. switch (account.fake_click_link_type)
  121. {
  122. case FakeClickLinkType.Deeplink:
  123. shortLinkUrl = string.Empty;
  124. break;
  125. case FakeClickLinkType.H5:
  126. deeplinkUrl = string.Empty;
  127. break;
  128. }
  129. DateTime expTime = item.tbPaidTime.Hour >= 21 ? item.tbPaidTime.AddHours(3) : item.tbPaidTime.Date.AddDays(1);
  130. var data = new TkOrderTrackingDTO()
  131. {
  132. channel = summary.channel,
  133. accountId = item.accountId,
  134. accountName = accountName,
  135. batchId = batchId,
  136. ip = summary.ip,
  137. oaid = summary.oaid,
  138. mktId = summary.mktId,
  139. itemId = summary.itemId,
  140. itemName = summary.itemName,
  141. taoToken = summary.taoToken,
  142. shortLinkUrl = shortLinkUrl,
  143. deeplinkUrl = deeplinkUrl,
  144. tradeId = item.tradeId,
  145. tradeParentId = item.tradeParentId,
  146. create_time = summary.create_time,
  147. click_time = item.clickTime,
  148. paid_time = item.tbPaidTime,
  149. exp_time = expTime,
  150. click_num = click_num,
  151. };
  152. conn.Replace(data);
  153. RemoveLinkSummary(item.accountId, item.itemId, item.mktId, item.itemTitle);
  154. return true;
  155. }
  156. }
  157. }