using SqlSugar;
|
using System.Collections.Generic;
|
using System.Linq;
|
using TProduct.HttpClient;
|
|
namespace TProduct.DAL
|
{
|
/// <summary>
|
/// 数据库访问基类(数据同步)
|
/// </summary>
|
/// <typeparam name="T">实体类类型</typeparam>
|
public partial class BaseLiteSqlDAL_DataSync<T> where T : Entity.BaseEntity, new()
|
{
|
/// <summary>
|
/// ID
|
/// </summary>
|
protected const string IDKey = "id";
|
|
/// <summary>
|
/// 构造函数
|
/// </summary>
|
public BaseLiteSqlDAL_DataSync()
|
{
|
this.TableName = EntityHelper.GetTableName<T>();
|
}
|
|
/// <summary>
|
/// 对象的表名
|
/// </summary>
|
public virtual string TableName { get; set; }
|
|
/// <summary>
|
/// 数据库连接
|
/// </summary>
|
public virtual ISqlSugarClient Connection
|
{
|
get
|
{
|
var connection = ConnectionFactory.CreateConnection<T>();
|
return connection;
|
}
|
}
|
|
#region Insert
|
|
/// <summary>
|
/// 插入指定对象到数据库中
|
/// </summary>
|
public virtual long Insert(T entity)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
var id = db.Insertable(entity).ExecuteReturnSnowflakeId();
|
|
if (id > 0 && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
var dto = GetByID(id);
|
HttpClientHelper.Build(this.TableName, "Insert@V1.0").Post<bool>(dto);
|
}
|
return id;
|
}
|
}
|
|
/// <summary>
|
/// 插入指定对象集合到数据库中
|
/// </summary>
|
public virtual bool Inserts(List<T> list)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
try
|
{
|
db.Ado.BeginTran();
|
var snowflakeIds = db.Insertable(list).ExecuteReturnSnowflakeIdList();
|
if (snowflakeIds.Count < list.Count)
|
{
|
db.Ado.RollbackTran();
|
return default;
|
}
|
if (snowflakeIds == null || snowflakeIds.Count < 1)
|
{
|
db.Ado.RollbackTran();
|
return default;
|
}
|
db.Ado.CommitTran();
|
|
if (TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
var dtos = GetByIds(snowflakeIds);
|
HttpClientHelper.Build(this.TableName, "Inserts@V1.0").Post<bool>(dtos);
|
}
|
return true;
|
}
|
catch
|
{
|
db.Ado.RollbackTran();
|
throw;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 插入并返回
|
/// </summary>
|
public virtual List<long> InsertsR(List<T> list)
|
{
|
if (list == null || list.Count() < 1)
|
return default;
|
using (ISqlSugarClient db = Connection)
|
{
|
try
|
{
|
db.Ado.BeginTran();
|
var snowflakeIds = db.Insertable(list).ExecuteReturnSnowflakeIdList();
|
if (snowflakeIds.Count < list.Count)
|
{
|
db.Ado.RollbackTran();
|
return default;
|
}
|
if (snowflakeIds == null || snowflakeIds.Count < 1)
|
{
|
db.Ado.RollbackTran();
|
return default;
|
}
|
|
db.Ado.CommitTran();
|
|
if (TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
var dtos = GetByIds(snowflakeIds);
|
HttpClientHelper.Build(this.TableName, "Inserts@V1.0").Post<bool>(dtos);
|
}
|
return snowflakeIds;
|
}
|
catch
|
{
|
db.Ado.RollbackTran();
|
throw;
|
}
|
}
|
}
|
|
#endregion
|
|
#region Update
|
|
/// <summary>
|
/// 更新对象属性到数据库中
|
/// </summary>
|
public virtual bool Update(T entity)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
var result = db.Updateable(entity).ExecuteCommandHasChange();
|
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
HttpClientHelper.Build(this.TableName, "Update@V1.0").Put<bool>(entity);
|
}
|
return result;
|
}
|
}
|
|
/// <summary>
|
/// 更新部分
|
/// </summary>
|
public virtual bool UpdatePart(T entity, List<string> pros)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
var result = db.Updateable(entity).UpdateColumns(pros.ToArray()).ExecuteCommandHasChange();//更新单 条根据ID
|
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
HttpClientHelper.Build(this.TableName, "Update@V1.0").Put<bool>(entity);
|
}
|
return result;
|
}
|
}
|
|
/// <summary>
|
/// 更新指定对象集合到数据库中
|
/// </summary>
|
public virtual bool Updates(List<T> list)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
try
|
{
|
db.Ado.BeginTran();
|
var result = db.Updateable(list).ExecuteCommandHasChange();
|
db.Ado.CommitTran();
|
|
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
HttpClientHelper.Build(this.TableName, "Updates@V1.0").Put<bool>(list);
|
}
|
return result;
|
}
|
catch
|
{
|
db.Ado.RollbackTran();
|
throw;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 批量更新部分
|
/// </summary>
|
public virtual bool UpdatesPart(List<T> list, List<string> pros)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
try
|
{
|
db.Ado.BeginTran();
|
var result = db.Updateable(list).UpdateColumns(pros.ToArray()).ExecuteCommandHasChange();
|
db.Ado.CommitTran();
|
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
HttpClientHelper.Build(this.TableName, "Updates@V1.0").Put<bool>(list);
|
}
|
return result;
|
}
|
catch
|
{
|
db.Ado.RollbackTran();
|
throw;
|
}
|
}
|
}
|
|
#endregion
|
|
#region Delete
|
|
/// <summary>
|
/// 从数据库中删除指定对象
|
/// </summary>
|
public virtual bool Delete(T entity)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
var result = db.Deleteable(entity).ExecuteCommand();
|
if (result > 0 && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
HttpClientHelper.Build(this.TableName, $"DeleteByID@V1.0?ID={entity.ID}").Delete<bool>();
|
}
|
return true;
|
}
|
}
|
|
/// <summary>
|
/// 从数据库中删除指定对象集合
|
/// </summary>
|
public virtual bool Deletes(List<T> list)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
try
|
{
|
db.Ado.BeginTran();
|
var result = db.Deleteable(list).ExecuteCommand();
|
db.Ado.CommitTran();
|
|
if (result > 0 && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
var ids = list.Select(x => x.ID);
|
var input = LongListHelper.ToString(ids);
|
HttpClientHelper.Build(this.TableName, $"DeleteByIds@V1.0?Ids={input}").Delete<bool>();
|
}
|
return true;
|
}
|
catch
|
{
|
db.Ado.RollbackTran();
|
throw;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 根据指定对象的ID,从数据库中删除指定对象
|
/// </summary>
|
public virtual bool DeleteByID(long ID)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
var result = db.Deleteable<T>(ID).ExecuteCommandHasChange();
|
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
HttpClientHelper.Build(this.TableName, $"DeleteByID@V1.0?ID={ID}").Delete<bool>();
|
}
|
return result;
|
}
|
}
|
|
/// <summary>
|
/// 通过id列表删除
|
/// </summary>
|
public virtual bool DeleteByIds(List<long> ids)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
try
|
{
|
db.Ado.BeginTran();
|
var result = db.Deleteable<T>(ids).ExecuteCommandHasChange(); //批量删除
|
db.Ado.CommitTran();
|
|
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
var input = LongListHelper.ToString(ids);
|
HttpClientHelper.Build(this.TableName, $"DeleteByIds@V1.0?Ids={input}").Delete<bool>();
|
}
|
return result;
|
}
|
catch
|
{
|
db.Ado.RollbackTran();
|
throw;
|
}
|
}
|
}
|
|
/// <summary>
|
/// 从数据库中删除所有对象
|
/// </summary>
|
public virtual bool DeleteAll()
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
try
|
{
|
db.Ado.BeginTran();
|
var result = db.Deleteable<T>().ExecuteCommandHasChange();
|
db.Ado.CommitTran();
|
|
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
|
{
|
HttpClientHelper.Build(this.TableName, $"DeleteAll@V1.0").Delete<bool>();
|
}
|
return result;
|
}
|
catch
|
{
|
db.Ado.RollbackTran();
|
throw;
|
}
|
}
|
}
|
#endregion
|
|
#region Query
|
|
/// <summary>
|
/// 返回数据库所有的对象集合
|
/// </summary>
|
public virtual List<T> GetAll()
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
return db.Queryable<T>()?.ToList();
|
}
|
}
|
|
/// <summary>
|
/// 查询数据库,返回指定ID的对象
|
/// </summary>
|
public virtual T GetByID(long id)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
return db.Queryable<T>().Single(x => x.ID == id);
|
}
|
}
|
|
/// <summary>
|
/// 通拓id列表获取
|
/// </summary>
|
/// <returns></returns>
|
public virtual List<T> GetByIds(List<long> ids)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
return db.Queryable<T>().Where(x => ids.Contains(x.ID))?.ToList();
|
}
|
}
|
|
#endregion
|
|
#region Count
|
|
/// <summary>
|
/// 获取数量
|
/// </summary>
|
public virtual long GetCount()
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
return db.Queryable<T>().Count();
|
}
|
}
|
|
#endregion
|
|
#region 分页数据
|
|
/// <summary>
|
/// 分页获取数据
|
/// </summary>
|
public virtual List<T> GetPageList(int PageIndex, int PageSize, ref int Total)
|
{
|
using (var conn = Connection)
|
{
|
int skip = 1;
|
if (PageIndex > 0)
|
{
|
skip = (PageIndex - 1) * PageSize + 1;
|
}
|
using (ISqlSugarClient db = Connection)
|
{
|
var page = db.Queryable<T>().ToPageList(PageIndex, PageSize, ref Total);
|
return page;
|
}
|
}
|
}
|
#endregion
|
|
|
/// <summary>
|
/// 表是否存在
|
/// </summary>
|
/// <returns></returns>
|
public bool IsExistTable()
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
//怎么获取sqlsugar实体类中的表名
|
//((SugarTable<T>)).TableName;
|
return db.DbMaintenance.IsAnyTable(this.TableName);
|
}
|
}
|
/// <summary>
|
/// 列是否存在
|
/// </summary>
|
/// <param name="TableName"></param>
|
/// <returns></returns>
|
public bool IsExistColumn(string columnName)
|
{
|
using (ISqlSugarClient db = Connection)
|
{
|
return db.DbMaintenance.IsAnyColumn(this.TableName, columnName);
|
}
|
}
|
|
/// <summary>
|
/// 添加列
|
/// </summary>
|
/// <param name="TableName"></param>
|
/// <returns></returns>
|
public bool AddColumnTEXT(string columnName)
|
{
|
DbColumnInfo col = new DbColumnInfo();
|
col.DbColumnName = columnName;
|
col.DataType = "TEXT";
|
using (ISqlSugarClient db = Connection)
|
{
|
return db.DbMaintenance.AddColumn(this.TableName, col);
|
}
|
}
|
|
/// <summary>
|
/// 添加列
|
/// </summary>
|
/// <param name="TableName"></param>
|
/// <returns></returns>
|
public bool AddColumnInt(string columnName)
|
{
|
DbColumnInfo col = new DbColumnInfo();
|
col.DbColumnName = columnName;
|
col.DataType = "integer";
|
using (ISqlSugarClient db = Connection)
|
{
|
return db.DbMaintenance.AddColumn(this.TableName, col);
|
}
|
}
|
|
/// <summary>
|
/// 添加列
|
/// </summary>
|
/// <param name="TableName"></param>
|
/// <returns></returns>
|
public bool AddColumnFloat(string columnName)
|
{
|
DbColumnInfo col = new DbColumnInfo();
|
col.DbColumnName = columnName;
|
col.DataType = "real";
|
using (ISqlSugarClient db = Connection)
|
{
|
return db.DbMaintenance.AddColumn(this.TableName, col);
|
}
|
}
|
}
|
}
|