增加多线程进行作业,并且分块日期范围

main
潘建东 11 months ago
parent 83513ec5f8
commit 0be30a7570

@ -100,5 +100,19 @@ namespace LanShengAPI.Controllers
return BadRequest(ex.Message); return BadRequest(ex.Message);
} }
} }
[HttpGet("DeviceErrorList")]
public async Task<ActionResult> DeviceErrorList()
{
try
{
return Ok(await DeviceService.GetErrorList());
}
catch (BadRequestException ex)
{
return BadRequest(ex.Message);
}
}
} }
} }

@ -38,5 +38,7 @@ namespace LanShengInterface
/// <param name="report">报表筛选</param> /// <param name="report">报表筛选</param>
/// <returns></returns> /// <returns></returns>
Task<DeviceErrorReport> GetDeviceErrorReport(DeviceErrorReport report); Task<DeviceErrorReport> GetDeviceErrorReport(DeviceErrorReport report);
Task<IEnumerable<DeviceError>> GetErrorList();
} }
} }

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace LanShengModel namespace LanShengModel
{ {
[SugarTable("LS_DeviceError_{year}{month}{day}")] [SugarTable("LS_DeviceError_{year}{month}{day}")]
[SplitTable(SplitType.Year)] [SplitTable(SplitType.Week)]
public class DeviceError public class DeviceError
{ {
/// <summary> /// <summary>
@ -45,6 +45,9 @@ namespace LanShengModel
[SugarColumn(IsNullable = true)] [SugarColumn(IsNullable = true)]
public DateTime? CreateDate { get; set; } public DateTime? CreateDate { get; set; }
[SugarColumn(IsIgnore = true)]
public string? CountDate { get; set; }
[SugarColumn(IsIgnore = true)] [SugarColumn(IsIgnore = true)]
public int? Count { get; set; } public int? Count { get; set; }
} }

@ -25,6 +25,9 @@ public class DeviceErrorReport
/// </summary> /// </summary>
public IEnumerable<string>? DeviceIds { get; set; } public IEnumerable<string>? DeviceIds { get; set; }
public IEnumerable<string>? GpsIds { get; set; }
/// <summary> /// <summary>
/// 开始日期 /// 开始日期
/// </summary> /// </summary>
@ -43,5 +46,21 @@ public class DeviceErrorReport
/// <summary> /// <summary>
/// 数据集 /// 数据集
/// </summary> /// </summary>
public IEnumerable<DeviceError>? Items { get; set; } public IEnumerable<DeviceErrorReportItem>? Items { get; set; }
}
public class DeviceErrorReportItem {
public DeviceErrorReportItem() {
Code = "";
Msg = "";
Counts = new Dictionary<string, int>();
}
public string Code { get; set; }
public string Msg { get; set; }
public Dictionary<string,int> Counts { get; set; }
public int Count { get; set; }
} }

