using SqlSugar;
using System.Collections.Generic;
using System.Linq;
using TProduct.HttpClient;
namespace TProduct.DAL
{
///
/// 测试记录
///
public class PumpNpshTestRecord
{
#region Insert
///
/// 插入指定对象到数据库中
///
public long Insert(int Year, Entity.PumpNpshTestRecord entity)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
var id = db.Insertable(entity).ExecuteReturnSnowflakeId();
if (id > 0 && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
{
var dto = db.Queryable().Single(x => x.ID == id);
HttpClientHelper.Build("PumpNpshTestRecord", "Insert@V1.0").Post(dto);
}
return id;
}
}
///
/// 插入指定对象集合到数据库中
///
public bool Inserts(int Year, List list)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
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 = db.Queryable().Where(x => snowflakeIds.Contains(x.ID));
HttpClientHelper.Build("PumpNpshTestRecord", "Inserts@V1.0").Post(dtos);
}
return true;
}
catch
{
db.Ado.RollbackTran();
throw;
}
}
}
///
/// 插入并返回
///
public List InsertsR(int Year, List list)
{
if (list == null || list.Count() < 1)
return default;
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
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 = db.Queryable().Where(x => snowflakeIds.Contains(x.ID));
HttpClientHelper.Build("PumpNpshTestRecord", "Inserts@V1.0").Post(dtos);
}
return snowflakeIds;
}
catch
{
db.Ado.RollbackTran();
throw;
}
}
}
#endregion
#region Update
///
/// 更新对象属性到数据库中
///
public bool Update(int Year, Entity.PumpNpshTestRecord entity)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
var result = db.Updateable(entity).ExecuteCommand() > 0;
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
{
HttpClientHelper.Build("PumpNpshTestRecord", "Update@V1.0").Put(entity);
}
return result;
}
}
///
/// 更新部分
///
public bool UpdatePart(int Year, Entity.PumpNpshTestRecord entity, List pros)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
var result = db.Updateable(entity).UpdateColumns(pros.ToArray()).ExecuteCommand() > 0;
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
{
HttpClientHelper.Build("PumpNpshTestRecord", "Update@V1.0").Put(entity);
}
return result;
}
}
///
/// 更新指定对象集合到数据库中
///
public bool Updates(int Year, List list)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
try
{
db.Ado.BeginTran();
var result = db.Updateable(list).ExecuteCommand() > 0;
db.Ado.CommitTran();
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
{
HttpClientHelper.Build("PumpNpshTestRecord", "Updates@V1.0").Put(list);
}
return result;
}
catch
{
db.Ado.RollbackTran();
throw;
}
}
}
///
/// 批量更新部分
///
public bool UpdatesPart(int Year, List list, List pros)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
try
{
db.Ado.BeginTran();
var result = db.Updateable(list).UpdateColumns(pros.ToArray()).ExecuteCommand() > 0;
db.Ado.CommitTran();
if (result && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
{
HttpClientHelper.Build("PumpNpshTestRecord", "Updates@V1.0").Put(list);
}
return result;
}
catch
{
db.Ado.RollbackTran();
throw;
}
}
}
#endregion
#region Delete
///
/// 从数据库中删除指定对象
///
public bool ClearAllRecord(int Year, long ItemID)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
var delList = db.Queryable().Where(it => it.TestItemID == ItemID).ToList();
if (delList != null && delList.Count > 0)
{
var result = db.Deleteable(delList).ExecuteCommand();
if (result > 0 && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
{
var ids = delList.Select(x => x.ID);
var input = LongListHelper.ToString(ids);
HttpClientHelper.Build("PumpNpshTestRecord", $"DeleteByIds@V1.0?Ids={input}").Delete();
}
}
return true;
}
}
///
/// 根据指定对象的ID,从数据库中删除指定对象
///
public bool DeleteByID(int Year, long ID)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
var delList = db.Queryable().Where(it => it.ID == ID).ToList();
if (delList != null && delList.Count > 0)
{
var result = db.Deleteable(delList).ExecuteCommand();
if (result > 0 && TProduct.CorpConfig.Instance.RealTimeRemoteService.IsSynMainData)
{
var ids = delList.Select(x => x.ID);
var input = LongListHelper.ToString(ids);
HttpClientHelper.Build("PumpNpshTestRecord", $"DeleteByIds@V1.0?Ids={input}").Delete();
}
}
return true;
}
}
#endregion
#region Query
///
/// 查询数据库,返回指定ID的对象
///
public List GetByTestItemID(int Year, long TestItemID)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
return db.Queryable().Where(x => x.TestItemID == TestItemID)?.ToList();
}
}
///
/// 查询数据库,返回指定ID的对象
///
public List GetAllByYear(int Year)
{
using (ISqlSugarClient db = TProduct.DAL.SQLite.ConfigHelper.RecordConn(Year))
{
return db.Queryable()?.ToList();
}
}
#endregion
}
}