RefundOrders.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. if (_account.is_hide) return total;
  33. string sql = @"
  34. UPDATE tk_report tr
  35. JOIN (
  36. SELECT
  37. accountId, DATE(tbTradeCreateTime) AS date, SUM(refundFee) AS refund_order_ord_amt,
  38. COUNT(*) AS refund_order_ord_num, SUM(showReturnFee) AS refund_order_ord_tfee
  39. FROM tk_refund_order_details
  40. WHERE DATE(tbTradeCreateTime) IN @orderTimes AND refundStatus=2 AND accountId=@accountId
  41. GROUP BY DATE(tbTradeCreateTime), accountId
  42. ) AS sub
  43. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  44. SET tr.refund_order_ord_amt = sub.refund_order_ord_amt,
  45. tr.refund_order_ord_tfee = sub.refund_order_ord_tfee,
  46. tr.refund_order_ord_num = sub.refund_order_ord_num;";
  47. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  48. sql = @"
  49. UPDATE tk_report tr
  50. JOIN (
  51. SELECT
  52. accountId, DATE(tbTradeCreateTime) AS date, SUM(refundFee) AS refund_order_ord_amt,
  53. COUNT(*) AS refund_order_ord_num, SUM(showReturnFee) AS refund_order_ord_tfee
  54. FROM tk_refund_order_details
  55. WHERE DATE(tbTradeCreateTime) IN @orderTimes AND refundStatus=4 AND accountId=@accountId
  56. GROUP BY DATE(tbTradeCreateTime), accountId
  57. ) AS sub
  58. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  59. SET tr.refund_order_ord_amt_4 = sub.refund_order_ord_amt,
  60. tr.refund_order_ord_tfee_4 = sub.refund_order_ord_tfee,
  61. tr.refund_order_ord_num_4 = sub.refund_order_ord_num;";
  62. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  63. sql = @"
  64. UPDATE tk_report tr
  65. JOIN (
  66. SELECT
  67. accountId, DATE(RefundCreateTime) AS date,
  68. SUM(showReturnFee) AS refund_order_ord_tfee
  69. FROM tk_refund_order_details
  70. WHERE DATE(RefundCreateTime) IN @orderTimes AND accountId=@accountId
  71. GROUP BY DATE(RefundCreateTime), accountId
  72. ) AS sub
  73. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  74. SET tr.refund_order_ord_freeze_tfee = 0-sub.refund_order_ord_tfee;";
  75. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  76. sql = @"
  77. UPDATE tk_report tr
  78. JOIN (
  79. SELECT
  80. accountId, DATE(RefundFinishTime) AS date,
  81. SUM(showReturnFee) AS refund_order_ord_tfee
  82. FROM tk_refund_order_details
  83. WHERE DATE(RefundFinishTime) IN @orderTimes AND refundStatus=2 AND accountId=@accountId
  84. GROUP BY DATE(RefundFinishTime), accountId
  85. ) AS sub
  86. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  87. SET tr.refund_order_ord_deduct_tfee = 0-sub.refund_order_ord_tfee;";
  88. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  89. sql = @"
  90. UPDATE tk_report tr
  91. JOIN (
  92. SELECT
  93. accountId, DATE(RefundFinishTime) AS date,
  94. SUM(showReturnFee) AS refund_order_ord_tfee
  95. FROM tk_refund_order_details
  96. WHERE DATE(RefundFinishTime) IN @orderTimes AND refundStatus IN (2,3) AND accountId=@accountId
  97. GROUP BY DATE(RefundFinishTime), accountId
  98. ) AS sub
  99. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  100. SET tr.refund_order_ord_unfreeze_tfee = sub.refund_order_ord_tfee;";
  101. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  102. //refundStatus3 失败
  103. //2 成功
  104. //4创建
  105. //public decimal refund_order_ord_deduct_tfee { get; set; } = 0;
  106. //public decimal refund_order_ord_freeze_tfee { get; set; } = 0;
  107. //public decimal refund_order_ord_unfreeze_tfee { get; set; } = 0;
  108. return total;
  109. }
  110. public int GetIncyRefundOrders(int sleep)
  111. {
  112. string cacheKey = $":cache:GetIncyRefundOrders:{_accountId}";
  113. DateTime endTime = DateTime.Now;
  114. DateTime startTime = endTime.AddDays(-1);
  115. DateTime last_modifiedTime = RedisHelper.Get<DateTime>(cacheKey);
  116. ////找出数据库最新的一天
  117. //var last = new DBContext.Table("tk_order_details")
  118. // .Order("modifiedTime DESC")
  119. // .Get<TkRefundOrderDetailDTO>("accountId=@accountId", new { accountId = _accountId });
  120. if (last_modifiedTime != DateTime.MinValue)
  121. {
  122. startTime = last_modifiedTime;
  123. }
  124. else
  125. {
  126. last_modifiedTime = startTime;
  127. }
  128. //当日往前查询
  129. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = GetTkRefundOrders(startTime, endTime, last_modifiedTime, true, sleep);
  130. if (max_modifiedTime != DateTime.MinValue)
  131. {
  132. RedisHelper.Set(cacheKey, max_modifiedTime);
  133. }
  134. if (_account.is_hide) return total;
  135. string sql = @"
  136. UPDATE tk_report tr
  137. JOIN (
  138. SELECT
  139. accountId, DATE(tbTradeCreateTime) AS date, SUM(refundFee) AS refund_order_ord_amt,
  140. COUNT(*) AS refund_order_ord_num, SUM(showReturnFee) AS refund_order_ord_tfee
  141. FROM tk_refund_order_details
  142. WHERE DATE(tbTradeCreateTime) IN @orderTimes AND refundStatus=2 AND accountId=@accountId
  143. GROUP BY DATE(tbTradeCreateTime), accountId
  144. ) AS sub
  145. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  146. SET tr.refund_order_ord_amt = sub.refund_order_ord_amt,
  147. tr.refund_order_ord_tfee = sub.refund_order_ord_tfee,
  148. tr.refund_order_ord_num = sub.refund_order_ord_num;";
  149. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  150. sql = @"
  151. UPDATE tk_report tr
  152. JOIN (
  153. SELECT
  154. accountId, DATE(tbTradeCreateTime) AS date, SUM(refundFee) AS refund_order_ord_amt,
  155. COUNT(*) AS refund_order_ord_num, SUM(showReturnFee) AS refund_order_ord_tfee
  156. FROM tk_refund_order_details
  157. WHERE DATE(tbTradeCreateTime) IN @orderTimes AND refundStatus=4 AND accountId=@accountId
  158. GROUP BY DATE(tbTradeCreateTime), accountId
  159. ) AS sub
  160. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  161. SET tr.refund_order_ord_amt_4 = sub.refund_order_ord_amt,
  162. tr.refund_order_ord_tfee_4 = sub.refund_order_ord_tfee,
  163. tr.refund_order_ord_num_4 = sub.refund_order_ord_num;";
  164. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  165. sql = @"
  166. UPDATE tk_report tr
  167. JOIN (
  168. SELECT
  169. accountId, DATE(RefundCreateTime) AS date,
  170. SUM(showReturnFee) AS refund_order_ord_tfee
  171. FROM tk_refund_order_details
  172. WHERE DATE(RefundCreateTime) IN @orderTimes AND accountId=@accountId
  173. GROUP BY DATE(RefundCreateTime), accountId
  174. ) AS sub
  175. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  176. SET tr.refund_order_ord_freeze_tfee = 0-sub.refund_order_ord_tfee;";
  177. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  178. sql = @"
  179. UPDATE tk_report tr
  180. JOIN (
  181. SELECT
  182. accountId, DATE(RefundFinishTime) AS date,
  183. SUM(showReturnFee) AS refund_order_ord_tfee
  184. FROM tk_refund_order_details
  185. WHERE DATE(RefundFinishTime) IN @orderTimes AND refundStatus=2 AND accountId=@accountId
  186. GROUP BY DATE(RefundFinishTime), accountId
  187. ) AS sub
  188. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  189. SET tr.refund_order_ord_deduct_tfee = 0-sub.refund_order_ord_tfee;";
  190. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  191. sql = @"
  192. UPDATE tk_report tr
  193. JOIN (
  194. SELECT
  195. accountId, DATE(RefundFinishTime) AS date,
  196. SUM(showReturnFee) AS refund_order_ord_tfee
  197. FROM tk_refund_order_details
  198. WHERE DATE(RefundFinishTime) IN @orderTimes AND refundStatus IN (2,3) AND accountId=@accountId
  199. GROUP BY DATE(RefundFinishTime), accountId
  200. ) AS sub
  201. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  202. SET tr.refund_order_ord_unfreeze_tfee = sub.refund_order_ord_tfee;";
  203. DBContext.Execute(sql, new { accountId = _accountId, orderTimes });
  204. return total;
  205. }
  206. public (int, DateTime, List<DateTime>) GetTkRefundOrders(DateTime startTime, DateTime endTime, DateTime last_modifiedTime, bool desc = true, int sleep = 100)
  207. {
  208. List<DateTime> orderTimes = [];
  209. // 确保 startTime 小于 endTime
  210. if (startTime > endTime)
  211. {
  212. (startTime, endTime) = (endTime, startTime);
  213. }
  214. DateTime max_modifiedTime = last_modifiedTime;
  215. int pageNo = 1;
  216. string positionIndex = string.Empty;
  217. int pageSize = 100;
  218. bool hasNext = true;
  219. int total = 0;
  220. while (hasNext)
  221. {
  222. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  223. //string jumpType = desc ? pageNo == 1 ? "0" : "1" : "-1";
  224. string jumpType = pageNo == 1 ? "0" : "1";
  225. //string queryType = "1";
  226. //1 创建时间;3结算时间 2付款时间 4更新时间
  227. //string queryType = "2";
  228. string queryType = desc ? "4" : "2";
  229. 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()}";
  230. string body = "";
  231. JsonElement data;
  232. try
  233. {
  234. for (var i = 1; i <= 5; i++)
  235. {
  236. var client = new WebClientUtility().SetContentType("application/json;charset=utf-8")
  237. .AddHeaders("X-Requested-With", "XMLHttpRequest")
  238. .AddHeaders("Cookie", _cookies);
  239. #if DEBUG
  240. #else
  241. client.Proxy = _proxy;
  242. #endif
  243. if (!string.IsNullOrEmpty(_user_agent)) client.UserAgent = _user_agent;
  244. var response = client.Request(url);
  245. if (response.ResponseException != null)
  246. {
  247. _ = new LoggerLibrary("api_error", "fail")
  248. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  249. .SaveAsync();
  250. throw response.ResponseException;
  251. }
  252. body = response.Body();
  253. if (body.Contains("{\"action\":\"captcha\""))
  254. {
  255. string message = $"【GetTkRefundOrders】【{_accountId}:{_accountName}】第 {i} 次出现滑动验证码";
  256. NotifyCore.Notify(new NifyMessage
  257. {
  258. message = message,
  259. priority = NifyMessagePriority.high,
  260. tags = ["red_circle"]
  261. });
  262. Thread.Sleep(i * 60 * 1000);
  263. continue;
  264. }
  265. if (body.Contains("\"resultCode\":500"))
  266. {
  267. string message = $"【GetTkRefundOrders】【{_accountId}:{_accountName}】第 {i} 次出现错误\n{body}";
  268. NotifyCore.Notify(new NifyMessage
  269. {
  270. message = message,
  271. priority = NifyMessagePriority.high,
  272. tags = ["red_circle"]
  273. });
  274. Thread.Sleep(i * 60 * 1000);
  275. continue;
  276. }
  277. break;
  278. }
  279. var root = body.Convert2JsonElement();
  280. var success = root.Read<bool>("success");
  281. data = root.ElementRead("data");
  282. //{"code":601,"info":{"ok":false,"message":"nologin"}}
  283. if (!success && body.Contains("nologin"))
  284. {
  285. (success, string message) = RenewCookie();
  286. if (!success)
  287. {
  288. TkPoolCore.Disabled(_accountId, _accountName, $"{body}");
  289. }
  290. return (0, DateTime.MinValue, new List<DateTime>());
  291. }
  292. if (!success)
  293. {
  294. throw new APIException(body);
  295. }
  296. }
  297. catch (Exception ex)
  298. {
  299. throw new APIException(body);
  300. }
  301. using var conn = DBContext.GetOpenConnection();
  302. conn.Open();
  303. try
  304. {
  305. foreach (var order in data.ElementRead("result").EnumerateArray())
  306. {
  307. TkRefundOrderDetailDTO item = order.GetRawText().Convert2Object<TkRefundOrderDetailDTO>();
  308. // 处理每个订单数据
  309. item.accountId = _accountId;
  310. item.accountName = _company;
  311. if (item.ModifiedTime > max_modifiedTime) max_modifiedTime = item.ModifiedTime;
  312. if (last_modifiedTime != DateTime.MinValue && last_modifiedTime >= item.ModifiedTime)
  313. {
  314. return (total, max_modifiedTime, orderTimes);
  315. }
  316. //进行订单和转链匹配
  317. orderTimes.Add(item.TbTradeCreateTime.Date);
  318. switch (item.RefundStatus)
  319. {
  320. case 4:
  321. orderTimes.Add(item.RefundCreateTime.Date);
  322. break;
  323. case 2:
  324. case 3:
  325. orderTimes.Add(item.RefundFinishTime.Date);
  326. break;
  327. }
  328. if (item.ShowReturnFee == 0)
  329. {
  330. item.ShowReturnFee = Math.Round(item.PubShareFee / item.AlipayTotalPrice * item.RefundFee, 2);
  331. }
  332. int? id = conn.Replace(item);
  333. if (id != null)
  334. {
  335. item.id = (int)id;
  336. }
  337. total++;
  338. }
  339. }
  340. catch (Exception ex)
  341. {
  342. throw;
  343. }
  344. finally
  345. {
  346. conn.Dispose();
  347. }
  348. pageNo++;
  349. hasNext = data.Read<bool>("hasNext");
  350. positionIndex = data.Read<string>("positionIndex");
  351. if (sleep > 0) Thread.Sleep(sleep);
  352. }
  353. return (total, max_modifiedTime, orderTimes);
  354. }
  355. }
  356. }