RefundOrders.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. using dodohold.core;
  2. using Spire.Pdf.Xmp;
  3. using System.Data;
  4. using System.Drawing.Printing;
  5. using System.Runtime.InteropServices;
  6. using System.Text.Json;
  7. using System.Text.RegularExpressions;
  8. using static System.Runtime.InteropServices.JavaScript.JSType;
  9. namespace molilian.core
  10. {
  11. public partial class AlimamaPlus
  12. {
  13. private const string base_refund_orders_url = "https://pub.alimama.com/openapi/param2/1/gateway.unionpub/report.publisher.getRefundOrderDetail.json";
  14. public int GetHistoryRefundOrders(int sleep, DateTime startTime, DateTime endTime)
  15. {
  16. //DateTime endTime = DateTime.Now;
  17. //DateTime startTime = endTime.AddDays(-90);
  18. //if (_accountId == 3) endTime = DateTime.Parse("2024-04-07");
  19. //#if DEBUG
  20. // startTime = DateTime.Parse("2024-05-01");
  21. // endTime = DateTime.Parse("2024-06-01");
  22. // int total = 0;
  23. // List<DateTime> orderTimes = new List<DateTime>();
  24. // for (DateTime date = startTime; date <= endTime; date = date.AddDays(1))
  25. // {
  26. // orderTimes.Add(date);
  27. // }
  28. //#else
  29. //当日往前查询
  30. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = GetTkRefundOrders(startTime, endTime, DateTime.MinValue, false, sleep);
  31. //#endif
  32. // string sql = @"
  33. //UPDATE tk_report tr
  34. //JOIN (
  35. // SELECT
  36. // accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  37. // COUNT(*) AS order_ord_num, SUM(pubShareFeeForCommission) AS order_ord_tfee
  38. // FROM tk_order_details
  39. // WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=3 AND accountId=@accountId
  40. // GROUP BY DATE(tbPaidTime), accountId
  41. //) AS sub
  42. //ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  43. //SET tr.order_ord_amt = sub.order_ord_amt, tr.order_ord_num_3 = sub.order_ord_num,
  44. // tr.order_ord_tfee = sub.order_ord_tfee;";
  45. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  46. // sql = @"
  47. //UPDATE tk_report tr
  48. //JOIN (
  49. // SELECT
  50. // accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  51. // COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  52. // FROM tk_order_details
  53. // WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=12 AND accountId=@accountId
  54. // GROUP BY DATE(tbPaidTime), accountId
  55. //) AS sub
  56. //ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  57. //SET tr.order_ord_amt_12 = sub.order_ord_amt, tr.order_ord_num_12 = sub.order_ord_num,
  58. // tr.order_ord_tfee_12 = sub.order_ord_tfee;";
  59. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  60. // sql = @"
  61. //UPDATE tk_report tr
  62. //JOIN (
  63. // SELECT
  64. // accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  65. // COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  66. // FROM tk_order_details
  67. // WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=13 AND accountId=@accountId
  68. // GROUP BY DATE(tbPaidTime), accountId
  69. //) AS sub
  70. //ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  71. //SET tr.order_ord_amt_13 = sub.order_ord_amt, tr.order_ord_num_13 = sub.order_ord_num,
  72. // tr.order_ord_tfee_13 = sub.order_ord_tfee;";
  73. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  74. // sql = @"
  75. //UPDATE tk_report tr
  76. //JOIN (
  77. // SELECT
  78. // accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  79. // COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  80. // FROM tk_order_details
  81. // WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=14 AND accountId=@accountId
  82. // GROUP BY DATE(tbPaidTime), accountId
  83. //) AS sub
  84. //ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  85. //SET tr.order_ord_amt_14 = sub.order_ord_amt, tr.order_ord_num_14 = sub.order_ord_num,
  86. // tr.order_ord_tfee_14 = sub.order_ord_tfee;";
  87. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  88. // sql = @"UPDATE tk_report
  89. //SET order_ord_num = order_ord_num_3 + order_ord_num_12 + order_ord_num_13 + order_ord_num_14
  90. // WHERE DATE(report_date) IN @orderTimes AND accountId=@accountId;";
  91. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  92. return total;
  93. }
  94. public int GetIncyRefundOrders(int sleep)
  95. {
  96. string cacheKey = $":cache:GetIncyRefundOrders:{_accountId}";
  97. DateTime endTime = DateTime.Now;
  98. DateTime startTime = endTime.AddDays(-1);
  99. DateTime last_modifiedTime = RedisHelper.Get<DateTime>(cacheKey);
  100. ////找出数据库最新的一天
  101. //var last = new DBContext.Table("tk_order_details")
  102. // .Order("modifiedTime DESC")
  103. // .Get<TkRefundOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
  104. if (last_modifiedTime != DateTime.MinValue)
  105. {
  106. startTime = last_modifiedTime;
  107. }
  108. else
  109. {
  110. last_modifiedTime = startTime;
  111. }
  112. //当日往前查询
  113. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = GetTkRefundOrders(startTime, endTime, last_modifiedTime, true, sleep);
  114. if (max_modifiedTime != DateTime.MinValue)
  115. {
  116. RedisHelper.Set(cacheKey, max_modifiedTime);
  117. }
  118. //todo 根据orderTimes、settlementTimes 更新被影响的相关行
  119. // string sql = @"
  120. //UPDATE tk_report tr
  121. //JOIN (
  122. // SELECT
  123. // accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  124. // COUNT(*) AS order_ord_num, SUM(pubShareFeeForCommission) AS order_ord_tfee
  125. // FROM tk_order_details
  126. // WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=3 AND accountId=@accountId
  127. // GROUP BY DATE(tbPaidTime), accountId
  128. //) AS sub
  129. //ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  130. //SET tr.order_ord_amt = sub.order_ord_amt, tr.order_ord_num_3 = sub.order_ord_num,
  131. // tr.order_ord_tfee = sub.order_ord_tfee;";
  132. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  133. // sql = @"
  134. //UPDATE tk_report tr
  135. //JOIN (
  136. // SELECT
  137. // accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  138. // COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  139. // FROM tk_order_details
  140. // WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=12 AND accountId=@accountId
  141. // GROUP BY DATE(tbPaidTime), accountId
  142. //) AS sub
  143. //ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  144. //SET tr.order_ord_amt_12 = sub.order_ord_amt, tr.order_ord_num_12 = sub.order_ord_num,
  145. // tr.order_ord_tfee_12 = sub.order_ord_tfee;";
  146. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  147. // sql = @"
  148. //UPDATE tk_report tr
  149. //JOIN (
  150. // SELECT
  151. // accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  152. // COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  153. // FROM tk_order_details
  154. // WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=13 AND accountId=@accountId
  155. // GROUP BY DATE(tbPaidTime), accountId
  156. //) AS sub
  157. //ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  158. //SET tr.order_ord_amt_13 = sub.order_ord_amt, tr.order_ord_num_13 = sub.order_ord_num,
  159. // tr.order_ord_tfee_13 = sub.order_ord_tfee;";
  160. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  161. // sql = @"
  162. //UPDATE tk_report tr
  163. //JOIN (
  164. // SELECT
  165. // accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  166. // COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  167. // FROM tk_order_details
  168. // WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=14 AND accountId=@accountId
  169. // GROUP BY DATE(tbPaidTime), accountId
  170. //) AS sub
  171. //ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  172. //SET tr.order_ord_amt_14 = sub.order_ord_amt, tr.order_ord_num_14 = sub.order_ord_num,
  173. // tr.order_ord_tfee_14 = sub.order_ord_tfee;";
  174. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  175. // sql = @"UPDATE tk_report
  176. //SET order_ord_num = order_ord_num_3 + order_ord_num_12 + order_ord_num_13 + order_ord_num_14
  177. // WHERE DATE(report_date) IN @orderTimes AND accountId=@accountId;";
  178. // DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  179. return total;
  180. }
  181. public (int, DateTime, List<DateTime>) GetTkRefundOrders(DateTime startTime, DateTime endTime, DateTime last_modifiedTime, bool desc = true, int sleep = 100)
  182. {
  183. List<DateTime> orderTimes = [];
  184. // 确保 startTime 小于 endTime
  185. if (startTime > endTime)
  186. {
  187. (startTime, endTime) = (endTime, startTime);
  188. }
  189. DateTime max_modifiedTime = last_modifiedTime;
  190. int pageNo = 1;
  191. string positionIndex = string.Empty;
  192. int pageSize = 100;
  193. bool hasNext = true;
  194. int total = 0;
  195. while (hasNext)
  196. {
  197. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  198. //string jumpType = desc ? pageNo == 1 ? "0" : "1" : "-1";
  199. string jumpType = pageNo == 1 ? "0" : "1";
  200. //string queryType = "1";
  201. //1 创建时间;3结算时间 2付款时间 4更新时间
  202. //string queryType = "2";
  203. string queryType = desc ? "4" : "2";
  204. string url = $"{base_refund_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}&toPage=1&perPageSize={pageSize * 2}&jumpType={jumpType}&isFullRefund=0&memberType=&positionIndex={positionIndex.UrlEncode()}";
  205. string body = "";
  206. JsonElement data;
  207. try
  208. {
  209. for (var i = 1; i <= 5; i++)
  210. {
  211. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  212. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  213. .AddHeaders("Cookie", _cookies);
  214. #if DEBUG
  215. #else
  216. client.Proxy = _proxy;
  217. #endif
  218. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  219. var response = client.Request(url);
  220. if (response.ResponseException != null)
  221. {
  222. _ = new LoggerLibrary("api_error", "fail")
  223. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  224. .SaveAsync();
  225. throw response.ResponseException;
  226. }
  227. body = response.Body();
  228. if (body.Contains("{\"action\":\"captcha\""))
  229. {
  230. string message = $"【GetTkRefundOrders】【{_accountId}:{_accountName}】第 {i} 次出现滑动验证码";
  231. NotifyCore.Notify(new NifyMessage
  232. {
  233. message = message,
  234. priority = NifyMessagePriority.high,
  235. tags = ["red_circle"]
  236. });
  237. Thread.Sleep(i * 60 * 1000);
  238. continue;
  239. }
  240. if (body.Contains("\"resultCode\":500"))
  241. {
  242. string message = $"【GetTkRefundOrders】【{_accountId}:{_accountName}】第 {i} 次出现错误\n{body}";
  243. NotifyCore.Notify(new NifyMessage
  244. {
  245. message = message,
  246. priority = NifyMessagePriority.high,
  247. tags = ["red_circle"]
  248. });
  249. Thread.Sleep(i * 60 * 1000);
  250. continue;
  251. }
  252. break;
  253. }
  254. var root = body.Convert2JsonElement();
  255. var success = root.Read<bool>("success");
  256. data = root.ElementRead("data");
  257. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  258. if (!success && body.Contains("nologin"))
  259. {
  260. (success, string message) = RenewCookie();
  261. if (!success)
  262. {
  263. TkPoolCore.Disabled(_accountId, _accountName, $"{body}");
  264. }
  265. return (0, DateTime.MinValue, new List<DateTime>());
  266. }
  267. if (!success)
  268. {
  269. throw new APIException(body);
  270. }
  271. }
  272. catch (Exception ex)
  273. {
  274. throw new APIException(body);
  275. }
  276. using var conn = DBContext.GetOpenConnection();
  277. conn.Open();
  278. try
  279. {
  280. foreach (var order in data.ElementRead("result").EnumerateArray())
  281. {
  282. TkRefundOrderDetailDTO item = order.GetRawText().Convert2Object<TkRefundOrderDetailDTO>();
  283. // 处理每个订单数据
  284. item.accountId = _accountId;
  285. item.accountName = _company;
  286. if (item.ModifiedTime > max_modifiedTime) max_modifiedTime = item.ModifiedTime;
  287. if (last_modifiedTime != DateTime.MinValue && last_modifiedTime >= item.ModifiedTime)
  288. {
  289. return (total, max_modifiedTime, orderTimes);
  290. }
  291. //进行订单和转链匹配
  292. orderTimes.Add(item.RefundCreateTime.Date);
  293. int? id = conn.Replace(item);
  294. if (id != null)
  295. {
  296. item.id = (int)id;
  297. }
  298. total++;
  299. }
  300. }
  301. catch (Exception ex)
  302. {
  303. throw;
  304. }
  305. finally
  306. {
  307. conn.Dispose();
  308. }
  309. pageNo++;
  310. hasNext = data.Read<bool>("hasNext");
  311. positionIndex = data.Read<string>("positionIndex");
  312. if (sleep > 0) Thread.Sleep(sleep);
  313. }
  314. return (total, max_modifiedTime, orderTimes);
  315. }
  316. }
  317. }