using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using PMSWPF.Data.Entities;
using PMSWPF.Extensions;
using PMSWPF.Helper;
using PMSWPF.Models;
using SqlSugar;
namespace PMSWPF.Data.Repositories;
///
/// VariableData仓储类,用于操作DbVariableData实体
///
public class VarDataRepository
{
///
/// 根据ID获取VariableData
///
/// 主键ID
///
public async Task GetByIdAsync(int id)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await _db.Queryable()
.In(id)
.SingleAsync();
stopwatch.Stop();
NlogHelper.Info($"根据ID '{id}' 获取VariableData耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
///
/// 获取所有VariableData
///
///
public async Task> GetAllAsync()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await _db.Queryable()
.Includes(d => d.VariableTable)
.Includes(d => d.VariableTable.Device)
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"获取所有VariableData耗时:{stopwatch.ElapsedMilliseconds}ms");
return result.Select(d => d.CopyTo())
.ToList();
}
}
///
/// 获取所有VariableData
///
///
public async Task> GetByVariableTableId(int varTableId)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await _db.Queryable()
.Where(d => d.VariableTableId == varTableId)
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"获取变量表的所有变量{result.Count()}个耗时:{stopwatch.ElapsedMilliseconds}ms");
return result.Select(d=>d.CopyTo()).ToList();
}
}
///
/// 新增VariableData
///
/// VariableData实体
///
public async Task AddAsync(VariableData variableData)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var db = DbContext.GetInstance())
{
var varData = await AddAsync(variableData, db);
stopwatch.Stop();
NlogHelper.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return varData;
}
}
///
/// 新增VariableData
///
/// VariableData实体
///
public async Task AddAsync(VariableData variableData, SqlSugarClient db)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var dbVarData = await db.Insertable(variableData.CopyTo())
.ExecuteReturnEntityAsync();
stopwatch.Stop();
NlogHelper.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return dbVarData.CopyTo();
}
///
/// 新增VariableData
///
/// VariableData实体
///
public async Task AddAsync(IEnumerable variableDatas)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var db = DbContext.GetInstance())
{
var varData = await AddAsync(variableDatas, db);
stopwatch.Stop();
NlogHelper.Info($"新增VariableData '{variableDatas.Count()}'个, 耗时:{stopwatch.ElapsedMilliseconds}ms");
return varData;
}
}
///
/// 新增VariableData
///
/// VariableData实体
///
public async Task AddAsync(IEnumerable variableDatas, SqlSugarClient db)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Stopwatch stopwatch2 = new Stopwatch();
stopwatch2.Start();
var dbList = variableDatas.Select(vb => vb.CopyTo())
.ToList();
stopwatch2.Stop();
NlogHelper.Info($"复制 VariableData'{variableDatas.Count()}'个, 耗时:{stopwatch2.ElapsedMilliseconds}ms");
var res = await db.Insertable(dbList)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"新增VariableData '{variableDatas.Count()}'个, 耗时:{stopwatch.ElapsedMilliseconds}ms");
return res;
}
///
/// 更新VariableData
///
/// VariableData实体
///
public async Task UpdateAsync(VariableData variableData)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await _db.UpdateNav(variableData.CopyTo())
.Include(d => d.Mqtts)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"更新VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
///
/// 更新VariableData
///
/// VariableData实体
///
public async Task UpdateAsync(List variableDatas)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (var _db = DbContext.GetInstance())
{
var result = await UpdateAsync(variableDatas, _db);
stopwatch.Stop();
NlogHelper.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}
///
/// 更新VariableData
///
/// VariableData实体
///
public async Task UpdateAsync(List variableDatas, SqlSugarClient db)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
var dbVarDatas = variableDatas.Select(vd => vd.CopyTo());
var result = await db.UpdateNav(dbVarDatas.ToList())
.Include(d => d.Mqtts)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 删除VariableData
///
/// 主键ID
///
public async Task DeleteAsync(VariableData variableData)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using var db = DbContext.GetInstance();
var result = await DeleteAsync(variableData, db);
stopwatch.Stop();
NlogHelper.Info($"删除VariableData: '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 根据ID删除VariableData
///
/// 主键ID
///
public async Task DeleteAsync(VariableData variableData, SqlSugarClient db)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using var _db = DbContext.GetInstance();
var result = await _db.Deleteable()
.Where(d => d.Id == variableData.Id)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"删除VariableData: '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 删除VariableData
///
/// 主键ID
///
public async Task DeleteAsync(IEnumerable variableDatas)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using var db = DbContext.GetInstance();
var result = await DeleteAsync(variableDatas, db);
stopwatch.Stop();
NlogHelper.Info($"删除VariableData: '{variableDatas.Count()}'个 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
///
/// 根据ID删除VariableData
///
/// 主键ID
///
public async Task DeleteAsync(IEnumerable variableDatas, SqlSugarClient db)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using var _db = DbContext.GetInstance();
var dbList = variableDatas.Select(vd => vd.CopyTo())
.ToList();
var result = await _db.Deleteable(dbList)
.ExecuteCommandAsync();
stopwatch.Stop();
NlogHelper.Info($"删除VariableData: '{variableDatas.Count()}'个 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
}