public async Task <IActionResult> AddRange([FromBody] List <ConfigVM> model) { if (model == null || model.Count == 0) { throw new ArgumentNullException("model"); } var configs = await _configService.GetByAppId(model.First().AppId); var oldDict = new Dictionary <string, string>(); configs.ForEach(item => { var newkey = item.Key; if (!string.IsNullOrEmpty(item.Group)) { newkey = $"{item.Group}:{item.Key}"; } oldDict.Add(newkey, item.Value); }); var addConfigs = new List <Config>(); //judge if json key already in configs foreach (var item in model) { var newkey = item.Key; if (!string.IsNullOrEmpty(item.Group)) { newkey = $"{item.Group}:{item.Key}"; } if (oldDict.ContainsKey(newkey)) { return(Json(new { success = false, message = "存在重复的配置:" + item.Key })); } var config = new Config(); config.Id = Guid.NewGuid().ToString("N"); config.Key = item.Key; config.AppId = item.AppId; config.Description = item.Description; config.Value = item.Value; config.Group = item.Group; config.Status = ConfigStatus.Enabled; config.CreateTime = DateTime.Now; config.UpdateTime = null; config.OnlineStatus = OnlineStatus.WaitPublish; addConfigs.Add(config); } var result = await _configService.AddRangeAsync(addConfigs); if (result) { //add syslogs var addSysLogs = new List <SysLog>(); addConfigs.ForEach(c => { addSysLogs.Add(new SysLog { LogTime = DateTime.Now, LogType = SysLogType.Normal, AppId = c.AppId, LogText = $"新增配置【Key:{c.Key}】【Value:{c.Value}】【Group:{c.Group}】【AppId:{c.AppId}】" }); }); await _sysLogService.AddRangeAsync(addSysLogs); //add modify log var addModifyLogs = new List <ModifyLog>(); addConfigs.ForEach(c => { addModifyLogs.Add(new ModifyLog { Id = Guid.NewGuid().ToString("N"), ConfigId = c.Id, Key = c.Key, Group = c.Group, Value = c.Value, ModifyTime = c.CreateTime }); }); await _modifyLogService.AddRangAsync(addModifyLogs); } return(Json(new { success = result, message = !result ? "批量新增配置失败,请查看错误日志" : "" })); }