orders.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. using dodohold.core;
  2. using Spire.Pdf.Xmp;
  3. using System.Data;
  4. using System.Drawing.Printing;
  5. using System.Text.Json;
  6. using System.Text.RegularExpressions;
  7. namespace molilian.core
  8. {
  9. public partial class AlimamaPlus
  10. {
  11. private const string base_orders_url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json";
  12. public int GetHistoryOrders(int sleep)
  13. {
  14. DateTime endTime = DateTime.Now;
  15. DateTime startTime = endTime.AddDays(-90);
  16. ////找出数据库最新的一天
  17. //var last = new DBContext.Table("tk_order_details")
  18. // .Order("modifiedTime DESC")
  19. // .Get<TkOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
  20. //if (last != null)
  21. //{
  22. // startTime = last.modifiedTime;
  23. //}
  24. //当日往前查询
  25. (int total, _) = GetTkOrders(startTime, endTime, DateTime.MinValue, false, sleep);
  26. return total;
  27. }
  28. public int GetIncyOrders(int sleep)
  29. {
  30. string cacheKey = $":cache:GetIncyOrders:{_accountName}";
  31. DateTime endTime = DateTime.Now;
  32. DateTime startTime = endTime.AddDays(-1);
  33. DateTime last_modifiedTime = RedisHelper.Get<DateTime>(cacheKey);
  34. ////找出数据库最新的一天
  35. //var last = new DBContext.Table("tk_order_details")
  36. // .Order("modifiedTime DESC")
  37. // .Get<TkOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
  38. if (last_modifiedTime != DateTime.MinValue)
  39. {
  40. startTime = last_modifiedTime;
  41. }
  42. else
  43. {
  44. last_modifiedTime = startTime;
  45. }
  46. //当日往前查询
  47. (int total, DateTime max_modifiedTime) = GetTkOrders(startTime, endTime, last_modifiedTime, true, sleep);
  48. if (max_modifiedTime != DateTime.MinValue)
  49. {
  50. RedisHelper.Set(cacheKey, max_modifiedTime);
  51. }
  52. return total;
  53. }
  54. public (int, DateTime) GetTkOrders(DateTime startTime, DateTime endTime, DateTime last_modifiedTime, bool desc = true, int sleep = 100)
  55. {
  56. // 确保 startTime 小于 endTime
  57. if (startTime > endTime)
  58. {
  59. (startTime, endTime) = (endTime, startTime);
  60. }
  61. DateTime max_modifiedTime = last_modifiedTime;
  62. int pageNo = 1;
  63. string positionIndex = string.Empty;
  64. int pageSize = 100;
  65. bool hasNext = true;
  66. int total = 0;
  67. while (hasNext)
  68. {
  69. var ts = DateTime.Now.Convert2UnixTimestamp();
  70. //string jumpType = pageNo == 1 ? "0" : desc ? "1" : "-1";
  71. string jumpType = desc ? pageNo == 1 ? "0" : "1" : "-1";
  72. //string queryType = "1";
  73. string queryType = "4";
  74. 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}";
  75. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  76. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  77. .AddHeaders("Cookie", _cookies);
  78. #if DEBUG
  79. #else
  80. client.Proxy = _proxy;
  81. #endif
  82. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  83. var response = client.Request(url);
  84. if (response.ResponseException != null)
  85. {
  86. new LoggerLibrary("api_error", "fail")
  87. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  88. .Save();
  89. throw response.ResponseException;
  90. }
  91. var body = response.Body();
  92. JsonElement data;
  93. try
  94. {
  95. var root = body.Convert2JsonElement();
  96. var succss = root.Read<bool>("success");
  97. data = root.ElementRead("data");
  98. if (!succss)
  99. {
  100. throw new APIException(body);
  101. }
  102. }
  103. catch (Exception ex)
  104. {
  105. throw new APIException(body);
  106. }
  107. using var conn = DBContext.GetOpenConnection();
  108. conn.Open();
  109. try
  110. {
  111. foreach (var order in data.ElementRead("result").EnumerateArray())
  112. {
  113. TkOrderDetailDTO item = order.GetRawText().Convert2Object<TkOrderDetailDTO>();
  114. // 处理每个订单数据
  115. item.accountId = _accountId;
  116. item.accountName = _accountName;
  117. if (item.modifiedTime > max_modifiedTime) max_modifiedTime = item.modifiedTime;
  118. if (last_modifiedTime != DateTime.MinValue && last_modifiedTime >= item.modifiedTime)
  119. {
  120. return (total, max_modifiedTime);
  121. }
  122. //进行订单和转链匹配
  123. //string mktItemLink = $"https:{item.mktItemLink}";
  124. //item.itemId = GetRawItemId(mktItemLink);
  125. int? id = conn.Replace(item);
  126. if (id != null)
  127. {
  128. item.id = (int)id;
  129. TkOrderTrackingCore.MatchOrder(item, conn);
  130. }
  131. total++;
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. throw;
  137. }
  138. finally
  139. {
  140. conn.Dispose();
  141. }
  142. pageNo++;
  143. hasNext = data.Read<bool>("hasNext");
  144. positionIndex = data.Read<string>("positionIndex");
  145. if (sleep > 0) Thread.Sleep(sleep);
  146. }
  147. return (total, max_modifiedTime);
  148. }
  149. string GetRawItemId(string url)
  150. {
  151. try
  152. {
  153. string cacheKey = $":cache:url2itemId:{url}";
  154. string itemId = RedisHelper.Get(cacheKey);
  155. if (itemId != null) return itemId;
  156. WebClientUtility client = new()
  157. {
  158. Proxy = _proxy,
  159. UserAgent = Sayaka.Common.ProviderFakeUserAgent.RandomComputer
  160. };
  161. #if DEBUG
  162. client.Proxy = null;
  163. #endif
  164. client.AllowAutoRedirect = false;
  165. var response = client.Request(url);
  166. if (response.Successed)
  167. {
  168. string location = response.ResponseMessage?.Headers?.Location?.ToString();
  169. if (!string.IsNullOrEmpty(location))
  170. {
  171. itemId = location.GetContentPart("id=", "");
  172. if (!string.IsNullOrEmpty(itemId))
  173. {
  174. RedisHelper.Set(cacheKey, itemId, 86400 * 30);
  175. }
  176. return itemId;
  177. }
  178. var responseBody = response.Body();
  179. if (response.ResponseMessage.IsSuccessStatusCode)
  180. {
  181. throw new APIException(responseBody);
  182. }
  183. else
  184. {
  185. throw new APIException($"{response.ResponseMessage.StatusCode}\t{responseBody}");
  186. }
  187. }
  188. else
  189. {
  190. throw response.ResponseException;
  191. }
  192. }
  193. catch (Exception ex)
  194. {
  195. string message = $"【GetRawItemId】[{url}\n{ex.Message}\n{ex.StackTrace}";
  196. NotifyCore.Notify(new NifyMessage
  197. {
  198. message = message,
  199. priority = NifyMessagePriority.high,
  200. tags = ["red_circle"]
  201. });
  202. }
  203. return null;
  204. }
  205. public void GetTkOrders222()
  206. {
  207. try
  208. {
  209. string url = "https://media.alimama.com/violationu2/warning_instance_page_v2.json?r=mx_208&toPage=1&pageSize=40&params=";
  210. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  211. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  212. .AddHeaders("Cookie", _cookies);
  213. client.Proxy = _proxy;
  214. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  215. var response = client.Request(url);
  216. var body = response.Body();
  217. var root = body.Convert2JsonElement();
  218. int resultCode = root.Read<int>("resultCode", 0);
  219. int totalCount = root.PathRead<int>("data.totalCount", 0);
  220. bool success = root.Read("success", false);
  221. if (!success)
  222. {
  223. new LoggerLibrary("violationu", "warning_error")
  224. .Info(url, body)
  225. .Save();
  226. throw new Exception(body);
  227. }
  228. if (totalCount == 0) return;
  229. new LoggerLibrary("violationu", "warning").Info(url, body).Save();
  230. var list = root.ElementRead("data").ElementRead("result").EnumerateArray();
  231. foreach (var data in list)
  232. {
  233. var violation_commission = data.PathRead<decimal>("mediaInfo.订单虚假交易违规佣金", 0);
  234. new DBContext.Table("alimama_warning")
  235. .Fill(data)
  236. .Loader<int>("filterRuleId", 0)
  237. .Loader<long>("gmtCreate", 0)
  238. .Loader<long>("id", 0, "ali_id")
  239. .Loader<string>("idStr", string.Empty)
  240. .Loader<int>("mediaDimension", 0)
  241. .Loader<long>("mediaId", 0)
  242. .Loader<int>("mediaInfo.abn_cnt", 0, "abn_cnt")
  243. .Add("violation_commission", violation_commission)
  244. .Loader<int>("mediaInfo.warning_cnt", 0, "warning_cnt")
  245. .Loader<string>("mediaName", string.Empty)
  246. .Loader<long>("memberId", 0)
  247. .Loader<string>("noticeCategory", string.Empty)
  248. .Loader<int>("noticeTemplateId", 0)
  249. .Loader<string>("noticeTemplateName", string.Empty)
  250. .Loader<string>("orderFile", string.Empty)
  251. .Loader<string>("pid", string.Empty)
  252. .Loader<int>("punishRuleId", 0)
  253. .Loader<string>("riskType", string.Empty)
  254. .Loader<string>("roleName", string.Empty)
  255. .Loader<string>("ruleLink", string.Empty)
  256. .Loader<bool>("showOrderDetail", false)
  257. .Loader<int>("status", 0, "ali_status")
  258. .Loader<string>("warningText", string.Empty)
  259. .Add("accountName", _accountName)
  260. .Add("status", true)
  261. .Add("create_time", DateTime.Now)
  262. .Add("last_time", DateTime.Now)
  263. .Create(DBContext.InsertType.REPLACE);
  264. }
  265. }
  266. catch (Exception ex) { }
  267. }
  268. }
  269. }