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

VioAnalysisController.cs 15 KiB

1 month ago
1 month ago
3 weeks ago
1 month ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. using NPOI.Util;
  2. using NPOI.XWPF.UserModel;
  3. using SafeCampus.Application.Services.Business.Warn.Dto;
  4. using SafeCampus.Application.Services.Business.Warn.Service;
  5. using System.Diagnostics;
  6. using MoYu.FriendlyException;
  7. using SafeCampus.Core.Extension;
  8. namespace SafeCampus.Web.Core.Controllers.Application.Violation;
  9. /// <summary>
  10. /// 预警分析控制器
  11. /// </summary>
  12. [ApiDescriptionSettings(ApiGroupConsts.SYSTEM_Business, Tag = "预警分析", Order = 86)]
  13. [Route("violation/analysis")]
  14. [RolePermission]
  15. public class VioAnalysisController
  16. {
  17. private readonly ISimpleCacheService _simpleCacheService;
  18. private readonly IWarnInfoService _warnInfoService;
  19. public VioAnalysisController(ISimpleCacheService simpleCacheService, IWarnInfoService warnInfoService)
  20. {
  21. _simpleCacheService = simpleCacheService;
  22. _warnInfoService = warnInfoService;
  23. }
  24. //场景报告导出
  25. //导出查询条件
  26. //生成word
  27. public async Task<IActionResult> ReportExport(ReportExportInput input)
  28. {
  29. //var categories = new List<string>() { "Category A", "Category B", "Category C", "Category D" };
  30. //var values = new List<int>() { 215, 130, 245, 210 };
  31. //GenBar("XXX场景告警统计", categories, values, Path.Combine(Directory.GetCurrentDirectory(), "Template", $"{Guid.NewGuid():N}.png"));
  32. //GenPie("XXX场景告警统计", categories, values, Path.Combine(Directory.GetCurrentDirectory(), "Template", $"{Guid.NewGuid():N}.png"));
  33. //switch (input.GroupCode)
  34. //{
  35. // case "MCLZ":
  36. // return await GetMCLZ(input);
  37. // case "ZHKT":
  38. // return await GetZHKT(input);
  39. // case "XSGQ":
  40. // return await GetXSGQ(input);
  41. // case "XYFBL":
  42. // return await GetXYFBL(input);
  43. // case "XYAQ":
  44. // return await GetXYAQ(input);
  45. // case "QYGK":
  46. // return await GetQYGK(input);
  47. // case "ABXL":
  48. // return await GetABXL(input);
  49. // default:
  50. // throw Oops.Oh("请选择正确的场景");
  51. //}
  52. //PythonEngine.BeginAllowThreads();
  53. var result = await GetMCLZ(input);
  54. //PythonEngine.Shutdown();
  55. return result;
  56. }
  57. #region 图表生成
  58. private bool GenCharts(string title, List<string> label, List<int> value, string name,string pyName)
  59. {
  60. try
  61. {
  62. var script = Path.Combine(Directory.GetCurrentDirectory(), "Template", pyName);
  63. string args = $"\"{script}\" \"{name}\" \"{title}\" \"{string.Join(",", label)}\" \"{string.Join(",", value)}\"";
  64. ProcessStartInfo start = new ProcessStartInfo();
  65. start.FileName = App.Configuration["AppInfo:Python"]; // Python 解释器路径
  66. start.Arguments = args; // 参数列表
  67. start.UseShellExecute = false;
  68. start.RedirectStandardOutput = true;
  69. start.RedirectStandardError = true;
  70. // 启动进程
  71. using (Process process = Process.Start(start))
  72. {
  73. using (StreamReader reader = process.StandardOutput)
  74. {
  75. string result = reader.ReadToEnd();
  76. Console.Write(result);
  77. }
  78. using (StreamReader reader = process.StandardError)
  79. {
  80. string error = reader.ReadToEnd();
  81. LogHelper.WriteToLog("python执行错误", error);
  82. }
  83. }
  84. }
  85. catch (global::System.Exception)
  86. {
  87. return false;
  88. }
  89. return true;
  90. }
  91. /// <summary>
  92. /// 生成柱状图
  93. /// </summary>
  94. /// <param name="label"></param>
  95. /// <param name="value"></param>
  96. /// <param name="name"></param>
  97. /// <returns></returns>
  98. private bool GenBar(string title, List<string> label, List<int> value, string name)
  99. {
  100. try
  101. {
  102. //var m_threadState = PythonEngine.BeginAllowThreads();
  103. //using (Py.GIL())
  104. //{
  105. // dynamic plt = Py.Import("matplotlib.pyplot");
  106. // dynamic fm = Py.Import("matplotlib.font_manager");
  107. // dynamic prop = fm.FontProperties(fname: "C:\\Windows\\Fonts\\msyh.ttc");
  108. // plt.rcParams["font.family"] = prop.get_name();
  109. // //var categories = new List<string>() { "Category A", "Category B", "Category C", "Category D" };
  110. // //var values = new List<int>() { 215, 130, 245, 210 };
  111. // var bars = plt.bar(ToPythonList(label), ToPythonList(value));
  112. // foreach (var bar in bars)
  113. // {
  114. // double yval = bar.get_height();
  115. // plt.text(bar.get_x() + bar.get_width() / 2.5, yval, Math.Round(yval, 1), va: "bottom");
  116. // }
  117. // // 设置图表标题并显示图表
  118. // plt.title(title);
  119. // plt.savefig(name);
  120. // //plt.show();
  121. // plt.close();
  122. //}
  123. //PythonEngine.EndAllowThreads(m_threadState);
  124. }
  125. catch (global::System.Exception)
  126. {
  127. return false;
  128. }
  129. return true;
  130. }
  131. /// <summary>
  132. /// 生成饼图
  133. /// </summary>
  134. /// <param name="label"></param>
  135. /// <param name="value"></param>
  136. /// <param name="name"></param>
  137. /// <returns></returns>
  138. private bool GenPie(string title, List<string> label, List<int> value, string name)
  139. {
  140. try
  141. {
  142. //var m_threadState = PythonEngine.BeginAllowThreads();
  143. //using (Py.GIL())
  144. //{
  145. // dynamic plt = Py.Import("matplotlib.pyplot");
  146. // dynamic fm = Py.Import("matplotlib.font_manager");
  147. // dynamic prop = fm.FontProperties(fname: "C:\\Windows\\Fonts\\msyh.ttc");
  148. // plt.rcParams["font.family"] = prop.get_name();
  149. // //var categories = new List<string>() { "Category A", "Category B", "Category C", "Category D" };
  150. // //var values = new List<int>() { 215, 130, 245, 210 };
  151. // var color = new List<string>() { "gold", "yellowgreen", "lightcoral", "lightskyblue" };
  152. // var pie = plt.pie(ToPythonList(value), labels: ToPythonList(label), colors: ToPythonList(color), autopct: "%1.1f%%");
  153. // plt.axis("equal");
  154. // // 设置图表标题并显示图表
  155. // plt.title(title);
  156. // plt.savefig(name);
  157. // //plt.show();
  158. // plt.close();
  159. //}
  160. //PythonEngine.EndAllowThreads(m_threadState);
  161. }
  162. catch (global::System.Exception)
  163. {
  164. return false;
  165. }
  166. return true;
  167. }
  168. #endregion
  169. /// <summary>
  170. /// 获取明厨亮灶报告
  171. /// </summary>
  172. /// <returns></returns>
  173. private async Task<FileContentResult> GetMCLZ(ReportExportInput input)
  174. {
  175. var warnGroup = _simpleCacheService
  176. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  177. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  178. var warnList = await _warnInfoService.GetListNoPage(new WarnInfoSearch
  179. {
  180. StartTick = input.StartTime,
  181. EndTick = input.EndTime.AddDays(1).AddSeconds(-1),
  182. AlarmTypes = warnGroup.Subset.Where(x=>x.State).Select(x => x.Code).ToArray(),
  183. PersonSetIds = input.PersonSetIds,
  184. CameraIds = warnGroup.CameraId
  185. });
  186. var templatePath = Path.Combine(Directory.GetCurrentDirectory(), "Template", $"WordTemplate.docx");
  187. await using var fs = new FileStream(templatePath, FileMode.Open, FileAccess.ReadWrite);
  188. XWPFDocument doc = new XWPFDocument(fs);
  189. var content = warnList.GroupBy(x => x.AlarmTypeDesc).ToList();
  190. if (!content.Any())
  191. {
  192. throw Oops.Oh("该时间段无数据,无法生成报告!");
  193. }
  194. Dictionary<string, string> replacements = new Dictionary<string, string>()
  195. {
  196. //图片
  197. { "{{ImagePie}}","" },
  198. { "{{ImageBar}}","" },
  199. //场景名称
  200. { "{{GroupName}}", warnGroup.Name},
  201. //摄 像 头
  202. { "{{CameraName}}",string.Join("、",warnGroup.CameraName)},
  203. //统计时间
  204. { "{{StatitionTime}}", $"{input.StartTime:yyyy-MM-dd} 至 {input.EndTime:yyyy-MM-dd}"},
  205. //累计告警
  206. { "{{Count}}",warnList.Count.ToString()},
  207. //内容详情
  208. { "{{Content}}", $"按类型统计如下:{string.Join("、",content.Select(x=>$"{x.Key}:{x.Count()}条"))}"}
  209. };
  210. // 遍历文档中的所有段落
  211. foreach (XWPFParagraph para in doc.Paragraphs)
  212. {
  213. foreach (var kvp in replacements)
  214. {
  215. if (para.Text.Contains(kvp.Key)) // 检查是否为图片文件名
  216. {
  217. if (kvp.Key == "{{ImageBar}}")
  218. {
  219. var categories = content.Select(x => x.Key).ToList();//new List<string>() { "Category A", "Category B", "Category C", "Category D" });
  220. var values = content.Select(x => x.Count()).ToList();// new List<int>() { 215, 130, 245, 210 };
  221. var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Template",
  222. $"{Guid.NewGuid():N}.png");
  223. GenCharts($"{warnGroup.Name}场景告警统计", categories, values, filePath,"GenBar.py");
  224. await using var file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  225. XWPFRun run = para.CreateRun();
  226. using MemoryStream imgStream = new MemoryStream();
  227. await file.CopyToAsync(imgStream);
  228. imgStream.Seek(0, SeekOrigin.Begin);
  229. run.AddPicture(imgStream,
  230. (int)PictureType.JPEG, "image.jpg", Units.ToEMU(403.2), Units.ToEMU(300.8));
  231. para.RemoveRun(0); // 移除原来的占位符文本
  232. await file.DisposeAsync();
  233. File.Delete(filePath);
  234. }else if (kvp.Key == "{{ImagePie}}")
  235. {
  236. var categories = content.Select(x => x.Key).ToList();//new List<string>() { "Category A", "Category B", "Category C", "Category D" });
  237. var values = content.Select(x => x.Count()).ToList();// new List<int>() { 215, 130, 245, 210 };
  238. var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Template",
  239. $"{Guid.NewGuid():N}.png");
  240. GenCharts($"{warnGroup.Name}场景告警统计", categories, values, filePath,"GenPie.py");
  241. await using var file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  242. XWPFRun run = para.CreateRun();
  243. using MemoryStream imgStream = new MemoryStream();
  244. await file.CopyToAsync(imgStream);
  245. imgStream.Seek(0, SeekOrigin.Begin);
  246. run.AddPicture(imgStream,
  247. (int)PictureType.JPEG, "image.jpg", Units.ToEMU(403.2), Units.ToEMU(300.8));
  248. para.RemoveRun(0); // 移除原来的占位符文本
  249. await file.DisposeAsync();
  250. File.Delete(filePath);
  251. }
  252. else
  253. {
  254. para.ReplaceText(kvp.Key, kvp.Value);
  255. }
  256. }
  257. }
  258. }
  259. await using MemoryStream wordStream = new MemoryStream();
  260. doc.Write(wordStream);
  261. var bytes = wordStream.ToArray();
  262. doc.Close();
  263. var wordFile = new FileContentResult(bytes, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  264. return await Task.FromResult(wordFile);
  265. }
  266. /// <summary>
  267. /// 获取智慧课堂报告
  268. /// </summary>
  269. /// <returns></returns>
  270. private async Task<FileContentResult> GetZHKT(ReportExportInput input)
  271. {
  272. var warnGroup = _simpleCacheService
  273. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  274. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  275. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  276. return await Task.FromResult(wordFile);
  277. }
  278. /// <summary>
  279. /// 获取学生归寝报告
  280. /// </summary>
  281. /// <returns></returns>
  282. private async Task<FileContentResult> GetXSGQ(ReportExportInput input)
  283. {
  284. var warnGroup = _simpleCacheService
  285. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  286. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  287. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  288. return await Task.FromResult(wordFile);
  289. }
  290. /// <summary>
  291. /// 获取校园防霸凌报告
  292. /// </summary>
  293. /// <returns></returns>
  294. private async Task<FileContentResult> GetXYFBL(ReportExportInput input)
  295. {
  296. var warnGroup = _simpleCacheService
  297. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  298. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  299. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  300. return await Task.FromResult(wordFile);
  301. }
  302. /// <summary>
  303. /// 获取校园安全报告
  304. /// </summary>
  305. /// <returns></returns>
  306. private async Task<FileContentResult> GetXYAQ(ReportExportInput input)
  307. {
  308. var warnGroup = _simpleCacheService
  309. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  310. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  311. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  312. return await Task.FromResult(wordFile);
  313. }
  314. /// <summary>
  315. /// 获取区域管控报告
  316. /// </summary>
  317. /// <returns></returns>
  318. private async Task<FileContentResult> GetQYGK(ReportExportInput input)
  319. {
  320. var warnGroup = _simpleCacheService
  321. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  322. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  323. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  324. return await Task.FromResult(wordFile);
  325. }
  326. /// <summary>
  327. /// 获取安保巡逻报告
  328. /// </summary>
  329. /// <returns></returns>
  330. private async Task<FileContentResult> GetABXL(ReportExportInput input)
  331. {
  332. var warnGroup = _simpleCacheService
  333. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  334. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  335. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  336. return await Task.FromResult(wordFile);
  337. }
  338. }