order.cs 15 KB

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