| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378 |
- using dodohold.core;
- using Spire.Pdf.Xmp;
- using System.Data;
- using System.Drawing.Printing;
- using System.Text.Json;
- using System.Text.RegularExpressions;
- namespace molilian.core
- {
- public partial class AlimamaPlus
- {
- private const string base_orders_url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json";
- public int GetHistoryOrders(int sleep)
- {
- DateTime endTime = DateTime.Now;
- DateTime startTime = endTime.AddDays(-90);
- ////找出数据库最新的一天
- //var last = new DBContext.Table("tk_order_details")
- // .Order("modifiedTime DESC")
- // .Get<TkOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
- //if (last != null)
- //{
- // startTime = last.modifiedTime;
- //}
- //当日往前查询
- (int total, _, _) = GetTkOrders(startTime, endTime, DateTime.MinValue, false, sleep);
- return total;
- }
- public int GetIncyOrders(int sleep)
- {
- string cacheKey = $":cache:GetIncyOrders:{_accountName}";
- DateTime endTime = DateTime.Now;
- DateTime startTime = endTime.AddDays(-1);
- DateTime last_modifiedTime = RedisHelper.Get<DateTime>(cacheKey);
- ////找出数据库最新的一天
- //var last = new DBContext.Table("tk_order_details")
- // .Order("modifiedTime DESC")
- // .Get<TkOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
- if (last_modifiedTime != DateTime.MinValue)
- {
- startTime = last_modifiedTime;
- }
- else
- {
- last_modifiedTime = startTime;
- }
- //当日往前查询
- (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = GetTkOrders(startTime, endTime, last_modifiedTime, true, sleep);
- if (max_modifiedTime != DateTime.MinValue)
- {
- RedisHelper.Set(cacheKey, max_modifiedTime);
- }
- //todo 根据orderTimes、settlementTimes 更新被影响的相关行
- /*
-
- UPDATE tk_report tr
- JOIN (
- SELECT
- accountId,
- DATE(tbPaidTime) AS date,
- SUM(alipayTotalPrice) AS order_ord_amt,
- SUM(pubShareFeeForCommission) AS order_ord_tfee
- FROM tk_order_details
- WHERE tkStatus=3
- GROUP BY DATE(tbPaidTime), accountId
- ) AS sub
- ON tr.accountId = sub.accountId AND tr.report_date = sub.date
- SET tr.order_ord_amt = sub.order_ord_amt,
- tr.order_ord_tfee = sub.order_ord_tfee;
- */
- string sql = @"
- UPDATE tk_report tr
- JOIN (
- SELECT
- accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt, SUM(pubShareFeeForCommission) AS order_ord_tfee
- FROM tk_order_details
- WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=3
- GROUP BY DATE(tbPaidTime), accountId
- ) AS sub
- ON tr.accountId = sub.accountId AND tr.report_date = sub.date
- SET tr.order_ord_amt = sub.order_ord_amt, tr.order_ord_tfee = sub.order_ord_tfee;";
- DBContext.Execute(sql, new { orderTimes });
- sql = @"
- UPDATE tk_report tr
- JOIN (
- SELECT
- accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
- FROM tk_order_details
- WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=12
- GROUP BY DATE(tbPaidTime), accountId
- ) AS sub
- ON tr.accountId = sub.accountId AND tr.report_date = sub.date
- SET tr.order_ord_amt_12 = sub.order_ord_amt, tr.order_ord_tfee_12 = sub.order_ord_tfee;";
- DBContext.Execute(sql, new { orderTimes });
- sql = @"
- UPDATE tk_report tr
- JOIN (
- SELECT
- accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
- FROM tk_order_details
- WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=13
- GROUP BY DATE(tbPaidTime), accountId
- ) AS sub
- ON tr.accountId = sub.accountId AND tr.report_date = sub.date
- SET tr.order_ord_amt_13 = sub.order_ord_amt, tr.order_ord_tfee_13 = sub.order_ord_tfee;";
- DBContext.Execute(sql, new { orderTimes });
- sql = @"
- UPDATE tk_report tr
- JOIN (
- SELECT
- accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
- FROM tk_order_details
- WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=14
- GROUP BY DATE(tbPaidTime), accountId
- ) AS sub
- ON tr.accountId = sub.accountId AND tr.report_date = sub.date
- SET tr.order_ord_amt_14 = sub.order_ord_amt, tr.order_ord_tfee_14 = sub.order_ord_tfee;";
- DBContext.Execute(sql, new { orderTimes });
- return total;
- }
- public (int, DateTime, List<DateTime>) GetTkOrders(DateTime startTime, DateTime endTime, DateTime last_modifiedTime, bool desc = true, int sleep = 100)
- {
- List<DateTime> orderTimes = [];
- // 确保 startTime 小于 endTime
- if (startTime > endTime)
- {
- (startTime, endTime) = (endTime, startTime);
- }
- DateTime max_modifiedTime = last_modifiedTime;
- int pageNo = 1;
- string positionIndex = string.Empty;
- int pageSize = 100;
- bool hasNext = true;
- int total = 0;
- while (hasNext)
- {
- var ts = DateTime.Now.Convert2UnixTimestamp();
- //string jumpType = pageNo == 1 ? "0" : desc ? "1" : "-1";
- string jumpType = desc ? pageNo == 1 ? "0" : "1" : "-1";
- //string queryType = "1";
- string queryType = "4";
- string url = $"{base_orders_url}?t={ts}&_tb_token_={_tb_token}&pageNo={pageNo}&pageSize={pageSize}&startTime={startTime:yyyy-MM-dd}&endTime={endTime:yyyy-MM-dd}&payStatus=&queryType={queryType}&jumpType={jumpType}&tkTradeId=&positionIndex={positionIndex}";
- var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
- .AddHeaders("X-Requested-With", "XMLHttpRequest")
- .AddHeaders("Cookie", _cookies);
- #if DEBUG
- #else
- client.Proxy = _proxy;
- #endif
- if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
- var response = client.Request(url);
- if (response.ResponseException != null)
- {
- new LoggerLibrary("api_error", "fail")
- .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
- .Save();
- throw response.ResponseException;
- }
- var body = response.Body();
- JsonElement data;
- try
- {
- var root = body.Convert2JsonElement();
- var succss = root.Read<bool>("success");
- data = root.ElementRead("data");
- if (!succss)
- {
- throw new APIException(body);
- }
- }
- catch (Exception ex)
- {
- throw new APIException(body);
- }
- using var conn = DBContext.GetOpenConnection();
- conn.Open();
- try
- {
- foreach (var order in data.ElementRead("result").EnumerateArray())
- {
- TkOrderDetailDTO item = order.GetRawText().Convert2Object<TkOrderDetailDTO>();
- // 处理每个订单数据
- item.accountId = _accountId;
- item.accountName = _company;
- if (item.modifiedTime > max_modifiedTime) max_modifiedTime = item.modifiedTime;
- if (last_modifiedTime != DateTime.MinValue && last_modifiedTime >= item.modifiedTime)
- {
- return (total, max_modifiedTime, orderTimes);
- }
- //进行订单和转链匹配
- //string mktItemLink = $"https:{item.mktItemLink}";
- //item.itemId = GetRawItemId(mktItemLink);
- if (item.tkStatus == 3 && item.tbPaidTime != DateTime.MinValue) orderTimes.Add(item.tbPaidTime.Date);
- int? id = conn.Replace(item);
- if (id != null)
- {
- item.id = (int)id;
- TkOrderTrackingCore.MatchOrder(item, conn);
- }
- total++;
- }
- }
- catch (Exception ex)
- {
- throw;
- }
- finally
- {
- conn.Dispose();
- }
- pageNo++;
- hasNext = data.Read<bool>("hasNext");
- positionIndex = data.Read<string>("positionIndex");
- if (sleep > 0) Thread.Sleep(sleep);
- }
- return (total, max_modifiedTime, orderTimes);
- }
- string GetRawItemId(string url)
- {
- try
- {
- string cacheKey = $":cache:url2itemId:{url}";
- string itemId = RedisHelper.Get(cacheKey);
- if (itemId != null) return itemId;
- WebClientUtility client = new()
- {
- Proxy = _proxy,
- UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
- };
- #if DEBUG
- client.Proxy = null;
- #endif
- client.AllowAutoRedirect = false;
- var response = client.Request(url);
- if (response.Successed)
- {
- string location = response.ResponseMessage?.Headers?.Location?.ToString();
- if (!string.IsNullOrEmpty(location))
- {
- itemId = location.GetContentPart("id=", "");
- if (!string.IsNullOrEmpty(itemId))
- {
- RedisHelper.Set(cacheKey, itemId, 86400 * 30);
- }
- return itemId;
- }
- var responseBody = response.Body();
- if (response.ResponseMessage.IsSuccessStatusCode)
- {
- throw new APIException(responseBody);
- }
- else
- {
- throw new APIException($"{response.ResponseMessage.StatusCode}\t{responseBody}");
- }
- }
- else
- {
- throw response.ResponseException;
- }
- }
- catch (Exception ex)
- {
- string message = $"【GetRawItemId】[{url}\n{ex.Message}\n{ex.StackTrace}";
- NotifyCore.Notify(new NifyMessage
- {
- message = message,
- priority = NifyMessagePriority.high,
- tags = ["red_circle"]
- });
- }
- return null;
- }
- public void GetTkOrders222()
- {
- try
- {
- string url = "https://media.alimama.com/violationu2/warning_instance_page_v2.json?r=mx_208&toPage=1&pageSize=40¶ms=";
- var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
- .AddHeaders("X-Requested-With", "XMLHttpRequest")
- .AddHeaders("Cookie", _cookies);
- client.Proxy = _proxy;
- if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
- var response = client.Request(url);
- var body = response.Body();
- var root = body.Convert2JsonElement();
- int resultCode = root.Read<int>("resultCode", 0);
- int totalCount = root.PathRead<int>("data.totalCount", 0);
- bool success = root.Read("success", false);
- if (!success)
- {
- new LoggerLibrary("violationu", "warning_error")
- .Info(url, body)
- .Save();
- throw new Exception(body);
- }
- if (totalCount == 0) return;
- new LoggerLibrary("violationu", "warning").Info(url, body).Save();
- var list = root.ElementRead("data").ElementRead("result").EnumerateArray();
- foreach (var data in list)
- {
- var violation_commission = data.PathRead<decimal>("mediaInfo.订单虚假交易违规佣金", 0);
- new DBContext.Table("alimama_warning")
- .Fill(data)
- .Loader<int>("filterRuleId", 0)
- .Loader<long>("gmtCreate", 0)
- .Loader<long>("id", 0, "ali_id")
- .Loader<string>("idStr", string.Empty)
- .Loader<int>("mediaDimension", 0)
- .Loader<long>("mediaId", 0)
- .Loader<int>("mediaInfo.abn_cnt", 0, "abn_cnt")
- .Add("violation_commission", violation_commission)
- .Loader<int>("mediaInfo.warning_cnt", 0, "warning_cnt")
- .Loader<string>("mediaName", string.Empty)
- .Loader<long>("memberId", 0)
- .Loader<string>("noticeCategory", string.Empty)
- .Loader<int>("noticeTemplateId", 0)
- .Loader<string>("noticeTemplateName", string.Empty)
- .Loader<string>("orderFile", string.Empty)
- .Loader<string>("pid", string.Empty)
- .Loader<int>("punishRuleId", 0)
- .Loader<string>("riskType", string.Empty)
- .Loader<string>("roleName", string.Empty)
- .Loader<string>("ruleLink", string.Empty)
- .Loader<bool>("showOrderDetail", false)
- .Loader<int>("status", 0, "ali_status")
- .Loader<string>("warningText", string.Empty)
- .Add("accountName", _accountName)
- .Add("status", true)
- .Add("create_time", DateTime.Now)
- .Add("last_time", DateTime.Now)
- .Create(DBContext.InsertType.REPLACE);
- }
- }
- catch (Exception ex) { }
- }
- }
- }
|