private static bool CheckUserData(OrderMealUserData userData, out string message) { message = string.Empty; if (string.IsNullOrEmpty(userData.userPhone)) { message = "请输入你的手机号码."; return false; } if (userData.companyId <= 0) { message = "商家不存在!"; return false; } if (userData.mansionId <= 0) { message = "请选择送餐大厦."; return false; } if (userData.msnLogId > 0 && string.IsNullOrEmpty(userData.checkCode)) { message = "请输入手机验证码."; return false; } if (string.IsNullOrEmpty(userData.address)) { message = "请输入送餐的地址."; return false; } return true; }
private static int SaveOrder(OrderMealUserData userData, List<OrderMealData> orderData, Order order, SysCompany company, SysCompanyItem companyItem, decimal orderSum, int qty, bool isNewOrder, out int orderId, out string message) { orderId = -1; bool isLogin = AppContextBase.Context.CurrentSubSys != SubSystem.ALL; decimal servicesSum = company.ServiceSum.HasValue ? company.ServiceSum.Value : 0; SysMember user = isLogin ? AppContextBase.Context.User : null; bool isNeedCheckPhone; if (!isLogin || !Utilities.Compare(user.MemberPhoneNumber, userData.userPhone)) isNeedCheckPhone = true; else isNeedCheckPhone = MsnBLL.IsPhoneNumNeedCheck(userData.userPhone); decimal userOrderAvgSum = 0; if (isLogin && companyItem != null && !CompanyItemBLL.CanClickCompanyMealItem(company, user, companyItem, userData.orderId, out userOrderAvgSum, out message)) return -1; bool isNew = false; using (TransactionScope ts = new TransactionScope( )) { using (SharedDbConnectionScope ss = new SharedDbConnectionScope( )) { if (isNeedCheckPhone && !MsnBLL.CheckPhoneNumCode(userData.userPhone, userData.checkCode, userData.msnLogId)) { message = "验证码有误或已超时,请重新验证。"; return -1; } user = user ?? UserBLL.GetOrNewMemberByPhoneNumber(userData.userPhone, company, out isNew); if (isNew) user.AreaDepth2 = company.AreaDepth; //第一次订快餐的地址,默认该地址所属商圈为其工作圈 else if (!Utilities.Compare(user.AreaDepth2, company.AreaDepth)) { if (string.IsNullOrEmpty(user.AreaDepth1)) user.AreaDepth1 = company.AreaDepth; else if (string.IsNullOrEmpty(user.AreaDepth3)) user.AreaDepth3 = company.AreaDepth; } if (!isNewOrder) { if (order.OrderMemberID != user.Id) { message = "你不能修改其他会员的订单."; return -1; } ClearOrderMenuDetail(order.Id); if (Utilities.ToDecimal(order.OrderPoint) > 0) RewardBLL.RewardMemberPointForOrderMeal(user, company, order); order.OrderStatus = (int)OrderStatus.Modified; } else { order.OrderNum = GetOrderNum( ); order.OrderStatus = (int)OrderStatus.NotStart; order.OrderPoint = 0; } var dtNow = DateTime.Now; //order.OrderCode = orderCode; order.OrderCard = string.Empty; order.OrderDate = dtNow; order.OrderDateDeliver = dtNow; order.OrderDateUpload = dtNow; order.OrderMemberID = user.Id; order.OrderMemo = userData.memo; order.OrderProduct = userData.address; //送餐地址 order.OrderQty = qty; order.OrderRateSale = 0; // 折扣 order.OrderRate = RewardBLL.OrderMealRewardRate(company) / 100M; //赠送比例 order.OrderPrePoint = (RewardBLL.OrderMealRewardRate(company) / 100M) * orderSum; //赠送积分 order.OrderSellerID = company.Id; order.OrderSum = orderSum; //订单总额 order.OrderSumOk = orderSum + servicesSum; //订单总额 order.OrderPay = orderSum + servicesSum; //现金支付 order.OrderPayCash = 0; //储值支付 order.OrderPayPoint = companyItem != null ? companyItem.ItemPoint : 0;//积分支付 order.OrderType = (int)OrderType.OrderMeal; order.ServiceSum = servicesSum; order.MansionId = userData.mansionId; order.OrderModel = company.IsUseMsg.HasValue && company.IsUseMsg.Value ? (int)OrderModel.Auto : (int)OrderModel.Manual; order.OrderUpdateOn = DateTime.Now; order.IsNonOut = false; order.HasOutOfStock = false; order.Save( ); //RewardBLL.RewardMemberPointForOrderMeal(user, company, order); //save detail List<OrdersDetail> details = new List<OrdersDetail>( ); foreach (var pair in orderData) { var d = new OrdersDetail( ) { MenuId = pair.menudId, MenuName = pair.menuName, OrderId = order.Id, OrderQty = pair.menuAmount, OrderPrice = pair.menuPrice.Value, IsChgPrice = false, IsOutOfStock = false, ItemInfo = pair.isCompanyItem && companyItem != null ? companyItem.ItemInfo : null }; d.Save( ); details.Add(d); } if (isLogin && companyItem != null) CompanyItemBLL.ClickCompanyMealItem(company, user, companyItem, order, userOrderAvgSum); UserBLL.AddUserFavAddress(user.Id, userData.mansionId, userData.address); //UserBLL.AddUserFavCompany(user.Id, company.Id); //OrderBLL.UpdateBalance( ); ts.Complete( ); //SendOrderMealMessage(company, user, order, details, out message); if (userData.orderId > 0) OrderProgressBLL.ClearOrderLog(order.Id); OrderProgressBLL.InitOrderLogProgress(order, company); message = "订餐成功."; orderId = order.Id; } } //Auto login if (!isLogin) { if (user != null) { if (user.MemberRoleId > 0) user.LastLoginSubSys = (int)SubSystem.Member; else if (user.CompanyRoleId > 0) user.LastLoginSubSys = (int)SubSystem.Company; else if (user.AdminRoleId > 0) user.LastLoginSubSys = (int)SubSystem.Admin; user.LastLoginDate = DateTime.Now; user.Save( ); Utilities.LoginSigIn(user.Id, (SubSystem)user.LastLoginSubSys); if (isNew) { message = "下次订餐您可以直接登录,账号是您的手机号码,密码为手机后6位数,记得及时修改密码哦!"; return 2; } return 1; } } return 1; }
// return -1 error,1 auto login 0 need redirect login private static int SaveOrderMeal(OrderMealUserData userData, List<OrderMealData> orderData, out int orderId, out string message) { orderId = -1; if (!CheckUserData(userData, out message)) return -1; SysCompany company = SysCompany.FetchByID(userData.companyId); if (company == null) { message = "你选择的商家不存在."; return -1; } if (Formatter.ToEnum<CompanyType>(company.CompanyType.Value) != CompanyType.MealCompany) { message = "你选择的商家不是快餐店."; return -1; } SysCompanyItem companyItem = userData.itemId > 0 ? SysCompanyItem.FetchByID(userData.itemId) : null; var menuDict = MealMenuBLL.LoadCompanyMenu(company.Id).ToDictionary(dr => Convert.ToInt32(dr[SysTakeawayMenu.IdColumn.ColumnName]), dr => { SysTakeawayMenu m = new SysTakeawayMenu( ); m.Load(dr); return m; }); int qty = 0; decimal orderSum = 0; orderData.Sort((t1, t2) => { if (t1.sort == t2.sort) return 0; else if (t1.sort > t2.sort) return 1; else return -1; }); foreach (var pair in orderData) { if (!pair.isCompanyItem && !menuDict.ContainsKey(pair.menudId)) { message = "你选择了一个已经缺货或不存在的菜单,请重新下订."; return -1; } if (pair.menuAmount <= 0) { message = "订餐的数量不能小于或等于零."; return -1; } if (!pair.isCompanyItem) { var m = menuDict[pair.menudId]; pair.menudId = m.Id; pair.menuPrice = (m.Price ?? (decimal?)0).Value; pair.menuName = m.Name; } else if (companyItem != null) { pair.menudId = -userData.itemId; pair.menuPrice = (companyItem.ItemNeedPay ?? (decimal?)0).Value; pair.menuName = companyItem.ItemTitle; } orderSum += (pair.menuPrice.Value * pair.menuAmount); qty += pair.menuAmount; } orderSum = Math.Round(orderSum, 1); Order order = null; if (userData.orderId > 0) { lock (LockScopeAction(userData.orderId)) { order = Order.FetchByID(userData.orderId); if (order == null) { message = "你选择了一个不存在的订单."; return -1; } if (Formatter.ToEnum<OrderType>(order.OrderType.Value) != OrderType.OrderMeal) { message = "你选择了一个不存在的订单."; return -1; } if (order.OrderStatus == (int)OrderStatus.Completed || order.OrderStatus == (int)OrderStatus.Canceled) { message = "此订单不允许修改."; return -1; } return SaveOrder(userData, orderData, order, company, companyItem, orderSum, qty, false, out orderId, out message); } } else { order = new Order( ); order.OrderCode = OrderBLL.GetOrderCode(company); return SaveOrder(userData, orderData, order, company, companyItem, orderSum, qty, true, out orderId, out message); } }