private void SetStyleBYQ(ProStyleBO style) { var byq = VMGlobal.BYQs.Find(o => o.BrandID == style.BrandID && o.Year == style.Year && o.Quarter == style.Quarter); if (byq != null) { style.BYQID = byq.ID; } else { byq = LinqOP.Search <ProBYQ>(o => o.BrandID == style.BrandID && o.Year == style.Year && o.Quarter == style.Quarter).FirstOrDefault(); if (byq != null) { style.BYQID = byq.ID; VMGlobal.BYQs.Add(byq); } else { byq = new ProBYQ { BrandID = style.BrandID, Year = style.Year, Quarter = style.Quarter }; int byqID = LinqOP.Add <ProBYQ, int>(byq, o => o.ID); style.BYQID = byqID; byq.ID = byqID; VMGlobal.BYQs.Add(byq); } } }
private OPResult Add(ProStyleBO style) { style.CreatorID = VMGlobal.CurrentUser.ID; //TransactionScopeOption在事务嵌套时比较有用 using (TransactionScope scope = new TransactionScope()) { try { SetStyleBYQ(style); int id = LinqOP.Add <ProStyle, int>(style, s => s.ID); style.ID = id;//将ID赋值,表明该对象不再是新增对象(新增对象ID都为0) List <Product> products = new List <Product>(); foreach (var color in style.Colors) { foreach (var size in style.Sizes) { Product product = new Product { Code = style.Code + color.Code + size.Code,//成品SKU码生成规则=款号+色号+尺码 StyleID = style.ID, ColorID = color.ID, SizeID = size.ID, CreatorID = VMGlobal.CurrentUser.ID }; products.Add(product); } } LinqOP.Add <Product>(products); scope.Complete(); return(new OPResult { IsSucceed = true, Message = "保存成功!" }); } catch (Exception e) { style.ID = default(int); return(new OPResult { IsSucceed = false, Message = "保存失败,失败原因:\n" + e.Message }); } } }
private OPResult Update(ProStyleBO style) { SetStyleBYQ(style); ProStyle orginStyle = LinqOP.GetById <ProStyle>(style.ID); List <Product> productsExist = LinqOP.Search <Product>(p => p.StyleID == style.ID).ToList(); List <Product> products = new List <Product>(); foreach (var color in style.Colors) { foreach (var size in style.Sizes) { var pcode = style.Code + color.Code + size.Code; Product product = productsExist.FirstOrDefault(p => p.ColorID == color.ID && p.SizeID == size.ID); if (product != null) { if (orginStyle.Code != style.Code) { product.Code = pcode; products.Add(product); } } else { product = new Product { Code = pcode, StyleID = style.ID, ColorID = color.ID, SizeID = size.ID, CreatorID = VMGlobal.CurrentUser.ID }; products.Add(product); } } } string changeMsg = ""; if (orginStyle.Price != style.Price) { changeMsg += string.Format("单价从{0}变动为{1},", orginStyle.Price, style.Price); } if (orginStyle.Code != style.Code) { changeMsg += string.Format("款号从{0}变动为{1},", orginStyle.Code, style.Code); } if (products.Count > 0) { changeMsg += "增加了SKU码:"; products.ForEach(o => { changeMsg += (o.Code + ","); }); } changeMsg = changeMsg.TrimEnd(','); OPResult result = null; using (TransactionScope scope = new TransactionScope()) { try { LinqOP.Update <ProStyle>(style); LinqOP.AddOrUpdate <Product>(products); if (!string.IsNullOrEmpty(changeMsg)) { ProStyleChange change = new ProStyleChange { CreateTime = DateTime.Now, CreatorID = VMGlobal.CurrentUser.ID, Description = changeMsg, StyleID = style.ID }; LinqOP.Add <ProStyleChange>(change); style.Changes.Insert(0, change); } result = new OPResult { IsSucceed = true, Message = "更新成功!" }; scope.Complete(); } catch (Exception e) { result = new OPResult { IsSucceed = false, Message = "更新失败,失败原因:\n" + e.Message }; } } if (result.IsSucceed && !string.IsNullOrEmpty(changeMsg)) { IMHelper.AsyncSendMessageTo(IMHelper.OnlineUsers, new IMessage { Message = changeMsg }, IMReceiveAccessEnum.成品资料变动); } return(result); }