// 创建收货信息 // public string createReceiveInformation(string buyerid, string receiverName, string phone, string country, string province, string city, string receivedId, string district, string detailAddr) public string createReceiveInformation(string buyerid, string receiverName, string phone, string detailAddr, string tag) { // 收货信息Id生成 CreateIdCount receivedCount = new CreateIdCount(_context); string receivedid = receivedCount.GetOrderCount(); ReceiveInformation receiveInformation = new ReceiveInformation { ReceivedId = receivedid, Phone = phone, ReceiverName = receiverName, BuyerId = buyerid, Country = "1", Province = "2", City = "3", District = "4", DetailAddr = detailAddr, Tag = tag }; _context.ReceiveInformations.Add(receiveInformation); if (_context.SaveChanges() > 0) { return(receivedid); } else { return(null); } }
// 从商品详情页面创建订单 public bool CreateOrderFromDetail(string buyerid, string commodityid, string receivedId, int amount, decimal price) { // OrderId生成 CreateIdCount orderCount = new CreateIdCount(_context); string orderId = orderCount.GetOrderCount(); Global.GOrderID.Clear(); Global.GOrderID.Add(orderId); Commodity commodity = _context.Commodities.Where(x => x.CommodityId == commodityid).FirstOrDefault(); // 创建联系集 - 初始时商品状态为待付款 OrdersCommodity ordersCommodity = new OrdersCommodity { OrdersId = orderId, CommodityId = commodityid, Status = COrders.ToBePay, Amount = amount }; _context.OrdersCommodities.Add(ordersCommodity); // 创建Order —— 初始状态为待付款 Order order = new Order { OrdersId = orderId, BuyerId = buyerid, OrdersDate = DateTime.Now, Status = COrders.ToBePay, ShopId = commodity.ShopId, ReceivedId = receivedId, Orderamount = price }; _context.Orders.Add(order); if (_context.SaveChanges() > 0) { return(true); } else { return(false); } }
// 从购物车页面创建订单 public bool CreateOrderFromChart(string buyerId, List <Good> cartCommodityList, string receivedId, decimal price) { if (buyerId == "" || cartCommodityList.Count == 0 || receivedId == "" || price == 0) { return(false); } else { List <Commodity> commodityList = new List <Commodity>(); List <Shop> shopList = new List <Shop>(); Global.GOrderID.Clear(); CreateIdCount create = new CreateIdCount(_context); decimal oldPrice = 0; foreach (Good cartCommodity in cartCommodityList) { Commodity newCommodity = _context.Commodities.FirstOrDefault(c => c.CommodityId == cartCommodity.ID); Shop newShop = _context.Shops.FirstOrDefault(s => s.Name == cartCommodity.shop); oldPrice = oldPrice + ((int)newCommodity.Price) * cartCommodity.Soldnum; commodityList.Add(newCommodity); if (shopList.Any(s => s.ShopId == newShop.ShopId) == false) { shopList.Add(_context.Shops.FirstOrDefault(s => s.ShopId == newShop.ShopId)); } AddShoppingCart scCommodity = _context.AddShoppingCarts.FirstOrDefault(a => a.CommodityId == cartCommodity.ID && a.BuyerId == buyerId); _context.AddShoppingCarts.Remove(scCommodity); } List <BuyerCoupon> couponList = _context.BuyerCoupons.Where(c => c.BuyerId == buyerId).ToList(); foreach (BuyerCoupon newBuyerCoupon in couponList) { Coupon newCoupon = _context.Coupons.FirstOrDefault(c => c.CouponId == newBuyerCoupon.CouponId); if (newCoupon.Threshold <= oldPrice && newCoupon.Discount1 == (oldPrice - price)) { newBuyerCoupon.Amount--; if (newBuyerCoupon.Amount == 0) { _context.BuyerCoupons.Remove(newBuyerCoupon); } else { _context.BuyerCoupons.Update(newBuyerCoupon); } } } decimal discountNumber = price / shopList.Count; foreach (Shop newShop in shopList) { Order newOrder = new Order(); string newOrderId = create.GetOrderCount(); newOrder.OrdersId = newOrderId; Global.GOrderID.Add(newOrderId); newOrder.BuyerId = buyerId; newOrder.ShopId = newShop.ShopId; newOrder.OrdersDate = DateTime.Now; newOrder.Status = COrders.ToBePay; newOrder.ReceivedId = receivedId; decimal amount = 0; foreach (Commodity newCommodity in commodityList) { Good newcart = cartCommodityList.FirstOrDefault(c => c.ID == newCommodity.CommodityId); OrdersCommodity newOrdersCommodity = new OrdersCommodity(); newOrdersCommodity.OrdersId = newOrder.OrdersId; newOrdersCommodity.CommodityId = newCommodity.CommodityId; newOrdersCommodity.Status = COrders.ToBePay; newOrdersCommodity.Amount = newcart.Soldnum; _context.OrdersCommodities.Add(newOrdersCommodity); if (newCommodity.ShopId == newShop.ShopId) { amount = amount + ((int)newCommodity.Price) * newcart.Soldnum; } } amount = amount - (oldPrice - price) / discountNumber; newOrder.Orderamount = amount; _context.Orders.Add(newOrder); } if (_context.SaveChanges() > 0) { return(true); } else { return(false); } } }
/************************* 活动服务 *******************/ // 发布活动 public bool releaseActivity(string name, string category, DateTime startTime, DateTime endTime, string description, decimal?constrict, decimal?minus) { CreateIdCount createIdCount = new CreateIdCount(_context); string activityID = createIdCount.GetActivityCount(); Activity activity = new Activity(); activity.ActivityId = activityID; activity.Name = name; switch (category) { case "全场活动": activity.Category = 1; break; case "特类商品": activity.Category = 2; break; case "店铺活动": activity.Category = 3; break; case "单个商品": activity.Category = 4; break; } activity.StartTime = startTime; activity.EndTime = endTime; activity.Description = description; _context.Activities.Add(activity); // 创建活动 // 发布优惠券 string couponID = createIdCount.GetCouponCount(); Coupon coupon = new Coupon(); coupon.CouponId = couponID; coupon.StartTime = startTime; coupon.EndTime = endTime; coupon.Threshold = constrict; coupon.Discount1 = minus; coupon.Discount2 = 0; switch (category) { case "全场活动": coupon.Category = 1; break; case "特类商品": coupon.Category = 2; break; case "店铺活动": coupon.Category = 3; break; case "单个商品": coupon.Category = 4; break; } coupon.ShopId = "1"; coupon.ActivityId = activityID; _context.Coupons.Add(coupon); // 添加优惠券 if (_context.SaveChanges() < 0) // 先注入优惠券,保证约束 { return(false); } // 给买家发布优惠券 List <Buyer> buyers = _context.Buyers.Where(x => x.BuyerId != null).ToList(); foreach (Buyer buyer in buyers) { BuyerCoupon buyerCoupon = new BuyerCoupon { BuyerId = buyer.BuyerId, CouponId = couponID, Amount = 10 }; _context.BuyerCoupons.Add(buyerCoupon); // 每个买家10张优惠券 } // 给店铺发布优惠券 CouponShop couponShop = new CouponShop { ShopId = "1", CouponId = couponID, Amount = 1000 }; _context.CouponShops.Add(couponShop); // 每个店铺1000张优惠券 if (_context.SaveChanges() > 0) { return(true); } else { return(false); } }