Files
KJD_WebService/Store.SyncLogistics/Services/VirtualLogisticsService.cs
T

250 lines
6.7 KiB
C#

using MAX.Core.Commons;
using MAX.Core.DependencyInjection;
using MAX.Core.Logger;
using Store.Contracts.Order.Entitys;
using Store.Contracts.Shops;
using Store.Contracts.User.Entitys;
using Store.Contracts.User.Interfaces;
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
{
/// <summary>
/// 固定费用1.7元
/// </summary>
const decimal FIXEDFEES = 1.7m;
private IFreeSql _freeSql;
private readonly ILoggerFacade _logger;
private readonly IUserService _userService;
private readonly VirtualLogisticsApi _api;
public VirtualLogisticsService(VirtualLogisticsApi api, IFreeSql freeSql, ILoggerFacade logger, IUserService userService)
{
_freeSql = freeSql ?? throw new ArgumentNullException(nameof(freeSql));
_logger = logger;
_userService = userService;
_api = api;
}
/// <summary>
/// 获取账户余额
/// </summary>
public decimal GetUserMoney()
{
var user = _api.GetUserMoney();
return user.Money;
}
/// <summary>
/// 获取快递列表
/// </summary>
public List<ExpressItem> GetExpressList()
{
return _api.GetExpressList();
}
/// <summary>
/// 获取仓库列表及价格
/// </summary>
public List<CangItem> GetCangList()
{
return _api.GetCangList();
}
/// <summary>
/// 获取礼品列表及价格
/// </summary>
/// <param name="kuaidiId">快递ID,可选</param>
public List<GoodsItem> GetGoodsList(string kuaidiId = null)
{
return _api.GetGoodsList(kuaidiId);
}
/// <summary>
/// 购买快递
/// </summary>
public List<YhSyncLogisticsEntity> Buy(string userName, string orderId, string agentOrderId)
{
if (string.IsNullOrEmpty(orderId) || string.IsNullOrWhiteSpace(agentOrderId))
{
throw new ArgumentNullException(nameof(orderId));
}
var user = _userService.GetUserByUserName(userName);
if (user == null) throw new Exception("错误的账号");
if (_userService.CanAfford(userName, Contracts.User.Enums.UserPaymentTypes.同步单号, FIXEDFEES) == false)
{
throw new Exception("余额不足");
}
//中间订单
var middleOrder = _freeSql.Select<OrderPddBuyerOrderEntity>()
.Where(a => a.OrderSn == orderId)
.First();
if (middleOrder == null) throw new Exception("错误的订单号");
//middleOrder.TrackingNumber = "YT0709726946568";
var w_order = _freeSql.Select<WeiOrderEntity>()
.Where(a => a.OrderId == middleOrder.WxOrderId)
.First();
if (w_order == null) throw new Exception("错误的订单号");
var productItem = _freeSql.Select<WeiOrderItemEntity>()
.Where(a => a.OrderId == w_order.OrderId)
.First();
if (productItem == null) throw new Exception("错误的订单详情号");
var shop = _freeSql.Select<ShopEntity>()
.Where(a => a.Id == productItem.ShopId)
.First();
if (shop == 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,
ShopOrder =w_order.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,
EwaybillOrderCode="wx7809625286c28f2f",
EncryptDatas=new EncryptDatas
{
DsOrderId=agentOrderId,
SkuId = productItem.SkuId,
ProductId = productItem.ProductId,
SkuCnt = productItem.SkuCnt,
FromShopCode = shop.AppKey,
},
ExtendParams=new ExtendParams{
Token="",
Api="",
DsOrderId =agentOrderId
},
OtherKuaidiSn= middleOrder.TrackingNumber,
}
]
};
Result<List<BuyResultItem>> ret = _api.Buy(request);
if (ret.Success == false)
{
_logger.Error($"购买同步单错误:{ret.Msg}");
throw new Exception("购买同步单失败");
}
List<YhSyncLogisticsEntity> yhSyncs = new List<YhSyncLogisticsEntity>();
foreach (var sync in ret.Data)
{
yhSyncs.Add(new YhSyncLogisticsEntity
{
OrderId = sync.OrderSn,
TrackingNumber = sync.KuaidiSn,
SourceTrackingNumber = sync.OtherKuaidiSn
});
}
var count = _freeSql.Insert<YhSyncLogisticsEntity>(yhSyncs).ExecuteAffrows();
if (count != yhSyncs.Count)
{
_logger.Error("购买同步单提交到数据库错误");
throw new Exception("记录同步单失败");
}
//todo 更新UserBlance
_userService.UserPayment(user.UserName, orderId, type: Contracts.User.Enums.UserPaymentTypes.同步单号);
return yhSyncs;
}
/// <summary>
/// 上传取件码
/// </summary>
public bool UploadPickup(string userName, string orderId, string pickupCode)
{
var user = _userService.GetUserByUserName(userName);
if (user == null) throw new Exception("错误的账号");
var syncLogistics = _freeSql.Select<YhSyncLogisticsEntity>()
.Where(a => a.OrderId == orderId)
.First();
if (syncLogistics == null) throw new Exception("错误的订单号");
var ret = _api.UploadPickup(new List<PickupItem>
{
new PickupItem
{
BufaSn = syncLogistics.SourceTrackingNumber,
PickupCode = pickupCode
}
});
syncLogistics.pickup_code = pickupCode;
syncLogistics.upload_pickup_Time = DateTime.Now;
syncLogistics.upload_pickup_status = ret.Success;
if (ret.Success == false)
{
string msg = $"推送取件码失败:{ret.Msg}";
syncLogistics.Remark = msg;
_logger.Error(msg);
throw new Exception("推送取件码失败");
}
var affrows = _freeSql.Update<YhSyncLogisticsEntity>()
.Set(a => a.pickup_code, syncLogistics.pickup_code)
.Set(a => a.upload_pickup_Time, syncLogistics.upload_pickup_Time)
.Set(a => a.upload_pickup_status, syncLogistics.upload_pickup_status)
.Set(a => a.Remark, syncLogistics.Remark)
.Where(a => a.Id == syncLogistics.Id)
.ExecuteAffrows();
if (affrows != 1)
{
_logger.Error("取件码保存错误");
throw new Exception("取件码保存错误");
}
return true;
}
public YhSyncLogisticsEntity GetSyncLogistics(string orderId)
{
var item = _freeSql.Select<YhSyncLogisticsEntity>()
.Where(a => a.OrderId == orderId).First();
return item;
}
public List<OrderStatusResultItem> GetOrderStatus(string[] orders)
{
var ret = _api.GetOrderStatus(orders);
if (ret.Success)
{
return ret.Data;
}
throw new Exception(ret.Msg);
}
}