|
|
|
using BBWYB.Common.Http;
|
|
|
|
using BBWYB.Common.Log;
|
|
|
|
using BBWYB.Common.Models;
|
|
|
|
using BBWYB.Server.Model;
|
|
|
|
using BBWYB.Server.Model.Db;
|
|
|
|
using BBWYB.Server.Model.Dto;
|
|
|
|
using FreeSql;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Yitter.IdGenerator;
|
|
|
|
|
|
|
|
namespace BBWYB.Server.Business
|
|
|
|
{
|
|
|
|
public class QiKuManager : BaseBusiness, IDenpendency
|
|
|
|
{
|
|
|
|
private RestApiService restApiService;
|
|
|
|
private IFreeSql fsql;
|
|
|
|
private NLogManager nLogManager;
|
|
|
|
private List<Enums.TimeLimitTaskType?> hgzTaskTypeList;
|
|
|
|
|
|
|
|
public QiKuManager(RestApiService restApiService, IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator) : base(fsql, nLogManager, idGenerator)
|
|
|
|
{
|
|
|
|
this.restApiService = restApiService;
|
|
|
|
hgzTaskTypeList = new List<Enums.TimeLimitTaskType?>()
|
|
|
|
{
|
|
|
|
Enums.TimeLimitTaskType.合格证拟定任务,
|
|
|
|
Enums.TimeLimitTaskType.合格证补充任务
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 通知齐库到货情况
|
|
|
|
/// <para>支持关联的采购平台以来源SKU为单位通知齐库</para>
|
|
|
|
/// <para>不支持关联的采购平台以采购单为单位通知齐库</para>
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="orderPurchaseInfo"></param>
|
|
|
|
/// <param name="orderPurchaseRelationInfoList"></param>
|
|
|
|
/// <param name="orderPurchaseSkuInfoList"></param>
|
|
|
|
/// <param name="purchaseExpressOrderList"></param>
|
|
|
|
public void PublishQiKuReceiveInfo(OrderPurchaseInfo orderPurchaseInfo,
|
|
|
|
IList<OrderPurchaseRelationInfo> orderPurchaseRelationInfoList,
|
|
|
|
IList<OrderPurchaseSkuInfo> orderPurchaseSkuInfoList,
|
|
|
|
IList<PurchaseExpressOrder> purchaseExpressOrderList)
|
|
|
|
{
|
|
|
|
if (orderPurchaseInfo.PurchasePlatform == Enums.Platform.阿里巴巴)
|
|
|
|
PublishQiKuByRelation(orderPurchaseRelationInfoList, orderPurchaseSkuInfoList, purchaseExpressOrderList);
|
|
|
|
else
|
|
|
|
PublishQiKuPurchaseExpressOrder(orderPurchaseInfo, purchaseExpressOrderList);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PublishQiKuByRelation(IList<OrderPurchaseRelationInfo> orderPurchaseRelationInfoList,
|
|
|
|
IList<OrderPurchaseSkuInfo> orderPurchaseSkuInfoList,
|
|
|
|
IList<PurchaseExpressOrder> purchaseExpressOrderList)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var relationGroups = orderPurchaseRelationInfoList.GroupBy(opri => opri.SourceSkuId);
|
|
|
|
foreach (var relationGroup in relationGroups)
|
|
|
|
{
|
|
|
|
bool isSignAll = true;
|
|
|
|
foreach (var relation in relationGroup)
|
|
|
|
{
|
|
|
|
var purchaseSku = orderPurchaseSkuInfoList.FirstOrDefault(x => x.PurchaseSkuId == relation.PurchaseSkuId);
|
|
|
|
if (purchaseSku == null || string.IsNullOrEmpty(purchaseSku.WaybillNo))
|
|
|
|
{
|
|
|
|
isSignAll = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
var purchaseExpressOrder = purchaseExpressOrderList.FirstOrDefault(x => x.WaybillNo == purchaseSku.WaybillNo);
|
|
|
|
if (purchaseExpressOrder == null || purchaseExpressOrder.ExpressState != "QianShou")
|
|
|
|
{
|
|
|
|
isSignAll = false;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
restApiService.SendRequest("http://qiku.qiyue666.com",
|
|
|
|
"/Api/PackPurchaseTask/UpdateAvailabilityState",
|
|
|
|
new
|
|
|
|
{
|
|
|
|
availability = isSignAll ? 0 : 1,
|
|
|
|
orderId = relationGroup.FirstOrDefault().OrderId,
|
|
|
|
skuId = relationGroup.Key
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
HttpMethod.Post);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void PublishQiKuPurchaseExpressOrder(OrderPurchaseInfo orderPurchaseInfo, IList<PurchaseExpressOrder> purchaseExpressOrderList)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(orderPurchaseInfo.BelongSkuIds))
|
|
|
|
return;
|
|
|
|
|
|
|
|
var orderId = purchaseExpressOrderList.FirstOrDefault().OrderId;
|
|
|
|
var orderSkuList = fsql.Select<OrderSku>().Where(osku => osku.OrderId == orderId).ToList();
|
|
|
|
|
|
|
|
var isSignAll = !purchaseExpressOrderList.Any(x => x.ExpressState != "QianShou");
|
|
|
|
|
|
|
|
var notifyList = orderSkuList.Where(osku => orderPurchaseInfo.BelongSkuIds.Contains(osku.SkuId)).ToList();
|
|
|
|
if (notifyList.Count() == 0)
|
|
|
|
return;
|
|
|
|
foreach (var notifySku in notifyList)
|
|
|
|
{
|
|
|
|
restApiService.SendRequest("http://qiku.qiyue666.com",
|
|
|
|
"/Api/PackPurchaseTask/UpdateAvailabilityState",
|
|
|
|
new
|
|
|
|
{
|
|
|
|
availability = isSignAll ? 0 : 1,
|
|
|
|
orderId = notifySku.OrderId,
|
|
|
|
skuId = notifySku.BelongSkuId
|
|
|
|
},
|
|
|
|
null,
|
|
|
|
HttpMethod.Post);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 查询齐库合格证
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="order"></param>
|
|
|
|
/// <param name="packTaskSkuPurchaseSchemeIdList"></param>
|
|
|
|
/// <param name="orderSkuList"></param>
|
|
|
|
public void SearchCerConfigured(Order order, IList<QiKuPackTaskSkuPurchaseSchemeIdRequest> packTaskSkuPurchaseSchemeIdList, IList<OrderSku> orderSkuList)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
nLogManager.Default().Info($"SearchCerConfigured OrderId {order.Id}, packTaskSkuPurchaseSchemeIdList {JsonConvert.SerializeObject(packTaskSkuPurchaseSchemeIdList)}");
|
|
|
|
var restApiResult = restApiService.SendRequest("http://qiku.qiyue666.com", "api/PackPurchaseTask/SearchCerConfigured", new
|
|
|
|
{
|
|
|
|
orderId = order.Id,
|
|
|
|
packTaskSkuPurchaseSchemeIdList
|
|
|
|
}, null, HttpMethod.Post);
|
|
|
|
|
|
|
|
if (restApiResult.StatusCode != System.Net.HttpStatusCode.OK)
|
|
|
|
throw new Exception(restApiResult.Content);
|
|
|
|
var response = JsonConvert.DeserializeObject<ApiResponse<QiKuBindingSchemeResponse>>(restApiResult.Content);
|
|
|
|
if (!response.Success)
|
|
|
|
throw new Exception(response.Msg);
|
|
|
|
|
|
|
|
if (response.Data.PackTaskSkuPurchaseSchemeIdList != null && response.Data.PackTaskSkuPurchaseSchemeIdList.Count() > 0)
|
|
|
|
{
|
|
|
|
IList<IUpdate<OrderSku>> updateOrderSkuList = new List<IUpdate<OrderSku>>();
|
|
|
|
List<TimeLimitTask> insertTimeLimitTaskList = new List<TimeLimitTask>();
|
|
|
|
|
|
|
|
var dbTimeLimitTaskList = fsql.Select<TimeLimitTask>().Where(t => t.OrderId == order.Id &&
|
|
|
|
t.TaskType == Enums.TimeLimitTaskType.合格证拟定任务).ToList();
|
|
|
|
foreach (var skuConfigured in response.Data.PackTaskSkuPurchaseSchemeIdList)
|
|
|
|
{
|
|
|
|
var orderSku = orderSkuList.FirstOrDefault(osku => osku.BelongSkuId == skuConfigured.SkuId);
|
|
|
|
if (orderSku == null)
|
|
|
|
continue;
|
|
|
|
var oldPackState = orderSku.PackConfigState ?? Enums.PackConfigState.待配置;
|
|
|
|
var qiKuPackState = skuConfigured.IsConfiguredCer ? Enums.PackConfigState.已配置 : Enums.PackConfigState.待配置;
|
|
|
|
if (oldPackState != qiKuPackState)
|
|
|
|
{
|
|
|
|
orderSku.PackConfigState = qiKuPackState;
|
|
|
|
var update = fsql.Update<OrderSku>(orderSku.Id).Set(osku => osku.PackConfigState, qiKuPackState);
|
|
|
|
updateOrderSkuList.Add(update);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qiKuPackState == Enums.PackConfigState.待配置 && order.ShopId != 9 && !dbTimeLimitTaskList.Any(t=>t.SkuId == orderSku.SkuId))
|
|
|
|
{
|
|
|
|
//创建合格证拟定任务
|
|
|
|
var t = new TimeLimitTask()
|
|
|
|
{
|
|
|
|
CreateTme = DateTime.Now,
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
OrderId = order.Id,
|
|
|
|
SkuId = orderSku.SkuId,
|
|
|
|
OrderSn = order.OrderSn,
|
|
|
|
ShopId = order.ShopId,
|
|
|
|
TaskType = Enums.TimeLimitTaskType.合格证拟定任务,
|
|
|
|
ExpirationTime = DateTime.Now.AddDays(1),
|
|
|
|
};
|
|
|
|
insertTimeLimitTaskList.Add(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
|
|
{
|
|
|
|
if (updateOrderSkuList.Count() > 0)
|
|
|
|
{
|
|
|
|
foreach (var update in updateOrderSkuList)
|
|
|
|
update.ExecuteAffrows();
|
|
|
|
}
|
|
|
|
if (insertTimeLimitTaskList.Count() > 0)
|
|
|
|
fsql.Insert(insertTimeLimitTaskList).ExecuteAffrows();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
nLogManager.Default().Error(ex, $"SearchCerConfigured\r\n{JsonConvert.SerializeObject(new { order.Id, packTaskSkuPurchaseSchemeIdList })}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// 齐库推送sku配置状态
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request"></param>
|
|
|
|
public void QikuPublishOrderSkuPackConfigState(QikuPublishOrderSkuPackConfigStateRequest request)
|
|
|
|
{
|
|
|
|
var order = fsql.Select<Order>(request.OrderId).ToOne();
|
|
|
|
if (order == null)
|
|
|
|
throw new BusinessException($"未查询到订单{request.OrderId}");
|
|
|
|
|
|
|
|
if (request.PackConfigState == Enums.PackConfigState.需修改 && order.ShopId != 9)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var orderSku = fsql.Select<OrderSku>().Where(osku => osku.OrderId == request.OrderId && osku.BelongSkuId == request.SkuId).ToOne();
|
|
|
|
if (orderSku == null)
|
|
|
|
throw new BusinessException($"未查询到订单来源sku{request.SkuId}");
|
|
|
|
|
|
|
|
|
|
|
|
IInsert<TimeLimitTask> insertTimeLimitTask = null;
|
|
|
|
IUpdate<TimeLimitTask> updateTimeLimitTask = null;
|
|
|
|
IUpdate<OrderSku> updateOrderSku = null;
|
|
|
|
|
|
|
|
if (orderSku.PackConfigState != request.PackConfigState)
|
|
|
|
updateOrderSku = fsql.Update<OrderSku>(orderSku.Id).Set(osku => osku.PackConfigState, request.PackConfigState);
|
|
|
|
|
|
|
|
if (request.PackConfigState == Enums.PackConfigState.需修改)
|
|
|
|
{
|
|
|
|
var isExists = fsql.Select<TimeLimitTask>().Where(t => t.OrderId == request.OrderId &&
|
|
|
|
t.SkuId == orderSku.SkuId &&
|
|
|
|
t.CompletionTime == null &&
|
|
|
|
t.TaskType == Enums.TimeLimitTaskType.合格证补充任务)
|
|
|
|
.Any();
|
|
|
|
if (!isExists)
|
|
|
|
{
|
|
|
|
var t = new TimeLimitTask()
|
|
|
|
{
|
|
|
|
CreateTme = DateTime.Now,
|
|
|
|
Id = idGenerator.NewLong(),
|
|
|
|
OrderId = request.OrderId,
|
|
|
|
SkuId = orderSku.SkuId,
|
|
|
|
OrderSn = order.OrderSn,
|
|
|
|
ShopId = order.ShopId,
|
|
|
|
TaskType = Enums.TimeLimitTaskType.合格证补充任务
|
|
|
|
};
|
|
|
|
if (DateTime.Now.Hour < 16)
|
|
|
|
t.ExpirationTime = DateTime.Now.AddHours(2);
|
|
|
|
else
|
|
|
|
t.ExpirationTime = DateTime.Now.Date.AddDays(1).AddHours(13);
|
|
|
|
insertTimeLimitTask = fsql.Insert(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (request.PackConfigState == Enums.PackConfigState.已配置)
|
|
|
|
{
|
|
|
|
|
|
|
|
updateTimeLimitTask = fsql.Update<TimeLimitTask>().Set(t => t.CompletionTime, DateTime.Now)
|
|
|
|
.Set(t => t.IsTimely == (DateTime.Now < t.ExpirationTime ? true : false))
|
|
|
|
.Where(t => t.OrderId == request.OrderId)
|
|
|
|
.Where(t => t.SkuId == orderSku.SkuId)
|
|
|
|
.Where(t => t.CompletionTime == null)
|
|
|
|
.Where(t => hgzTaskTypeList.Contains(t.TaskType));
|
|
|
|
}
|
|
|
|
|
|
|
|
fsql.Transaction(() =>
|
|
|
|
{
|
|
|
|
insertTimeLimitTask?.ExecuteAffrows();
|
|
|
|
updateTimeLimitTask?.ExecuteAffrows();
|
|
|
|
updateOrderSku?.ExecuteAffrows();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|