/// <summary> /// 第三方电商发起售后服务申请 /// </summary> /// <returns></returns> public static ResultDTO CreateService(CommodityOrder order, OrderItem orderItem, OrderRefundAfterSales refund) { string param = string.Format("orderId={0}&refundId={1}", order.Id, refund.Id); LogHelper.Debug(string.Format("ThirdECommerceServiceHelper.CreateService第三方电商发起售后服务申请,入参:{0}", param)); var result = new ResultDTO { Message = "操作失败,请稍后重试", ResultCode = -1 }; try { #region 判断是否第三方电商订单及获取订单信息 var orderSku = ThirdECOrderPackageSku.ObjectSet().FirstOrDefault(o => o.OrderId == order.Id && o.OrderItemId == orderItem.Id); if (orderSku == null) { return(result); } var thirdECommerce = ThirdECommerceHelper.GetThirdECommerce(order.AppId); if (thirdECommerce == null || string.IsNullOrEmpty(thirdECommerce.OpenApiKey) || string.IsNullOrEmpty(thirdECommerce.OpenApiCallerId) || string.IsNullOrEmpty(thirdECommerce.ServiceCreateUrl)) { return(result); } if (ThirdECService.ObjectSet().Any(p => p.OrderRefundAfterSalesId == refund.Id)) { return(result); } #endregion #region 调用第三方发起售后服务申请接口 var jsonStr = string.Empty; var response = ThirdECommerceSV.CreateService(new ThirdApiInfo { Apikey = thirdECommerce.OpenApiKey, CallerId = thirdECommerce.OpenApiCallerId, ApiUrl = thirdECommerce.ServiceCreateUrl }, new ThirdServiceCreate { ServiceId = refund.Id.ToString().ToLower(), OrderId = order.Id.ToString().ToLower(), CustomerName = order.ReceiptUserName, CustomerPhone = order.ReceiptPhone, SkuId = orderSku.SkuId, Number = orderItem.Number, RefundReason = refund.RefundReason, RefundDesc = refund.RefundDesc, RefundImgs = refund.OrderRefundImgs }, ref jsonStr); #endregion var isSuccess = response.Successed && response.Result != null; #region 保存ThirdECService if (isSuccess) { var stateName = string.Empty; var stateDesc = string.Empty; if (response.Result.ServiceStatus == 0) { stateName = "不支持售后"; result = new ResultDTO { Message = response.Result.RejectReason, ResultCode = 1 }; } else if (response.Result.ServiceStatus == 1) { stateName = "待审核"; result = new ResultDTO { isSuccess = true, Message = "请等待审核", ResultCode = 2 }; } stateDesc = response.Result.RejectReason; var service = new ThirdECService { Id = Guid.NewGuid(), OrderId = orderSku.OrderId, OrderCode = orderSku.OrderCode, OrderItemId = orderSku.OrderItemId, OrderRefundAfterSalesId = refund.Id, SubId = order.SubId, SkuId = orderSku.SkuId, Number = orderItem.Number, StateName = stateName, StateDesc = stateDesc, EntityState = EntityState.Added }; ContextFactory.CurrentThreadContext.SaveObject(service); } else { ContextFactory.ReleaseContextSession(); } #endregion #region 保存ThirdECServiceJournal var journal = new ThirdECServiceJournal { Id = Guid.NewGuid(), OrderId = orderSku.OrderId, OrderCode = orderSku.OrderCode, OrderItemId = orderSku.OrderItemId, OrderRefundAfterSalesId = refund.Id, Name = "发起售后服务申请", Details = response.Msg, Json = jsonStr, EntityState = EntityState.Added }; ContextFactory.CurrentThreadContext.SaveObject(journal); #endregion if (!isSuccess) //失败则只保存日志 { int count = ContextFactory.CurrentThreadContext.SaveChanges(); if (count == 0) { LogHelper.Error(string.Format("ThirdECommerceServiceHelper.CreateService第三方电商发起售后服务申请数据保存失败,入参:{0}", param)); } } return(result); } catch (Exception ex) { LogHelper.Error(string.Format("ThirdECommerceServiceHelper.CreateService第三方电商发起售后服务申请异常,入参:{0}", param), ex); return(result); } }
/// <summary> /// 创建第三方电商订单 /// </summary> /// <param name="orderId"></param> public static void CreateOrder(Guid orderId) { string param = string.Format("orderId={0}", orderId); LogHelper.Debug(string.Format("ThirdECOrderHelper.CreateOrder创建第三方电商订单,入参:{0}", param)); if (orderId == Guid.Empty) { return; } Task.Factory.StartNew(() => { try { #region 判断是否第三方电商订单及获取订单信息 var order = CommodityOrder.ObjectSet().FirstOrDefault(o => o.Id == orderId); if (order == null || !order.PaymentTime.HasValue || order.State != 1 || order.EsAppId != YJB.Deploy.CustomDTO.YJBConsts.YJAppId) { return; } var thirdECommerce = ThirdECommerceHelper.GetThirdECommerce(order.AppId); if (thirdECommerce == null || string.IsNullOrEmpty(thirdECommerce.OpenApiKey) || string.IsNullOrEmpty(thirdECommerce.OpenApiCallerId) || string.IsNullOrEmpty(thirdECommerce.OrderCreateUrl)) { return; } var orderItemList = OrderItem.ObjectSet() .Where(p => p.CommodityOrderId == order.Id && p.JDCode != null && p.JDCode != string.Empty) .Select(p => new { p.JDCode, p.Name, p.Number, CostPrice = p.CostPrice ?? 0 }).ToList(); if (orderItemList.Count == 0) { return; } if (ThirdECOrder.ObjectSet().Any(p => p.OrderId == order.Id)) { return; } #endregion #region 调用第三方电商订单提交接口 var jsonStr = string.Empty; var response = ThirdECommerceSV.CreateOrder(new ThirdApiInfo { Apikey = thirdECommerce.OpenApiKey, CallerId = thirdECommerce.OpenApiCallerId, ApiUrl = thirdECommerce.OrderCreateUrl }, new ThirdOrderCreate { OrderId = order.Id.ToString().ToLower(), SubTime = order.SubTime.ToString("yyyy-MM-dd HH:mm:ss"), PayTime = order.PaymentTime.Value.ToString("yyyy-MM-dd HH:mm:ss"), Address = new ThirdAddress { Name = order.ReceiptUserName, Phone = order.ReceiptPhone, ProvinceName = order.Province, CityName = order.City, CountyName = order.District, TownName = order.Street, AddressDetail = order.ReceiptAddress, FullAddress = order.Province + order.City + order.District + order.Street + order.ReceiptAddress, ZipCode = order.RecipientsZipCode }, OrderSkus = orderItemList.Select(p => new ThirdOrderCreateSku { SkuId = p.JDCode, Name = p.Name, Number = p.Number, Price = p.CostPrice }).ToList() }, ref jsonStr); #endregion #region 保存ThirdECOrderJournal var thirdOrderJournal = new ThirdECOrderJournal { Id = Guid.NewGuid(), OrderId = order.Id, OrderCode = order.Code, SubTime = DateTime.Now, SubId = order.SubId, Name = "提交订单", Details = response.Msg, Json = jsonStr, EntityState = EntityState.Added }; ContextFactory.CurrentThreadContext.SaveObject(thirdOrderJournal); #endregion #region 保存错误日志 if (!response.Successed) { SaveErrorLog(new ThirdECOrderErrorLogDTO { Content = response.Msg, AppId = order.AppId, OrderId = order.Id, OrderCode = order.Code, AppName = order.AppName, CommodityNames = string.Join("、", orderItemList.Select(x => x.Name).ToList()), Json = jsonStr }); return; } #endregion #region 保存ThirdECOrder var thirdOrder = new ThirdECOrder { Id = Guid.NewGuid(), OrderId = order.Id, OrderCode = order.Code, OrderSubTime = order.SubTime, PayTime = order.PaymentTime.Value, SubId = order.SubId, AppId = order.AppId, EsAppId = order.EsAppId.Value, AppName = order.AppName, AppType = order.AppType, SupplierName = order.SupplierName, SupplierCode = order.SupplierCode, SupplierType = order.SupplierType, ShipperType = order.ShipperType, StateName = "下单成功", EntityState = EntityState.Added }; ContextFactory.CurrentThreadContext.SaveObject(thirdOrder); #endregion int count = ContextFactory.CurrentThreadContext.SaveChanges(); if (count == 0) { LogHelper.Error(string.Format("ThirdECOrderHelper.CreateOrder创建第三方电商订单失败,入参:{0}", param)); } } catch (Exception ex) { LogHelper.Error(string.Format("ThirdECOrderHelper.CreateOrder创建第三方电商订单异常,入参:{0}", param), ex); } }); }
/// <summary> /// 第三方电商取消售后服务申请 /// </summary> /// <param name="appId"></param> /// <param name="orderId"></param> /// <param name="serviceId"></param> /// <returns></returns> public static ResultDTO CancelService(Guid appId, Guid orderId, Guid serviceId) { string param = string.Format("appId={0}&orderId={1}&serviceId={2}", appId, orderId, serviceId); LogHelper.Debug(string.Format("ThirdECommerceServiceHelper.CancelService第三方电商取消售后服务申请,入参:{0}", param)); var result = new ResultDTO { Message = "操作失败,请稍后重试" }; try { #region 判断是否第三方电商售后服务单及获取售后服务单信息 var service = ThirdECService.ObjectSet().FirstOrDefault(o => o.OrderRefundAfterSalesId == serviceId); if (service == null) { return(result); } var thirdECommerce = ThirdECommerceHelper.GetThirdECommerce(appId); if (thirdECommerce == null || string.IsNullOrEmpty(thirdECommerce.OpenApiKey) || string.IsNullOrEmpty(thirdECommerce.OpenApiCallerId) || string.IsNullOrEmpty(thirdECommerce.ServiceCancelUrl)) { return(result); } if (service.UserCancelTime.HasValue) { return(result); } #endregion #region 调用第三方取消售后服务申请接口 var jsonStr = string.Empty; var response = ThirdECommerceSV.CancelService(new ThirdApiInfo { Apikey = thirdECommerce.OpenApiKey, CallerId = thirdECommerce.OpenApiCallerId, ApiUrl = thirdECommerce.ServiceCancelUrl }, serviceId.ToString(), ref jsonStr); #endregion var isSuccess = response.Successed; #region 修改ThirdECService if (isSuccess) { service.UserCancelTime = DateTime.Now; service.StateName = "取消售后服务申请成功"; result = new ResultDTO { isSuccess = true }; } else { ContextFactory.ReleaseContextSession(); service.StateName = "取消售后服务申请失败"; result = new ResultDTO { Message = response.Msg ?? "不允许取消" }; } service.StateDesc = response.Msg; #endregion #region 保存ThirdECServiceJournal var journal = new ThirdECServiceJournal { Id = Guid.NewGuid(), OrderId = service.OrderId, OrderCode = service.OrderCode, OrderItemId = service.OrderItemId, OrderRefundAfterSalesId = service.OrderRefundAfterSalesId, Name = "取消售后服务申请", Details = response.Msg, Json = jsonStr, EntityState = EntityState.Added }; ContextFactory.CurrentThreadContext.SaveObject(journal); #endregion if (!isSuccess) //失败则只保存日志 { int count = ContextFactory.CurrentThreadContext.SaveChanges(); if (count == 0) { LogHelper.Error(string.Format("ThirdECommerceServiceHelper.CreateService第三方电商取消售后服务申请数据保存失败,入参:{0}", param)); } } return(result); } catch (Exception ex) { LogHelper.Error(string.Format("ThirdECommerceServiceHelper.CreateService第三方电商取消售后服务申请异常,入参:{0}", param), ex); return(result); } }
/// <summary> /// 取消第三方电商订单 /// </summary> /// <param name="orderId"></param> public static ResultDTO CancelOrder(Guid orderId) { string param = string.Format("orderId={0}", orderId); LogHelper.Debug(string.Format("ThirdECOrderHelper.CancelOrder取消第三方电商订单,入参:{0}", param)); if (orderId == Guid.Empty) { return new ResultDTO { Message = "参数有误" } } ; var result = new ResultDTO { Message = "操作失败,请稍后重试", ResultCode = -1 }; try { #region 判断是否第三方电商订单及获取订单信息 var order = ThirdECOrder.ObjectSet().FirstOrDefault(o => o.OrderId == orderId); if (order == null) { return(result); } var thirdECommerce = ThirdECommerceHelper.GetThirdECommerce(order.AppId); if (thirdECommerce == null || string.IsNullOrEmpty(thirdECommerce.OpenApiKey) || string.IsNullOrEmpty(thirdECommerce.OpenApiCallerId) || string.IsNullOrEmpty(thirdECommerce.OrderCancelUrl)) { return(result); } if (order.CancelResultTime.HasValue) { return(result); } #endregion order.CancelApplyTime = DateTime.Now; #region 调用第三方电商订单取消接口 var jsonStr = string.Empty; var response = ThirdECommerceSV.CancelOrder(new ThirdApiInfo { Apikey = thirdECommerce.OpenApiKey, CallerId = thirdECommerce.OpenApiCallerId, ApiUrl = thirdECommerce.OrderCancelUrl }, orderId.ToString(), ref jsonStr); #endregion var isSuccess = response.Successed && response.Result != null; #region 修改ThirdECOrder if (isSuccess) { order.CancelResultTime = DateTime.Now; if (response.Result.CancelStatus == 0) { order.StateName = "不允许取消"; result = new ResultDTO { Message = response.Result.RejectReason, ResultCode = 1 }; } else if (response.Result.CancelStatus == 1) { order.StateName = "允许取消"; // 模拟订单取消审核结果回调 Timer timer = null; timer = new Timer(_ => { CancelOrderCallback(JsonConvert.SerializeObject(new { OrderId = order.Id.ToString().ToLower(), CancelStatus = 1 })); timer.Dispose(); }, null, 2000, System.Threading.Timeout.Infinite); result = new ResultDTO { isSuccess = true, Message = response.Result.RejectReason, ResultCode = 0 }; } else if (response.Result.CancelStatus == 2) { order.StateName = "待审核"; result = new ResultDTO { isSuccess = true, Message = "请等待审核", ResultCode = 2 }; } order.StateDesc = response.Result.RejectReason; } else { ContextFactory.ReleaseContextSession(); order.StateName = "取消订单申请失败"; order.StateDesc = response.Msg; } #endregion #region 保存ThirdECOrderJournal var thirdOrderJournal = new ThirdECOrderJournal { Id = Guid.NewGuid(), OrderId = order.OrderId, OrderCode = order.OrderCode, SubTime = DateTime.Now, SubId = order.SubId, Name = "取消订单申请", Details = response.Msg, Json = jsonStr, EntityState = EntityState.Added }; ContextFactory.CurrentThreadContext.SaveObject(thirdOrderJournal); #endregion if (!isSuccess) //失败则只保存日志 { int count = ContextFactory.CurrentThreadContext.SaveChanges(); if (count == 0) { LogHelper.Error(string.Format("ThirdECOrderHelper.CancelOrder取消第三方电商订单数据保存失败,入参:{0}", param)); } } return(result); } catch (Exception ex) { LogHelper.Error(string.Format("ThirdECOrderHelper.CancelOrder取消第三方电商订单异常,入参:{0}", param), ex); return(result); } }