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 Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using System.Text; using Yitter.IdGenerator; namespace BBWYB.Server.Business { public class QiKuManager : BaseBusiness, IDenpendency { private RestApiService restApiService; private IFreeSql fsql; private NLogManager nLogManager; private List hgzTaskTypeList; private Lazy dingDingBusinessLazy; private Lazy venderBusinessLazy; private DingDingBusiness dingDingBusiness => dingDingBusinessLazy.Value; private VenderBusiness venderBusiness => venderBusinessLazy.Value; public QiKuManager(RestApiService restApiService, IFreeSql fsql, NLogManager nLogManager, IIdGenerator idGenerator, IServiceProvider serviceProvider) : base(fsql, nLogManager, idGenerator) { this.restApiService = restApiService; hgzTaskTypeList = new List() { Enums.TimeLimitTaskType.合格证拟定任务, Enums.TimeLimitTaskType.合格证补充任务 }; this.dingDingBusinessLazy = new Lazy(() => serviceProvider.GetService()); this.venderBusinessLazy = new Lazy(() => serviceProvider.GetService()); } /// /// 通知齐库到货情况 /// 支持关联的采购平台以来源SKU为单位通知齐库 /// 不支持关联的采购平台以采购单为单位通知齐库 /// /// /// /// /// public void PublishQiKuReceiveInfo(OrderPurchaseInfo orderPurchaseInfo, IList orderPurchaseRelationInfoList, IList orderPurchaseSkuInfoList, IList purchaseExpressOrderList) { if (orderPurchaseInfo.PurchasePlatform == Enums.Platform.阿里巴巴) PublishQiKuByRelation(orderPurchaseRelationInfoList, orderPurchaseSkuInfoList, purchaseExpressOrderList); else PublishQiKuPurchaseExpressOrder(orderPurchaseInfo, purchaseExpressOrderList); } private void PublishQiKuByRelation(IList orderPurchaseRelationInfoList, IList orderPurchaseSkuInfoList, IList 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 purchaseExpressOrderList) { try { if (string.IsNullOrEmpty(orderPurchaseInfo.BelongSkuIds)) return; var orderId = purchaseExpressOrderList.FirstOrDefault().OrderId; var orderSkuList = fsql.Select().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 { } } /// /// 查询齐库合格证 /// /// /// /// public void SearchCerConfigured(Order order, IList packTaskSkuPurchaseSchemeIdList, IList 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>(restApiResult.Content); if (!response.Success) throw new Exception(response.Msg); if (response.Data.PackTaskSkuPurchaseSchemeIdList != null && response.Data.PackTaskSkuPurchaseSchemeIdList.Count() > 0) { IList> updateOrderSkuList = new List>(); List insertTimeLimitTaskList = new List(); var dbTimeLimitTaskList = fsql.Select().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.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 })}"); } } /// /// 齐库推送sku配置状态 /// /// public void QikuPublishOrderSkuPackConfigState(QikuPublishOrderSkuPackConfigStateRequest request) { var order = fsql.Select(request.OrderId).ToOne(); if (order == null) throw new BusinessException($"未查询到订单{request.OrderId}"); if (request.PackConfigState == Enums.PackConfigState.需修改 && order.ShopId != 9) return; var orderSku = fsql.Select().Where(osku => osku.OrderId == request.OrderId && osku.BelongSkuId == request.SkuId).ToOne(); if (orderSku == null) throw new BusinessException($"未查询到订单来源sku{request.SkuId}"); IInsert insertTimeLimitTask = null; IUpdate updateTimeLimitTask = null; IUpdate updateOrderSku = null; if (orderSku.PackConfigState != request.PackConfigState) { //updateOrderSku = fsql.Select() // .InnerJoin((osku, o) => osku.OrderId == o.Id) // .Where((osku, o) => o.OrderState != Enums.OrderState.已取消 && // o.IntoStoreType == Enums.IntoStoreType.发回齐越 && // osku.SkuId == orderSku.SkuId && // osku.PackConfigState != request.PackConfigState) // .ToUpdate() // .Set(osku => osku.PackConfigState, request.PackConfigState); updateOrderSku = fsql.Update(orderSku.Id).Set(osku => osku.PackConfigState, request.PackConfigState); } if (request.PackConfigState == Enums.PackConfigState.需修改) { var isExists = fsql.Select().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); #region 发送钉钉通知 try { var shop = venderBusiness.GetShopList(order.ShopId.Value).FirstOrDefault(); var content = new StringBuilder(); content.AppendLine($"拳探店铺:{shop?.ShopName}"); content.AppendLine($"订单号:{order.OrderSn}"); content.AppendLine($"拳探SKU:{orderSku.SkuId}"); content.AppendLine("信息:需补充合格证信息"); dingDingBusiness.SendDingDingBotMessage("SECf32e6111bb4bd633cfe44cf0c1d4c3384cda4b91096bcdd962402fdfd67f31c6", "https://oapi.dingtalk.com/robot/send?access_token=c87a037e038ec38e379ad5bc6fe50adc679ba89d4957f8fc5396e15fbc3a9df7", content.ToString()); } catch { } #endregion } } else if (request.PackConfigState == Enums.PackConfigState.已配置) { updateTimeLimitTask = fsql.Update().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(); }); } } }