From eed1c6b1cd255c275e7230896a9726b8e837724f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BD=98=E5=BB=BA=E4=B8=9C?= <617601767@qq.com> Date: Fri, 29 Dec 2023 15:54:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E6=8E=A5=E5=8F=A3=E5=BC=80?= =?UTF-8?q?=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/DeviceController.cs | 20 ++++- LanSheng/LanShengAPI/Program.cs | 2 + LanSheng/LanShengInterface/IDeviceService.cs | 7 ++ LanSheng/LanShengModel/DeviceError.cs | 3 + LanSheng/LanShengModel/DeviceErrorReport.cs | 4 +- LanSheng/LanShengService/DeviceService.cs | 79 ++++++++++++++++++- 6 files changed, 108 insertions(+), 7 deletions(-) diff --git a/LanSheng/LanShengAPI/Controllers/DeviceController.cs b/LanSheng/LanShengAPI/Controllers/DeviceController.cs index 502d338..d21e5e1 100644 --- a/LanSheng/LanShengAPI/Controllers/DeviceController.cs +++ b/LanSheng/LanShengAPI/Controllers/DeviceController.cs @@ -48,7 +48,8 @@ namespace LanShengAPI.Controllers } [HttpGet("ResetRunCount")] - public async Task ResetRunCount([FromQuery] string id) { + public async Task ResetRunCount([FromQuery] string id) + { try { await DeviceService.ResetRunCount(id); @@ -61,7 +62,8 @@ namespace LanShengAPI.Controllers } [HttpPost("GetDeviceDatas")] - public async Task GetDeviceData(string[] ids) { + public async Task GetDeviceData(string[] ids) + { try { return Ok(await DeviceService.GetDeviceData(ids)); @@ -84,5 +86,19 @@ namespace LanShengAPI.Controllers return BadRequest(ex.Message); } } + + [HttpPost("DeviceErrorReport")] + + public async Task DeviceErrorReport(DeviceErrorReport report) + { + try + { + return Ok(await DeviceService.GetDeviceErrorReport(report)); + } + catch (BadRequestException ex) + { + return BadRequest(ex.Message); + } + } } } diff --git a/LanSheng/LanShengAPI/Program.cs b/LanSheng/LanShengAPI/Program.cs index b690384..7919f8f 100644 --- a/LanSheng/LanShengAPI/Program.cs +++ b/LanSheng/LanShengAPI/Program.cs @@ -11,6 +11,8 @@ builder.Services.AddControllers().AddNewtonsoftJson(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver(); options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; + + options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; }).AddDapr(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); diff --git a/LanSheng/LanShengInterface/IDeviceService.cs b/LanSheng/LanShengInterface/IDeviceService.cs index 5f077c4..33e7a2a 100644 --- a/LanSheng/LanShengInterface/IDeviceService.cs +++ b/LanSheng/LanShengInterface/IDeviceService.cs @@ -31,5 +31,12 @@ namespace LanShengInterface Task ResetRunCount(string id = ""); Task> GetDeviceErrorPage(PageYearSearch search); + + /// + /// 获取设备故障报表 + /// + /// 报表筛选 + /// + Task GetDeviceErrorReport(DeviceErrorReport report); } } diff --git a/LanSheng/LanShengModel/DeviceError.cs b/LanSheng/LanShengModel/DeviceError.cs index 1828ef4..e9a7ad7 100644 --- a/LanSheng/LanShengModel/DeviceError.cs +++ b/LanSheng/LanShengModel/DeviceError.cs @@ -44,5 +44,8 @@ namespace LanShengModel [SplitField] [SugarColumn(IsNullable = true)] public DateTime? CreateDate { get; set; } + + [SugarColumn(IsIgnore = true)] + public int? Count{get;set;} } } diff --git a/LanSheng/LanShengModel/DeviceErrorReport.cs b/LanSheng/LanShengModel/DeviceErrorReport.cs index 612fe19..7c3ff59 100644 --- a/LanSheng/LanShengModel/DeviceErrorReport.cs +++ b/LanSheng/LanShengModel/DeviceErrorReport.cs @@ -1,3 +1,5 @@ +using LanShengModel; + /// /// 设备故障报表 /// @@ -41,5 +43,5 @@ public class DeviceErrorReport /// /// 数据集 /// - public Dictionary? Items { get; set; } + public IEnumerable? Items { get; set; } } \ No newline at end of file diff --git a/LanSheng/LanShengService/DeviceService.cs b/LanSheng/LanShengService/DeviceService.cs index 808da46..7d52d05 100644 --- a/LanSheng/LanShengService/DeviceService.cs +++ b/LanSheng/LanShengService/DeviceService.cs @@ -1280,12 +1280,83 @@ namespace LanShengService var TempUnits = new string[] { "year", "month", "day" }; if (!string.IsNullOrEmpty(report.Unit) && !TempUnits.Any(x => x == report.Unit.ToLower())) { - throw new ArgumentException("单元无效"); + throw new BadRequestException("单元无效"); } - - #endregion - + var TempQuery = Db.Queryable() + .WhereIF(report.ErrCodes != null, x => report.ErrCodes!.Any(q => q == x.ErrCode)) + .WhereIF(report.DeviceIds != null, x => report.DeviceIds!.Any(q => q == x.DeviceId)) + .Where(x => x.CreateDate >= report.StartDate && x.CreateDate <= report.EndDate); + DateTime TempStart = (DateTime)report.StartDate!; + DateTime TempEnd = (DateTime)report.EndDate!; + if (string.IsNullOrEmpty(report.Unit)) + { + report.Items = await TempQuery + .GroupBy(x => new { x.ErrCode, x.ErrMsg }) + .Select(x => new DeviceError + { + ErrCode = x.ErrCode, + ErrMsg = x.ErrMsg, + Count = SqlFunc.AggregateCount(x.ErrCode) + }) + .SplitTable(TempStart, TempEnd) + .ToListAsync(); + } + else if (report.Unit.ToLower() == "year") + { + var TempYears = TempEnd.Year - TempStart.Year + 1; + var TempYearArray = Enumerable.Range(0, TempYears).Select(x => Convert.ToDateTime(TempStart.AddYears(x).ToString("yyyy-MM-dd"))).ToList(); + var TempQueryYear = Db.Reportable(TempYearArray).ToQueryable(); + 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(); + 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") + { + 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(); + 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; } #endregion }