using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; namespace SqlSugar { public interface IStorageable where T : class, new() { IStorageable TableDataRange(Expression> exp); IStorageable WhereColumns(Expression> columns); IStorageable WhereColumns(Expression> columns,Func formatTime); IStorageable WhereColumns(string [] columns); IStorageable WhereColumns(string[] columns, Func formatTime); IStorageable SplitInsert(Func, bool> conditions, string message=null); IStorageable SplitUpdate(Func, bool> conditions, string message = null); IStorageable Saveable(string inserMessage = null, string updateMessage = null); IStorageable SplitError(Func, bool> conditions, string message = null); IStorageable SplitIgnore(Func, bool> conditions, string message = null); IStorageable DisableFilters(); IStorageable TranLock(DbLockType LockType = DbLockType.Wait); IStorageable SplitDelete(Func, bool> conditions, string message = null); IStorageable SplitOther(Func, bool> conditions, string message = null); StorageableResult ToStorage(); Task> ToStorageAsync(); IStorageable As(string tableName); int ExecuteCommand(); Task ExecuteCommandAsync(); } public class StorageableInfo where T : class, new() { public T Item { get; set; } internal List Database { get; set; } internal string[] PkFields { get; set; } public bool Any(Func expression) { return Database.Any(expression); } public bool NotAny(Func expression) { return !Database.Any(expression); } public bool Any() { var list = Database.Where(it=>true); foreach (var pk in PkFields) { list = list.Where(it =>IsEquals(it, pk)); } return list.Any(); } private bool IsEquals(T it, string pk) { var leftValue = it.GetType().GetProperty(pk).GetValue(it, null); var rightValue = Item.GetType().GetProperty(pk).GetValue(Item, null); var left = leftValue.ObjToString(); var rigth = rightValue.ObjToString(); if (leftValue!=null&& (leftValue is decimal||leftValue is decimal?)) { return Convert.ToDecimal(leftValue) == Convert.ToDecimal(rightValue); } else { return left.EqualCase(rigth); } } public bool NotAny() { return !Any(); } } public class StorageableMessage : StorageableInfo where T : class, new() { public string StorageMessage { get; set; } public StorageType? StorageType { get; set; } } public enum StorageType { Insert=0, Update=1, Delete=2, Error=3, Other=4, Ignore=5, } internal struct KeyValuePair { public TKey key; public TValue value1; public TValue2 value2; public KeyValuePair(TKey key, TValue value1, TValue2 value2) { this.key = key; this.value1 = value1; this.value2 = value2; } } public class StorageableResult where T : class, new() { public List> TotalList { get; set; } public List> InsertList { get; set; } public List> UpdateList { get; set; } public List> DeleteList { get; set; } public List> ErrorList { get; set; } public List> IgnoreList { get; set; } public List> OtherList { get; set; } public IInsertable AsInsertable { get; set; } public IUpdateable AsUpdateable { get; set; } public IDeleteable AsDeleteable { get; set; } internal List _WhereColumnList { get; set; } internal string _AsName { get; set; } internal SqlSugarProvider _Context { get; set; } public int BulkCopy() { return this._Context.Fastest().AS(_AsName).BulkCopy(InsertList.Select(it=>it.Item).ToList()); } public Task BulkCopyAsync() { return this._Context.Fastest().AS(_AsName).BulkCopyAsync(InsertList.Select(it => it.Item).ToList()); } public int BulkUpdate() { var isWhereColums = _WhereColumnList != null && _WhereColumnList.Any(); if (isWhereColums) { var updateColumns = this._Context.EntityMaintenance.GetEntityInfo().Columns.Where(it => !it.IsPrimarykey && !it.IsIdentity && !it.IsOnlyIgnoreUpdate && !it.IsIgnore).Select(it => it.DbColumnName ?? it.PropertyName).ToArray(); return BulkUpdate(updateColumns); } else { return this._Context.Fastest().AS(_AsName).BulkUpdate(UpdateList.Select(it => it.Item).ToList()); } } public Task BulkUpdateAsync() { var isWhereColums = _WhereColumnList != null && _WhereColumnList.Any(); if (isWhereColums) { var updateColumns = this._Context.EntityMaintenance.GetEntityInfo().Columns.Where(it => !it.IsPrimarykey && !it.IsIdentity && !it.IsOnlyIgnoreUpdate && !it.IsIgnore).Select(it => it.DbColumnName ?? it.PropertyName).ToArray(); return BulkUpdateAsync(updateColumns); } else { return this._Context.Fastest().AS(_AsName).BulkUpdateAsync(UpdateList.Select(it => it.Item).ToList()); } } public int BulkUpdate(params string[] UpdateColumns) { Check.Exception(UpdateColumns==null, "UpdateColumns is null"); if (_WhereColumnList != null && _WhereColumnList.Any()) { return this._Context.Fastest().AS(_AsName).BulkUpdate(UpdateList.Select(it => it.Item).ToList(), _WhereColumnList.Select(it => it.DbColumnName).ToArray(), UpdateColumns); } else { var pkColumns = this._Context.EntityMaintenance.GetEntityInfo().Columns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName).ToArray(); Check.Exception(pkColumns.Count()==0,"need primary key"); return this._Context.Fastest().AS(_AsName).BulkUpdate(UpdateList.Select(it => it.Item).ToList(), pkColumns, UpdateColumns); } } public async Task BulkUpdateAsync(params string[] UpdateColumns) { Check.Exception(UpdateColumns == null, "UpdateColumns is null"); if (_WhereColumnList != null && _WhereColumnList.Any()) { return await this._Context.Fastest().AS(_AsName).BulkUpdateAsync(UpdateList.Select(it => it.Item).ToList(), _WhereColumnList.Select(it => it.DbColumnName).ToArray(), UpdateColumns); } else { var pkColumns = this._Context.EntityMaintenance.GetEntityInfo().Columns.Where(it => it.IsPrimarykey).Select(it => it.DbColumnName).ToArray(); Check.Exception(pkColumns.Count() == 0, "need primary key"); return await this._Context.Fastest().AS(_AsName).BulkUpdateAsync(UpdateList.Select(it => it.Item).ToList(), pkColumns, UpdateColumns); } } } }