using Dapper; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; using XYY.Common.Standard; namespace XYY.Core.Standard.Data.Infrastructure { public interface IBaseRepository where T : class { Task ExecuteSqlNotTransferAsync(string sql); Task ExecuteSqlAsync(string sql); Task ExecuteSqlAsync(string sql, object param); Task InsertAsync(T t); Task InsertAsync(List t); Task InsertNotIdentityAsync(T t); Task ExecuteScalarAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null); /// /// 向没有主键、非自增表插入数据 /// /// Task InsertOperateLogAsync(T t); Task> QueryPager2Async(QueryModel queryModel); Task UpdateAsync(T t); Task UpdateByPrimaryKeyAsync(int pk, string field, object value); Task UpdateByPrimaryKeyAsync(Expression> expression, int id, object value); Task UpdateByConditionAsync(Expression> expression, string[] propertys, object[] values); Task UpdateByConditionAsync(Expression> expression, object t); Task UpdateBySqlAsync(Expression> expression, string sql); Task DeleteAsync(T t); Task DeleteAsync(Expression> expression); [NonTrans] Task GetAsync(int id); [NonTrans] Task IsExistsAsync(Expression> expression); [NonTrans] Task> QueryAsync(string sql, object param = null); [NonTrans] Task> QueryAsync(string sql, object param = null); [NonTrans] Task> QueryBySqlAsync(string sql); [NonTrans] Task> QueryBySqlAsync(string sql, object param); [NonTrans] Task> QueryProc(string procName, object param = null); [NonTrans] Task> QueryAsync(Expression> expression); [NonTrans] Task> QueryAsync(Expression> expression, string customSql); [NonTrans] Task> QueryAsync(); [NonTrans] Task> QueryPagerAsync(QueryModel queryModel); [NonTrans] Task> QueryPagerAsync(QueryModel queryModel, string customSql); [NonTrans] Task CountAsync(Expression> expression); [NonTrans] Task CountAsync(Expression> expression, QueryModel model); [NonTrans] Task> GroupCountAsync(Expression> propName, Expression> expression = null, QueryModel model = null); Task ExecuteStoredProcedureAsync(string procName, object param = null, int outtime = 60); Task BulkToDBAsync(List t); Task DeleteByIdAsync(int id); [NonTrans] string GetWhere(List paramer); Task GetDataTableAsync(string sql); } public abstract class BaseRepository : IBaseRepository where T : class { protected readonly IUnitOfWork _unitOfWork; public BaseRepository(IUnitOfWork unitOfWork) { _unitOfWork = unitOfWork; } public async Task ExecuteSqlNotTransferAsync(string sql) { using (var sqlConnection = new SqlConnection(_unitOfWork.ConnectionAddress)) { sqlConnection.Open(); var sqlCommand = new SqlCommand(sql, sqlConnection); return await sqlCommand.ExecuteNonQueryAsync(); } } public async Task GetDataTableAsync(string sql) { using (var sqlConnection = new SqlConnection(_unitOfWork.ConnectionAddress)) { DataTable table = new DataTable("MyTable"); sqlConnection.Open(); var dr = await sqlConnection.ExecuteReaderAsync(sql); table.Load(dr); return table; } } public async Task ExecuteSqlNotTransferAsync(string sql, object obj) { using (var sqlConnection = new SqlConnection(_unitOfWork.ConnectionAddress)) { sqlConnection.Open(); return await sqlConnection.ExecuteScalarAsync(sql, obj, null, null, CommandType.StoredProcedure); } } public async Task ExecuteSqlAsync(string sql) { return await _unitOfWork.Connection.ExecuteAsync(sql, null, _unitOfWork.Transaction); } public async Task ExecuteSqlAsync(string sql, object param) { return await _unitOfWork.Connection.ExecuteAsync(sql, param, _unitOfWork.Transaction); } public async Task ExecuteScalarAsync(string sql, object param = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null) { if (transaction == null) { transaction = _unitOfWork.Transaction; } return await _unitOfWork.Connection.ExecuteScalarAsync(sql, param, transaction, commandTimeout, commandType); } public virtual async Task GetAsync(int id) { return await _unitOfWork.GetAsync(id); } public virtual async Task> QueryAsync(string sql, object param = null) { Ensure.NotNullOrEmpty(sql, "sql"); return await _unitOfWork.Connection.QueryAsync(sql, param); } [NonTrans] public virtual async Task> QueryAsync(string sql, object param = null) { Ensure.NotNullOrEmpty(sql, "sql"); return await _unitOfWork.Connection.QueryAsync(sql, param); } public virtual async Task> QueryBySqlAsync(string sql) { Ensure.NotNullOrEmpty(sql, "sql"); return await _unitOfWork.QueryBySqlAsync(sql); } public virtual async Task> QueryBySqlAsync(string sql, object param) { Ensure.NotNullOrEmpty(sql, "sql"); return await _unitOfWork.QueryBySqlAsync(sql, param: param); } public virtual async Task> QueryProc(string procName, object param = null) { throw new Exception("暂不实现"); //Ensure.NotNullOrEmpty(procName, "procName"); //return await _unitOfWork.Connection.QueryAsync(sql: procName, // param: param, // transaction: _unitOfWork.Transaction, // buffered: true, // commandTimeout: null, // commandType: CommandType.StoredProcedure); } public virtual async Task QueryMultipleAsync(string sql, object param) { Ensure.NotNullOrEmpty(sql, "sql"); return await _unitOfWork.Connection.QueryMultipleAsync(sql, param); } public virtual async Task> QueryAsync(Expression> expression) { Ensure.NotNull(expression, "expression"); return await _unitOfWork.QueryAsync(expression); } public virtual async Task> QueryAsync(Expression> expression, string customSql) { Ensure.NotNull(expression, "expression"); if (string.IsNullOrEmpty(customSql)) return await this.QueryAsync(expression); return await _unitOfWork.QueryAsync(expression, customSql); } public virtual async Task> QueryPagerAsync(QueryModel queryModel) { Ensure.NotNull(queryModel, "queryModel"); Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex"); Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize"); return await _unitOfWork.GetPagingListAsync(queryModel); } public virtual async Task> QueryPager2Async(QueryModel queryModel) { Ensure.NotNull(queryModel, "queryModel"); Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex"); Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize"); return await _unitOfWork.GetPagingList2Async(queryModel); } public virtual async Task> QueryPagerAsync(QueryModel queryModel, string customSql) { if (string.IsNullOrEmpty(customSql.Trim())) return await QueryPagerAsync(queryModel); Ensure.NotNull(queryModel, "queryModel"); Ensure.GreaterThan(0, queryModel.PageIndex, "queryModel.PageIndex"); Ensure.GreaterThan(0, queryModel.PageSize, "queryModel.PageSize"); return await _unitOfWork.GetPagingListAsync(queryModel, customSql); } public virtual async Task IsExistsAsync(Expression> expression) { Ensure.NotNull(expression, "expression"); return await _unitOfWork.IsExistsAsync(expression); } public virtual async Task InsertAsync(T t) { Ensure.NotNull(t, "t"); return await _unitOfWork.InsertAsync(t); } public virtual async Task UpdateAsync(T t) { Ensure.NotNull(t, "t"); return await _unitOfWork.UpdateAsync(t); } public virtual async Task DeleteAsync(T t) { Ensure.NotNull(t, "t"); return await _unitOfWork.DeleteAsync(t); } public virtual async Task DeleteAsync(Expression> expression) { var t = await QueryAsync(expression); return await _unitOfWork.DeleteAsync(t.ToList()); } public virtual async Task DeleteByIdAsync(int id) { Ensure.GreaterThan(0, id, "id传递有误"); return await _unitOfWork.DeleteByIdAsync(id); } public virtual async Task InsertAsync(List t) { foreach (var item in t) { await InsertAsync(item); } return true; } public virtual async Task> QueryAsync() { return await _unitOfWork.QueryAsync(); } public virtual async Task UpdateByPrimaryKeyAsync(int pk, string field, object value) { Ensure.NotNullOrEmpty(field, "field"); return await _unitOfWork.UpdateByPrimaryKeyAsync(field, value, pk); } public async Task UpdateByPrimaryKeyAsync(Expression> expression, int id, object value) { return await _unitOfWork.UpdateByPrimaryKeyAsync(expression, id, value); } public async Task UpdateByConditionAsync(Expression> expression, string[] propertys, object[] values) { return await _unitOfWork.UpdateByConditionAsync(expression, propertys, values); } public async Task UpdateByConditionAsync(Expression> expression, object t) { return await _unitOfWork.UpdateByConditionAsync(expression, t); } public async Task UpdateByConditionNoTransferAsync(Expression> expression, object t) { return await _unitOfWork.UpdateByConditionNoTransferAsync(expression, t); } public async Task CountAsync(Expression> expression) { return await _unitOfWork.CountAsync(expression); } /// /// 执行存储过程 /// /// 存储过程名称 /// 参数列表 /// public async Task ExecuteStoredProcedureAsync(string procName, object param = null, int timeout = 60) { return await _unitOfWork.ExecuteStoredProcedureAsync(procName, param, timeout); } /// /// Id非自增表的数据插入 /// /// public async Task InsertNotIdentityAsync(T t) { await _unitOfWork.InsertNotIdentityAsync(t); return true; } /// /// 向没有主键、非自增表插入数据 /// /// public async Task InsertOperateLogAsync(T t) { await _unitOfWork.InsertOperateLogAsync(t); return true; } public virtual async Task BulkToDBAsync(List t) { return await _unitOfWork.BulkToDBAsync(t); } public string GetWhere(List paramer) { return _unitOfWork.GetWhere(paramer); } public async Task CountAsync(Expression> expression, QueryModel model) { return await _unitOfWork.CountAsync(expression, model); } public async Task> GroupCountAsync(Expression> propName, Expression> expression = null, QueryModel model = null) { return await _unitOfWork.GroupCountAsync(propName, expression, model); } public async Task UpdateBySqlAsync(Expression> expression, string sql) { return await _unitOfWork.UpdateByConditionAsync(expression, sql); } } }