public static RestfulResult Create(OrderRequest request,UserModel authUser,out bool createSuccess) { request.AuthUser = authUser; createSuccess = false; var _productRepo = ServiceLocator.Current.Resolve<IProductRepository>(); var _orderRepo = ServiceLocator.Current.Resolve<IOrderRepository>(); var _orderLogRepo = ServiceLocator.Current.Resolve<IOrderLogRepository>(); var _orderItemRepo = ServiceLocator.Current.Resolve<IOrderItemRepository>(); var _inventoryRepo = ServiceLocator.Current.Resolve<IInventoryRepository>(); var _orderexRepo = ServiceLocator.Current.Resolve<IOrder2ExRepository>(); decimal totalAmount = 0m; foreach (var product in request.OrderModel.Products) { var productEntity = _productRepo.Find(product.ProductId); if (productEntity == null) return CommonUtil.RenderError(r => r.Message = string.Format("{0} 不存在!", product.ProductId)); if (!productEntity.Is4Sale.HasValue || productEntity.Is4Sale.Value == false) return CommonUtil.RenderError(r => r.Message = string.Format("{0} 不能销售!", productEntity.Id)); totalAmount += productEntity.Price * product.Quantity; } if (totalAmount <= 0) return CommonUtil.RenderError(r => r.Message = "商品销售价信息错误!"); var orderNo = OrderRule.CreateCode(0); var otherFee = OrderRule.ComputeFee(); var erpOrder = new { orderSource = request.Channel??string.Empty, dealPayType = request.OrderModel.Payment.PaymentCode, shipType = request.OrderModel.ShippingType, needInvoice = request.OrderModel.NeedInvoice ? 1 : 0, invoiceTitle = request.OrderModel.InvoiceTitle??string.Empty, invoiceMemo = request.OrderModel.InvoiceDetail??string.Empty, orderMemo = request.OrderModel.Memo??string.Empty, lastUpdateTime = DateTime.Now.ToString(ErpServiceHelper.DATE_FORMAT), payTime = string.Empty, recvfeeReturnTime = string.Empty, dealState = "STATE_WG_WAIT_PAY", buyerName = authUser.Nickname??string.Empty, sellerConsignmentTime = string.Empty, receiverPostcode = "", freight = 0, couponFee = "0", comboInfo = string.Empty, dealRateState = "DEAL_RATE_NO_EVAL", totalCash = "0", dealNote = request.OrderModel.Memo??string.Empty, dealFlag = string.Empty, dealCode = orderNo, createTime = DateTime.Now.ToString(ErpServiceHelper.DATE_FORMAT), receiverMobile = request.OrderModel.ShippingAddress==null?string.Empty:request.OrderModel.ShippingAddress.ShippingContactPhone, receiverPhone = request.OrderModel.ShippingAddress == null ? string.Empty : request.OrderModel.ShippingAddress.ShippingContactPhone, receiverName = request.OrderModel.ShippingAddress == null ? string.Empty : request.OrderModel.ShippingAddress.ShippingContactPerson, dealPayFeeTicket = "0", dealNoteType = "UN_LABEL", recvfeeTime = string.Empty, dealPayFeeTotal = totalAmount, ppCodId = string.Empty, receiverAddress = request.OrderModel.ShippingAddress == null ? string.Empty : request.OrderModel.ShippingAddress.ShippingAddress, transportType = "TRANSPORT_NONE", wuliuId = "0", vipCard = string.Empty, itemList = new List<dynamic>() }; using (var ts = new TransactionScope()) { var orderEntity = _orderRepo.Insert(new OrderEntity() { BrandId = 0, CreateDate = DateTime.Now, CreateUser = request.AuthUser.Id, CustomerId = request.AuthUser.Id, InvoiceDetail = request.OrderModel.InvoiceDetail, InvoiceSubject = request.OrderModel.InvoiceTitle, NeedInvoice = request.OrderModel.NeedInvoice, Memo = request.OrderModel.Memo, PaymentMethodCode = request.OrderModel.Payment.PaymentCode, PaymentMethodName = request.OrderModel.Payment.PaymentName, ShippingAddress = request.OrderModel.ShippingAddress==null?string.Empty:request.OrderModel.ShippingAddress.ShippingAddress, ShippingContactPerson = request.OrderModel.ShippingAddress == null ? string.Empty : request.OrderModel.ShippingAddress.ShippingContactPerson, ShippingContactPhone = request.OrderModel.ShippingAddress == null ? string.Empty : request.OrderModel.ShippingAddress.ShippingContactPhone, ShippingFee = otherFee.TotalFee, ShippingZipCode = request.OrderModel.ShippingAddress == null ? string.Empty : request.OrderModel.ShippingAddress.ShippingZipCode, Status = (int)OrderStatus.Create, StoreId = 0, UpdateDate = DateTime.Now, UpdateUser = request.AuthUser.Id, TotalAmount = totalAmount, InvoiceAmount = totalAmount, OrderNo = orderNo, TotalPoints = otherFee.TotalPoints, OrderSource = request.Channel }); foreach (var product in request.OrderModel.Products) { var productEntity = _productRepo.Find(product.ProductId); var inventoryEntity = Context.Set<InventoryEntity>().Where(pm => pm.ProductId == product.ProductId && pm.PColorId == product.Properties.ColorValueId && pm.PSizeId == product.Properties.SizeValueId).FirstOrDefault(); if (inventoryEntity == null) return CommonUtil.RenderError(r => r.Message = string.Format("{0}库存 不存在!", productEntity.Id)); if (inventoryEntity.Amount < product.Quantity) return CommonUtil.RenderError(r => r.Message = string.Format("{0}库存不足!", productEntity.Id)); var productSizeEntity = Context.Set<ProductPropertyValueEntity>().Where(ppv => ppv.Id == product.Properties.SizeValueId).FirstOrDefault(); var productColorEntity = Context.Set<ProductPropertyValueEntity>().Where(ppv => ppv.Id == product.Properties.ColorValueId).FirstOrDefault(); _orderItemRepo.Insert(new OrderItemEntity() { BrandId = productEntity.Brand_Id, CreateDate = DateTime.Now, CreateUser = request.AuthUser.Id, ItemPrice = productEntity.Price, OrderNo = orderNo, ProductId = productEntity.Id, ProductName = productEntity.Name, Quantity = product.Quantity, Status = (int)DataStatus.Normal, StoreId = productEntity.Store_Id, UnitPrice = productEntity.UnitPrice, UpdateDate = DateTime.Now, UpdateUser = request.AuthUser.Id, ExtendPrice = productEntity.Price * product.Quantity, ProductDesc = product.ProductDesc, ColorId = productColorEntity == null ? 0 : productColorEntity.PropertyId, ColorValueId = productColorEntity == null ? 0 : productColorEntity.Id, SizeId = productSizeEntity == null ? 0 : productSizeEntity.PropertyId, SizeValueId = productSizeEntity == null ? 0 : productSizeEntity.Id, ColorValueName = productColorEntity == null ? string.Empty : productColorEntity.ValueDesc, SizeValueName = productSizeEntity == null ? string.Empty : productSizeEntity.ValueDesc, StoreItemNo = productEntity.SkuCode, Points = 0 }); inventoryEntity.Amount = inventoryEntity.Amount - product.Quantity; inventoryEntity.UpdateDate = DateTime.Now; _inventoryRepo.Update(inventoryEntity); int? storeId = null; int? saleCodeId = null; if (product.StoreId.HasValue && product.StoreId != 0 && product.SectionId.HasValue && product.SectionId != 0) { var storeEntity = Context.Set<StoreEntity>().Find(product.StoreId.Value); storeId = storeEntity.ExStoreId; var sectionEntity = Context.Set<SectionEntity>().Find(product.SectionId.Value); saleCodeId = sectionEntity.ChannelSectionId; } erpOrder.itemList.Add(new { itemName = productEntity.Name, itemFlag = string.Empty, itemCode = string.Empty, account = string.Empty, refundStateDesc = string.Empty, itemAdjustPrice = "0", itemRetailPrice = productEntity.UnitPrice, tradePropertymask = "256", itemDealState = "STATE_WG_WAIT_PAY", itemDealCount = product.Quantity, skuId = inventoryEntity.ChannelInventoryId, itemDealPrice = productEntity.Price, storeId = storeId.HasValue?storeId.ToString():string.Empty, saleCodeSid = saleCodeId.HasValue?saleCodeId.ToString():string.Empty }); } _orderLogRepo.Insert(new OrderLogEntity() { CreateDate = DateTime.Now, CreateUser = request.AuthUser.Id, CustomerId = request.AuthUser.Id, Operation = string.Format("创建订单"), OrderNo = orderNo, Type = (int)OrderOpera.FromCustomer }); string exOrderNo = string.Empty; bool isSuccess = ErpServiceHelper.SendHttpMessage(ConfigManager.ErpBaseUrl, new { func = "DivideOrderToSaleFromJSON", OrdersJSON = erpOrder }, r => exOrderNo = r.order_no , null); if (isSuccess) { var exOrderEntity= _orderexRepo.Insert(new Order2ExEntity() { ExOrderNo = exOrderNo, OrderNo = orderEntity.OrderNo, UpdateTime = DateTime.Now }); ts.Complete(); createSuccess = true; return CommonUtil.RenderSuccess<OrderResponse>(m => m.Data = new OrderResponse().FromEntity<OrderResponse>(orderEntity,o=>o.ExOrderNo = exOrderEntity.ExOrderNo)); } else { return CommonUtil.RenderError(r => r.Message = "失败"); } } }
private static dynamic CreateErpItem(OrderItemEntity item) { var skuId = Context.Set<InventoryEntity>().Where(i=>i.ProductId == item.ProductId && i.PColorId == item.ColorValueId && i.PSizeId == item.SizeValueId).Select(t=>t.ChannelInventoryId).FirstOrDefault(); var store = Context.Set<StoreEntity>() .Join(Context.Set<ProductEntity>().Where(p => p.Id == item.ProductId), s => s.Id, p => p.Store_Id, (s, p) => s.ExStoreId); var erpItem = new { itemName = item.ProductName, itemFlag = string.Empty, itemCode = string.Empty, account = string.Empty, refundStateDesc = string.Empty, itemAdjustPrice = "0", itemRetailPrice = item.ItemPrice, tradePropertymask = "256", itemDealState = "STATE_WG_WAIT_PAY", itemDealCount = item.Quantity, skuId, itemDealPrice = item.ItemPrice, ////微客多上拿不到以下信息 storeId = store, saleCodeSid = string.Empty //saleCodeSid = saleCodeId.HasValue ? saleCodeId.ToString() : string.Empty }; return erpItem; }
private ActionResult DoRMA(RMARequest request, UserModel authUser,bool ifNeedValidate) { var dbContext = Context; var orderEntity = dbContext.Set<OrderEntity>().Where(o => o.OrderNo == request.OrderNo && o.CustomerId == authUser.Id).FirstOrDefault(); if (orderEntity == null) { return this.RenderError(m => m.Message = "订单号不存在"); } if (ifNeedValidate) { if (orderEntity.Status != (int)OrderStatus.Shipped) { return this.RenderError(m => m.Message = "订单状态现在不能申请退货"); } var rmaEntity = dbContext.Set<RMAEntity>().Where(r => r.OrderNo == request.OrderNo && r.Status != (int)RMAStatus.Reject2Customer && r.Status != (int)RMAStatus.Void).FirstOrDefault(); if (rmaEntity != null) { return this.RenderError(m => m.Message = "已经申请了退货单,不能再次申请!"); } } var erpRma = new { dealCode = orderEntity.OrderNo, CUSTOMER_FRT = "0", COMPANY_FRT = "0", REAL_NAME = orderEntity.ShippingContactPerson, USER_SID = "0", Detail = new List<dynamic>() }; request.Reason = string.Format("{0}-{1}",dbContext.Set<RMAReasonEntity>().Find(request.RMAReason).Reason,request.Reason??string.Empty); using (var ts = new TransactionScope()) { decimal rmaAmount = 0; var newRma = _rmaRepo.Insert(new RMAEntity() { RMANo = OrderRule.CreateRMACode(), Reason = request.Reason, RMAReason = request.RMAReason, Status = (int)RMAStatus.Created, OrderNo = request.OrderNo, CreateDate = DateTime.Now, CreateUser = authUser.Id, RMAAmount = rmaAmount, RMAType = (int)RMAType.FromOnline, UpdateDate = DateTime.Now, UpdateUser = authUser.Id, ContactPhone = request.ContactPhone, UserId = orderEntity.CustomerId }); foreach (var rma in request.Products2) { var orderItemEntity = dbContext.Set<OrderItemEntity>().Where(o => o.OrderNo == request.OrderNo && o.ProductId == rma.ProductId && o.ColorValueId == rma.Properties.ColorValueId && o.SizeValueId == rma.Properties.SizeValueId).FirstOrDefault(); if (orderItemEntity == null) return this.RenderError(r => r.Message = string.Format("{0} not in order", rma.ProductId)); if (orderItemEntity.Quantity < rma.Quantity) return this.RenderError(r => r.Message = string.Format("{0} 超出购买数量", rma.ProductId)); rmaAmount += orderItemEntity.ItemPrice * rma.Quantity; _rmaitemRepo.Insert(new RMAItemEntity() { CreateDate = DateTime.Now, ItemPrice = orderItemEntity.ItemPrice, ExtendPrice = orderItemEntity.ItemPrice * rma.Quantity, ProductDesc = orderItemEntity.ProductDesc, ProductId = orderItemEntity.ProductId, ColorValueId = orderItemEntity.ColorValueId, SizeValueId = orderItemEntity.SizeValueId, Quantity = rma.Quantity, RMANo = newRma.RMANo, Status = (int)DataStatus.Normal, UnitPrice = orderItemEntity.UnitPrice, SizeValueName = orderItemEntity.SizeValueName, ColorValueName = orderItemEntity.ColorValueName, UpdateDate = DateTime.Now, StoreItem = orderItemEntity.StoreItemNo, StoreDesc = orderItemEntity.StoreItemDesc }); var exInventory = Context.Set<InventoryEntity>().Where(i => i.ProductId == rma.ProductId && i.PColorId == rma.Properties.ColorValueId && i.PSizeId == rma.Properties.SizeValueId).FirstOrDefault(); if (exInventory == null) return this.RenderError(r => r.Message = string.Format("{0} no channel product", rma.ProductId)); erpRma.Detail.Add(new { SelectPro_detail_sid = exInventory.ChannelInventoryId, RefundNum = rma.Quantity }); } newRma.RMAAmount = rmaAmount; _rmaRepo.Update(newRma); _rmalogRepo.Insert(new RMALogEntity { CreateDate = DateTime.Now, CreateUser = authUser.Id, RMANo = newRma.RMANo, Operation = "申请线上退货" }); string exRMANo = string.Empty; bool isSuccess = ErpServiceHelper.SendHttpMessage(ConfigManager.ErpBaseUrl, new { func = "Agree_Refund", jsonSales = new { Data = new List<dynamic>() { erpRma } } }, r => exRMANo = r.orders_refund_frt_sid , null); if (isSuccess) { _rmaexRepo.Insert(new RMA2ExEntity() { ExRMA = exRMANo??string.Empty, RMANo = newRma.RMANo, UpdateDate = DateTime.Now }); ts.Complete(); return this.RenderSuccess<MyRMAResponse>(r => r.Data = new MyRMAResponse().FromEntity<MyRMAResponse>(newRma)); } else { return this.RenderError(r => r.Message = "创建失败"); } } }
public static bool SyncOrder2Erp(string orderNo, string orderSource = "wgw") { if (string.IsNullOrEmpty(orderNo)) { throw new ArgumentNullException("orderNo"); } var order = Context.Set<OrderEntity>().FirstOrDefault(o => o.OrderNo == orderNo && o.OrderSource == orderSource); if (order == null) { throw new NullReferenceException(string.Format("Not exist order ({0})",orderNo)); } var dealCode = Context.Set<Map4Order>().First(o => o.OrderNo == orderNo && o.Channel == orderSource).ChannelOrderCode; var customer = Context.Set<UserEntity>().FirstOrDefault(u => u.Id == order.CustomerId); var erpOrder = new { orderSource, dealPayType = order.PaymentMethodCode, shipType = (int)ShipType.TrdParty, //request.OrderModel.ShippingType, needInvoice = order.NeedInvoice.HasValue && order.NeedInvoice.Value ? 1 : 0, invoiceTitle = order.InvoiceSubject ?? string.Empty, invoiceMemo = order.InvoiceDetail ?? string.Empty, orderMemo = order.Memo ?? string.Empty, lastUpdateTime = DateTime.Now.ToString(ErpServiceHelper.DATE_FORMAT), payTime = string.Empty, recvfeeReturnTime = string.Empty, dealState = "STATE_WG_WAIT_PAY", buyerName = customer != null ? customer.Name: string.Empty, sellerConsignmentTime = string.Empty, receiverPostcode = "", freight = 0, couponFee = "0", comboInfo = string.Empty, dealRateState = "DEAL_RATE_NO_EVAL", totalCash = "0", dealNote = order.Memo ?? string.Empty, dealFlag = string.Empty, dealCode, createTime = DateTime.Now.ToString(ErpServiceHelper.DATE_FORMAT), receiverMobile = order.ShippingContactPhone ?? string.Empty, receiverPhone = order.ShippingContactPhone??string.Empty, receiverName = order.ShippingContactPerson??string.Empty, dealPayFeeTicket = "0", dealNoteType = "UN_LABEL", recvfeeTime = string.Empty, dealPayFeeTotal = order.TotalAmount/100, ppCodId = string.Empty, receiverAddress = order.ShippingAddress, transportType = "TRANSPORT_NONE", wuliuId = "0", vipCard = string.Empty, itemList = new List<dynamic>() }; foreach (var item in Context.Set<OrderItemEntity>().Where(o => o.OrderNo == orderNo)) { erpOrder.itemList.Add(CreateErpItem(item)); } string exOrderNo = string.Empty; bool isSuccess = ErpServiceHelper.SendHttpMessage(ConfigManager.ErpBaseUrl, new { func = "DivideOrderToSaleFromJSON", OrdersJSON = erpOrder }, r => exOrderNo = r.order_no , null); if (!isSuccess) return false; Context.Set<Order2ExEntity>().Add(new Order2ExEntity() { ExOrderNo = exOrderNo, OrderNo = order.OrderNo, UpdateTime = DateTime.Now }); if (order.Status == (int) OrderStatus.Paid) { var oderTransaction = Context.Set<OrderTransactionEntity>().FirstOrDefault(ot => ot.OrderNo == order.OrderNo); if (oderTransaction != null) { oderTransaction.CanSync = 0; //同步给衡和系统后可以同步支付状态 } } Context.SaveChanges(); return true; }
public void Execute(IJobExecutionContext context) { ILog log = LogManager.GetLogger(this.GetType()); JobDataMap data = context.JobDetail.JobDataMap; var privatekey = data.GetString("privatekey"); var publickey = data.GetString("publickey"); var minPoints = data.GetInt("minpoints"); var groupPointConvertUrl = data.GetString("pointconverturl"); int point2GroupRatio = int.Parse(ConfigurationManager.AppSettings["point2groupratio"]); string appStoreNo = ConfigurationManager.AppSettings["appStoreNoInGroup"]; if (string.IsNullOrEmpty(appStoreNo)) { log.Info("app store no in group is empty!"); return; } int cursor = 0; int successCount = 0; int size = JobConfig.DEFAULT_PAGE_SIZE; int totalCount = 0; Stopwatch sw = new Stopwatch(); sw.Start(); Type linqType = new { U = (UserAccountEntity)null, C = (CardEntity)null }.GetType(); using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { Query(db, minPoints, a => totalCount = a.Count()); } int lastMaxId = 0; while (cursor < totalCount) { List<LinqInner> accounts = null; using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { Query(db, minPoints, acs => accounts = acs.Where(a=>a.U.Id>lastMaxId).OrderBy(a => a.U.Id).Take(size).ToList()); } foreach (var account in accounts) { using (var ts = new TransactionScope()) { // step1: check account balanced using (var db = new YintaiHangzhouContext("YintaiHangzhouContext")) { var correctPoints = db.PointHistories.Where(p => p.User_Id == account.U.User_Id && p.Status != (int)DataStatus.Deleted).Sum(p => p.Amount); var convertPoints = correctPoints - correctPoints % point2GroupRatio; if (convertPoints < minPoints) continue; //step 2: insert deduct point history db.PointHistories.Add(new PointHistoryEntity() { Amount = -convertPoints, CreatedDate = DateTime.Now, CreatedUser = 0, Description = string.Format("转换为集团积点{0}", -convertPoints), Name = string.Format("转换为集团积点{0}", -convertPoints), PointSourceType = (int)PointSourceType.System, PointSourceId = 0, Status = (int)DataStatus.Normal, Type = (int)PointType.Convert2Group, UpdatedDate = DateTime.Now, UpdatedUser = 0, User_Id = account.U.User_Id }); db.SaveChanges(); // step 3: call group service to convert points string errorMsg; bool canConvert = GroupServiceHelper.SendHttpMessage(groupPointConvertUrl, publickey, privatekey, new { cardno = account.C.CardNo, amount = convertPoints / point2GroupRatio, storeno = appStoreNo }, out errorMsg); if (canConvert) { ts.Complete(); successCount++; } else { log.Info(string.Format("convert points failed for cardno:{0},msg:{1}", account.C.CardNo, errorMsg)); } } } } cursor += size; lastMaxId = accounts.Max(a => a.U.Id); } sw.Stop(); log.Info(string.Format("{0} points from app2group in {1} => {2} docs/s", successCount, sw.Elapsed, successCount / sw.Elapsed.TotalSeconds)); }