using MoYu.RemoteRequest.Extensions; using Newtonsoft.Json.Linq; using SafeCampus.Application.Manager.DeepelephManager; using SafeCampus.Application.Services.Business.Warn.Dto; using SafeCampus.Application.Services.Business.Warn.Service; using SimpleTool; using SafeCampus.Core.Extension; using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical; using SafeCampus.Application.Services.Business; using SafeCampus.Web.Core.Controllers.Application.Business.Dto; using System.Collections.Generic; using SafeCampus.Application.Services.Business.CameraInfoService; using OfficeOpenXml.FormulaParsing.Excel.Functions.Text; namespace SafeCampus.Web.Core.Controllers.Application.Business; /// /// 预警事件查询接口 /// [ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Order = 92, Tag = "预警事件")] [Route("/business/warn")] [RolePermission] public class WarnInfoController { private readonly IWarnInfoService _warnInfoService; private readonly IDeepelephManager _deepelephManager; private readonly ISimpleCacheService _simpleCacheService; private readonly IConfigService _configService; private readonly ICameraInfoService _cameraInfoService; private readonly ISysUserService _sysUserService; public WarnInfoController(IWarnInfoService warnInfoService, IDeepelephManager deepelephManager, ISimpleCacheService simpleCacheService, IConfigService configService, ICameraInfoService cameraInfoService, ISysUserService sysUserService) { _warnInfoService = warnInfoService; _deepelephManager = deepelephManager; _simpleCacheService = simpleCacheService; _configService = configService; _cameraInfoService = cameraInfoService; _sysUserService = sysUserService; } /// /// 删除预警信息 /// /// /// [HttpPost] public async Task DeleteInfoD(BaseIdListInput input) { return await _warnInfoService.DeleteBatch(input.Ids); } /// /// 编辑预警信息 /// /// /// public async Task UpdateInfoU(WarnInfoDto input) { return await _warnInfoService.Update(input); } /// /// 获取预警信息 /// /// /// public async Task GetInfo(long Id) { var model = await _warnInfoService.GetInfo(Id); var config = _simpleCacheService.Get>(SafeCampusConst.WarnGroup); if (!string.IsNullOrEmpty(model.Extend)) { if (string.IsNullOrEmpty(model.PersonId)) { var extend = JsonConvert.DeserializeObject(model.Extend); model.Extend = extend["age"] != null ? $"年龄:{extend["age"]};年龄置信度:{Convert.ToInt32(Convert.ToDecimal(extend["ageProb"]) * 100)}%;" : ""; } } if (model.AlarmType == "crowd") { model.Extend = $"人群聚集数量:{model.Count}"; } if (config!=null) { model.SceneName = config.FirstOrDefault(x => x.Subset.Any(xx => xx.Code == model.AlarmType) && x.CameraId.Contains(model.CameraId))?.Name; } try { var appSettings = App.GetOptionsMonitor(); var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/alarm/query" .SetBody(new { token = _deepelephManager.GetToken(), tenantCode = appSettings.TenantCode, poiId = appSettings.PoiId, alarmId = model.AlarmId }) .SetContentType("application/json") .PostAsAsync(); var json = JsonConvert.DeserializeObject(str); if ((bool)json["success"]) { if (json["data"]["videoUrl"] != null) { model.VideoUrl = json["data"]["videoUrl"].ToString(); } } } catch (SystemException) { } return model; } /// /// 获取预警信息列表 /// /// /// [AllowAnonymous] public async Task> GetList([FromQuery] WarnInfoSearch input) { var config = _simpleCacheService.Get>(SafeCampusConst.WarnGroup); var page= await _warnInfoService.GetList(input); if (config == null) return page; foreach (var item in page.List) { item.SceneName = config.FirstOrDefault(x => x.Subset.Any(xx => xx.Code == item.AlarmType) && x.CameraId.Contains(item.CameraId))?.Name; } return page; } /// /// 获取告警分组 /// /// public async Task GetWarnGroup(string cameraId) { var list = new List(); list = _simpleCacheService.Get>(SafeCampusConst.WarnGroup); if (list != null) { if (!string.IsNullOrEmpty(cameraId)) { return list.Where(x => x.CameraId.Contains(cameraId)).ToList(); } return list; } var basePath = AppContext.BaseDirectory;//获取项目目录 var json = basePath.CombinePath("SeedData", "Json", "WarnGroup.json");//获取文件路径 var dataString = FileHelper.ReadFile(json);//读取文件 list = JsonConvert.DeserializeObject>(dataString); _simpleCacheService.Set(SafeCampusConst.WarnGroup, list); return list.Where(x=>x.CameraId.Contains(cameraId)).ToList(); } /// /// 设置场景开关 /// /// public async Task SetWarnGroupDevice(ConfigJsonInput input) { var jsonList = JsonConvert.DeserializeObject>(input.ConfigJson); var oldlist = _simpleCacheService.Get>(SafeCampusConst.WarnGroup); foreach (var item in jsonList) { var model = oldlist.FirstOrDefault(x => x.Code == item.Code); if (model != null) model.Subset = item.Subset; } _simpleCacheService.Set(SafeCampusConst.WarnGroup, oldlist); return true; } /// /// 关联摄像头和告警分组 /// /// /// public async Task SetWarnGroupCamera(ConfigJsonCamera input) { var list = _simpleCacheService.Get>(SafeCampusConst.WarnGroup); var model = list.FirstOrDefault(x => x.Code == input.Code); if (model != null) { model.CameraId = input.CameraList; } _simpleCacheService.Set(SafeCampusConst.WarnGroup, list); return true; } /// /// 设置预警推送设置 /// /// public async Task SetWarnPushSetting(ConfigPush push) { var sysConfig=await _configService.GetByConfigKey(CateGoryConst.CONFIG_BIZ_DEFINE, BizConfigConst.BIZ_SMS_STATES); if (sysConfig != null) { await _configService.Edit(new ConfigEditInput { Category = CateGoryConst.CONFIG_BIZ_DEFINE, ConfigKey = BizConfigConst.BIZ_SMS_STATES, ConfigValue = push.PushState.ToString() }); } else { await _configService.Add(new ConfigAddInput { Category = CateGoryConst.CONFIG_BIZ_DEFINE, ConfigKey = BizConfigConst.BIZ_SMS_STATES, ConfigValue = push.PushState.ToString() }); } var sysConfigDay = await _configService.GetByConfigKey(CateGoryConst.CONFIG_BIZ_DEFINE, BizConfigConst.BIZ_SMS_STATES_DAY); if (sysConfigDay != null) { await _configService.Edit(new ConfigEditInput { Category = CateGoryConst.CONFIG_BIZ_DEFINE, ConfigKey = BizConfigConst.BIZ_SMS_STATES_DAY, ConfigValue = push.DaySum.ToString() }); } else { await _configService.Add(new ConfigAddInput { Category = CateGoryConst.CONFIG_BIZ_DEFINE, ConfigKey = BizConfigConst.BIZ_SMS_STATES_DAY, ConfigValue = push.DaySum.ToString() }); } return true; } /// /// 获取预警推送内容 /// /// public async Task GetWarnPushSetting() { var configPush = new ConfigPush { PushState = (await _configService.GetByConfigKey(CateGoryConst.CONFIG_BIZ_DEFINE, BizConfigConst.BIZ_SMS_STATES)) .ConfigValue.ToBoolean(), DaySum = (await _configService.GetByConfigKey(CateGoryConst.CONFIG_BIZ_DEFINE, BizConfigConst.BIZ_SMS_STATES_DAY)) .ConfigValue.ToInt() }; return configPush; } /// /// 预警统计-饼图 Alarm 今日告警情况,Hand今日处理情况 /// /// public async Task GetWarnStatistics() { //今日告警情况饼图 //今日处理情况 var startTime = DateTime.Now.Date; var entTime = DateTime.Now; var list = await _warnInfoService.GetListNoPage(new WarnInfoSearch { StartTick = startTime, EndTick = entTime }); var warnType = typeof(AlarmType).EnumToEnumModel(); var alarmStatistics = warnType.Select(x => new { Name = x.Description, Code=x.Name, Count = list.Count(xx => xx.AlarmType == x.Name) }).Where(x => x.Count > 0).ToList(); var numbers = new Dictionary { { 0, "未处理" }, { 1, "已处理" } }; var handStatistics = numbers.Select(x => new { Name = x.Value, Count = list.Count(xx => xx.WarnHand == x.Key) }).ToList(); return new { Alarm = alarmStatistics, Hand = handStatistics }; } /// /// 获取七天统计柱状图 /// /// public async Task GetWeekWarnStatistics() { var result = new StatistionModel { DataX = new List(), DataY = new List() }; var startTime = DateTime.Now.AddDays(-7).Date; var entTime = DateTime.Now; var list = await _warnInfoService.GetListNoPage(new WarnInfoSearch { StartTick = startTime, EndTick = entTime }); //var warnType = typeof(AlarmType).EnumToEnumModel(); var climb_over = new List(); var crowd = new List(); var fire_smoke = new List(); var visual_fence = new List(); var wander = new List(); for (DateTime i = startTime; i <= entTime;) { var itemList = list.Where(x => x.Tick.Year == i.Year && x.Tick.Month == i.Month && x.Tick.Day == i.Day) .ToList(); //var alarmStatistics = warnType.Where(x=>x.Value<=4).Select(x => new StatistionFunModel //{ // Name = x.Description, // Code=x.Name, // Count = itemList.Count(xx => xx.AlarmType == x.Name), // Date = i.ToString("MM-dd") //}).ToList(); result.DataX.Add(i.ToString("MM-dd")); climb_over.Add(itemList.Count(x=>x.AlarmType== "climb_over")); crowd.Add(itemList.Count(x=>x.AlarmType== "crowd")); fire_smoke.Add(itemList.Count(x=>x.AlarmType== "fire_smoke")); visual_fence.Add(itemList.Count(x=>x.AlarmType== "visual_fence")); wander.Add(itemList.Count(x=>x.AlarmType== "wander")); //result.Add(new StatistionModel //{ // Date = i.ToString("MM-dd"), // visual_fence = // //FunList = alarmStatistics //}); i = i.AddDays(1); } result.DataY.Add(new SeriesData { Name = AlarmType.climb_over.GetDescription(), Data = climb_over }); result.DataY.Add(new SeriesData { Name = AlarmType.crowd.GetDescription(), Data = crowd }); result.DataY.Add(new SeriesData { Name = AlarmType.fire_smoke.GetDescription(), Data = fire_smoke }); result.DataY.Add(new SeriesData { Name = AlarmType.visual_fence.GetDescription(), Data = visual_fence }); result.DataY.Add(new SeriesData { Name = AlarmType.wander.GetDescription(), Data = wander }); return result ; } /// /// 获取告警枚举列表 /// /// public async Task> GetAlarmType() { return await _warnInfoService.GetWarnType(); } /// /// 根据监控id获取大屏告警数据 /// /// [AllowAnonymous] public async Task GetLargeScreenData(string cameraId) { var startTime = DateTime.Now.AddDays(-7).Date; var entTime = DateTime.Now; var list=await _warnInfoService.GetListNoPage(new WarnInfoSearch { StartTick = startTime, EndTick = entTime, PageNum = 1, PageSize = 20, CameraId = cameraId }); var warnType = typeof(AlarmType).EnumToEnumModel(); var alarmStatistics = warnType.Select(x => new StatistionFunModel { Name = x.Description, Code = x.Name, Count = list.Count(xx => xx.AlarmType == x.Name), }).Where(x=>x.Count>0).ToList(); return new { item = list, statistion = alarmStatistics }; } /// /// 处理预警 /// /// public async Task HandWarn(HandWarnInput input) { return await _warnInfoService.HandWarn(input); } /// /// 获取首页基础数据 /// /// public async Task GetBaseStatistics() { var camera = await _cameraInfoService.GetCameraCount(new CameraSearch()); var warnCount = await _warnInfoService.GetWarnCount(new WarnInfoSearch()); var handCount = await _warnInfoService.GetWarnCount(new WarnInfoSearch{WarnHand = 1}); var tacherCount = await _sysUserService.Page(new UserPageInput{PageSize = 2,PageNum = 1,OrgId = 0}); return new { CameraCount= camera, WarnCount= warnCount, HandCount= handCount, TeacherCount= tacherCount.Total }; } /// /// 获取高危信息 /// /// public async Task GetHighRisk() { var warnlist = await _warnInfoService.GetWarnType(); var list = new List(); //var group = warnlist.GroupBy(x => new { x.AlarmType, x.AlarmTypeDesc }).ToList(); var startTime = DateTime.Now.AddDays(-7).Date; var entTime = DateTime.Now; foreach (var item in warnlist) { var model = new { Name = item.Name, Day =await _warnInfoService.GetWarnCount(new WarnInfoSearch { AlarmType = item.Code, EndTick = entTime, StartTick = new DateTime(entTime.Year,entTime.Month,entTime.Day).Date }), Week =await _warnInfoService.GetWarnCount(new WarnInfoSearch { AlarmType = item.Code,EndTick = entTime,StartTick = startTime}), Count =await _warnInfoService.GetWarnCount(new WarnInfoSearch{AlarmType = item.Code}) }; list.Add(model); } return list.Where(x=>x.Count>10).OrderByDescending(x=>x.Day).ToList(); } }