public Data.Trade[] AddRange(long @operator, string parentNo, IClassEditInfo[] adds) { ExceptionHelper.ThrowIfNotId(@operator, "@operator"); ExceptionHelper.ThrowIfNullOrWhiteSpace(parentNo, "parentNo"); if (adds == null) adds = new IClassEditInfo[0]; adds = adds.Where(a => a != null).ToArray(); ExceptionHelper.ThrowIfNullOrEmpty(adds, "adds"); for (int i = 0; i < adds.Length; i++) { ExceptionHelper.ThrowIfNullOrWhiteSpace(adds[i].Name, String.Format("第{0}项的名称为空", i)); } parentNo = parentNo.Trim(); var parent = _TradeRepository.EnabledTrades.FirstOrDefault(c => c.no == parentNo); if (parent == null) throw new FlhException(ErrorCode.NotExists, "父级不存在或已被删除"); var fullName = parent.full_name.Split(','); var fullNameEn = (parent.full_name_en??string.Empty).Split(','); using (var scope = new System.Transactions.TransactionScope()) { var noLength = parent.no.Length + 4; var maxChild = _TradeRepository .Entities .Where(c => c.no.StartsWith(parent.no) && c.no.Length == noLength) .OrderByDescending(c => c.no) .FirstOrDefault(); var num = 1; if (maxChild != null) { num = Convert.ToInt32(maxChild.no.Substring(parent.no.Length)) + 1; } var addEntites = new List<Data.Trade>(); foreach (var item in adds) { var entity = new Data.Trade { enabled = true, created = DateTime.Now, creater = @operator, name = item.Name.Trim(), name_en =( item.EnName??string.Empty).Trim(), no = parent.no + num.ToString().PadLeft(4, '0'), order_by = item.Order, updated = DateTime.Now, updater = @operator, }; entity.full_name_en = fullNameEn.Length == 0 ? entity.name_en : String.Join(",", fullNameEn.Concat(new[] { entity.name_en })); entity.full_name = fullName.Length == 0 ? entity.name : String.Join(",", fullName.Concat(new[] { entity.name })); num++; addEntites.Add(entity); } _TradeRepository.AddRange(addEntites); _TradeRepository.SaveChanges(); scope.Complete(); return addEntites.ToArray(); } }
public Data.Trade[] AddRange(long @operator, string parentNo, IClassEditInfo[] adds) { ExceptionHelper.ThrowIfNotId(@operator, "@operator"); ExceptionHelper.ThrowIfNullOrWhiteSpace(parentNo, "parentNo"); if (adds == null) { adds = new IClassEditInfo[0]; } adds = adds.Where(a => a != null).ToArray(); ExceptionHelper.ThrowIfNullOrEmpty(adds, "adds"); for (int i = 0; i < adds.Length; i++) { ExceptionHelper.ThrowIfNullOrWhiteSpace(adds[i].Name, String.Format("第{0}项的名称为空", i)); } parentNo = parentNo.Trim(); var parent = _TradeRepository.EnabledTrades.FirstOrDefault(c => c.no == parentNo); if (parent == null) { throw new FlhException(ErrorCode.NotExists, "父级不存在或已被删除"); } var fullName = parent.full_name.Split(','); var fullNameEn = (parent.full_name_en ?? string.Empty).Split(','); using (var scope = new System.Transactions.TransactionScope()) { var noLength = parent.no.Length + 4; var maxChild = _TradeRepository .Entities .Where(c => c.no.StartsWith(parent.no) && c.no.Length == noLength) .OrderByDescending(c => c.no) .FirstOrDefault(); var num = 1; if (maxChild != null) { num = Convert.ToInt32(maxChild.no.Substring(parent.no.Length)) + 1; } var addEntites = new List <Data.Trade>(); foreach (var item in adds) { var entity = new Data.Trade { enabled = true, created = DateTime.Now, creater = @operator, name = item.Name.Trim(), name_en = (item.EnName ?? string.Empty).Trim(), no = parent.no + num.ToString().PadLeft(4, '0'), order_by = item.Order, updated = DateTime.Now, updater = @operator, }; entity.full_name_en = fullNameEn.Length == 0 ? entity.name_en : String.Join(",", fullNameEn.Concat(new[] { entity.name_en })); entity.full_name = fullName.Length == 0 ? entity.name : String.Join(",", fullName.Concat(new[] { entity.name })); num++; addEntites.Add(entity); } _TradeRepository.AddRange(addEntites); _TradeRepository.SaveChanges(); scope.Complete(); return(addEntites.ToArray()); } }