@ -6,6 +6,7 @@ using Microsoft.Data.SqlClient;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NewLife.Reflection; using NewLife.Reflection;
using Newtonsoft.Json.Linq;
using SqlSugar; using SqlSugar;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -13,7 +14,9 @@ using System.Data;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Text.Json; using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
using ZhongLianModel;
namespace LanShengService namespace LanShengService
{ {
@ -906,124 +909,124 @@ namespace LanShengService
switch (deviceData.Data17) switch (deviceData.Data17)
{ {
case 2: case 2:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0002";
deviceData.ErrMsg = "加速过电流"; deviceData.ErrMsg = "门机故障-加速过电流";
break; break;
case 3: case 3:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0003";
deviceData.ErrMsg = "减速过电流"; deviceData.ErrMsg = "门机故障-减速过电流";
break; break;
case 4: case 4:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0004";
deviceData.ErrMsg = "恒速过电流"; deviceData.ErrMsg = "门机故障-恒速过电流";
break; break;
case 5: case 5:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0005";
deviceData.ErrMsg = "加速过电压"; deviceData.ErrMsg = "门机故障-加速过电压";
break; break;
case 6: case 6:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0006";
deviceData.ErrMsg = "减速过电压"; deviceData.ErrMsg = "门机故障-减速过电压";
break; break;
case 7: case 7:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0007";
deviceData.ErrMsg = "恒速过电压"; deviceData.ErrMsg = "门机故障-恒速过电压";
break; break;
case 8: case 8:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0008";
deviceData.ErrMsg = "控制电源异常"; deviceData.ErrMsg = "门机故障-控制电源异常";
break; break;
case 9: case 9:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0009";
deviceData.ErrMsg = "欠电压"; deviceData.ErrMsg = "门机故障-欠电压";
break; break;
case 10: case 10:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0010";
deviceData.ErrMsg = "变频器过载"; deviceData.ErrMsg = "门机故障-变频器过载";
break; break;
case 11: case 11:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0011";
deviceData.ErrMsg = "电机过载"; deviceData.ErrMsg = "门机故障-电机过载";
break; break;
case 12: case 12:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0012";
deviceData.ErrMsg = "输入缺相"; deviceData.ErrMsg = "门机故障-输入缺相";
break; break;
case 13: case 13:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0013";
deviceData.ErrMsg = "输出缺相"; deviceData.ErrMsg = "门机故障-输出缺相";
break; break;
case 14: case 14:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0014";
deviceData.ErrMsg = "模块过热"; deviceData.ErrMsg = "门机故障-模块过热";
break; break;
case 15: case 15:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0015";
deviceData.ErrMsg = "外部故障"; deviceData.ErrMsg = "门机故障-外部故障";
break; break;
case 16: case 16:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0016";
deviceData.ErrMsg = "通讯异常"; deviceData.ErrMsg = "门机故障-通讯异常";
break; break;
case 17: case 17:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0017";
deviceData.ErrMsg = "接触器异常"; deviceData.ErrMsg = "门机故障-接触器异常";
break; break;
case 18: case 18:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0018";
deviceData.ErrMsg = "电流检测异常"; deviceData.ErrMsg = "门机故障-电流检测异常";
break; break;
case 19: case 19:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0019";
deviceData.ErrMsg = "电机调谐异常"; deviceData.ErrMsg = "门机故障-电机调谐异常";
break; break;
case 20: case 20:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0020";
deviceData.ErrMsg = "编码器/PG卡异常"; deviceData.ErrMsg = "门机故障-编码器/PG卡异常";
break; break;
case 21: case 21:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0021";
deviceData.ErrMsg = "EPPROM读写异常"; deviceData.ErrMsg = "门机故障-EPPROM读写异常";
break; break;
case 22: case 22:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0022";
deviceData.ErrMsg = "变频器硬件故障"; deviceData.ErrMsg = "门机故障-变频器硬件故障";
break; break;
case 23: case 23:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0023";
deviceData.ErrMsg = "电机对地短路"; deviceData.ErrMsg = "门机故障-电机对地短路";
break; break;
case 26: case 26:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0026";
deviceData.ErrMsg = "累计运行时间到达"; deviceData.ErrMsg = "门机故障-累计运行时间到达";
break; break;
case 29: case 29:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0029";
deviceData.ErrMsg = "累计上电时间到达"; deviceData.ErrMsg = "门机故障-累计上电时间到达";
break; break;
case 30: case 30:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0030";
deviceData.ErrMsg = "掉载"; deviceData.ErrMsg = "门机故障-掉载";
break; break;
case 31: case 31:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0031";
deviceData.ErrMsg = "运行时PID反馈丢失"; deviceData.ErrMsg = "门机故障-运行时PID反馈丢失";
break; break;
case 40: case 40:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0040";
deviceData.ErrMsg = "CBC限流保护"; deviceData.ErrMsg = "门机故障-CBC限流保护";
break; break;
case 42: case 42:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0040";
deviceData.ErrMsg = "速度偏差过大"; deviceData.ErrMsg = "门机故障-速度偏差过大";
break; break;
case 43: case 43:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0043";
deviceData.ErrMsg = "过速度"; deviceData.ErrMsg = "门机故障-过速度";
break; break;
case 45: case 45:
deviceData.ErrCode = "门机故障"; deviceData.ErrCode = "MJ0045";
deviceData.ErrMsg = "电机过热"; deviceData.ErrMsg = "门机故障-电机过热";
break; break;
default: default:
break; break;
@ -1283,80 +1286,152 @@ namespace LanShengService
throw new BadRequestException("单元无效"); throw new BadRequestException("单元无效");
} }
#endregion #endregion
var TempQuery = Db.Queryable<DeviceError>() if ((report.CompanyIds != null && report.CompanyIds.Count() > 0) || (report.ProjectIds != null && report.ProjectIds.Count() > 0) || (report.DeviceIds != null && report.DeviceIds.Count() > 0)) {
.WhereIF(report.ErrCodes != null, x => report.ErrCodes!.Any(q => q == x.ErrCode)) var TempCompanyIds = report.CompanyIds;
.WhereIF(report.DeviceIds != null, x => report.DeviceIds!.Any(q => q == x.DeviceId)) var TempProjectIds = report.ProjectIds;
.Where(x => x.CreateDate >= report.StartDate && x.CreateDate <= report.EndDate); var TempDeviceIds = report.DeviceIds;
DateTime TempStart = (DateTime)report.StartDate!; var TempGpss = Db.Queryable<DeviceDO>()
DateTime TempEnd = (DateTime)report.EndDate!; .LeftJoin<ProjectDO>((x, y) => x.ProjectId == y.Id)
.WhereIF(TempCompanyIds != null, (x, y) => TempCompanyIds!.Contains(y.CompanyId))
.WhereIF(TempProjectIds != null, (x, y) => TempProjectIds!.Contains(x.ProjectId))
.WhereIF(TempDeviceIds != null, (x, y) => TempDeviceIds!.Contains(x.Id))
.Where((x, y) => x.Deleted == 0)
.Select((x,y)=> x.Gps!.ToString())
.ToList();
var TempGpsIds = new List<string>();
TempGpss.ForEach(v => {
try
{
var tempGps = JsonSerializer.Deserialize<IEnumerable<DeviceGpsDO>>(v!);
TempGpsIds.AddRange(tempGps!.Select(x => x.Id!));
}
catch {
}
});
report.GpsIds = TempGpsIds!;
}
var TempErrors = await GetDayError(report);
var TempErrorGroup = TempErrors.Select(x => x.ErrCode).Distinct();
var TempItems = new List<DeviceErrorReportItem>();
if (string.IsNullOrEmpty(report.Unit)) if (string.IsNullOrEmpty(report.Unit))
{ {
report.Items = await TempQuery foreach (var group in TempErrorGroup)
.GroupBy(x => new { x.ErrCode, x.ErrMsg }) {
TempItems.Add(new DeviceErrorReportItem
{
Code = group!,
Msg = TempErrors.First(x => x.ErrCode == group).ErrMsg!,
Count = TempErrors.Where(x => x.ErrCode == group).Select(x => x.Count).Sum().ToInt()
});
}
}
else if (report.Unit.ToLower() == "year")
{
var TempErrorDate = TempErrors.Select(x => x.Id!.Substring(0, 4)).Distinct();
foreach (var group in TempErrorGroup)
{
var item = new DeviceErrorReportItem
{
Code = group!,
Msg = TempErrors.First(x => x.ErrCode == group).ErrMsg!,
Count = TempErrors.Where(x => x.ErrCode == group).Select(x => x.Count).Sum().ToInt()
};
foreach (var date in TempErrorDate)
{
item.Counts.Add(date, TempErrors.Where(x => x.ErrCode == group && x.Id!.IndexOf(date) == 0).Select(x => x.Count).Sum().ToInt());
}
TempItems.Add(item);
}
}
else if (report.Unit.ToLower() == "month") {
var TempErrorDate = TempErrors.Select(x => x.Id!.Substring(0, 7)).Distinct();
foreach (var group in TempErrorGroup)
{
var item = new DeviceErrorReportItem
{
Code = group!,
Msg = TempErrors.First(x => x.ErrCode == group).ErrMsg!,
Count = TempErrors.Where(x => x.ErrCode == group).Select(x => x.Count).Sum().ToInt()
};
foreach (var date in TempErrorDate)
{
item.Counts.Add(date, TempErrors.Where(x => x.ErrCode == group && x.Id!.IndexOf(date) == 0).Select(x => x.Count).Sum().ToInt());
}
TempItems.Add(item);
}
}
else if (report.Unit.ToLower() == "day")
{
var TempErrorDate = TempErrors.Select(x => x.Id).Distinct();
foreach (var group in TempErrorGroup)
{
var item = new DeviceErrorReportItem
{
Code = group!,
Msg = TempErrors.First(x => x.ErrCode == group).ErrMsg!,
Count = TempErrors.Where(x => x.ErrCode == group).Select(x => x.Count).Sum().ToInt()
};
foreach (var date in TempErrorDate)
{
item.Counts.Add(date, TempErrors.Where(x => x.ErrCode == group && x.Id!.IndexOf(date) == 0).Select(x => x.Count).Sum().ToInt());
}
TempItems.Add(item);
}
}
report.Items = TempItems;
report.GpsIds = null;
return report;
}
private Task<List<DeviceError>> GetDayError(DeviceErrorReport report)
{
var TempStart = (DateTime)report.StartDate!;
var TempEnd = (DateTime)report.EndDate!;
var TempSpan = TempEnd - TempStart;
var TempDayCount = (int)TempSpan.TotalDays;
var TempDaySpan = 5;
var TempTasks = new Task<List<DeviceError>>[TempDayCount % TempDaySpan > 0 ? TempDayCount / TempDaySpan + 1 : TempDayCount / TempDaySpan];
for (var i = 0; i < TempTasks.Length; i++)
{
SqlSugarClient client = new SqlSugarClient(Db.CurrentConnectionConfig);
TempTasks[i] = client.Queryable<DeviceError>()
.WhereIF(report.ErrCodes != null && report.ErrCodes.Count() > 0, x => report.ErrCodes!.Contains(x.ErrCode))
.WhereIF(report.GpsIds != null && report.GpsIds.Count() > 0, x => report.GpsIds!.Contains(x.DeviceId!))
.Select(x => new DeviceError .Select(x => new DeviceError
{ {
ErrCode = x.ErrCode, ErrCode = x.ErrCode,
ErrMsg = x.ErrMsg, ErrMsg = x.ErrMsg,
Id = ((DateTime)x.CreateDate!).ToString("yyyy-MM-dd"),
Count = SqlFunc.AggregateCount(x.ErrCode) Count = SqlFunc.AggregateCount(x.ErrCode)
}) })
.SplitTable(TempStart, TempEnd) .GroupBy(x => new { x.ErrCode, x.ErrMsg, Date = ((DateTime)x.CreateDate!).ToString("yyyy-MM-dd") })
.SplitTable(TempStart.AddDays(i * TempDaySpan), TempStart.AddDays((i + 1) * TempDaySpan))
.OrderBy(x => x.Id)
.ToListAsync(); .ToListAsync();
} }
else if (report.Unit.ToLower() == "year") Task.WaitAll(TempTasks);
List<DeviceError> result = new List<DeviceError>();
foreach (var task in TempTasks)
{ {
var TempYears = TempEnd.Year - TempStart.Year + 1; result.AddRange(task.Result);
var TempYearArray = Enumerable.Range(0, TempYears).Select(x => Convert.ToDateTime(TempStart.AddYears(x).ToString("yyyy-MM-dd"))).ToList();
var TempQueryYear = Db.Reportable(TempYearArray).ToQueryable<DateTime>();
report.Items = TempQueryYear
.LeftJoin(TempQuery.SplitTable(TempStart, TempEnd), (x, y) => x.ColumnName.ToString("yyyy") == ((DateTime)y.CreateDate!).ToString("yyyy"))
.GroupBy((x, y) => new { x.ColumnName, y.ErrCode, y.ErrMsg })
.Select((x, y) => new DeviceError
{
ErrCode = y.ErrCode,
ErrMsg = y.ErrMsg,
Count = SqlFunc.AggregateCount(y.ErrCode),
Id = x.ColumnName.ToString("yyyy"),
CreateDate = x.ColumnName
})
.ToList().OrderBy(x => x.Id);
}
else if (report.Unit.ToLower() == "month")
{
var TempMonths = (TempEnd.Year - TempStart.Year) * 12 + (TempEnd.Month - TempStart.Month + 1);
var TempMonthArray = Enumerable.Range(0, TempMonths).Select(x => Convert.ToDateTime(TempStart.AddMonths(x).ToString("yyyy-MM-dd"))).ToList();
var TempQueryMonth = Db.Reportable(TempMonthArray).ToQueryable<DateTime>();
report.Items = TempQueryMonth
.LeftJoin(TempQuery.SplitTable(TempStart, TempEnd), (x, y) => x.ColumnName.ToString("yyyy-MM") == ((DateTime)y.CreateDate!).ToString("yyyy-MM"))
.GroupBy((x, y) => new { x.ColumnName, y.ErrCode, y.ErrMsg })
.Select((x, y) => new DeviceError
{
ErrCode = y.ErrCode,
ErrMsg = y.ErrMsg,
Count = SqlFunc.AggregateCount(y.ErrCode),
Id = x.ColumnName.ToString("yyyy-MM"),
CreateDate = x.ColumnName
})
.ToList().OrderBy(x => x.Id);
} }
else if (report.Unit.ToLower() == "day") return Task.FromResult(result);
{
var TempDays = (TempEnd - TempStart).TotalDays.ToInt();
var TempDayArray = Enumerable.Range(0, TempDays).Select(x => Convert.ToDateTime(TempStart.AddDays(x).ToString("yyyy-MM-dd"))).ToList();
var TempQueryDay = Db.Reportable(TempDayArray).ToQueryable<DateTime>();
report.Items = TempQueryDay
.LeftJoin(TempQuery.SplitTable(TempStart, TempEnd), (x, y) => x.ColumnName.ToString("yyyy-MM-dd") == ((DateTime)y.CreateDate!).ToString("yyyy-MM-dd"))
.GroupBy((x, y) => new { x.ColumnName, y.ErrCode, y.ErrMsg })
.Select((x, y) => new DeviceError
{
ErrCode = y.ErrCode,
ErrMsg = y.ErrMsg,
Count = SqlFunc.AggregateCount(y.ErrCode),
Id = x.ColumnName.ToString("yyyy-MM-dd"),
CreateDate = x.ColumnName
})
.ToList().OrderBy(x => x.Id);
} }
return report;
public async Task<IEnumerable<DeviceError>> GetErrorList() {
return await Db.Queryable<DeviceError>()
.Select(x => new DeviceError
{
ErrCode = x.ErrCode,
ErrMsg = x.ErrMsg
}).Distinct()
.SplitTable(DateTime.Now.AddYears(-1), DateTime.Now)
.ToListAsync();
} }
#endregion #endregion
} }

