/// <summary> /// 根据订单编号,完成订单支付后续操作 /// </summary> /// <param name="orderId"></param> /// <returns></returns> public int FinshPackageOrder(int orderId) { int number = 0;//实际操作影响行数 using (SqlConnection conn = (SqlConnection)DataSource.GetConnection()) { conn.Open(); using (SqlTransaction trans = conn.BeginTransaction()) { try { //第一步骤:修改订单状态为支付完成但未配货 string strSql1 = $"update CsOrder set OrderState=2 where OrderId={orderId}"; number += DbClient.ExecuteSql(conn, trans, strSql1, null); //第二步骤:修改套餐库存 CsOrderDetailDal orderDetailDal = new CsOrderDetailDal(); //查询出本次订单中购买螃蟹列表 List <CsOrderDetail> DetailList = orderDetailDal.GetModelList(" and OrderId=" + orderId + " and ChoseType=3"); if (DetailList.Count > 0) { foreach (CsOrderDetail OrderDetail in DetailList) { string strSql2 = $"update CsPackage set PackageStock=PackageStock-{OrderDetail.ProductNumber} where PackageId={OrderDetail.ProductId}"; number += DbClient.ExecuteSql(conn, trans, strSql2, null); } } //第三步,修改用户的购买累计重量 //decimal total = totalWeight * orderCopies; //string strSql3 = $"update CsUsers set TotalWight=TotalWight+{total} where UserId={userId}"; //number += DbClient.ExecuteSql(conn, trans, strSql3, null); if (number == (DetailList.Count + 1)) { trans.Commit(); return(number); } else { trans.Rollback(); return(0); } } catch (Exception ex) { LogHelper.Log(ex.Message); trans.Rollback(); return(0); } } } }
/// <summary> /// 带有事务操作 添加支付订单 /// </summary> /// <param name="order"></param> /// <returns></returns> public int AddOrder(OrderModel order, out string orderNumber) { var number = 0;//实际操作影响行数 using (var conn = (SqlConnection)DataSource.GetConnection()) { conn.Open(); using (var trans = conn.BeginTransaction(IsolationLevel.ReadUncommitted)) { try { orderNumber = DateTime.Now.ToString("yyyyMMddHHmmssffff"); //添加订单表 获取订单编号 var _csOrder = new CsOrder(); _csOrder.OrderNumber = orderNumber; _csOrder.UserId = order.userid; _csOrder.TotalMoney = order.totalmoney; _csOrder.DiscountMoney = 0; _csOrder.ActualMoney = order.finalPrice; _csOrder.OrderDate = DateTime.Now; _csOrder.OrderState = 1; _csOrder.OrderAddress = order.orderaddress; _csOrder.SendAddress = order.sendaddress; _csOrder.CargoNumber = 1; _csOrder.OrderCopies = order.totalnumber; _csOrder.TotalWeight = order.totalweight; _csOrder.BillWeight = order.sendweight; _csOrder.ExpressMoney = order.expressmoney; _csOrder.ServiceMoney = order.servicemoney; _csOrder.RowStatus = 1; _csOrder.IsInvoice = order.isInvoice ? 1 : 0; _csOrder.OrderRemarks = order.remarks; var strSql1 = new StringBuilder(); strSql1.Append("insert into CsOrder (OrderNumber,UserId,TotalMoney,DiscountMoney,ActualMoney,OrderDate,OrderState,OrderAddress,"); strSql1.Append("SendAddress,CargoNumber,OrderCopies,TotalWeight,BillWeight,RowStatus,ExpressMoney,ServiceMoney,IsInvoice,OrderRemarks ) values (@OrderNumber,"); strSql1.Append("@UserId,@TotalMoney,@DiscountMoney,@ActualMoney,@OrderDate,@OrderState,@OrderAddress,@SendAddress,@CargoNumber,@OrderCopies,"); strSql1.Append("@TotalWeight,@BillWeight,@RowStatus,@ExpressMoney,@ServiceMoney,@IsInvoice,@OrderRemarks);select @@Identity;"); SqlParameter[] parameter1 = { new SqlParameter("@OrderNumber", SqlDbType.VarChar, 50), new SqlParameter("@UserId", SqlDbType.Int, 4), new SqlParameter("@TotalMoney", SqlDbType.Decimal, 18), new SqlParameter("@DiscountMoney", SqlDbType.Decimal, 18), new SqlParameter("@ActualMoney", SqlDbType.Decimal, 18), new SqlParameter("@OrderDate", SqlDbType.DateTime), new SqlParameter("@OrderState", SqlDbType.Int, 4), new SqlParameter("@OrderAddress", SqlDbType.NVarChar, 500), new SqlParameter("@SendAddress", SqlDbType.VarChar, 5000), new SqlParameter("@CargoNumber", SqlDbType.Int, 4), new SqlParameter("@OrderCopies", SqlDbType.Int, 4), new SqlParameter("@TotalWeight", SqlDbType.Decimal, 18), new SqlParameter("@BillWeight", SqlDbType.Decimal, 18), new SqlParameter("@RowStatus", SqlDbType.TinyInt, 4), new SqlParameter("@ExpressMoney", SqlDbType.Decimal, 18), new SqlParameter("@ServiceMoney", SqlDbType.Decimal, 18), new SqlParameter("@IsInvoice", SqlDbType.Int, 4), new SqlParameter("@OrderRemarks", SqlDbType.Text) }; parameter1[0].Value = _csOrder.OrderNumber; parameter1[1].Value = _csOrder.UserId; parameter1[2].Value = _csOrder.TotalMoney; parameter1[3].Value = _csOrder.DiscountMoney; parameter1[4].Value = _csOrder.ActualMoney; parameter1[5].Value = _csOrder.OrderDate; parameter1[6].Value = _csOrder.OrderState; parameter1[7].Value = _csOrder.OrderAddress; parameter1[8].Value = _csOrder.SendAddress; parameter1[9].Value = _csOrder.CargoNumber; parameter1[10].Value = _csOrder.OrderCopies; parameter1[11].Value = _csOrder.TotalWeight; parameter1[12].Value = _csOrder.BillWeight; parameter1[13].Value = _csOrder.RowStatus; parameter1[14].Value = _csOrder.ExpressMoney; parameter1[15].Value = _csOrder.ServiceMoney; parameter1[16].Value = _csOrder.IsInvoice; parameter1[17].Value = _csOrder.OrderRemarks; object obj = DbClient.ExecuteScalar(conn, trans, strSql1.ToString(), parameter1); if (obj != null) { int orderid = Convert.ToInt32(obj); int totalNumber = 0; //应总共操作记录影响行数 //decimal weight = 0; int carbNumber = 0; //总共购买螃蟹数 //添加购物详细 //购买螃蟹列表 List <CsOrderDetail> detailList = new List <CsOrderDetail>(); foreach (CartItem cart in order.cartFoodList) { CsOrderDetail orderDetail = new CsOrderDetail(); orderDetail.OrderId = orderid; orderDetail.ProductId = cart.id; orderDetail.UnitPrice = cart.price; orderDetail.ProductNumber = cart.num; orderDetail.TotalPrice = (cart.price * cart.weight * 2 * cart.num); orderDetail.ChoseType = 1; //weight += (cart.weight * cart.num); totalNumber++; carbNumber += cart.num; detailList.Add(orderDetail); } //购买可选配件列表 if (order.partNumList.Count > 0) { foreach (CartItem cart in order.partNumList) { if (cart.id == 0) { continue; } CsOrderDetail orderDetail = new CsOrderDetail(); orderDetail.OrderId = orderid; orderDetail.ProductId = cart.id; orderDetail.UnitPrice = cart.price; orderDetail.ProductNumber = cart.id == 10009 ? carbNumber : 1; orderDetail.TotalPrice = (cart.price * cart.num); orderDetail.ChoseType = 2; detailList.Add(orderDetail); totalNumber++; } } //购买必选配件列表 foreach (PartItem part in order.partList) { CsOrderDetail orderDetail = new CsOrderDetail(); orderDetail.OrderId = orderid; orderDetail.ProductId = part.PartId; orderDetail.UnitPrice = part.PartPrice; orderDetail.ProductNumber = part.PartId == 10004 ? carbNumber : 1; orderDetail.TotalPrice = (part.PartPrice * orderDetail.ProductNumber); orderDetail.ChoseType = 2; detailList.Add(orderDetail); totalNumber++; } foreach (CsOrderDetail detail in detailList) { StringBuilder strSql2 = new StringBuilder(); strSql2.Append("insert into CsOrderDetail (OrderId,ProductId,UnitPrice,ProductNumber,TotalPrice,ChoseType )"); strSql2.Append("values(@OrderId,@ProductId,@UnitPrice,@ProductNumber,@TotalPrice,@ChoseType)"); SqlParameter[] parameter2 = { new SqlParameter("@OrderId", SqlDbType.Int, 4), new SqlParameter("@ProductId", SqlDbType.Int, 4), new SqlParameter("@UnitPrice", SqlDbType.Money, 18), new SqlParameter("@ProductNumber", SqlDbType.Int), new SqlParameter("@TotalPrice", SqlDbType.Decimal, 18), new SqlParameter("@ChoseType", SqlDbType.Int, 4) }; parameter2[0].Value = detail.OrderId; parameter2[1].Value = detail.ProductId; parameter2[2].Value = detail.UnitPrice; parameter2[3].Value = detail.ProductNumber; parameter2[4].Value = detail.TotalPrice; parameter2[5].Value = detail.ChoseType; number += DbClient.ExecuteSql(conn, trans, strSql2.ToString(), parameter2); } if (number != totalNumber) { trans.Rollback(); return(0); } else { trans.Commit(); return(orderid); } } else { trans.Rollback(); return(0); } } catch (Exception ex) { LogHelper.Log(ex.ToJson(), "订单添加异常"); trans.Rollback(); orderNumber = ""; return(0); } } } }