using AllBase; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using WxOrder.Framework.Commons; using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace Work.ERP.API.Orders { class PullWXOrderResult : Result { public List Data { get; set; } public class FailedItem { public long shopId { get; set; } public string shopName { get; set; } public string message { get; set; } } } /// /// 拉取微信订单 /// public class PullWXOrderServiceApi { /// /// 拉取指定店铺的指定订单 /// /// 店铺ID /// 订单号 /// 服务器异步任务ID;为 0 表示服务器已直接完成 /// public async Task PullOrderDetail(long shopId, string orderId) { string data = JsonConvert.SerializeObject(new { platformCode = "WEISHOP", shopId = shopId, orderId = orderId, clientUserName = BaseClass.UserName }); var result = await PostAndGetTaskIdAsync("/prod-api/api/oms-api/client/sync/pull_order_detail", data); return result.Code == 200; } /// /// 拉取指定店铺的订单列表 /// /// 店铺ID /// 订单号列表 /// 服务器异步任务ID;为 0 表示服务器已直接完成 /// public async Task PullOrdersDetail(long shopId, string[] orderIds) { string data = JsonConvert.SerializeObject(new { platformCode = "WEISHOP", shopId = shopId, orderId = string.Join(",", orderIds), clientUserName = BaseClass.UserName, }); var result = await PostAndGetTaskIdAsync("/prod-api/api/oms-api/client/sync/pull_order_detail/batch", data); if (result.Code == 200) { return true; } else { string msg = string.Join("\n", result.Data.Select(a => $"{a.shopName},{a.message}")); throw new Exception(msg); } } /// /// 拉取指定商铺列表时间范围内的数据 /// /// 店铺ID列表 /// 开始时间 /// 结束时间 /// 服务器异步任务ID;为 0 表示服务器已直接完成 /// public async Task PullOrderByShopIdsAndTimeRange(long[] shopIds, DateTime startTime, DateTime endTime) { TimeSpan time = endTime - startTime; if (time.TotalDays > 7) throw new Exception("时间范围最大7天"); string data = JsonConvert.SerializeObject(new { platformCode = "WEISHOP", shopIds = string.Join(",", shopIds), startTime = startTime.ToString("yyyy-MM-dd HH:mm:ss"), endTime = endTime.ToString("yyyy-MM-dd HH:mm:ss"), clientUserName = BaseClass.UserName, remark = $"同步 {startTime.ToString("yyyy-MM-dd")}/{endTime.ToString("dd")}" }); var result = await PostAndGetTaskIdAsync("/prod-api/api/oms-api/client/sync/pull_order", data); if (result.Code == 200) { return true; } else { string msg = string.Join("\n", result.Data.Select(a => $"{a.shopName},任务正在执行中")); throw new Exception(msg); } } /// /// 拉取指定店铺的售后订单 /// /// 店铺ID列表 /// 服务器异步任务ID;为 0 表示服务器已直接完成 /// public async Task PullRefundOrder(long[] shopIds, DateTime startTime, DateTime endTime) { TimeSpan time = endTime - startTime; if (time.TotalDays > 7) throw new Exception("时间范围最大7天"); string data = JsonConvert.SerializeObject(new { platformCode = "WEISHOP", shopIdList = shopIds, startTime = startTime.ToString("yyyy-MM-dd HH:mm:ss"), endTime = endTime.ToString("yyyy-MM-dd HH:mm:ss"), clientUserName = BaseClass.UserName, remark = $"同步售后订单" }); var result = await PostAndGetTaskIdAsync("/prod-api/api/oms-api/client/sync/oms/pull_refund_order", data); if (result.Code == 200) { return true; } else { string msg = string.Join("\n", result.Data.Select(a => $"{a.shopName},任务正在执行中")); throw new Exception(msg); } } /// /// 拉取指定商铺的售后订单 /// /// 店铺ID /// 售后订单号列表 /// 服务器异步任务ID;为 0 表示服务器已直接完成 /// public async Task PullAfterSaleOrders(long shopId, string[] afterSaleOrderIds) { string data = JsonConvert.SerializeObject(new { platformCode = "WEISHOP", shopId = shopId, afterSaleOrderIds = afterSaleOrderIds, clientUserName = BaseClass.UserName, remark = $"同步售后订单" }); var result = await PostAndGetTaskIdAsync("/prod-api/api/oms-api/client/sync/oms/pull_list/batch", data); if (result.Code == 200) { return true; } else { string msg = string.Join("\n", result.Data.Select(a => $"{a.shopName},{a.message}")); throw new Exception(msg); } } /// /// 提交拉取请求并从返回结果中解析服务器异步任务ID。 /// 兼容 data 为纯数字、数字字符串或对象(含 taskId/task_id/id 字段)等返回格式; /// 无法解析时返回 0(视为服务器直接完成)。 /// private async Task PostAndGetTaskIdAsync(string url, string data) { var result = await ERPHttp.e57PostResponseAsync(url, data); return result; } } }