2025-07-03 12:55:00 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-07-03 13:53:29 +08:00
|
|
|
|
using System.Diagnostics;
|
2025-07-03 12:55:00 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2025-07-03 13:53:29 +08:00
|
|
|
|
using NLog;
|
2025-07-03 12:55:00 +08:00
|
|
|
|
using PMSWPF.Data.Entities;
|
|
|
|
|
|
using PMSWPF.Extensions;
|
|
|
|
|
|
using PMSWPF.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace PMSWPF.Data.Repositories;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// VariableData仓储类,用于操作DbVariableData实体
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class VarDataRepository
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
|
2025-07-03 12:55:00 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据ID获取VariableData
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">主键ID</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<DbVariableData> GetByIdAsync(int id)
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 12:55:00 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
var result = await _db.Queryable<DbVariableData>().In(id).SingleAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"根据ID '{id}' 获取VariableData耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 12:55:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取所有VariableData
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<List<VariableData>> GetAllAsync()
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 12:55:00 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
var result = await _db.Queryable<DbVariableData>().Select(dbVarData => dbVarData.CopyTo<VariableData>())
|
2025-07-03 12:55:00 +08:00
|
|
|
|
.ToListAsync();
|
2025-07-03 13:53:29 +08:00
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"获取所有VariableData耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 12:55:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 新增VariableData
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="variableData">VariableData实体</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<VariableData> AddAsync(VariableData variableData)
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 12:55:00 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
|
|
|
|
|
var dbVarData = await _db.Insertable(variableData.CopyTo<DbVariableData>()).ExecuteReturnEntityAsync();
|
2025-07-03 13:53:29 +08:00
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"新增VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
2025-07-03 12:55:00 +08:00
|
|
|
|
return dbVarData.CopyTo<VariableData>();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新VariableData
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="variableData">VariableData实体</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<int> UpdateAsync(VariableData variableData)
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 12:55:00 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
var result = await _db.Updateable(variableData.CopyTo<DbVariableData>()).ExecuteCommandAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"更新VariableData '{variableData.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 12:55:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-03 22:16:47 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 更新VariableData
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="variableData">VariableData实体</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<int> UpdateAsync(List<VariableData> variableDatas)
|
|
|
|
|
|
{
|
|
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
|
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
|
|
|
|
|
var dbVarDatas = variableDatas.Select(vd=>vd.CopyTo<DbVariableData>());
|
|
|
|
|
|
var result = await _db.Updateable(dbVarDatas.ToList()).ExecuteCommandAsync();
|
|
|
|
|
|
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"更新VariableData {variableDatas.Count()}个 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-07-03 12:55:00 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 根据ID删除VariableData
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="id">主键ID</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<int> DeleteAsync(int id)
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
Stopwatch stopwatch = new Stopwatch();
|
|
|
|
|
|
stopwatch.Start();
|
2025-07-03 12:55:00 +08:00
|
|
|
|
using (var _db = DbContext.GetInstance())
|
|
|
|
|
|
{
|
2025-07-03 13:53:29 +08:00
|
|
|
|
var result = await _db.Deleteable<DbVariableData>().In(id).ExecuteCommandAsync();
|
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
Logger.Info($"删除VariableData ID '{id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
|
|
|
|
|
return result;
|
2025-07-03 12:55:00 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|