RefundOrders.cs 18 KB

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