126 lines
4.9 KiB
C#
126 lines
4.9 KiB
C#
|
|
using System.Data;
|
||
|
|
using Microsoft.Data.SqlClient;
|
||
|
|
|
||
|
|
namespace YY.Admin.Services.Service.EquipmentDb;
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// 开炼机动作中间表 MCSToMES_Act_Mill 只读查询(直连设备库)。
|
||
|
|
/// </summary>
|
||
|
|
public interface IMcsActMillService
|
||
|
|
{
|
||
|
|
Task<(IReadOnlyList<McsActMillRow> Records, long Total)> PageAsync(
|
||
|
|
int pageNo,
|
||
|
|
int pageSize,
|
||
|
|
string? actName = null,
|
||
|
|
int? actAddr = null,
|
||
|
|
CancellationToken cancellationToken = default);
|
||
|
|
}
|
||
|
|
|
||
|
|
public sealed class McsActMillRow
|
||
|
|
{
|
||
|
|
public string? Guid { get; set; }
|
||
|
|
public int? ActAddr { get; set; }
|
||
|
|
public string? ActName { get; set; }
|
||
|
|
public string? ActNameEn { get; set; }
|
||
|
|
public string? ActMemo { get; set; }
|
||
|
|
public int? ActRepaddr { get; set; }
|
||
|
|
public DateTime? WriteTime { get; set; }
|
||
|
|
public DateTime? ReadTime { get; set; }
|
||
|
|
public int? RwFlag { get; set; }
|
||
|
|
}
|
||
|
|
|
||
|
|
public sealed class McsActMillService : IMcsActMillService
|
||
|
|
{
|
||
|
|
private const string TableName = "MCSToMES_Act_Mill";
|
||
|
|
private readonly IEquipmentDbConnectionFactory _factory;
|
||
|
|
|
||
|
|
public McsActMillService(IEquipmentDbConnectionFactory factory)
|
||
|
|
{
|
||
|
|
_factory = factory;
|
||
|
|
}
|
||
|
|
|
||
|
|
public async Task<(IReadOnlyList<McsActMillRow> Records, long Total)> PageAsync(
|
||
|
|
int pageNo,
|
||
|
|
int pageSize,
|
||
|
|
string? actName = null,
|
||
|
|
int? actAddr = null,
|
||
|
|
CancellationToken cancellationToken = default)
|
||
|
|
{
|
||
|
|
if (pageNo < 1) pageNo = 1;
|
||
|
|
if (pageSize < 1) pageSize = 20;
|
||
|
|
|
||
|
|
_factory.Reload();
|
||
|
|
await using var conn = _factory.CreateConnection();
|
||
|
|
await conn.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||
|
|
|
||
|
|
var where = new List<string>();
|
||
|
|
var parameters = new List<SqlParameter>();
|
||
|
|
if (!string.IsNullOrWhiteSpace(actName))
|
||
|
|
{
|
||
|
|
where.Add("act_name LIKE @actName");
|
||
|
|
parameters.Add(new SqlParameter("@actName", SqlDbType.NVarChar, 50) { Value = "%" + actName.Trim() + "%" });
|
||
|
|
}
|
||
|
|
if (actAddr.HasValue)
|
||
|
|
{
|
||
|
|
where.Add("act_addr = @actAddr");
|
||
|
|
parameters.Add(new SqlParameter("@actAddr", SqlDbType.Int) { Value = actAddr.Value });
|
||
|
|
}
|
||
|
|
var whereSql = where.Count == 0 ? "" : " WHERE " + string.Join(" AND ", where);
|
||
|
|
|
||
|
|
var countSql = $"SELECT COUNT(1) FROM [{TableName}]{whereSql}";
|
||
|
|
EquipmentDbWhitelist.ValidateSql(countSql, TableName);
|
||
|
|
long total;
|
||
|
|
await using (var countCmd = conn.CreateCommand())
|
||
|
|
{
|
||
|
|
countCmd.CommandText = countSql;
|
||
|
|
countCmd.Parameters.AddRange(CloneParams(parameters));
|
||
|
|
var scalar = await countCmd.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false);
|
||
|
|
total = Convert.ToInt64(scalar ?? 0);
|
||
|
|
}
|
||
|
|
|
||
|
|
var offset = (pageNo - 1) * pageSize;
|
||
|
|
var listSql = $@"
|
||
|
|
SELECT [GUID], [act_addr], [act_name], [act_name_en], [act_memo], [act_Repaddr], [WriteTime], [ReadTime], [RW_Flag]
|
||
|
|
FROM [{TableName}]{whereSql}
|
||
|
|
ORDER BY [WriteTime] DESC, [GUID]
|
||
|
|
OFFSET @offset ROWS FETCH NEXT @pageSize ROWS ONLY";
|
||
|
|
EquipmentDbWhitelist.ValidateSql(listSql, TableName);
|
||
|
|
|
||
|
|
var rows = new List<McsActMillRow>();
|
||
|
|
await using (var listCmd = conn.CreateCommand())
|
||
|
|
{
|
||
|
|
listCmd.CommandText = listSql;
|
||
|
|
listCmd.Parameters.AddRange(CloneParams(parameters));
|
||
|
|
listCmd.Parameters.Add(new SqlParameter("@offset", SqlDbType.Int) { Value = offset });
|
||
|
|
listCmd.Parameters.Add(new SqlParameter("@pageSize", SqlDbType.Int) { Value = pageSize });
|
||
|
|
await using var reader = await listCmd.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);
|
||
|
|
while (await reader.ReadAsync(cancellationToken).ConfigureAwait(false))
|
||
|
|
{
|
||
|
|
rows.Add(new McsActMillRow
|
||
|
|
{
|
||
|
|
Guid = reader["GUID"] as string,
|
||
|
|
ActAddr = reader["act_addr"] == DBNull.Value ? null : Convert.ToInt32(reader["act_addr"]),
|
||
|
|
ActName = reader["act_name"] as string,
|
||
|
|
ActNameEn = reader["act_name_en"] as string,
|
||
|
|
ActMemo = reader["act_memo"] as string,
|
||
|
|
ActRepaddr = reader["act_Repaddr"] == DBNull.Value ? null : Convert.ToInt32(reader["act_Repaddr"]),
|
||
|
|
WriteTime = reader["WriteTime"] == DBNull.Value ? null : Convert.ToDateTime(reader["WriteTime"]),
|
||
|
|
ReadTime = reader["ReadTime"] == DBNull.Value ? null : Convert.ToDateTime(reader["ReadTime"]),
|
||
|
|
RwFlag = reader["RW_Flag"] == DBNull.Value ? null : Convert.ToInt32(reader["RW_Flag"]),
|
||
|
|
});
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
return (rows, total);
|
||
|
|
}
|
||
|
|
|
||
|
|
private static SqlParameter[] CloneParams(List<SqlParameter> source)
|
||
|
|
{
|
||
|
|
return source.Select(p => new SqlParameter(p.ParameterName, p.SqlDbType)
|
||
|
|
{
|
||
|
|
Size = p.Size,
|
||
|
|
Value = p.Value ?? DBNull.Value
|
||
|
|
}).ToArray();
|
||
|
|
}
|
||
|
|
}
|