一.下单要设计2个表一个主表一个从表
1.表结构
主表:订单编号,商家编号,顾客编号,添加时间,是否包邮,邮费,收货人,收货地址,
收货人电话,快递单号,快递名称,总价格,收益价格。
从表:主表编号,商品编号,数量,价格
2.订单从表存储的是单个商品的信息包括商品价格数量用户购买的商品。主表存储的是商家信息商家
对于的商品对应商家。
二.下单如何将数据存入数据库
通过将viewModel的数据存如数据库对应的表中
1.构造一个接收页面类的类
格式为
{
"收货ID":1 //收货地址表主键
"商品":[
{
"商品编号":1,
"商家编号":1,
"价格":15.00,
"数量":10
"是否包邮":0,
"邮费":15.00,
},
{
"商品编号":2,
"商家编号":2,
"价格":25.00,
"数量":20
"是否包邮":0,
"邮费":15.00,
}
...
]
}
public class OrderInformation
{
public int ShippingAddressID { get; set; }
public List<ProductInformation> ProductInformations { get; set; }
}
public class ProductInformation
{
public int ProductID { get; set; }
public int BusinessID { get; set; }
public decimal Price { get; set; }
public int Count { get; set; }
public int IsPackagemail { get; set; }
public decimal Postage { get; set; }
}
2.数据库model类
public partial class Order
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Order()
{
this.Comment = new HashSet<Comment>();
this.OrderDetails = new HashSet<OrderDetails>();
}
public int ID{ get; set; }
public string OrderNumber { get; set; } public Nullable<int> BusinessID { get; set; } public Nullable<int> CustomerID { get; set; } public Nullable<System.DateTime> OrderDate { get; set; } public Nullable<int> IsPackagemail { get; set; } public Nullable<decimal> Postage { get; set; } public string Recipient { get; set; } public string RecipientPhone { get; set; } public string RecipientAddress { get; set; } public Nullable<int> ExpressesID { get; set; } public string CourierNumber { get; set; } public Nullable<decimal> TotalAmount { get; set; } public Nullable<int> State { get; set; } public virtual Business Business { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<Comment> Comment { get; set; } public virtual Customer Customer { get; set; } public virtual Expresses Expresses { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection<OrderDetails> OrderDetails { get; set; } }
public partial class OrderDetails:BaseModel
{
public int ID{ get; set; }
public Nullable<int> OrderID { get; set; } public Nullable<int> ProductID { get; set; } public Nullable<int> Count { get; set; } public Nullable<decimal> Price { get; set; } public virtual Product Product { get; set; } public virtual Order Order { get; set; } }
方法(通过group分组存入数据)
public bool AddOrder(int uid, OrderInformation orderInformation, out string mgs)
{
mgs = "";
IUserDAL userDal = new UserDAL();
User user = userDal.Search(x => x.ID == uid).First();
IShippingAddressDAL dal = new ShippingAddressDAL();
ShippingAddress shippingAddress = dal.Search(orderInformation.ShippingAddressID);
IProductBLL bll = new ProductBLL();
DateTime? dataTime= DateTime.Now;
List<Order> orderList = orderInformation.ProductInformations.GroupBy(x => x.BusinessID).Select(g => new Order()
{
BusinessID = g.Key,
Postage = g.Sum(x => x.Postage * x.Count),
CustomerID = user.CustomerID,
OrderDate = dataTime,
State = 1,
OrderNumber = CheckOrderNumber(g.Key),
IsPackagemail = g.Sum(x => x.Postage) > 0 ? 2 : 1,
Recipient = shippingAddress.Recipient,
RecipientPhone = shippingAddress.Phone,
RecipientAddress = shippingAddress.Area.Name + shippingAddress.Area1.Name + shippingAddress.Area2.Name + shippingAddress.Address,
TotalAmount = g.Sum(x => x.Price / (1+bll.Search(x.ProductID).CommodityType.PriceStrategy.First().PricePercentage) * x.Count),
TotalRevenue = g.Sum(x => x.Price /(1+bll.Search(x.ProductID).CommodityType.PriceStrategy.First().PricePercentage)* bll.Search(x.ProductID).CommodityType.PriceStrategy.First().PricePercentage*x.Count),
OrderDetails = GetDetailOrder(orderInformation.ProductInformations.Where(x=>x.BusinessID==g.Key).ToList())
}).ToList();
foreach (var item in orderList)
{
Dal.AddNoSave(item);
}
return Dal.Result() > 0;
}
//构造从表信息
public static List<OrderDetails> GetDetailOrder(List<ProductInformation> productInformations)
{
List<OrderDetails> detailOrderList = new List<OrderDetails>();
foreach (var item in productInformations)
{
OrderDetails orderDetail = new OrderDetails();
orderDetail.ProductID = item.ProductID;
orderDetail.Price = item.Price;
orderDetail.Count = item.Count;
detailOrderList.Add(orderDetail);
}
return detailOrderList;
}