orders.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using dodohold.core;
  2. using Spire.Pdf.Xmp;
  3. using System.Data;
  4. using System.Drawing.Printing;
  5. using System.Text.Json;
  6. namespace molilian.core
  7. {
  8. public partial class AlimamaPlus
  9. {
  10. private const string base_orders_url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/report.getTbkOrderDetails.json";
  11. public int GetHistoryOrders(int sleep)
  12. {
  13. DateTime endTime = DateTime.Now;
  14. DateTime startTime = endTime.AddDays(-90);
  15. ////找出数据库最新的一天
  16. //var last = new DBContext.Table("tk_order_details")
  17. // .Order("modifiedTime DESC")
  18. // .Get<TkOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
  19. //if (last != null)
  20. //{
  21. // startTime = last.modifiedTime;
  22. //}
  23. //当日往前查询
  24. return GetTkOrders(startTime, endTime, DateTime.MinValue, false, sleep);
  25. }
  26. public int GetIncyOrders(int sleep)
  27. {
  28. DateTime endTime = DateTime.Now;
  29. DateTime startTime = endTime.AddDays(-1);
  30. //找出数据库最新的一天
  31. var last = new DBContext.Table("tk_order_details")
  32. .Order("modifiedTime DESC")
  33. .Get<TkOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
  34. DateTime last_modifiedTime = DateTime.MinValue;
  35. if (last != null)
  36. {
  37. startTime = last.modifiedTime;
  38. last_modifiedTime = last.modifiedTime;
  39. }
  40. //当日往前查询
  41. return GetTkOrders(startTime, endTime, last_modifiedTime, true, sleep);
  42. }
  43. public int GetTkOrders(DateTime startTime, DateTime endTime, DateTime last_modifiedTime, bool desc = true, int sleep = 100)
  44. {
  45. // 确保 startTime 小于 endTime
  46. if (startTime > endTime)
  47. {
  48. (startTime, endTime) = (endTime, startTime);
  49. }
  50. int pageNo = 1;
  51. string positionIndex = string.Empty;
  52. int pageSize = 20;
  53. bool hasNext = true;
  54. int total = 0;
  55. while (hasNext)
  56. {
  57. var ts = DateTime.Now.Convert2UnixTimestamp();
  58. //string jumpType = pageNo == 1 ? "0" : desc ? "1" : "-1";
  59. string jumpType = desc ? pageNo == 1 ? "0" : "1" : "-1";
  60. //string queryType = "1";
  61. string queryType = "4";
  62. 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}";
  63. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  64. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  65. .AddHeaders("Cookie", _cookies);
  66. #if DEBUG
  67. #else
  68. client.Proxy = _proxy;
  69. #endif
  70. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  71. var response = client.Request(url);
  72. if (response.ResponseException != null)
  73. {
  74. new LoggerLibrary("api_error", "fail")
  75. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  76. .Save();
  77. throw response.ResponseException;
  78. }
  79. var body = response.Body();
  80. JsonElement data;
  81. try
  82. {
  83. var root = body.Convert2JsonElement();
  84. var succss = root.Read<bool>("success");
  85. data = root.ElementRead("data");
  86. if (!succss)
  87. {
  88. throw new APIException(body);
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. throw new APIException(body);
  94. }
  95. using var conn = DBContext.GetOpenConnection();
  96. conn.Open();
  97. try
  98. {
  99. foreach (var order in data.ElementRead("result").EnumerateArray())
  100. {
  101. TkOrderDetailDTO item = order.GetRawText().Convert2Object<TkOrderDetailDTO>();
  102. // 处理每个订单数据
  103. item.accountId = _accountId;
  104. item.accountName = _accountName;
  105. if (last_modifiedTime != DateTime.MinValue && last_modifiedTime.AddMinutes(-2) >= item.modifiedTime)
  106. {
  107. return total;
  108. }
  109. int? id = conn.Replace(item);
  110. total++;
  111. }
  112. }
  113. catch (Exception ex)
  114. {
  115. throw;
  116. }
  117. finally
  118. {
  119. conn.Dispose();
  120. }
  121. pageNo++;
  122. hasNext = data.Read<bool>("hasNext");
  123. positionIndex = data.Read<string>("positionIndex");
  124. if (sleep > 0) Thread.Sleep(sleep);
  125. }
  126. return total;
  127. }
  128. public void GetTkOrders222()
  129. {
  130. try
  131. {
  132. string url = "https://media.alimama.com/violationu2/warning_instance_page_v2.json?r=mx_208&toPage=1&pageSize=40&params=";
  133. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  134. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  135. .AddHeaders("Cookie", _cookies);
  136. client.Proxy = _proxy;
  137. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  138. var response = client.Request(url);
  139. var body = response.Body();
  140. var root = body.Convert2JsonElement();
  141. int resultCode = root.Read<int>("resultCode", 0);
  142. int totalCount = root.PathRead<int>("data.totalCount", 0);
  143. bool success = root.Read("success", false);
  144. if (!success)
  145. {
  146. new LoggerLibrary("violationu", "warning_error")
  147. .Info(url, body)
  148. .Save();
  149. throw new Exception(body);
  150. }
  151. if (totalCount == 0) return;
  152. new LoggerLibrary("violationu", "warning").Info(url, body).Save();
  153. var list = root.ElementRead("data").ElementRead("result").EnumerateArray();
  154. foreach (var data in list)
  155. {
  156. var violation_commission = data.PathRead<decimal>("mediaInfo.订单虚假交易违规佣金", 0);
  157. new DBContext.Table("alimama_warning")
  158. .Fill(data)
  159. .Loader<int>("filterRuleId", 0)
  160. .Loader<long>("gmtCreate", 0)
  161. .Loader<long>("id", 0, "ali_id")
  162. .Loader<string>("idStr", string.Empty)
  163. .Loader<int>("mediaDimension", 0)
  164. .Loader<long>("mediaId", 0)
  165. .Loader<int>("mediaInfo.abn_cnt", 0, "abn_cnt")
  166. .Add("violation_commission", violation_commission)
  167. .Loader<int>("mediaInfo.warning_cnt", 0, "warning_cnt")
  168. .Loader<string>("mediaName", string.Empty)
  169. .Loader<long>("memberId", 0)
  170. .Loader<string>("noticeCategory", string.Empty)
  171. .Loader<int>("noticeTemplateId", 0)
  172. .Loader<string>("noticeTemplateName", string.Empty)
  173. .Loader<string>("orderFile", string.Empty)
  174. .Loader<string>("pid", string.Empty)
  175. .Loader<int>("punishRuleId", 0)
  176. .Loader<string>("riskType", string.Empty)
  177. .Loader<string>("roleName", string.Empty)
  178. .Loader<string>("ruleLink", string.Empty)
  179. .Loader<bool>("showOrderDetail", false)
  180. .Loader<int>("status", 0, "ali_status")
  181. .Loader<string>("warningText", string.Empty)
  182. .Add("accountName", _accountName)
  183. .Add("status", true)
  184. .Add("create_time", DateTime.Now)
  185. .Add("last_time", DateTime.Now)
  186. .Create(DBContext.InsertType.REPLACE);
  187. }
  188. }
  189. catch (Exception ex) { }
  190. }
  191. }
  192. }