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 List hgzTaskTypeList; private Lazy dingDingBusinessLazy; private Lazy venderBusinessLazy; private Lazy timeLimitRulesLazy; private DingDingBusiness dingDingBusiness => dingDingBusinessLazy.Value; private VenderBusiness venderBusiness => venderBusinessLazy.Value; private TimeLimitRules timeLimitRules => timeLimitRulesLazy.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()); this.timeLimitRulesLazy = new Lazy(() => serviceProvider.GetService()); } public void PublishQikuReceiveInfo(string orderId, string wayBillNo, IList currentPurchaseOrderList, IList currentOrderSkuList, IList purchaseExpressOrderList_all, IList orderPurchaseRelationList_all, IList orderPurchaseSkuList_all) { try { //查询跟此快递单有关的当前订单的采购单 var relationByWaybillNoPurchaseOrderIds = purchaseExpressOrderList_all.Where(peo => peo.OrderId == orderId && peo.WaybillNo == wayBillNo) .Select(peo => peo.PurchaseOrderId) .ToList(); var relationByWaybillNoPurchaseOrderList = currentPurchaseOrderList.Where(po => relationByWaybillNoPurchaseOrderIds.Contains(po.PurchaseOrderId)).ToList(); //查询跟采购单有关联的订单sku var relationOrderSkuList = currentOrderSkuList.Where(osku => relationByWaybillNoPurchaseOrderList.Any(po => po.BelongSkuIds.Contains(osku.SkuId))).ToList(); foreach (var osku in relationOrderSkuList) { //查询跟此订单sku有关的采购单 var relationByOrderSkuPurchaseOrderList = currentPurchaseOrderList.Where(po => po.BelongSkuIds.Contains(osku.SkuId)).ToList(); var isSignAll = true; foreach (var po in relationByOrderSkuPurchaseOrderList) { if (po.PurchasePlatform == Enums.Platform.阿里巴巴) { //查询跟此sku有关的关联信息 var oriRelationList = orderPurchaseRelationList_all.Where(ori => ori.OrderId == osku.OrderId && ori.PurchaseOrderId == po.PurchaseOrderId && ori.BelongSkuId == osku.SkuId).ToList(); if (oriRelationList.Count() == 0) { isSignAll = false; break; } foreach (var relation in oriRelationList) { var pos = orderPurchaseSkuList_all.FirstOrDefault(pos => pos.OrderId == relation.OrderId && pos.PurchaseOrderId == relation.PurchaseOrderId && pos.PurchaseSkuId == relation.PurchaseSkuId); if (pos == null || string.IsNullOrEmpty(pos.WaybillNo)) { isSignAll = false; break; } var peo = purchaseExpressOrderList_all.FirstOrDefault(x => x.WaybillNo == pos.WaybillNo); if (peo == null || peo.ExpressState != "QianShou") { isSignAll = false; break; } } if (!isSignAll) break; } else { var peoList = purchaseExpressOrderList_all.Where(x => x.OrderId == osku.OrderId && x.PurchaseOrderId == po.PurchaseOrderId); if (peoList.Count() == 0 || peoList.Any(peo => peo.ExpressState != "QianShou")) { isSignAll = false; break; } } } //推送齐库 var publishResult = restApiService.SendRequest("http://qiku.qiyue666.com", "/Api/PackPurchaseTask/UpdateAvailabilityState", new { availability = isSignAll ? 0 : 1, orderId = osku.OrderId, skuId = osku.BelongSkuId }, null, HttpMethod.Post); if (publishResult.StatusCode != System.Net.HttpStatusCode.OK) throw new Exception(publishResult.Content); } } 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 = timeLimitRules.CalculateExpirationTime(Enums.TimeLimitTaskType.合格证拟定任务, DateTime.Now) //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.合格证补充任务, ExpirationTime = timeLimitRules.CalculateExpirationTime(Enums.TimeLimitTaskType.合格证补充任务, DateTime.Now) //DateTime.Now.AddHours(2) }; //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(); }); } } }