using Operater.DAL;
using Operater.DTO.TopMessage;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using CommonUtil;
using AlibabaSDK.Request;
using AlibabaSDK.Api;
using System.Linq;
using YouZanSDKStandard.Api.Request;
using YouZanSDKStandard.Api;
using Operater.DbModel;
namespace Operater.Service.TopMessage
{
public class AliTradeFlowService : IAliTradeFlowService
{
///
/// 1688发货后,有赞发货
///
///
///
public async Task AliTradeSendGoods(AliTradeSendGoodsRequest request)
{
AliTradeFlowResponse response = new AliTradeFlowResponse()
{
Success = false
};
try
{
//获取订单关系
var tradeRelat = new TradeRelationDAL().GetById(request.OrderId);
if (tradeRelat.IsNull() || tradeRelat.AliTid.IsNullOrEmpty())
{//异常
response.Msg = $"发货失败-订单{request.OrderId}没有找到对应有赞的订单关系";
response.Mark = request.OrderId;
return response;
}
//获取关联的子订单
var orderList = new OrderRelationDAL().ListGet(t => t.AliTid == request.OrderId);
if (orderList.IsNull() || orderList.Count == 0)
{
response.Msg = $"发货失败-订单{request.OrderId}没有找到子订单信息";
response.Mark = request.OrderId;
return response;
}
//获取1688物流信息
AlibabaGetLogisticsInfosRequest alibabaGetLogisticsInfosRequest = new AlibabaGetLogisticsInfosRequest()
{
orderId = long.Parse(request.OrderId),
webSite = "1688",
fields = "company,name,sender,receiver,sendgood"
};
var alibabaGetLogisticsInfosRes = AliabaClient.Execute(alibabaGetLogisticsInfosRequest, request.AliToken);
if (alibabaGetLogisticsInfosRes.IsNull() || !alibabaGetLogisticsInfosRes.Success || alibabaGetLogisticsInfosRes.result.IsNull())
{//获取1688物流信息失败
response.Msg = $"发货失败-订单{request.OrderId}获取1688物流信息失败:" + (alibabaGetLogisticsInfosRes.IsNull() ? "null" : alibabaGetLogisticsInfosRes.ErrorMsg);
response.Mark = request.OrderId;
return response;
}
//获取1688与有赞的快递公司关联关系
var expressId = new AliExpressDAL().GetById(alibabaGetLogisticsInfosRes.result.logisticsCompanyId);
string outStype = expressId.YzId.ToString();
//有赞发货
//根据1688子订单获取有赞子订单数据
var yzOids = orderList.Where(t => alibabaGetLogisticsInfosRes.result.orderEntryIds.Contains(t.AlisOid)).ToList();
if (yzOids.IsNull() || yzOids.Count == 0 || yzOids.Count != alibabaGetLogisticsInfosRes.result.orderEntryIds.Split(',').Length)
{
response.Msg = $"发货失败-1688订单{request.OrderId}获取有赞子订单失败,1688子订单:" + alibabaGetLogisticsInfosRes.result.orderEntryIds;
response.Mark = request.OrderId;
return response;
}
var oidArr = yzOids.Select(t => t.YzOid).ToArray();
string oids = string.Join(',', oidArr);
YouZanLogisticsConfirmRequest yzRequest = new YouZanLogisticsConfirmRequest()
{
tid = tradeRelat.YzTid,
out_stype = outStype,
out_sid = alibabaGetLogisticsInfosRes.result.logisticsBillNo,
is_no_express = 0,
oids = oids
};
var yzResponse = YouZanClient.Execute(yzRequest, request.YzToken);
if (yzResponse.IsNull() || !yzResponse.success)
{
response.Msg = $"发货失败-1688订单{request.OrderId}有赞发货失败:" + (yzResponse.IsNull() ? "null" : yzResponse.message);
response.Mark = request.OrderId;
return response;
}
LogUtil.Info($"1688订单{request.OrderId}发货成功:{JSONUtil.ObjectToJson(yzRequest)}", "发货日志");
//修改1688订单对有赞订单的发货状态
AliTradeinfo aliTradeinfo = new AliTradeinfo()
{
YzSendgoodsStatus = 1
};
new AliTradeinfoDAL().Update(aliTradeinfo, i => new { i.YzSendgoodsStatus }, t => t.Tid == request.OrderId);
}
catch (Exception e)
{
response.Success = false;
response.Msg = "处理错误:" + e.Message;
response.Mark = request.OrderId;
}
return response;
}
}
}