diff --git a/Src/Asp.Net/SqlServerTest/Demo/Demo4_Deleteable.cs b/Src/Asp.Net/SqlServerTest/Demo/Demo4_Deleteable.cs index 8e39e2013..9736890b9 100644 --- a/Src/Asp.Net/SqlServerTest/Demo/Demo4_Deleteable.cs +++ b/Src/Asp.Net/SqlServerTest/Demo/Demo4_Deleteable.cs @@ -41,8 +41,19 @@ namespace OrmTest //by expression db.Deleteable().Where(it => it.Id == 11111).ExecuteCommand(); + //logic delete + db.CodeFirst.InitTables(); + ; + db.Deleteable().Where(it=>it.Id==1).IsLogic().ExecuteCommand(); Console.WriteLine("#### Deleteable End ####"); } } + public class LogicTest + { + [SugarColumn(IsPrimaryKey =true,IsIdentity =true)] + public int Id { get; set; } + + public bool isdeleted { get; set; } + } } diff --git a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs index c8bf71b2b..0fb37be67 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/DeleteableProvider.cs @@ -21,7 +21,7 @@ namespace SqlSugar public bool IsEnableDiffLogEvent { get; set; } public DiffLogModel diffModel { get; set; } public List tempPrimaryKeys { get; set; } - private Action RemoveCacheFunc { get; set; } + internal Action RemoveCacheFunc { get; set; } public EntityInfo EntityInfo { get @@ -272,6 +272,13 @@ namespace SqlSugar result.deleteobj = this; return result; } + public LogicDeleteProvider IsLogic() + { + LogicDeleteProvider result = new LogicDeleteProvider(); + result.DeleteBuilder = this.DeleteBuilder; + result.Deleteable = this; + return result; + } public IDeleteable RemoveDataCache(string likeString) { this.RemoveCacheFunc = () => diff --git a/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/LogicDeleteProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/LogicDeleteProvider.cs new file mode 100644 index 000000000..103b189e8 --- /dev/null +++ b/Src/Asp.Net/SqlSugar/Abstract/DeleteProvider/LogicDeleteProvider.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SqlSugar +{ + public class LogicDeleteProvider where T : class, new() + { + public DeleteableProvider Deleteable { get; set; } + public DeleteBuilder DeleteBuilder { get; set; } + + public int ExecuteCommand(string LogicFieldName = null) + { + ISqlSugarClient db; + List pars; + string where; + LogicFieldName = _ExecuteCommand(LogicFieldName, out db, out where, out pars); + var updateable = db.Updateable().SetColumns(LogicFieldName, "@IsDeleted"); + if (pars != null) + updateable.UpdateBuilder.Parameters.AddRange(pars); + Convert(updateable as UpdateableProvider); + var result = updateable.Where(where, new { IsDeleted = true }).ExecuteCommand(); + return result; + } + public async Task ExecuteCommandAsync(string LogicFieldName = null) + { + ISqlSugarClient db; + List pars; + string where; + LogicFieldName = _ExecuteCommand(LogicFieldName, out db, out where, out pars); + var updateable = db.Updateable().SetColumns(LogicFieldName, "@IsDeleted"); + if (pars != null) + updateable.UpdateBuilder.Parameters.AddRange(pars); + Convert(updateable as UpdateableProvider); + var result =await updateable.Where(where, new { IsDeleted = true }).ExecuteCommandAsync(); + return result; + } + + private void Convert(UpdateableProvider updateable) + { + updateable.IsEnableDiffLogEvent = Deleteable.IsEnableDiffLogEvent; + updateable.diffModel = Deleteable.diffModel; + updateable.UpdateBuilder.TableWithString = DeleteBuilder.TableWithString; + updateable.RemoveCacheFunc = Deleteable.RemoveCacheFunc; + } + + private string _ExecuteCommand(string LogicFieldName, out ISqlSugarClient db, out string where, out List pars) + { + var entityInfo = Deleteable.EntityInfo; + db = Deleteable.Context; + where = DeleteBuilder.GetWhereString.Substring(5); + pars = DeleteBuilder.Parameters; + if (LogicFieldName.IsNullOrEmpty()) + { + var column = entityInfo.Columns.FirstOrDefault(it => + it.PropertyName.EqualCase("isdelete") || + it.PropertyName.EqualCase("isdeleted") || + it.DbColumnName.EqualCase("isdelete") || + it.DbColumnName.EqualCase("isdeleted")); + if (column != null) + { + LogicFieldName = column.DbColumnName; + } + } + Check.Exception(LogicFieldName == null, ErrorMessage.GetThrowMessage( + $"{entityInfo.EntityName} is not isdelete or isdeleted" + , $"{entityInfo.EntityName} 没有IsDelete或者IsDeleted 的属性, 你也可以用 IsLogic().ExecuteCommand(\"列名\")")); + return LogicFieldName; + } + } +} diff --git a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs index dcbd922c3..a2984f32d 100644 --- a/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs +++ b/Src/Asp.Net/SqlSugar/Abstract/UpdateProvider/UpdateableProvider.cs @@ -32,7 +32,7 @@ namespace SqlSugar public bool IsAs { get; set; } public bool IsEnableDiffLogEvent { get; set; } public DiffLogModel diffModel { get; set; } - private Action RemoveCacheFunc { get; set; } + internal Action RemoveCacheFunc { get; set; } private int SetColumnsIndex { get; set; } private List columns { get; set; } #endregion @@ -291,6 +291,19 @@ namespace SqlSugar #endregion #region Update by expression + public IUpdateable SetColumns(string fieldName, object fieldValue) + { + ThrowUpdateByObject(); + var columnInfo = this.EntityInfo.Columns.FirstOrDefault(it => it.PropertyName.EqualCase(fieldName)); + if (columnInfo != null) + { + fieldName = columnInfo.DbColumnName; + } + UpdateBuilder.SetValues.Add(new KeyValuePair(fieldName, fieldValue+"")); + this.UpdateBuilder.DbColumnInfoList = this.UpdateBuilder.DbColumnInfoList.Where(it => (UpdateParameterIsNull == false && IsPrimaryKey(it)) || UpdateBuilder.SetValues.Any(v => SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.DbColumnName, StringComparison.CurrentCultureIgnoreCase) || SqlBuilder.GetNoTranslationColumnName(v.Key).Equals(it.PropertyName, StringComparison.CurrentCultureIgnoreCase)) || it.IsPrimarykey == true).ToList(); + AppendSets(); + return this; + } public IUpdateable SetColumns(Expression> columns) { ThrowUpdateByObject(); diff --git a/Src/Asp.Net/SqlSugar/Interface/IDeleteable.cs b/Src/Asp.Net/SqlSugar/Interface/IDeleteable.cs index 9da83f812..f2bda9590 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IDeleteable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IDeleteable.cs @@ -37,6 +37,7 @@ namespace SqlSugar KeyValuePair> ToSql(); IDeleteable EnableQueryFilter(); SplitTableDeleteProvider SplitTable(Func, IEnumerable> getTableNamesFunc); + LogicDeleteProvider IsLogic(); void AddQueue(); } } diff --git a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs index 0718cdc22..733deb9e7 100644 --- a/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs +++ b/Src/Asp.Net/SqlSugar/Interface/IUpdateable.cs @@ -66,7 +66,7 @@ namespace SqlSugar /// /// IUpdateable SetColumns(Expression> columns); - + IUpdateable SetColumns(string fieldName,object fieldValue); IUpdateable UpdateColumnsIF(bool isUpdateColumns,Expression> columns); diff --git a/Src/Asp.Net/SqlSugar/SqlSugar.csproj b/Src/Asp.Net/SqlSugar/SqlSugar.csproj index b741eed60..0b8f4fe9d 100644 --- a/Src/Asp.Net/SqlSugar/SqlSugar.csproj +++ b/Src/Asp.Net/SqlSugar/SqlSugar.csproj @@ -84,6 +84,7 @@ +