@ -26,6 +26,7 @@
<ProjectReference Include="..\..\Cache\CacheService\CacheService.csproj" /> <ProjectReference Include="..\..\Cache\CacheService\CacheService.csproj" />
<ProjectReference Include="..\..\CommonExtend\CommonExtend.csproj" /> <ProjectReference Include="..\..\CommonExtend\CommonExtend.csproj" />
<ProjectReference Include="..\..\CommonModel\CommonModel.csproj" /> <ProjectReference Include="..\..\CommonModel\CommonModel.csproj" />
<ProjectReference Include="..\..\ZhongLian\ZhongLianModel\ZhongLianModel.csproj" />
<ProjectReference Include="..\LanShengInterface\LanShengInterface.csproj" /> <ProjectReference Include="..\LanShengInterface\LanShengInterface.csproj" />
<ProjectReference Include="..\LanShengModel\LanShengModel.csproj" /> <ProjectReference Include="..\LanShengModel\LanShengModel.csproj" />
</ItemGroup> </ItemGroup>

@ -25,10 +25,41 @@ namespace ZhongLianModel
[SugarColumn(Length = 64, IsNullable = true)] [SugarColumn(Length = 64, IsNullable = true)]
public string? ProjectId { get; set; } public string? ProjectId { get; set; }
/// <summary>
/// GPS数组
/// </summary>
[SugarColumn(Length = 500, IsNullable = true, IsJson = true)]
public IEnumerable<DeviceGpsDO>? Gps { get; set; }
/// <summary> /// <summary>
/// 删除标识 /// 删除标识
/// </summary> /// </summary>
[SugarColumn(Length = 1, IsNullable = true)] [SugarColumn(Length = 1, IsNullable = true)]
public int? Deleted { get; set; } public int? Deleted { get; set; }
} }
public class DeviceGpsDO
{
/// <summary>
/// GPS编号
/// </summary>
public string? Id { get; set; }
/// <summary>
/// GPS名称
/// </summary>
public string? Name { get; set; }
/// <summary>
/// 设备编号
/// </summary>
public string? DeviceId { get; set; }
/// <summary>
/// 博瓦监控设备编号
/// </summary>
public string? Bw { get; set; }
public string? Bw1 { get; set; }
}
} }

Loading…
Cancel
Save