public void EditOrder(OrderEditDto orderEditDto, bool changeOneToMany = false) { if (changeOneToMany) { List <OrderBox> orderBoxes = SnHelper.GetListboxNumber(orderEditDto.SnMin, orderEditDto.OnLineQuantity).Select(item => new OrderBox() { //设置外键Id OrderId = orderEditDto.OrderId, //盒号 BoxNumber = item, //新建时间 OrderBoxCreateTime = DateTime.Now }).ToList(); var editEntity = _mapper.Map <OrderPlacing>(orderEditDto); //editEntity.OrderBoxes = orderBoxes; _orderBoxRepository.DelOrderBox(editEntity.OrderId); _orderBoxRepository.AddOrderAllBoxes(orderBoxes); _orderRepository.Update(editEntity); _unitOfWork.SaveChanges(); } else { var editEntity = _mapper.Map <OrderPlacing>(orderEditDto); _orderRepository.Update(editEntity); _unitOfWork.SaveChanges(); } }
public void AddOrder(OrderAddDto orderAddDto) { List <OrderBox> orderBoxes = SnHelper.GetListboxNumber(orderAddDto.SnMin, orderAddDto.OnLineQuantity).Select(item => new OrderBox() { //设置外键Id OrderId = orderAddDto.OrderId, //盒号 BoxNumber = item, //新建时间 OrderBoxCreateTime = DateTime.Now }).ToList(); var newEntity = _mapper.Map <OrderPlacing>(orderAddDto); newEntity.OrderBoxes = orderBoxes; _orderRepository.Insert(newEntity); _unitOfWork.SaveChanges(); ; }
/// <summary> /// 生成订单和 对应的盒号 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOrder_Click(object sender, EventArgs e) { try { using (_dbcontext = new FlowManageSystemEntities()) { orderRepositoy = new OrderRepository(_dbcontext); orderBoxRepository = new OrderBoxRepository(_dbcontext); OrderPlacing orderPlacing = new OrderPlacing() { OrderDate = Convert.ToDateTime(this.OrderDateTimePic.Text), Type = this.TypeTxtBox.Text, PlanNo = this.PlanNoTxtBox.Text, FinishedProductNo = this.FinishedProductNocBox.Text, ProductTypeNo = this.ProductTypecBox.Text, OnLineQuantity = Convert.ToInt32(this.OnlineQuantityTxtBox.Text), SnMin = SnMinTxtBox.Text, SnMax = SnMinTxtBox.Text, LabelingRequirements = this.LableRequireTxtBox.Text, TestReport = this.TestReportTxtBox.Text, CodingRequirements = this.CodingRequireTxtBox.Text, PackingRequirement = this.PackingRequireTxtBox.Text, SpecialRequirement = this.SpecialRequireTxtBox.Text, TestSoftConfigFile = this.TestSoftConfigFIleTxtBox.Text, Remark = this.RemarkTxtBox.Text, CustomDemandDate = Convert.ToDateTime(this.CustonRequireTimePic.Text), PlannedLaunchDate = Convert.ToDateTime(this.PlanOnlineTimePic.Text), PlanStorageDate = Convert.ToDateTime(this.PlanStorageTimePic.Text), DaysOfProduction = Convert.ToByte(this.ProductDaysTxtBox.Text), OrderCreateTime = DateTime.Now }; //注意此处一对多关联数据的保存 设置关联 IEnumerable <OrderBox> orderBoxes = SnHelper.GetListboxNumber(this.SnMinTxtBox.Text, Convert.ToInt32(this.OnlineQuantityTxtBox.Text)).Select(item => new OrderBox() { //设置导航属性 OrderPlacing = orderPlacing, //设置外键Id OrderId = orderPlacing.OrderId, //盒号 BoxNumber = item, //新建时间 OrderBoxCreateTime = DateTime.Now }); //添加所有 多的 一端数据 orderBoxRepository.AddOrderAllBoxes(orderBoxes); //添加 一的 一端数据 //下单 orderRepositoy.AddOrder(orderPlacing); //一对多的数据一起保存 两个仓储公用一个dbcontext 可以防止添加部分失败 会以一个事务的形式保存 _dbcontext.SaveChanges(); MessageBox.Show("下单成功!"); } } catch (Exception ex) { MessageBox.Show("下单失败,请检查数据" + ex.Message); } }
/// <summary> /// 生成订单和 对应的盒号 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnOrder_Click(object sender, EventArgs e) { //此时是修改 重复的绑值太恶心了 用数据绑定 if (Modify) { var unitOfWork = new UnitOfWork(); var orderBoxService = new OrderBoxService(unitOfWork, unitOfWork.OrderBoxRepository); var orderService = new OrderService(unitOfWork, unitOfWork.OrderRepository); //判断是否修改了订单数量 修改了 删除盒号 重新生成 if ((beforModifyCount != _orderPlacing.OnLineQuantity) || (beforModifySnMin != _orderPlacing.SnMin)) { //生成订单盒 注意此处一对多关联数据的保存 设置关联 IEnumerable <OrderBox> orderBoxes = SnHelper.GetListboxNumber(this.SnMinTxtBox.Text, Convert.ToInt32(this.OnlineQuantityTxtBox.Text)).Select(item => new OrderBox() { //设置导航属性 OrderPlacing = _orderPlacing, //设置外键Id OrderId = _orderPlacing.OrderId, //盒号 BoxNumber = item, //新建时间 OrderBoxCreateTime = DateTime.Now }); try { //添加所有 多的 一端数据 orderBoxService.AddOrderBoxAfterDel(_orderPlacing.OrderId, orderBoxes); } catch (Exception ex) { MessageBox.Show(ex.Message); } } try { orderService.UpdateOrder(_orderPlacing); MessageBox.Show("修改成功!"); this.Dispose(); } catch (Exception ex) { MessageBox.Show(ex.Message); } #region old //using (_dbcontext = new FlowManageSystemEntities()) //{ // orderRepositoy = new OrderRepository(_dbcontext); // //判断是否修改了订单数量 修改了 删除盒号 重新生成 // if ((beforModifyCount != _orderPlacing.OnLineQuantity) || (beforModifySnMin != _orderPlacing.SnMin)) // { // orderBoxRepository = new OrderBoxRepository(_dbcontext); // //生成订单盒 // //注意此处一对多关联数据的保存 设置关联 // IEnumerable<OrderBox> orderBoxes = SnHelper.GetListboxNumber(this.SnMinTxtBox.Text, Convert.ToInt32(this.OnlineQuantityTxtBox.Text)).Select(item => new OrderBox() // { // //设置导航属性 // OrderPlacing = _orderPlacing, // //设置外键Id // OrderId = _orderPlacing.OrderId, // //盒号 // BoxNumber = item, // //新建时间 // OrderBoxCreateTime = DateTime.Now // }); // //添加所有 多的 一端数据 // orderBoxRepository.AddOrderBoxAfterDel(_orderPlacing.OrderId, orderBoxes); // } // if (orderRepositoy.Edit(_orderPlacing) != 0) // { // MessageBox.Show("修改成功!"); // this.Dispose(); // } //} #endregion } else { try { var unitOfWork = new UnitOfWork(); var orderBoxService = new OrderBoxService(unitOfWork, unitOfWork.OrderBoxRepository); var orderService = new OrderService(unitOfWork, unitOfWork.OrderRepository); OrderPlacing orderPlacing = new OrderPlacing() { OrderDate = Convert.ToDateTime(this.OrderDateTimePic.Text), Type = this.TypeTxtBox.Text, PlanNo = this.PlanNoTxtBox.Text, FinishedProductNo = this.FinishedProductNocBox.Text, ProductTypeNo = this.ProductTypecBox.Text, OnLineQuantity = Convert.ToInt32(this.OnlineQuantityTxtBox.Text), SnMin = SnMinTxtBox.Text, SnMax = this.SnMaxTxtBox.Text, LabelingRequirements = this.LableRequireTxtBox.Text, TestReport = this.TestReportTxtBox.Text, CodingRequirements = this.CodingRequireTxtBox.Text, PackingRequirement = this.PackingRequireTxtBox.Text, SpecialRequirement = this.SpecialRequireTxtBox.Text, TestSoftConfigFile = this.TestSoftConfigFIleTxtBox.Text, Remark = this.RemarkTxtBox.Text, CustomDemandDate = Convert.ToDateTime(this.CustonRequireTimePic.Text), PlannedLaunchDate = Convert.ToDateTime(this.PlanOnlineTimePic.Text), PlanStorageDate = Convert.ToDateTime(this.PlanStorageTimePic.Text), DaysOfProduction = Convert.ToByte(this.ProductDaysTxtBox.Text), OrderCreateTime = DateTime.Now, StorageFinishedFlag = "0" }; if (orderService.PlanNoExist(this.PlanNoTxtBox.Text.Trim())) { MessageBox.Show("当前计划单号" + this.PlanNoTxtBox.Text + "数据库已存在"); this.PlanNoTxtBox.Select(); return; } //注意此处一对多关联数据的保存 设置关联 IEnumerable <OrderBox> orderBoxes = SnHelper.GetListboxNumber(this.SnMinTxtBox.Text, Convert.ToInt32(this.OnlineQuantityTxtBox.Text)).Select(item => new OrderBox() { //设置导航属性 OrderPlacing = orderPlacing, //设置外键Id OrderId = orderPlacing.OrderId, //盒号 BoxNumber = item, //新建时间 OrderBoxCreateTime = DateTime.Now }); //添加所有 多的 一端数据 orderBoxService.AddOrderAllBoxes(orderBoxes); //添加 一的 一端数据 //下单 orderService.InsertOrder(orderPlacing); ////一对多的数据一起保存 两个仓储公用一个dbcontext 可以防止添加部分失败 会以一个事务的形式保存 //_dbcontext.SaveChanges(); this.lbInfo.Text = orderPlacing.PlanNo + "下单成功!"; btnClear_Click(null, null); #region old //using (_dbcontext = new FlowManageSystemEntities()) //{ // orderRepositoy = new OrderRepository(_dbcontext); // orderBoxRepository = new OrderBoxRepository(_dbcontext); // OrderPlacing orderPlacing = new OrderPlacing() // { // OrderDate = Convert.ToDateTime(this.OrderDateTimePic.Text), // Type = this.TypeTxtBox.Text, // PlanNo = this.PlanNoTxtBox.Text, // FinishedProductNo = this.FinishedProductNocBox.Text, // ProductTypeNo = this.ProductTypecBox.Text, // OnLineQuantity = Convert.ToInt32(this.OnlineQuantityTxtBox.Text), // SnMin = SnMinTxtBox.Text, // SnMax = this.SnMaxTxtBox.Text, // LabelingRequirements = this.LableRequireTxtBox.Text, // TestReport = this.TestReportTxtBox.Text, // CodingRequirements = this.CodingRequireTxtBox.Text, // PackingRequirement = this.PackingRequireTxtBox.Text, // SpecialRequirement = this.SpecialRequireTxtBox.Text, // TestSoftConfigFile = this.TestSoftConfigFIleTxtBox.Text, // Remark = this.RemarkTxtBox.Text, // CustomDemandDate = Convert.ToDateTime(this.CustonRequireTimePic.Text), // PlannedLaunchDate = Convert.ToDateTime(this.PlanOnlineTimePic.Text), // PlanStorageDate = Convert.ToDateTime(this.PlanStorageTimePic.Text), // DaysOfProduction = Convert.ToByte(this.ProductDaysTxtBox.Text), // OrderCreateTime = DateTime.Now, // StorageFinishedFlag = "0" // }; // if (orderRepositoy.PlanNoExist(this.PlanNoTxtBox.Text.Trim())) // { // MessageBox.Show("当前计划单号" + this.PlanNoTxtBox.Text + "数据库已存在"); // this.PlanNoTxtBox.Select(); // return; // } // //注意此处一对多关联数据的保存 设置关联 // IEnumerable<OrderBox> orderBoxes = SnHelper.GetListboxNumber(this.SnMinTxtBox.Text, Convert.ToInt32(this.OnlineQuantityTxtBox.Text)).Select(item => new OrderBox() // { // //设置导航属性 // OrderPlacing = orderPlacing, // //设置外键Id // OrderId = orderPlacing.OrderId, // //盒号 // BoxNumber = item, // //新建时间 // OrderBoxCreateTime = DateTime.Now // }); // //添加所有 多的 一端数据 // orderBoxRepository.AddOrderAllBoxes(orderBoxes); // //添加 一的 一端数据 //下单 // orderRepositoy.AddOrder(orderPlacing); // //一对多的数据一起保存 两个仓储公用一个dbcontext 可以防止添加部分失败 会以一个事务的形式保存 // _dbcontext.SaveChanges(); // this.lbInfo.Text = orderPlacing.PlanNo + "下单成功!"; // btnClear_Click(null, null); //} #endregion } catch (Exception ex) { MessageBox.Show("下单失败,请检查数据是否符合规范" + ex.Message); } } }