order.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. using dodohold.core;
  2. using Sayaka.Common;
  3. using System.Text.Json;
  4. using System.Threading;
  5. namespace molilian.core
  6. {
  7. public partial class JdUnionPlus
  8. {
  9. public async Task<int> GetHistoryOrders(int sleep, DateTime startTime, DateTime endTime)
  10. {
  11. //当日往前查询
  12. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = await GetTkOrdersAsync(startTime, endTime, DateTime.MinValue, optType: 2, sleep);
  13. string sql = @"
  14. UPDATE tk_report tr
  15. JOIN (
  16. SELECT
  17. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  18. COUNT(*) AS order_ord_num, SUM(pubShareFeeForCommission) AS order_ord_tfee
  19. FROM tk_order_details
  20. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=3 AND accountId=@accountId
  21. GROUP BY DATE(tbPaidTime), accountId
  22. ) AS sub
  23. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  24. SET tr.order_ord_amt = sub.order_ord_amt, tr.order_ord_num_3 = sub.order_ord_num,
  25. tr.order_ord_tfee = sub.order_ord_tfee;";
  26. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  27. sql = @"
  28. UPDATE tk_report tr
  29. JOIN (
  30. SELECT
  31. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  32. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  33. FROM tk_order_details
  34. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=12 AND accountId=@accountId
  35. GROUP BY DATE(tbPaidTime), accountId
  36. ) AS sub
  37. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  38. SET tr.order_ord_amt_12 = sub.order_ord_amt, tr.order_ord_num_12 = sub.order_ord_num,
  39. tr.order_ord_tfee_12 = sub.order_ord_tfee;";
  40. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  41. sql = @"
  42. UPDATE tk_report tr
  43. JOIN (
  44. SELECT
  45. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  46. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  47. FROM tk_order_details
  48. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=13 AND accountId=@accountId
  49. GROUP BY DATE(tbPaidTime), accountId
  50. ) AS sub
  51. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  52. SET tr.order_ord_amt_13 = sub.order_ord_amt, tr.order_ord_num_13 = sub.order_ord_num,
  53. tr.order_ord_tfee_13 = sub.order_ord_tfee;";
  54. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  55. sql = @"
  56. UPDATE tk_report tr
  57. JOIN (
  58. SELECT
  59. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  60. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  61. FROM tk_order_details
  62. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=14 AND accountId=@accountId
  63. GROUP BY DATE(tbPaidTime), accountId
  64. ) AS sub
  65. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  66. SET tr.order_ord_amt_14 = sub.order_ord_amt, tr.order_ord_num_14 = sub.order_ord_num,
  67. tr.order_ord_tfee_14 = sub.order_ord_tfee;";
  68. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  69. sql = @"UPDATE tk_report
  70. SET order_ord_num = order_ord_num_3 + order_ord_num_12 + order_ord_num_13 + order_ord_num_14
  71. WHERE DATE(report_date) IN @orderTimes AND accountId=@accountId;";
  72. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  73. return total;
  74. }
  75. public async Task<int> GetIncyOrders(int sleep)
  76. {
  77. string cacheKey = $":cache:GetIncyJDOrders:{_account.name}";
  78. string cacheKey2 = $":cache:GetIncyJDOrders:{_account.id}";
  79. DateTime endTime = DateTime.Now;
  80. DateTime startTime = endTime.AddDays(-7);
  81. DateTime last_modifiedTime = RedisHelper.Get<DateTime>(cacheKey);
  82. if (last_modifiedTime != DateTime.MinValue)
  83. {
  84. startTime = last_modifiedTime;
  85. }
  86. else
  87. {
  88. last_modifiedTime = startTime;
  89. }
  90. //当日往前查询
  91. (int total, DateTime max_modifiedTime, List<DateTime> orderTimes) = await GetTkOrdersAsync(startTime, endTime, last_modifiedTime, optType: 1, sleep);
  92. if (max_modifiedTime != DateTime.MinValue)
  93. {
  94. RedisHelper.Set(cacheKey, max_modifiedTime);
  95. RedisHelper.Set(cacheKey2, max_modifiedTime);
  96. }
  97. //todo 根据orderTimes、settlementTimes 更新被影响的相关行
  98. string sql = @"
  99. UPDATE tk_report tr
  100. JOIN (
  101. SELECT
  102. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  103. COUNT(*) AS order_ord_num, SUM(pubShareFeeForCommission) AS order_ord_tfee
  104. FROM tk_order_details
  105. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=3 AND accountId=@accountId
  106. GROUP BY DATE(tbPaidTime), accountId
  107. ) AS sub
  108. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  109. SET tr.order_ord_amt = sub.order_ord_amt, tr.order_ord_num_3 = sub.order_ord_num,
  110. tr.order_ord_tfee = sub.order_ord_tfee;";
  111. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  112. sql = @"
  113. UPDATE tk_report tr
  114. JOIN (
  115. SELECT
  116. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  117. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  118. FROM tk_order_details
  119. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=12 AND accountId=@accountId
  120. GROUP BY DATE(tbPaidTime), accountId
  121. ) AS sub
  122. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  123. SET tr.order_ord_amt_12 = sub.order_ord_amt, tr.order_ord_num_12 = sub.order_ord_num,
  124. tr.order_ord_tfee_12 = sub.order_ord_tfee;";
  125. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  126. sql = @"
  127. UPDATE tk_report tr
  128. JOIN (
  129. SELECT
  130. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  131. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  132. FROM tk_order_details
  133. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=13 AND accountId=@accountId
  134. GROUP BY DATE(tbPaidTime), accountId
  135. ) AS sub
  136. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  137. SET tr.order_ord_amt_13 = sub.order_ord_amt, tr.order_ord_num_13 = sub.order_ord_num,
  138. tr.order_ord_tfee_13 = sub.order_ord_tfee;";
  139. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  140. sql = @"
  141. UPDATE tk_report tr
  142. JOIN (
  143. SELECT
  144. accountId, DATE(tbPaidTime) AS date, SUM(alipayTotalPrice) AS order_ord_amt,
  145. COUNT(*) AS order_ord_num, SUM(pubSharePreFeeForCommission) AS order_ord_tfee
  146. FROM tk_order_details
  147. WHERE DATE(tbPaidTime) IN @orderTimes AND tkStatus=14 AND accountId=@accountId
  148. GROUP BY DATE(tbPaidTime), accountId
  149. ) AS sub
  150. ON tr.accountId = sub.accountId AND tr.report_date = sub.date
  151. SET tr.order_ord_amt_14 = sub.order_ord_amt, tr.order_ord_num_14 = sub.order_ord_num,
  152. tr.order_ord_tfee_14 = sub.order_ord_tfee;";
  153. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  154. sql = @"UPDATE tk_report
  155. SET order_ord_num = order_ord_num_3 + order_ord_num_12 + order_ord_num_13 + order_ord_num_14
  156. WHERE DATE(report_date) IN @orderTimes AND accountId=@accountId;";
  157. //DBContext.Execute(sql, new { accountId = _account.id, orderTimes });
  158. return total;
  159. }
  160. public async Task<(int, DateTime, List<DateTime>)> GetTkOrdersAsync(DateTime startTime, DateTime endTime, DateTime last_modifiedTime, int optType, int sleep = 100)
  161. {
  162. List<DateTime> orderTimes = [];
  163. // 确保 startTime 小于 endTime
  164. if (startTime > endTime)
  165. {
  166. (startTime, endTime) = (endTime, startTime);
  167. }
  168. DateTime max_modifiedTime = last_modifiedTime;
  169. int pageNo = 1;
  170. int pageSize = 100;
  171. bool hasNext = true;
  172. int total = 0;
  173. while (hasNext)
  174. {
  175. string body = "";
  176. JsonElement data;
  177. try
  178. {
  179. for (var i = 1; i <= 5; i++)
  180. {
  181. var ts = DateTime.Now.Convert2UnixTimestamp(true);
  182. string cookies = _account.cookies;
  183. string uuid = cookies.GetContentPart("__jdu=", ";");
  184. string __jda = cookies.GetContentPart("__jda=", ";");
  185. if (__jda.Split('.').Length > 1)
  186. {
  187. uuid = __jda.Split('.')[1];
  188. }
  189. string useragent = _account.user_agent;
  190. if (string.IsNullOrEmpty(useragent)) useragent = ProviderFakeUserAgent.RandomComputer;
  191. string url = $"https://api.m.jd.com/api?functionId=listOrderSku&" +
  192. $"appid=unionpc&_={ts}&loginType=3&uuid={uuid}";
  193. var args = new
  194. {
  195. funName = "listOrderSku",
  196. page = new
  197. {
  198. pageNo,
  199. pageSize
  200. },
  201. param = new
  202. {
  203. unionRole = 1,
  204. startTime = startTime.ToString("yyyy-MM-dd HH:mm:ss"),
  205. endTime = endTime.ToString("yyyy-MM-dd HH:mm:ss"),
  206. orderStatus = 0,
  207. unionTags = new string[] { "0" },
  208. optType,
  209. spId = ""
  210. },
  211. lientPageId = "jingfen_pc"
  212. };
  213. string post = args.Convert2Json().UrlEncode();
  214. WebClientUtility client = new WebClientUtility();
  215. client.Proxy = _proxy;
  216. #if DEBUG
  217. client.Proxy = null;
  218. #endif
  219. client.SetContentType("application/x-www-form-urlencoded");
  220. client.AddHeaders("X-Referer-Page", "https://union.jd.com/proManager/index");
  221. client.AddHeaders("X-Rp-Client", "h5_1.0.0");
  222. client.AddHeaders("Referer", "https://union.jd.com/");
  223. client.AddHeaders("Origin", "https://union.jd.com");
  224. client.UserAgent = useragent;
  225. client.AddHeaders("Cookie", cookies);
  226. client.Post($"body={post}");
  227. var response = await client.RequestAsync(url, "POST");
  228. #if DEBUG
  229. #else
  230. client.Proxy = _proxy;
  231. #endif
  232. if (response.ResponseException != null)
  233. {
  234. _ = new LoggerLibrary("api_error", "fail")
  235. .Info(response.ResponseException.Message, response.ResponseException.StackTrace)
  236. .SaveAsync();
  237. throw response.ResponseException;
  238. }
  239. body = response.Body();
  240. #if DEBUG
  241. _ = new LoggerLibrary("debug", "GetJdOrders")
  242. .Info(body)
  243. .SaveAsync();
  244. #endif
  245. break;
  246. }
  247. data = body.Convert2JsonElement();
  248. //{"code":452,"hasNext":false,"message":"单位时间内订单量太多,请缩短查询时间范围","totalNum":0}
  249. var code = data.Read<int>("code");
  250. if (code != 200)
  251. {
  252. if (body.Contains("no login"))
  253. {
  254. JdPoolCore.Disabled(_account.id, _account.name, $"{body}");
  255. return (0, DateTime.MinValue, new List<DateTime>());
  256. }
  257. throw new APIException(body);
  258. }
  259. }
  260. catch (Exception ex)
  261. {
  262. throw new APIException(body);
  263. }
  264. using var conn = DBContext.GetOpenConnection();
  265. conn.Open();
  266. try
  267. {
  268. foreach (var order in data.ElementRead("result").EnumerateArray())
  269. {
  270. JdOrderDetailsDTO item;
  271. try
  272. {
  273. item = order.GetRawText().Convert2Object<JdOrderDetailsDTO>();
  274. //todo 处理结算时间和结算金额 balanceExt "{\"20240920\":100.539}"
  275. }
  276. catch (Exception ex)
  277. {
  278. throw;
  279. }
  280. //todo 处理结算时间和结算金额 balanceExt "{\"20240920\":100.539}"
  281. item.accountId = _account.id;
  282. item.accountName = _account.name;
  283. if (item.finishTime > max_modifiedTime) max_modifiedTime = item.finishTime;
  284. if (last_modifiedTime != DateTime.MinValue && last_modifiedTime >= item.finishTime)
  285. {
  286. return (total, max_modifiedTime, orderTimes);
  287. }
  288. var exist = new DBContext.Table("jd_order_details")
  289. .Fields("id, orderId")
  290. .Get<dynamic>("orderid=@orderid", new { item.orderId });
  291. if (exist != null)
  292. {
  293. var id = DBContext.Update<JdOrderDetailsDTO>(exist.id, item);
  294. }
  295. else
  296. {
  297. conn.Insert(item);
  298. }
  299. total++;
  300. }
  301. }
  302. catch (Exception ex)
  303. {
  304. throw;
  305. }
  306. finally
  307. {
  308. conn.Dispose();
  309. }
  310. pageNo++;
  311. hasNext = data.Read<bool>("hasNext");
  312. if (sleep > 0) Thread.Sleep(sleep);
  313. }
  314. return (total, max_modifiedTime, orderTimes);
  315. }
  316. }
  317. }