From 846c921a65a95c8cd00478f8756a47ff8301d8bf Mon Sep 17 00:00:00 2001 From: yanghongwei Date: Fri, 14 Jun 2024 00:39:07 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A6=96=E9=A1=B5=EF=BC=8C=E6=8A=A5=E8=AD=A6?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=EF=BC=8C=E6=A3=80=E6=B5=8B=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- langguanApi.xml | 72 +++++++++++++++++++++- langguanApi/Controllers/AlertController.cs | 44 +++++++++++++ langguanApi/Model/Alert.cs | 10 +++ langguanApi/Model/Entity/Detection.cs | 30 +++++++++ langguanApi/Service/AlertService.cs | 55 +++++++++++------ langguanApi/Service/DetectionService.cs | 47 ++++++++++++++ langguanApi/Service/HomeService.cs | 38 +++++++++--- 7 files changed, 267 insertions(+), 29 deletions(-) create mode 100644 langguanApi/Controllers/AlertController.cs create mode 100644 langguanApi/Model/Entity/Detection.cs create mode 100644 langguanApi/Service/DetectionService.cs diff --git a/langguanApi.xml b/langguanApi.xml index a539827..c5c27f9 100644 --- a/langguanApi.xml +++ b/langguanApi.xml @@ -97,6 +97,21 @@ 需要解密的字符串 解密后的字符串 + + + export data + + + + + + + + add alert + + + + @@ -879,6 +894,36 @@ id + + + deviceId + + + + + deviceName + + + + + organizedName + + + + + keyword + + + + + standard + + + + + deviceValue + + 台账 @@ -1550,19 +1595,27 @@ 注册日期 - + 新加 - + 首页数据,最近7天的 + + + 导出数据 + + + + + 分页取数据 @@ -1723,6 +1776,19 @@ + + + //获取最新的数据 + + + + + + + 导出数据 + + + 新加 @@ -1914,7 +1980,7 @@ HomeService - + HomeService diff --git a/langguanApi/Controllers/AlertController.cs b/langguanApi/Controllers/AlertController.cs new file mode 100644 index 0000000..d97b87f --- /dev/null +++ b/langguanApi/Controllers/AlertController.cs @@ -0,0 +1,44 @@ +using langguanApi.Model; +using langguanApi.Service; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; + +namespace langguanApi.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class AlertController : ControllerBase + { + private readonly AlertService _alertService; + public AlertController(AlertService alertService) + { + _alertService = alertService; + } + /// + /// export data 默认最近7天数据 + /// + /// + /// + /// + [HttpGet("export")] + public async Task export(DateTime? start, DateTime? end) + { + start = start.HasValue ? start.Value.Date : DateTime.Now.AddDays(-7); + end = end.HasValue ? end.Value.Date.AddDays(1) : DateTime.Now.Date.AddDays(1); + var alerts = await _alertService.ExportData(start, end); + return File(alerts, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", + $"{start.Value.ToString("yyyy-MM-dd")}-{end.Value.ToString("yyyy-MM-dd")}_设备报警.xlsx"); + } + /// + /// add alert + /// + /// + /// + [HttpPost("TestAdd")] + public async Task TestAdd(AddAlertDto input) + { + var result = await _alertService.Add(input); + return Ok(result); + } + } +} diff --git a/langguanApi/Model/Alert.cs b/langguanApi/Model/Alert.cs index 35fa78c..3af781f 100644 --- a/langguanApi/Model/Alert.cs +++ b/langguanApi/Model/Alert.cs @@ -10,4 +10,14 @@ public string AlertType { get; set; } public string AlertContent { get; set; } } + public class AddAlertDto + { + public string DeviceId { get; set; } + public string DeviceMn { get; set; } + public string deviceName { get; set; } + public string DeviceType { get; set; } + public string DeviceStatus { get; set; } + public string AlertType { get; set; } + public string AlertContent { get; set; } + } } diff --git a/langguanApi/Model/Entity/Detection.cs b/langguanApi/Model/Entity/Detection.cs new file mode 100644 index 0000000..738434c --- /dev/null +++ b/langguanApi/Model/Entity/Detection.cs @@ -0,0 +1,30 @@ +namespace langguanApi.Model.Entity +{ + public class Detection : BaseModel + { + /// + /// deviceId + /// + public string DeviceId { get; set; } + /// + /// deviceName + /// + public string DeviceName { get; set; } + /// + /// organizedName + /// + public string OrganizedName { get; set; } + /// + /// keyword + /// + public string keyword { get; set; } + /// + /// standard + /// + public string Standard { get; set; } + /// + /// deviceValue + /// + public string deviceValue { get; set; } + } +} diff --git a/langguanApi/Service/AlertService.cs b/langguanApi/Service/AlertService.cs index ce099b2..eeac91e 100644 --- a/langguanApi/Service/AlertService.cs +++ b/langguanApi/Service/AlertService.cs @@ -2,6 +2,7 @@ using langguanApi.Model; using langguanApi.Model.Dto; using langguanApi.Model.Entity; +using Mapster; using Npoi.Mapper; using System.Linq.Expressions; @@ -18,11 +19,12 @@ namespace langguanApi.Service /// /// /// - public async Task Add(Alert input) + public async Task Add(AddAlertDto input) { if (input != null) { - await base.CreateAsync(input); + var entity = input.Adapt(); + await base.CreateAsync(entity); return new ApiResult { code = 0, msg = "" }; } return new ApiResult { code = -1, msg = "" }; ; @@ -31,28 +33,45 @@ namespace langguanApi.Service /// 首页数据,最近7天的 /// /// - public async Task IndexData() + public async Task IndexData(int num = 50) { - Expression> exp = filter => filter.CreateDateTime >= DateTime.Now.AddDays(-7) && filter.IsDelete == false; - return (await base.GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime); + Expression> exp = filter => filter.IsDelete == false; + return (await base.GetListWithExp(exp)) + .OrderByDescending(s => s.CreateDateTime) + .Take(num) + .Select(s => new + { + s.AlertContent, + s.deviceName, + s.CreateDateTime + }).ToList(); } - public async Task ExportData(DateTime start, DateTime end) + /// + ///导出数据 + /// + /// + /// + /// + public async Task ExportData(DateTime? start, DateTime? end) { + Expression> exp = filter => filter.CreateDateTime >= start && filter.CreateDateTime <= end && filter.IsDelete == false; - var list = await base.GetListWithExp(exp); - var mapper = new Mapper(); - mapper.Map("报警开始时间", s => s.CreateDateTime.ToString("yyyy-MM-dd HH:mm:ss")) - .Map("设备名称", s => s.deviceName) - // .Map("工序", s => s.) - .Map("新能源", s => s.NewCar ? "是" : "否") - .Map("燃油车", s => s.Emissions) - .Map("出厂日间 ", s => s.OutTime) - .Format("yyyy-MM-dd HH:mm:ss", s => s.CreateDateTime); - MemoryStream stream = new MemoryStream(); - mapper.Save(stream, list.ToList(), sheetName: "sheet1", leaveOpen: true); - return stream.ToArray(); + var list = (await base.GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime).ToList(); + if (list.Any()) + { + var mapper = new Mapper(); + mapper.Map("报警开始时间", s => s.CreateDateTime) + .Map("设备名称", s => s.deviceName) + // .Map("工序", s => s.) + .Map("内容", s => s.AlertContent) + .Format("yyyy-MM-dd HH:mm:ss", s => s.CreateDateTime); + MemoryStream stream = new MemoryStream(); + mapper.Save(stream, list, sheetName: "sheet1", leaveOpen: true); + return stream.ToArray(); + } + return null; } /// /// 分页取数据 diff --git a/langguanApi/Service/DetectionService.cs b/langguanApi/Service/DetectionService.cs new file mode 100644 index 0000000..45f48f8 --- /dev/null +++ b/langguanApi/Service/DetectionService.cs @@ -0,0 +1,47 @@ +using langguanApi.Extensions.AutoDI; +using langguanApi.Model; +using langguanApi.Model.Entity; +using Npoi.Mapper; +using System.Collections.Generic; +using System.Linq.Expressions; + +namespace langguanApi.Service +{ + [ServiceInjection(InjectionType.Transient)] + public class DetectionService : BaseService + { + public DetectionService(IConfiguration config) : base(config, nameof(Detection)) + { + } + /// + /// //获取最新的数据 + /// + /// + /// + public async Task GetIndexData(int num = 50) + { + Expression> exp = filter => true; + var result = (await GetListWithExp(exp)).OrderByDescending(s => s.CreateDateTime).Take(num).ToList(); + return result; + } + /// + /// 导出数据 + /// + /// + public async Task Export(DateTime start, DateTime end) + { + Expression> exp = filter => filter.CreateDateTime >= start && filter.CreateDateTime <= end; + var result = (await GetListWithExp(exp)).OrderBy(s => s.CreateDateTime).ToList(); + var mapper = new Mapper(); + mapper.Map("时间", s => s.CreateDateTime.ToString("yyyy-MM-dd HH:mm:ss")) + .Map("工序", s => s.OrganizedName) + .Map("设备名称", s => s.DeviceName) + .Map("项目", s => s.keyword) + .Map("检测值", s => s.deviceValue) + .Format("yyyy-MM-dd HH:mm:ss", s => s.CreateDateTime); + MemoryStream stream = new MemoryStream(); + mapper.Save(stream, result.ToList(), sheetName: "sheet1", leaveOpen: true); + return stream.ToArray(); + } + } +} diff --git a/langguanApi/Service/HomeService.cs b/langguanApi/Service/HomeService.cs index f9550a8..bf7b49e 100644 --- a/langguanApi/Service/HomeService.cs +++ b/langguanApi/Service/HomeService.cs @@ -18,6 +18,7 @@ namespace langguanApi.Service private CacheManager _cacheManager; private readonly WeatherService _weatherService; private readonly AlertService _alertService; + private readonly DetectionService _detectionService; /// /// HomeService /// @@ -25,7 +26,8 @@ namespace langguanApi.Service /// public HomeService(DeviceService device, Hj212Service hj212Service, IConfiguration configuration, CacheManager cacheManager, - WeatherService weatherService, AlertService alertService) + WeatherService weatherService, AlertService alertService, + DetectionService detectionService) { _deviceService = device; _hj212Service = hj212Service; @@ -33,6 +35,7 @@ namespace langguanApi.Service _cacheManager = cacheManager; _weatherService = weatherService; _alertService = alertService; + _detectionService = detectionService; } /// /// view @@ -46,16 +49,33 @@ namespace langguanApi.Service Yesterday = 0.8, LastWeek = 0.6, }; - + Expression> filter = exp => true; - var Realtime = await _hj212Service.Realtime(); - var getViewTop = await _hj212Service.GetViewTop(); + // 首页设备报警数据 var alerts = await _alertService.IndexData(); + // 首页设备检测数据 + var detections = await _detectionService.GetIndexData(); // 获取天气信息,缓存1小时,如果不存在,则调用WeatherService获取 Func> getWeatherFunc = async () => await _weatherService.GetWeather(); var weather = await _cacheManager.GetConvertVale(RedisKeylist.Weather, getWeatherFunc, 60 * 60); - Func> getAriQualityFunc= async () => await _weatherService.GetAirQuality(); + // 获取空气质量,缓存2小时,如果不存在,则调用WeatherService获取 + Func> getAriQualityFunc = async () => await _weatherService.GetAirQuality(); var ariQuality = await _cacheManager.GetConvertVale(RedisKeylist.AriQuality, getAriQualityFunc, 60 * 120); + + //首页清洁运输比例 + var cleaData = new + { + yesterday = 0.8, + lastWeek = 0.6 + }; + var day = new + { + day = new { voc = "70mg/m3", cemes = "100mg/m3" }, + week = new + { + + } + }; return new ApiResult { code = 0, @@ -71,11 +91,13 @@ namespace langguanApi.Service title = _configuration.GetValue("Home:Title"), }, devices, + cleanData, ariQuality, - Realtime, - getViewTop, + Realtime = "", + getViewTop = "", weather, - alerts + alerts, + detections } }; }