平安校园
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.
 
 
 
 
 
 

372 lines
13 KiB

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