using MAX.Core.Commons;
using MAX.Core.DependencyInjection;
using MAX.Core.Logger;
using Store.Contracts.Order.Entitys;
using Store.VirtualLogistics.Apis;
using Store.VirtualLogistics.Entitys;
using Store.VirtualLogistics.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Store.VirtualLogistics.Services;
public class VirtualLogisticsService : ITransientDependency
{
private IFreeSql _freeSql;
private readonly ILoggerFacade _logger;
private readonly VirtualLogisticsApi _api;
public VirtualLogisticsService(VirtualLogisticsApi api, IFreeSql freeSql, ILoggerFacade logger)
{
_freeSql = freeSql ?? throw new ArgumentNullException(nameof(freeSql));
_logger = logger;
_api = api;
}
///
/// 获取账户余额
///
public decimal GetUserMoney()
{
var user = _api.GetUserMoney();
return user.Money;
}
///
/// 获取快递列表
///
public List GetExpressList()
{
return _api.GetExpressList();
}
///
/// 获取仓库列表及价格
///
public List GetCangList()
{
return _api.GetCangList();
}
///
/// 获取礼品列表及价格
///
/// 快递ID,可选
public List GetGoodsList(string kuaidiId = null)
{
return _api.GetGoodsList(kuaidiId);
}
///
/// 购买快递
///
public List Buy(string orderId, string ds_orderId)
{
var item = _freeSql.Select()
.Where(a => a.OrderSn == orderId)
.First();
if (item == null) throw new Exception("错误的订单号");
var w_order = _freeSql.Select()
.Where(a => a.OrderId == item.WxOrderId)
.First();
if (w_order == null) throw new Exception("错误的订单号");
var productItem = _freeSql.Select()
.Where(a => a.OrderId == w_order.OrderId)
.First();
if (productItem == null) throw new Exception("错误的订单详情号");
BuyRequest request = new BuyRequest()
{
KuaidiId = "874",
GoodsId = "8473",
CangId = "914",
Platform = 6,
Auth = 1,
SendName = RandomNameHelper.GetRandomName(),
SendMobile = "13024622251",
SendProvince = "上海市",
SendCity = "上海市",
SendDistrict = "普陀区",
SendAddress = "真如镇街道真北路2653弄F区808号楼",
RecList = [
new RecItem
{
OrderSn = orderId,
RecName = w_order.UserName,
RecMobile = w_order.TelNumber,
RecProvince = w_order.ProvinceName,
RecCity = w_order.CityName,
RecDistrict = w_order.CountyName,
RecAddress = w_order.DetailInfo,
EncryptDatas=new EncryptDatas
{
DsOrderId=ds_orderId,
SkuId = productItem.SkuId,
ProductId = productItem.ProductId,
SkuCnt = productItem.SkuCnt.ToString(),
},
OtherKuaidiSn= "6738192431"//item.TrackingNumber,
}
]
};
Result> ret = _api.Buy(request);
if (ret.Success == false)
{
_logger.Error($"购买同步单错误:{ret.Msg}");
throw new Exception("购买同步单失败");
}
List yhSyncs = new List();
foreach (var sync in ret.Data)
{
yhSyncs.Add(new YhSyncLogisticsEntity
{
OrderId = sync.OrderSn,
TrackingNumber = sync.KuaidiSn,
SourceTrackingNumber = sync.OtherKuaidiSn
});
}
var count = _freeSql.Insert(yhSyncs).ExecuteAffrows();
if (count != yhSyncs.Count)
{
_logger.Error("购买同步单提交到数据库错误");
throw new Exception("记录同步单失败");
}
return yhSyncs;
}
///
/// 上传取件码
///
public Result> UploadPickup(List data)
{
return _api.UploadPickup(data);
}
public YhSyncLogisticsEntity GetSyncLogistics(string orderId)
{
var item = _freeSql.Select()
.Where(a => a.OrderId == orderId).First();
return item;
}
}