/// <summary> /// 提交订单表单(新增、编辑) /// </summary> /// <param name="orderId">订单主键</param> /// <param name="entity">订单实体</param> /// <param name="saleOrderItemJson">订单明细</param> /// <returns></returns> public ActionResult SubmitOrderForm(string orderId, SaleOrderEntity entity, string saleOrderItemJson) { SaleOrderEntity oldEntity = null; IDatabase database = DataFactory.Database(); if (!string.IsNullOrEmpty(orderId)) { oldEntity = database.FindEntity <SaleOrderEntity>("OrderId", orderId); } if (oldEntity != null && oldEntity.Status != (int)OrderStatus.Initial && oldEntity.Status != (int)OrderStatus.WaitAudit) { return(Content(new JsonMessage { Success = false, Code = "-1", Message = "操作失败:只有初始和待审核订单才能修改" }.ToString())); } DbTransaction isOpenTrans = database.BeginTrans(); try { string message = orderId == "" ? "新增成功。" : "修改成功。"; if (oldEntity != null) { database.Delete <SaleOrderEntity>("OrderId", oldEntity.OrderId, isOpenTrans); database.Delete <SaleOrderItemEntity>("OrderId", oldEntity.OrderId, isOpenTrans); entity.Create(); entity.OrderDate = oldEntity.OrderDate; entity.Status = oldEntity.Status; entity.Modify(orderId); database.Insert(entity, isOpenTrans); } else { entity.Create(); entity.OrderDate = DateTime.Now; database.Insert(entity, isOpenTrans); _codeRuleBll.OccupyBillCode(ManageProvider.Provider.Current().UserId, SaleOrderCodeName, isOpenTrans); } List <SaleOrderItemViewModel> orderItemList = saleOrderItemJson.JonsToList <SaleOrderItemViewModel>(); foreach (SaleOrderItemViewModel item in orderItemList) { if (!string.IsNullOrEmpty(item.ProductId)) { ProductEntity product = _productBll.GetProduct(item.ProductId); var orderItem = new SaleOrderItemEntity(); orderItem.Create(); orderItem.OrderId = entity.OrderId; orderItem.OrderNo = entity.OrderNo; orderItem.ProductId = product.ProductId; orderItem.Code = product.Code; orderItem.ProductName = product.ProductName; orderItem.Volume = product.Volume; orderItem.Weight = product.Weight; orderItem.BarCode = product.BarCode; orderItem.BaseUnit = product.BaseUnit; orderItem.Price = product.Price; orderItem.Specification = product.Specification; orderItem.Qty = int.Parse(item.Qty); database.Insert(orderItem, isOpenTrans); } } database.Commit(); if (oldEntity == null) { try { _orderBll.ExecSPOrderAllocInventory(); } catch { } } return(Content(new JsonMessage { Success = true, Code = entity.OrderId, Message = message }.ToString())); } catch (Exception ex) { database.Rollback(); return(Content(new JsonMessage { Success = false, Code = "-1", Message = "操作失败:" + ex.Message }.ToString())); } }