Merge pull request '完成接口开发' (#1) from 增加设备故障统计接口 into main

Reviewed-on: http://118.195.165.218:3000/panda/fycore/pulls/1
main
panda 11 months ago
commit 915ca83e77

@ -48,7 +48,8 @@ namespace LanShengAPI.Controllers
}
[HttpGet("ResetRunCount")]
public async Task<ActionResult> ResetRunCount([FromQuery] string id) {
public async Task<ActionResult> ResetRunCount([FromQuery] string id)
{
try
{
await DeviceService.ResetRunCount(id);
@ -61,7 +62,8 @@ namespace LanShengAPI.Controllers
}
[HttpPost("GetDeviceDatas")]
public async Task<ActionResult> GetDeviceData(string[] ids) {
public async Task<ActionResult> 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<ActionResult> DeviceErrorReport(DeviceErrorReport report)
{
try
{
return Ok(await DeviceService.GetDeviceErrorReport(report));
}
catch (BadRequestException ex)
{
return BadRequest(ex.Message);
}
}
}
}

@ -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();

@ -31,5 +31,12 @@ namespace LanShengInterface
Task ResetRunCount(string id = "");
Task<PageData<DeviceError>> GetDeviceErrorPage(PageYearSearch<DeviceError> search);
/// <summary>
/// 获取设备故障报表
/// </summary>
/// <param name="report">报表筛选</param>
/// <returns></returns>
Task<DeviceErrorReport> GetDeviceErrorReport(DeviceErrorReport report);
}
}

@ -44,5 +44,8 @@ namespace LanShengModel
[SplitField]
[SugarColumn(IsNullable = true)]
public DateTime? CreateDate { get; set; }
[SugarColumn(IsIgnore = true)]
public int? Count{get;set;}
}
}

@ -1,3 +1,5 @@
using LanShengModel;
/// <summary>
/// 设备故障报表
/// </summary>
@ -41,5 +43,5 @@ public class DeviceErrorReport
/// <summary>
/// 数据集
/// </summary>
public Dictionary<string, int>? Items { get; set; }
public IEnumerable<DeviceError>? Items { get; set; }
}

@ -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<DeviceError>()
.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<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")
{
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;
}
#endregion
}

Loading…
Cancel
Save