/// <summary> /// 添加 /// </summary> /// <param name="input"></param> /// <returns></returns> protected virtual async Task <GetDictionaryOutput> CreateDictionary(CreateOrEditDictionaryInput input) { Dictionary dic = new Dictionary(); var cnt = _dictionary.GetAll().FirstOrDefault(r => (r.Value == input.Value.Trim() && r.ParentID == input.ParentID)); if (cnt != null) { throw new Exception("值" + input.Value + "已存在"); } ObjectMapper.Map(input, dic); var id = _dictionary.InsertAndGetIdAsync(dic); var data = from a in _dictionary.GetAll() where a.Id == id.Result select new GetDictionaryOutput { Id = a.Id, ParentID = a.ParentID, Title = a.Title, Value = a.Value, Label = a.Title, Data = a.Value, Note = a.Note, Code = a.Code, Other = a.Other, Sort = a.Sort, ExpandedIcon = "", CollapsedIcon = "", ChildrenCount = _dictionary.GetAll().Where(r => r.ParentID == a.Id).Count() }; return(await data.FirstOrDefaultAsync()); }
public async Task <GetDictionaryOutput> CreateOrUpdateDictionary(CreateOrEditDictionaryInput input) { if (input.Id.HasValue) { return(await UpdateDictionary(input)); } else { return(await CreateDictionary(input)); } }
/// <summary> /// 修改 /// </summary> /// <param name="input"></param> /// <returns></returns> protected virtual async Task <GetDictionaryOutput> UpdateDictionary(CreateOrEditDictionaryInput input) { //System.Diagnostics.Debug.Assert(input.id != null, "input.id should be set."); if (!input.Id.HasValue) { throw new Exception("id 必须输入"); } var dic = await _dictionary.GetAsync(input.Id.Value); var cnt = _dictionary.GetAll().FirstOrDefault(r => (r.Value == input.Value.Trim() && r.ParentID == input.ParentID && r.Id != input.Id)); if (cnt != null) { throw new Exception("值" + input.Value + "已存在"); } ObjectMapper.Map(input, dic); await _dictionary.UpdateAsync(dic); var data = from a in _dictionary.GetAll() where a.Id == input.Id select new GetDictionaryOutput { Id = a.Id, ParentID = a.ParentID, Title = a.Title, Value = a.Value, Label = a.Title, Data = a.Value, Note = a.Note, Code = a.Code, Other = a.Other, Sort = a.Sort, ExpandedIcon = "", CollapsedIcon = "", ChildrenCount = _dictionary.GetAll().Where(r => r.ParentID == a.Id).Count() }; return(data.FirstOrDefault()); }