using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using SqlSugar;
|
using WMS.Entity;
|
using WMS.IDAL;
|
|
namespace WMS.DAL
|
{
|
public class DbHelper<T> where T:BaseEntity,new()
|
{
|
private readonly SqlSugarScope _db;
|
|
public DbHelper(SqlSugarScope db)
|
{
|
_db = db;
|
}
|
|
#region sql
|
|
public int Cud(string sql, params SugarParameter[] parameters)
|
{
|
var i = _db.Ado.ExecuteCommand(sql,parameters);
|
return i;
|
}
|
public async Task<int> CudAsync(string sql, params SugarParameter[] parameters)
|
{
|
var i = await _db.Ado.ExecuteCommandAsync(sql, parameters);
|
return i;
|
}
|
public object GetList(string sql)
|
{
|
var i = _db.Ado.SqlQuery<object>(sql);
|
return i;
|
}
|
public List<T> GetModels(string sqlString)
|
{
|
var models = _db.Ado.SqlQuery<T>(sqlString);
|
return models;
|
}
|
|
public T GetModel(string sqlString)
|
{
|
var model = _db.Ado.SqlQuery<T>(sqlString);
|
return model.FirstOrDefault();
|
}
|
#endregion
|
|
#region sugar 新增
|
|
public int Add(T t)
|
{
|
return _db.Insertable(t).ExecuteCommand();
|
}
|
public async Task<int> AddAsync(T t)
|
{
|
return await _db.Insertable(t).ExecuteCommandAsync();
|
}
|
|
#endregion
|
|
#region sugar 编辑
|
|
public int Edit(T t)
|
{
|
return _db.Updateable(t).IgnoreColumns(m => new { m.CreateTime, m.CreateUser }).ExecuteCommand();
|
}
|
public async Task<int> EditAsync(T t)
|
{
|
return await _db.Updateable(t).IgnoreColumns(m => new { m.CreateTime, m.CreateUser }).ExecuteCommandAsync();
|
}
|
|
#endregion
|
|
#region sugar 删除
|
|
public int Remove(int id, int userId)
|
{
|
var data = _db.Queryable<T>().First(m => m.Id == id && m.IsDel == "0"); ;
|
data.IsDel = "1";
|
data.UpdateTime = DateTime.Now;
|
data.UpdateUser = userId;
|
return _db.Updateable(data).UpdateColumns(m => new { m.IsDel, m.UpdateUser, m.UpdateTime }).ExecuteCommand();
|
}
|
public async Task<int> RemoveAsync(int id, int userId = 0)
|
{
|
var data = await _db.Queryable<T>().FirstAsync(m => m.Id == id && m.IsDel == "0");
|
data.IsDel = "1";
|
data.UpdateTime = DateTime.Now;
|
data.UpdateUser = userId;
|
return await _db.Updateable(data).UpdateColumns(m => new { m.IsDel, m.UpdateUser, m.UpdateTime }).ExecuteCommandAsync();
|
}
|
|
public int Remove(T t, int userId = 0)
|
{
|
t.IsDel = "1";
|
t.UpdateTime = DateTime.Now;
|
t.UpdateUser = userId;
|
return _db.Updateable(t).UpdateColumns(m => new { m.IsDel, m.UpdateUser, m.UpdateTime }).ExecuteCommand();
|
}
|
public async Task<int> RemoveAsync(T t, int userId)
|
{
|
t.IsDel = "1";
|
t.UpdateTime = DateTime.Now;
|
t.UpdateUser = userId;
|
return await _db.Updateable(t).UpdateColumns(m => new { m.IsDel, m.UpdateUser, m.UpdateTime }).ExecuteCommandAsync();
|
}
|
public int RemoveAll(List<T> t, int userId)
|
{
|
foreach (var item in t)
|
{
|
item.IsDel = "1";
|
item.UpdateTime = DateTime.Now;
|
item.UpdateUser = userId;
|
}
|
|
return _db.Updateable(t).UpdateColumns(m => new { m.IsDel,m.UpdateUser,m.UpdateTime }).ExecuteCommand();
|
}
|
public async Task<int> RemoveAllAsync(List<T> t, int userId)
|
{
|
foreach (var item in t)
|
{
|
item.IsDel = "1";
|
item.UpdateTime = DateTime.Now;
|
item.UpdateUser = userId;
|
}
|
|
return await _db.Updateable(t).UpdateColumns(m => new { m.IsDel, m.UpdateUser, m.UpdateTime }).ExecuteCommandAsync();
|
}
|
|
#endregion
|
|
#region sugar 查询
|
|
public T GetOneById(int id)
|
{
|
var data = _db.Queryable<T>().First(m => m.Id == id && m.IsDel == "0");
|
return data;
|
}
|
public async Task<T> GetOneByIdAsync(int id)
|
{
|
var data = await _db.Queryable<T>().FirstAsync(m => m.Id == id && m.IsDel == "0");
|
return data;
|
}
|
public ISugarQueryable<T> GetAllAsync()
|
{
|
var data = _db.Queryable<T>().Where(m => m.IsDel == "0");
|
return data;
|
}
|
public ISugarQueryable<T> GetAllWhereAsync(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
|
{
|
var data = GetAllAsync().Where(predicate);
|
return data;
|
}
|
#endregion
|
|
|
|
}
|
}
|