/// <summary> /// 得到一个对象实体 /// </summary> public Order DataRowToModel(DataRow row) { Order model = new Order(); if (row != null) { foreach (DataColumn col in row.Table.Columns) { if (row[col] != null && row[col].ToString() != "") { switch (col.ColumnName) { case "id": model.OrderID = int.Parse(row[col].ToString()); break; case "uid": model.Uid = int.Parse(row["uid"].ToString()); break; case "number": model.Number = row["number"].ToString(); break; case "total_price": model.Total = decimal.Parse(row["total_price"].ToString()); break; case "state": model.OrderState = row["state"].ToString(); break; case "address": model.Address = row["address"].ToString(); break; case "phone": model.Phone = row["phone"].ToString(); break; case "linkman": model.LinkMan = row["linkman"].ToString(); break; case "add_time": model.AddTime = DateTime.Parse(row["add_time"].ToString()); break; case "delivery_time": model.DeliveryTime = DateTime.Parse(row["delivery_time"].ToString()); break; case "remark": model.Remark = row["remark"].ToString(); break; case "delivery": model.Delivery = row["delivery"].ToString(); break; } } } } return model; }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Order model) { StringBuilder strSql = new StringBuilder(); strSql.Append("insert into tc_order("); strSql.Append("uid,number,total_price,address,phone,linkman,add_time,delivery_time,remark,delivery,`state`)"); strSql.Append(" values ("); strSql.Append("?uid,?number,?total_price,?address,?phone,?linkman,?add_time,?delivery_time,?remark,?delivery,?state);"); strSql.Append(" select LAST_INSERT_ID() "); MySqlParameter[] parameters = { new MySqlParameter("?uid", MySqlDbType.Int32,10), new MySqlParameter("?number", MySqlDbType.VarChar,20), new MySqlParameter("?total_price", MySqlDbType.Decimal,12), new MySqlParameter("?address", MySqlDbType.Text), new MySqlParameter("?phone", MySqlDbType.VarChar,200), new MySqlParameter("?linkman", MySqlDbType.VarChar,50), new MySqlParameter("?add_time", MySqlDbType.DateTime), new MySqlParameter("?delivery_time", MySqlDbType.DateTime), new MySqlParameter("?remark", MySqlDbType.Text), new MySqlParameter("?delivery", MySqlDbType.Enum), new MySqlParameter("?state", MySqlDbType.Enum)}; parameters[0].Value = model.Uid; parameters[1].Value = model.Number; parameters[2].Value = model.Total; parameters[3].Value = model.Address; parameters[4].Value = model.Phone; parameters[5].Value = model.LinkMan; parameters[6].Value = model.AddTime; parameters[7].Value = model.DeliveryTime; parameters[8].Value = model.Remark; parameters[9].Value = model.Delivery; parameters[10].Value = model.OrderState; object obj = MysqlHelper.ExecuteScalar(strSql.ToString(), parameters); int id = 0; if (obj != null) { id = int.Parse(obj.ToString()); } return id; }
public bool SaveToDB() { if (uid <= 0) return false; if (orderList.Count <= 0) return false; Order order = new Order(); order.Uid = uid; order.OrderState = OrderState.MakingUp; order.Number = GetNumber(); order.Total = TotalPrice; order.Address = Address; order.AddTime = DateTime.Now; order.LinkMan = LinkMan; order.Phone = Phone; order.Delivery = Delivery; OrderOperation orderop = new OrderOperation(); int orderid = orderop.Add(order); int rows =0; bool result =false; if (orderid > 0) { List<OrderItem> orderItems = new List<OrderItem>(); foreach (DetailItem detail in orderList) { OrderItem item = detail.ToOrderItem(orderid); orderItems.Add(item); } OrderItemOperation oiop = new OrderItemOperation(); rows = oiop.AddList(orderItems); if (rows > 0) { result = true; } else { orderop.Delete(orderid); } } return result; }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Order model) { StringBuilder strSql = new StringBuilder(); strSql.Append("update tc_order set "); strSql.Append("uid=?uid,"); strSql.Append("number=?number,"); strSql.Append("total_price=?total_price,"); strSql.Append("address=?address,"); strSql.Append("phone=?phone,"); strSql.Append("linkman=?linkman,"); strSql.Append("add_time=?add_time,"); strSql.Append("delivery_time=?delivery_time,"); strSql.Append("remark=?remark,"); strSql.Append("delivery=?delivery"); strSql.Append("`state`=?state"); strSql.Append(" where id=?id"); MySqlParameter[] parameters = { new MySqlParameter("?uid", MySqlDbType.Int32,10), new MySqlParameter("?number", MySqlDbType.VarChar,20), new MySqlParameter("?total_price", MySqlDbType.Decimal,12), new MySqlParameter("?address", MySqlDbType.Text), new MySqlParameter("?phone", MySqlDbType.VarChar,200), new MySqlParameter("?linkman", MySqlDbType.VarChar,50), new MySqlParameter("?add_time", MySqlDbType.DateTime), new MySqlParameter("?delivery_time", MySqlDbType.DateTime), new MySqlParameter("?remark", MySqlDbType.Text), new MySqlParameter("?delivery", MySqlDbType.Enum), new MySqlParameter("?state", MySqlDbType.Enum), new MySqlParameter("?id", MySqlDbType.Int32,10)}; parameters[0].Value = model.Uid; parameters[1].Value = model.Number; parameters[2].Value = model.Total; parameters[3].Value = model.Address; parameters[4].Value = model.Phone; parameters[5].Value = model.LinkMan; parameters[6].Value = model.AddTime; parameters[7].Value = model.DeliveryTime; parameters[8].Value = model.Remark; parameters[9].Value = model.Delivery; parameters[10].Value = model.OrderState; parameters[11].Value = model.OrderID; int rows = MysqlHelper.ExecuteNonQuery(strSql.ToString(), parameters); if (rows > 0) { return true; } else { return false; } }
/// <summary> /// 得到一个对象实体 /// </summary> public Order GetModel(int id) { StringBuilder strSql = new StringBuilder(); strSql.Append("select id,uid,`state`,number,total_price,address,phone,linkman,add_time,delivery_time,remark,delivery from tc_order "); strSql.Append(" where id=?id"); MySqlParameter[] parameters = { new MySqlParameter("?id", MySqlDbType.Int32) }; parameters[0].Value = id; Order model = new Order(); DataSet ds = MysqlHelper.ExecuteDataSet(strSql.ToString(), parameters); if (ds.Tables[0].Rows.Count > 0) { return DataRowToModel(ds.Tables[0].Rows[0]); } else { return null; } }
/// <summary> /// 增加一条数据 /// </summary> public int Add(Order model) { return dal.Add(model); }
/// <summary> /// 更新一条数据 /// </summary> public bool Update(Order model) { return dal.Update(model); }
public ActionResult Order(int id) { TextileCity.Entity.Order dataModel = new Order(); dataModel = new OrderOperation().GetModel(id); TextileCity.Entity.User loginUser = Session["LoginUser"] as TextileCity.Entity.User; if (dataModel != null && dataModel.Uid == loginUser.Uid) { List<OrderItem> items = new OrderItemOperation().GetItems(dataModel.OrderID); ViewData["Items"] = items; ViewData["DataModel"] = dataModel; Dictionary<int, string> craftNames = new Dictionary<int, string>(); List<Craft> craftsList = new CraftOperation().GetMinList(); foreach (Craft c in craftsList) { craftNames.Add(c.CraftID, c.Name); } ViewData["CraftNames"] = craftNames; ViewBag.NaviCss.Current = TextileCity.Models.Navigation.Account; return View(); } else { return new HttpStatusCodeResult(System.Net.HttpStatusCode.NotFound); } }