/// <summary> /// 更新数据实体 /// </summary> /// <param name="entity">要更新的数据实体</param> /// <returns>返回更新后的数据实体</returns> public WebApiResult<Basic_DataDictionary> Update(Basic_DataDictionary entity) { var result = new WebApiResult<Basic_DataDictionary>(); #region 验证 if (Validate(result, entity)) { if (DataContext.Basic_DataDictionary.Any(x => x.Deleted == false && x.Id != entity.Id && x.Name == entity.Name)) { result.AddError(string.Format("名称 '{0}' 已经存在。", entity.Name)); } } #endregion #region 保存 if (result.Success) { var origin = DataContext.Basic_DataDictionary.FirstOrDefault(x => x.Id == entity.Id); if (origin != null) { origin.Name = entity.Name; origin.Code = entity.Code; origin.StringValue = entity.StringValue; origin.IntValue = entity.IntValue; origin.FloatValue = entity.FloatValue; origin.BoolValue = entity.BoolValue; origin.DateTimeValue = entity.DateTimeValue; origin.UpdatedAt = DateTime.Now; DataContext.SaveChanges(); result.Data = origin; } } #endregion return result; }
/// <summary> /// 验证数据实体 /// </summary> /// <param name="result">result 参数</param> /// <param name="entity">要验证的数据实体</param> /// <returns>成功返回 true;否则返回 false。</returns> protected bool Validate(WebApiResult<Basic_DataDictionary> result, Basic_DataDictionary entity) { if (string.IsNullOrWhiteSpace(entity.Name)) { result.AddError("请输入参数名称。"); } return result.Success; }
/// <summary> /// 新建数据实体 /// </summary> /// <param name="entity">要新建的数据实体</param> /// <returns>返回新建的数据实体</returns> public virtual WebApiResult<Basic_DataDictionary> Add(Basic_DataDictionary entity) { var result = new WebApiResult<Basic_DataDictionary>(); #region 验证 if (Validate(result, entity)) { if (DataContext.Basic_DataDictionary.Any(x => x.Deleted == false && x.Name == entity.Name)) { result.AddError(string.Format("名称 '{0}' 已经存在。", entity.Name)); } } #endregion #region 保存 if (result.Success) { var maxSortOrder = 0; var code = entity.Code; if (DataContext.Basic_DataDictionary.Any()) { maxSortOrder = DataContext.Basic_DataDictionary.Max(x => x.SortOrder); } entity.SortOrder = maxSortOrder + 1; entity.CreatedAt = DateTime.Now; var ParentId = entity.ParentId; if (string.IsNullOrEmpty(entity.Code) && ParentId != null) { entity.Code = DataContext.Basic_DataDictionary.FirstOrDefault(x => x.Id == ParentId).Code + entity.SortOrder; } DataContext.Basic_DataDictionary.Add(entity); DataContext.SaveChanges(); result.Data = entity; } #endregion return result; }