OrderTrackingCore.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. cacheKey = $":cache:order_summary:{item.accountId}:{item.itemName}";
  44. RedisHelper.Set(cacheKey, newData, ttl);
  45. }
  46. public static TkDataDTO GetLinkSummary(int accountId, string itemId, string itemTitle)
  47. {
  48. var result = EndPointCore.ProcessEndPointNodes<TkDataDTO>(node =>
  49. {
  50. if (!node.is_public_api) return null;
  51. if (string.IsNullOrEmpty(node.redis_server)) return null;
  52. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  53. if (!string.IsNullOrEmpty(itemId))
  54. {
  55. string cacheKey = $":cache:order_summary:{accountId}:{itemId}";
  56. var result = redis.Get<TkDataDTO>(cacheKey);
  57. if (result != null) return result;
  58. }
  59. if (!string.IsNullOrEmpty(itemId))
  60. {
  61. string cacheKey = $":cache:order_summary:{accountId}:{itemTitle}";
  62. var result = redis.Get<TkDataDTO>(cacheKey);
  63. if (result != null) return result;
  64. }
  65. return null;
  66. });
  67. return result.Count > 0 ? result[0] : null;
  68. }
  69. public static async void RemoveLinkSummary(int accountId, string itemId)
  70. {
  71. string cacheKey = $":cache:order_summary:{accountId}:{itemId}";
  72. await EndPointCore.ProcessEndPointNodesAsync(node =>
  73. {
  74. if (!node.is_public_api) return Task.CompletedTask;
  75. if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
  76. var redis = RedisClientManager.GetRedisClient(node.redis_server);
  77. redis.Del(cacheKey);
  78. return Task.CompletedTask;
  79. });
  80. }
  81. public static bool MatchOrder(TkOrderDetailDTO item, IDbConnection conn)
  82. {
  83. if (item.tbPaidTime < DateTime.Now.AddDays(-1)) return false;
  84. var summary = GetLinkSummary(item.accountId, item.itemId, item.itemTitle);
  85. if (summary == null) return false;
  86. var config = TkConfigCore.Get();
  87. Random rand = new();
  88. int click_num = rand.Next(config.fake_click_min, config.fake_click_max + 1);
  89. DateTime paidTime = item.tbPaidTime;
  90. if (paidTime < summary.create_time) return false;
  91. int minutes = paidTime.Minute / 10 * 10;
  92. DateTime rounded = new(paidTime.Year, paidTime.Month, paidTime.Day, paidTime.Hour, minutes, 0);
  93. string batchId = rounded.ToString("yyyyMMddHHmm");
  94. string accountName = item.accountName;
  95. DateTime expTime = item.tbPaidTime.Hour >= 21 ? item.tbPaidTime.AddHours(3) : item.tbPaidTime.Date.AddDays(1);
  96. var data = new TkOrderTrackingDTO()
  97. {
  98. channel = summary.channel,
  99. accountId = item.accountId,
  100. accountName = accountName,
  101. batchId = batchId,
  102. ip = summary.ip,
  103. oaid = summary.oaid,
  104. itemId = summary.itemId,
  105. itemName = summary.itemName,
  106. taoToken = summary.taoToken,
  107. shortLinkUrl = summary.shortLinkurl,
  108. deeplinkUrl = summary.deeplink_url,
  109. tradeId = item.tradeId,
  110. tradeParentId = item.tradeParentId,
  111. create_time = summary.create_time,
  112. click_time = item.clickTime,
  113. paid_time = item.tbPaidTime,
  114. exp_time = expTime,
  115. click_num = click_num,
  116. };
  117. conn.Replace(data);
  118. RemoveLinkSummary(item.accountId, item.itemTitle);
  119. return true;
  120. }
  121. }
  122. }