| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc.Controllers;
- using Microsoft.AspNetCore.Mvc.Filters;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using dodohold.core;
- using Dataoke;
- using Google.Protobuf.WellKnownTypes;
- using System.Diagnostics;
- using System.Text.Json;
- using TencentCloud.Tcm.V20210413.Models;
- using System.Security.Cryptography;
- using System.Data;
- using System.Threading.Channels;
- using YunhuiKit;
- namespace molilian.core
- {
- public partial class TkOrderTrackingCore
- {
- public static void SaveLinkSummary(TkDataDTO item)
- {
- //清洗数据
- item.content = null;
- item.rawContent = null;
- item.rawContent2 = null;
- TkDataDTO newData = new()
- {
- channel = item.channel,
- ip = item.ip,
- oaid = item.oaid,
- itemId = item.itemId,
- itemName = item.itemName,
- taoToken = item.taoToken,
- shortLinkurl = item.shortLinkurl,
- deeplink_url = item.deeplink_url,
- create_time = item.create_time,
- };
- var config = TkConfigCore.Get();
- var ttl = config.fake_click_ttl * 3600;
- string cacheKey = $":cache:order_summary:{item.accountId}:{item.itemId}";
- RedisHelper.Set(cacheKey, newData, ttl);
- string mktId = item.mktId;
- if (!string.IsNullOrEmpty(mktId) && mktId.Contains('-'))
- {
- mktId = mktId.Split('-')[^1];
- cacheKey = $":cache:order_summary:{item.accountId}:mktId:{mktId}";
- RedisHelper.Set(cacheKey, newData, ttl);
- }
- cacheKey = $":cache:order_summary:{item.accountId}:{item.itemName}";
- RedisHelper.Set(cacheKey, newData, ttl);
- }
- public static async Task<TkDataDTO> GetLinkSummaryAsync(int accountId, string itemId, string mktId, string itemTitle)
- {
- try
- {
- mktId = !string.IsNullOrEmpty(mktId) && mktId.Contains('-')
- ? mktId.Split('-')[^1]
- : mktId;
- var tasks = EndPointCore.List()
- .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
- .Select(async node =>
- {
- var redisServer = EndPointCore.GetRedisServer(node);
- if (string.IsNullOrEmpty(redisServer)) return null;
- using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
- var redis = scope.Client;
- // 按优先级依次查询不同的缓存key
- TkDataDTO result = null;
- if (!string.IsNullOrEmpty(mktId))
- {
- result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:mktId:{mktId}");
- if (result != null) return result;
- }
- if (!string.IsNullOrEmpty(itemId))
- {
- result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemId}");
- if (result != null) return result;
- if (!string.IsNullOrEmpty(itemTitle))
- {
- result = await redis.GetAsync<TkDataDTO>($":cache:order_summary:{accountId}:{itemTitle}");
- if (result != null) return result;
- }
- }
- return null;
- });
- var results = await Task.WhenAll(tasks);
- return results.FirstOrDefault(r => r != null);
- }
- catch (Exception)
- {
- // TODO: 添加日志记录
- return null;
- }
- }
- public static async Task RemoveLinkSummaryAsync(int accountId, string itemId, string mktId, string itemTitle)
- {
- try
- {
- var tasks = EndPointCore.List()
- .Where(node => node.is_public_api && !string.IsNullOrEmpty(node.redis_server))
- .Select(async node =>
- {
- var redisServer = EndPointCore.GetRedisServer(node);
- if (string.IsNullOrEmpty(redisServer)) return;
- using var scope = await RedisClientFactory.CreateScopeAsync(redisServer);
- var redis = scope.Client;
- var keys = new[]
- {
- $":cache:order_summary:{accountId}:{itemId}",
- $":cache:order_summary:{accountId}:mktId:{mktId}",
- $":cache:order_summary:{accountId}:{itemTitle}"
- }.Where(k => !string.IsNullOrEmpty(k));
- // 批量删除所有相关缓存
- await redis.DelAsync(keys.ToArray());
- });
- await Task.WhenAll(tasks);
- }
- catch (Exception)
- {
- // TODO: 添加日志记录
- }
- }
- //public static async void RemoveLinkSummary(int accountId, string itemId, string mktId, string itemTitle)
- //{
- // await EndPointCore.ProcessEndPointNodesAsync(node =>
- // {
- // if (!node.is_public_api) return Task.CompletedTask;
- // if (string.IsNullOrEmpty(node.redis_server)) return Task.CompletedTask;
- // var redis = RedisClientManager.GetRedisClient(node.redis_server);
- // string cacheKey = $":cache:order_summary:{accountId}:{itemId}";
- // redis.Del(cacheKey);
- // cacheKey = $":cache:order_summary:{accountId}:mktId:{mktId}";
- // redis.Del(cacheKey);
- // cacheKey = $":cache:order_summary:{accountId}:{itemTitle}";
- // redis.Del(cacheKey);
- // return Task.CompletedTask;
- // });
- //}
- public static async Task<bool> MatchOrder(TkPoolDTO account, TkOrderDetailDTO item)
- {
- if (item.tbPaidTime < DateTime.Now.AddDays(-1)) return false;
- var summary = await GetLinkSummaryAsync(item.accountId, item.itemId, item.mktId, item.itemTitle);
- if (summary == null) return false;
- var config = TkConfigCore.Get();
- Random rand = new();
- int click_num = rand.Next(config.fake_click_min, config.fake_click_max + 1);
- if (account.fake_click_min != 0 && account.fake_click_max != 0)
- click_num = rand.Next(account.fake_click_min, account.fake_click_max + 1);
- DateTime paidTime = item.tbPaidTime;
- if (paidTime < summary.create_time) return false;
- int minutes = paidTime.Minute / 10 * 10;
- DateTime rounded = new(paidTime.Year, paidTime.Month, paidTime.Day, paidTime.Hour, minutes, 0);
- string batchId = rounded.ToString("yyyyMMddHHmm");
- string accountName = item.accountName;
- string shortLinkUrl = summary.shortLinkurl;
- string deeplinkUrl = summary.deeplink_url;
- switch (account.fake_click_link_type)
- {
- case FakeClickLinkType.Deeplink:
- shortLinkUrl = string.Empty;
- break;
- case FakeClickLinkType.H5:
- deeplinkUrl = string.Empty;
- break;
- }
- DateTime expTime = item.tbPaidTime.Hour >= 21 ? item.tbPaidTime.AddHours(3) : item.tbPaidTime.Date.AddDays(1);
- var data = new TkOrderTrackingDTO()
- {
- channel = summary.channel,
- accountId = item.accountId,
- accountName = accountName,
- batchId = batchId,
- ip = summary.ip,
- oaid = summary.oaid,
- mktId = summary.mktId,
- itemId = summary.itemId,
- itemName = summary.itemName,
- taoToken = summary.taoToken,
- shortLinkUrl = shortLinkUrl,
- deeplinkUrl = deeplinkUrl,
- tradeId = item.tradeId,
- tradeParentId = item.tradeParentId,
- create_time = summary.create_time,
- click_time = item.clickTime,
- paid_time = item.tbPaidTime,
- exp_time = expTime,
- click_num = click_num,
- };
- using var conn = DBContext.GetOpenConnection();
- conn.Replace(data);
- //RemoveLinkSummary(item.accountId, item.itemId, item.mktId, item.itemTitle);
- await RemoveLinkSummaryAsync(item.accountId, item.itemId, item.mktId, item.itemTitle);
- return true;
- }
- }
- }
|