186 lines
5.9 KiB
C#
186 lines
5.9 KiB
C#
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<FailedItem> Data { get; set; }
|
|
public class FailedItem
|
|
{
|
|
public long shopId { get; set; }
|
|
public string shopName { get; set; }
|
|
public string message { get; set; }
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 拉取微信订单
|
|
/// </summary>
|
|
public class PullWXOrderServiceApi
|
|
{
|
|
/// <summary>
|
|
/// 拉取指定店铺的指定订单
|
|
/// </summary>
|
|
/// <param name="shopId">店铺ID</param>
|
|
/// <param name="orderId">订单号</param>
|
|
/// <returns>服务器异步任务ID;为 0 表示服务器已直接完成</returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public async Task<bool> 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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拉取指定店铺的订单列表
|
|
/// </summary>
|
|
/// <param name="shopId">店铺ID</param>
|
|
/// <param name="orderIds">订单号列表</param>
|
|
/// <returns>服务器异步任务ID;为 0 表示服务器已直接完成</returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public async Task<bool> 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拉取指定商铺列表时间范围内的数据
|
|
/// </summary>
|
|
/// <param name="shopIds">店铺ID列表</param>
|
|
/// <param name="startTime">开始时间</param>
|
|
/// <param name="endTime">结束时间</param>
|
|
/// <returns>服务器异步任务ID;为 0 表示服务器已直接完成</returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public async Task<bool> 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拉取指定店铺的售后订单
|
|
/// </summary>
|
|
/// <param name="shopIds">店铺ID列表</param>
|
|
/// <returns>服务器异步任务ID;为 0 表示服务器已直接完成</returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public async Task<bool> 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 拉取指定商铺的售后订单
|
|
/// </summary>
|
|
/// <param name="shopId">店铺ID</param>
|
|
/// <param name="afterSaleOrderIds">售后订单号列表</param>
|
|
/// <returns>服务器异步任务ID;为 0 表示服务器已直接完成</returns>
|
|
/// <exception cref="Exception"></exception>
|
|
public async Task<bool> 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);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 提交拉取请求并从返回结果中解析服务器异步任务ID。
|
|
/// 兼容 data 为纯数字、数字字符串或对象(含 taskId/task_id/id 字段)等返回格式;
|
|
/// 无法解析时返回 0(视为服务器直接完成)。
|
|
/// </summary>
|
|
private async Task<PullWXOrderResult> PostAndGetTaskIdAsync(string url, string data)
|
|
{
|
|
var result = await ERPHttp.e57PostResponseAsync<PullWXOrderResult>(url, data);
|
|
return result;
|
|
}
|
|
}
|
|
}
|