平安校园
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WarnInfoController.cs 16 KiB

4 months ago
1 month ago
4 months ago
1 month ago
4 months ago
1 month ago
4 months ago
1 month ago
4 months ago
1 month ago
4 months ago
1 month ago
4 months ago
3 weeks ago
4 months ago
1 month ago
4 months ago
3 weeks ago
4 months ago
3 weeks ago
4 months ago
1 month ago
3 weeks ago
1 month ago
3 weeks ago
1 month ago
4 months ago
4 months ago
3 months ago
4 months ago
4 months ago
3 months ago
4 months ago
4 months ago
1 month ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
1 month ago
4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. using MoYu.RemoteRequest.Extensions;
  2. using Newtonsoft.Json.Linq;
  3. using SafeCampus.Application.Manager.DeepelephManager;
  4. using SafeCampus.Application.Services.Business.Warn.Dto;
  5. using SafeCampus.Application.Services.Business.Warn.Service;
  6. using SimpleTool;
  7. using SafeCampus.Core.Extension;
  8. using OfficeOpenXml.FormulaParsing.Excel.Functions.Logical;
  9. using SafeCampus.Application.Services.Business;
  10. using SafeCampus.Web.Core.Controllers.Application.Business.Dto;
  11. using System.Collections.Generic;
  12. using SafeCampus.Application.Services.Business.CameraInfoService;
  13. using OfficeOpenXml.FormulaParsing.Excel.Functions.Text;
  14. namespace SafeCampus.Web.Core.Controllers.Application.Business;
  15. /// <summary>
  16. /// 预警事件查询接口
  17. /// </summary>
  18. [ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Order = 92, Tag = "预警事件")]
  19. [Route("/business/warn")]
  20. [RolePermission]
  21. public class WarnInfoController
  22. {
  23. private readonly IWarnInfoService _warnInfoService;
  24. private readonly IDeepelephManager _deepelephManager;
  25. private readonly ISimpleCacheService _simpleCacheService;
  26. private readonly IConfigService _configService;
  27. private readonly ICameraInfoService _cameraInfoService;
  28. private readonly ISysUserService _sysUserService;
  29. public WarnInfoController(IWarnInfoService warnInfoService, IDeepelephManager deepelephManager, ISimpleCacheService simpleCacheService, IConfigService configService, ICameraInfoService cameraInfoService, ISysUserService sysUserService)
  30. {
  31. _warnInfoService = warnInfoService;
  32. _deepelephManager = deepelephManager;
  33. _simpleCacheService = simpleCacheService;
  34. _configService = configService;
  35. _cameraInfoService = cameraInfoService;
  36. _sysUserService = sysUserService;
  37. }
  38. /// <summary>
  39. /// 删除预警信息
  40. /// </summary>
  41. /// <param name="Id"></param>
  42. /// <returns></returns>
  43. [HttpPost]
  44. public async Task<bool> DeleteInfoD(BaseIdListInput input)
  45. {
  46. return await _warnInfoService.DeleteBatch(input.Ids);
  47. }
  48. /// <summary>
  49. /// 编辑预警信息
  50. /// </summary>
  51. /// <param name="input"></param>
  52. /// <returns></returns>
  53. public async Task<bool> UpdateInfoU(WarnInfoDto input)
  54. {
  55. return await _warnInfoService.Update(input);
  56. }
  57. /// <summary>
  58. /// 获取预警信息
  59. /// </summary>
  60. /// <param name="Id"></param>
  61. /// <returns></returns>
  62. public async Task<WarnInfoDto> GetInfo(long Id)
  63. {
  64. var model = await _warnInfoService.GetInfo(Id);
  65. var config = _simpleCacheService.Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup);
  66. if (!string.IsNullOrEmpty(model.Extend))
  67. {
  68. if (string.IsNullOrEmpty(model.PersonId))
  69. {
  70. var extend = JsonConvert.DeserializeObject<JObject>(model.Extend);
  71. model.Extend = extend["age"] != null ? $"年龄:{extend["age"]};年龄置信度:{Convert.ToInt32(Convert.ToDecimal(extend["ageProb"]) * 100)}%;" : "";
  72. }
  73. }
  74. if (model.AlarmType == "crowd")
  75. {
  76. model.Extend = $"人群聚集数量:{model.Count}";
  77. }
  78. if (config!=null)
  79. {
  80. model.SceneName = config.FirstOrDefault(x => x.Subset.Any(xx => xx.Code == model.AlarmType) && x.CameraId.Contains(model.CameraId))?.Name;
  81. }
  82. try
  83. {
  84. var appSettings = App.GetOptionsMonitor<AppInfoOptions>();
  85. var str = await $"{appSettings.SXAPIURL}/dfield-api/ecology/alarm/query"
  86. .SetBody(new
  87. {
  88. token = _deepelephManager.GetToken(),
  89. tenantCode = appSettings.TenantCode,
  90. poiId = appSettings.PoiId,
  91. alarmId = model.AlarmId
  92. })
  93. .SetContentType("application/json")
  94. .PostAsAsync<string>();
  95. var json = JsonConvert.DeserializeObject<JObject>(str);
  96. if ((bool)json["success"])
  97. {
  98. if (json["data"]["videoUrl"] != null)
  99. {
  100. model.VideoUrl = json["data"]["videoUrl"].ToString();
  101. }
  102. }
  103. }
  104. catch (SystemException)
  105. {
  106. }
  107. return model;
  108. }
  109. /// <summary>
  110. /// 获取预警信息列表
  111. /// </summary>
  112. /// <param name="input"></param>
  113. /// <returns></returns>
  114. [AllowAnonymous]
  115. public async Task<SqlSugarPagedList<WarnInfoList>> GetList([FromQuery] WarnInfoSearch input)
  116. {
  117. var config = _simpleCacheService.Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup);
  118. var page= await _warnInfoService.GetList(input);
  119. if (config == null) return page;
  120. foreach (var item in page.List)
  121. {
  122. item.SceneName = config.FirstOrDefault(x => x.Subset.Any(xx => xx.Code == item.AlarmType) && x.CameraId.Contains(item.CameraId))?.Name;
  123. }
  124. return page;
  125. }
  126. /// <summary>
  127. /// 获取告警分组
  128. /// </summary>
  129. /// <returns></returns>
  130. public async Task<dynamic> GetWarnGroup(string cameraId)
  131. {
  132. var list = new List<WarnGroupInfo>();
  133. list = _simpleCacheService.Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup);
  134. if (list != null)
  135. {
  136. if (!string.IsNullOrEmpty(cameraId))
  137. {
  138. return list.Where(x => x.CameraId.Contains(cameraId)).ToList();
  139. }
  140. return list;
  141. }
  142. var basePath = AppContext.BaseDirectory;//获取项目目录
  143. var json = basePath.CombinePath("SeedData", "Json", "WarnGroup.json");//获取文件路径
  144. var dataString = FileHelper.ReadFile(json);//读取文件
  145. list = JsonConvert.DeserializeObject<List<WarnGroupInfo>>(dataString);
  146. _simpleCacheService.Set(SafeCampusConst.WarnGroup, list);
  147. return list.Where(x=>x.CameraId.Contains(cameraId)).ToList();
  148. }
  149. /// <summary>
  150. /// 设置场景开关
  151. /// </summary>
  152. /// <returns></returns>
  153. public async Task<bool> SetWarnGroupDevice(ConfigJsonInput input)
  154. {
  155. var jsonList = JsonConvert.DeserializeObject<List<WarnGroupInfo>>(input.ConfigJson);
  156. var oldlist = _simpleCacheService.Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup);
  157. foreach (var item in jsonList)
  158. {
  159. var model = oldlist.FirstOrDefault(x => x.Code == item.Code);
  160. if (model != null) model.Subset = item.Subset;
  161. }
  162. _simpleCacheService.Set(SafeCampusConst.WarnGroup, oldlist);
  163. return true;
  164. }
  165. /// <summary>
  166. /// 关联摄像头和告警分组
  167. /// </summary>
  168. /// <param name="input"></param>
  169. /// <returns></returns>
  170. public async Task<bool> SetWarnGroupCamera(ConfigJsonCamera input)
  171. {
  172. var list = _simpleCacheService.Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup);
  173. var model = list.FirstOrDefault(x => x.Code == input.Code);
  174. if (model != null)
  175. {
  176. model.CameraId = input.CameraList;
  177. }
  178. _simpleCacheService.Set(SafeCampusConst.WarnGroup, list);
  179. return true;
  180. }
  181. /// <summary>
  182. /// 设置预警推送设置
  183. /// </summary>
  184. /// <returns></returns>
  185. public async Task<bool> SetWarnPushSetting(ConfigPush push)
  186. {
  187. var sysConfig=await _configService.GetByConfigKey(CateGoryConst.CONFIG_BIZ_DEFINE, BizConfigConst.BIZ_SMS_STATES);
  188. if (sysConfig != null)
  189. {
  190. await _configService.Edit(new ConfigEditInput
  191. {
  192. Category = CateGoryConst.CONFIG_BIZ_DEFINE,
  193. ConfigKey = BizConfigConst.BIZ_SMS_STATES,
  194. ConfigValue = push.PushState.ToString()
  195. });
  196. }
  197. else
  198. {
  199. await _configService.Add(new ConfigAddInput
  200. {
  201. Category = CateGoryConst.CONFIG_BIZ_DEFINE,
  202. ConfigKey = BizConfigConst.BIZ_SMS_STATES,
  203. ConfigValue = push.PushState.ToString()
  204. });
  205. }
  206. var sysConfigDay = await _configService.GetByConfigKey(CateGoryConst.CONFIG_BIZ_DEFINE, BizConfigConst.BIZ_SMS_STATES_DAY);
  207. if (sysConfigDay != null)
  208. {
  209. await _configService.Edit(new ConfigEditInput
  210. {
  211. Category = CateGoryConst.CONFIG_BIZ_DEFINE,
  212. ConfigKey = BizConfigConst.BIZ_SMS_STATES_DAY,
  213. ConfigValue = push.DaySum.ToString()
  214. });
  215. }
  216. else
  217. {
  218. await _configService.Add(new ConfigAddInput
  219. {
  220. Category = CateGoryConst.CONFIG_BIZ_DEFINE,
  221. ConfigKey = BizConfigConst.BIZ_SMS_STATES_DAY,
  222. ConfigValue = push.DaySum.ToString()
  223. });
  224. }
  225. return true;
  226. }
  227. /// <summary>
  228. /// 获取预警推送内容
  229. /// </summary>
  230. /// <returns></returns>
  231. public async Task<dynamic> GetWarnPushSetting()
  232. {
  233. var configPush = new ConfigPush
  234. {
  235. PushState = (await _configService.GetByConfigKey(CateGoryConst.CONFIG_BIZ_DEFINE, BizConfigConst.BIZ_SMS_STATES))
  236. .ConfigValue.ToBoolean(),
  237. DaySum = (await _configService.GetByConfigKey(CateGoryConst.CONFIG_BIZ_DEFINE, BizConfigConst.BIZ_SMS_STATES_DAY))
  238. .ConfigValue.ToInt()
  239. };
  240. return configPush;
  241. }
  242. /// <summary>
  243. /// 预警统计-饼图 Alarm 今日告警情况,Hand今日处理情况
  244. /// </summary>
  245. /// <returns></returns>
  246. public async Task<dynamic> GetWarnStatistics()
  247. {
  248. //今日告警情况饼图
  249. //今日处理情况
  250. var startTime = DateTime.Now.Date;
  251. var entTime = DateTime.Now;
  252. var list = await _warnInfoService.GetListNoPage(new WarnInfoSearch
  253. {
  254. StartTick = startTime,
  255. EndTick = entTime
  256. });
  257. var warnType = typeof(AlarmType).EnumToEnumModel();
  258. var alarmStatistics = warnType.Select(x => new
  259. {
  260. Name = x.Description,
  261. Code=x.Name,
  262. Count = list.Count(xx => xx.AlarmType == x.Name)
  263. }).Where(x => x.Count > 0).ToList();
  264. var numbers = new Dictionary<int, string> { { 0, "未处理" }, { 1, "已处理" } };
  265. var handStatistics = numbers.Select(x => new
  266. {
  267. Name = x.Value,
  268. Count = list.Count(xx => xx.WarnHand == x.Key)
  269. }).ToList();
  270. return new { Alarm = alarmStatistics, Hand = handStatistics };
  271. }
  272. /// <summary>
  273. /// 获取七天统计柱状图
  274. /// </summary>
  275. /// <returns></returns>
  276. public async Task<dynamic> GetWeekWarnStatistics()
  277. {
  278. var result = new StatistionModel
  279. {
  280. DataX = new List<string>(),
  281. DataY = new List<SeriesData>()
  282. };
  283. var startTime = DateTime.Now.AddDays(-7).Date;
  284. var entTime = DateTime.Now;
  285. var list = await _warnInfoService.GetListNoPage(new WarnInfoSearch
  286. {
  287. StartTick = startTime,
  288. EndTick = entTime
  289. });
  290. //var warnType = typeof(AlarmType).EnumToEnumModel();
  291. var climb_over = new List<int>();
  292. var crowd = new List<int>();
  293. var fire_smoke = new List<int>();
  294. var visual_fence = new List<int>();
  295. var wander = new List<int>();
  296. for (DateTime i = startTime; i <= entTime;)
  297. {
  298. var itemList = list.Where(x =>
  299. x.Tick.Year == i.Year && x.Tick.Month == i.Month && x.Tick.Day == i.Day)
  300. .ToList();
  301. //var alarmStatistics = warnType.Where(x=>x.Value<=4).Select(x => new StatistionFunModel
  302. //{
  303. // Name = x.Description,
  304. // Code=x.Name,
  305. // Count = itemList.Count(xx => xx.AlarmType == x.Name),
  306. // Date = i.ToString("MM-dd")
  307. //}).ToList();
  308. result.DataX.Add(i.ToString("MM-dd"));
  309. climb_over.Add(itemList.Count(x=>x.AlarmType== "climb_over"));
  310. crowd.Add(itemList.Count(x=>x.AlarmType== "crowd"));
  311. fire_smoke.Add(itemList.Count(x=>x.AlarmType== "fire_smoke"));
  312. visual_fence.Add(itemList.Count(x=>x.AlarmType== "visual_fence"));
  313. wander.Add(itemList.Count(x=>x.AlarmType== "wander"));
  314. //result.Add(new StatistionModel
  315. //{
  316. // Date = i.ToString("MM-dd"),
  317. // visual_fence =
  318. // //FunList = alarmStatistics
  319. //});
  320. i = i.AddDays(1);
  321. }
  322. result.DataY.Add(new SeriesData
  323. {
  324. Name = AlarmType.climb_over.GetDescription(),
  325. Data = climb_over
  326. });
  327. result.DataY.Add(new SeriesData
  328. {
  329. Name = AlarmType.crowd.GetDescription(),
  330. Data = crowd
  331. });
  332. result.DataY.Add(new SeriesData
  333. {
  334. Name = AlarmType.fire_smoke.GetDescription(),
  335. Data = fire_smoke
  336. });
  337. result.DataY.Add(new SeriesData
  338. {
  339. Name = AlarmType.visual_fence.GetDescription(),
  340. Data = visual_fence
  341. });
  342. result.DataY.Add(new SeriesData
  343. {
  344. Name = AlarmType.wander.GetDescription(),
  345. Data = wander
  346. });
  347. return result ;
  348. }
  349. /// <summary>
  350. /// 获取告警枚举列表
  351. /// </summary>
  352. /// <returns></returns>
  353. public async Task<List<WarnType>> GetAlarmType()
  354. {
  355. return await _warnInfoService.GetWarnType();
  356. }
  357. /// <summary>
  358. /// 根据监控id获取大屏告警数据
  359. /// </summary>
  360. /// <returns></returns>
  361. [AllowAnonymous]
  362. public async Task<dynamic> GetLargeScreenData(string cameraId)
  363. {
  364. var startTime = DateTime.Now.AddDays(-7).Date;
  365. var entTime = DateTime.Now;
  366. var list=await _warnInfoService.GetListNoPage(new WarnInfoSearch
  367. {
  368. StartTick = startTime,
  369. EndTick = entTime,
  370. PageNum = 1,
  371. PageSize = 20,
  372. CameraId = cameraId
  373. });
  374. var warnType = typeof(AlarmType).EnumToEnumModel();
  375. var alarmStatistics = warnType.Select(x => new StatistionFunModel
  376. {
  377. Name = x.Description,
  378. Code = x.Name,
  379. Count = list.Count(xx => xx.AlarmType == x.Name),
  380. }).Where(x=>x.Count>0).ToList();
  381. return new { item = list, statistion = alarmStatistics };
  382. }
  383. /// <summary>
  384. /// 处理预警
  385. /// </summary>
  386. /// <returns></returns>
  387. public async Task<bool> HandWarn(HandWarnInput input)
  388. {
  389. return await _warnInfoService.HandWarn(input);
  390. }
  391. /// <summary>
  392. /// 获取首页基础数据
  393. /// </summary>
  394. /// <returns></returns>
  395. public async Task<dynamic> GetBaseStatistics()
  396. {
  397. var camera = await _cameraInfoService.GetCameraCount(new CameraSearch());
  398. var warnCount = await _warnInfoService.GetWarnCount(new WarnInfoSearch());
  399. var handCount = await _warnInfoService.GetWarnCount(new WarnInfoSearch{WarnHand = 1});
  400. var tacherCount = await _sysUserService.Page(new UserPageInput{PageSize = 2,PageNum = 1,OrgId = 0});
  401. return new
  402. {
  403. CameraCount= camera,
  404. WarnCount= warnCount,
  405. HandCount= handCount,
  406. TeacherCount= tacherCount.Total
  407. };
  408. }
  409. /// <summary>
  410. /// 获取高危信息
  411. /// </summary>
  412. /// <returns></returns>
  413. public async Task<dynamic> GetHighRisk()
  414. {
  415. var warnlist = await _warnInfoService.GetWarnType();
  416. var list = new List<dynamic>();
  417. //var group = warnlist.GroupBy(x => new { x.AlarmType, x.AlarmTypeDesc }).ToList();
  418. var startTime = DateTime.Now.AddDays(-7).Date;
  419. var entTime = DateTime.Now;
  420. foreach (var item in warnlist)
  421. {
  422. var model = new
  423. {
  424. Name = item.Name,
  425. Day =await _warnInfoService.GetWarnCount(new WarnInfoSearch { AlarmType = item.Code, EndTick = entTime, StartTick = new DateTime(entTime.Year,entTime.Month,entTime.Day).Date }),
  426. Week =await _warnInfoService.GetWarnCount(new WarnInfoSearch { AlarmType = item.Code,EndTick = entTime,StartTick = startTime}),
  427. Count =await _warnInfoService.GetWarnCount(new WarnInfoSearch{AlarmType = item.Code})
  428. };
  429. list.Add(model);
  430. }
  431. return list.Where(x=>x.Count>10).OrderByDescending(x=>x.Day).ToList();
  432. }
  433. }