平安校园
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

VioAnalysisController.cs 15 KiB

5 meses atrás
5 meses atrás
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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,
  182. AlarmTypes = warnGroup.Subset.Select(x => x.Code).ToArray(),
  183. PersonSetIds = input.PersonSetIds
  184. });
  185. var templatePath = Path.Combine(Directory.GetCurrentDirectory(), "Template", $"WordTemplate.docx");
  186. await using var fs = new FileStream(templatePath, FileMode.Open, FileAccess.ReadWrite);
  187. XWPFDocument doc = new XWPFDocument(fs);
  188. var content = warnList.GroupBy(x => x.AlarmTypeDesc).ToList();
  189. if (!content.Any())
  190. {
  191. throw Oops.Oh("该时间段无数据,无法生成报告!");
  192. }
  193. Dictionary<string, string> replacements = new Dictionary<string, string>()
  194. {
  195. //图片
  196. { "{{ImagePie}}","" },
  197. { "{{ImageBar}}","" },
  198. //场景名称
  199. { "{{GroupName}}", warnGroup.Name},
  200. //摄 像 头
  201. { "{{CameraName}}",string.Join("、",warnGroup.CameraName)},
  202. //统计时间
  203. { "{{StatitionTime}}", $"{input.StartTime:yyyy-MM-dd} 至 {input.EndTime:yyyy-MM-dd}"},
  204. //累计告警
  205. { "{{Count}}",warnList.Count.ToString()},
  206. //内容详情
  207. { "{{Content}}", $"按类型统计如下:{string.Join("、",content.Select(x=>$"{x.Key}:{x.Count()}条"))}"}
  208. };
  209. // 遍历文档中的所有段落
  210. foreach (XWPFParagraph para in doc.Paragraphs)
  211. {
  212. foreach (var kvp in replacements)
  213. {
  214. if (para.Text.Contains(kvp.Key)) // 检查是否为图片文件名
  215. {
  216. if (kvp.Key == "{{ImageBar}}")
  217. {
  218. var categories = content.Select(x => x.Key).ToList();//new List<string>() { "Category A", "Category B", "Category C", "Category D" });
  219. var values = content.Select(x => x.Count()).ToList();// new List<int>() { 215, 130, 245, 210 };
  220. var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Template",
  221. $"{Guid.NewGuid():N}.png");
  222. GenCharts($"{warnGroup.Name}场景告警统计", categories, values, filePath,"GenBar.py");
  223. await using var file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  224. XWPFRun run = para.CreateRun();
  225. using MemoryStream imgStream = new MemoryStream();
  226. await file.CopyToAsync(imgStream);
  227. imgStream.Seek(0, SeekOrigin.Begin);
  228. run.AddPicture(imgStream,
  229. (int)PictureType.JPEG, "image.jpg", Units.ToEMU(403.2), Units.ToEMU(300.8));
  230. para.RemoveRun(0); // 移除原来的占位符文本
  231. await file.DisposeAsync();
  232. File.Delete(filePath);
  233. }else if (kvp.Key == "{{ImagePie}}")
  234. {
  235. var categories = content.Select(x => x.Key).ToList();//new List<string>() { "Category A", "Category B", "Category C", "Category D" });
  236. var values = content.Select(x => x.Count()).ToList();// new List<int>() { 215, 130, 245, 210 };
  237. var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Template",
  238. $"{Guid.NewGuid():N}.png");
  239. GenCharts($"{warnGroup.Name}场景告警统计", categories, values, filePath,"GenPie.py");
  240. await using var file = new FileStream(filePath, FileMode.Open, FileAccess.Read);
  241. XWPFRun run = para.CreateRun();
  242. using MemoryStream imgStream = new MemoryStream();
  243. await file.CopyToAsync(imgStream);
  244. imgStream.Seek(0, SeekOrigin.Begin);
  245. run.AddPicture(imgStream,
  246. (int)PictureType.JPEG, "image.jpg", Units.ToEMU(403.2), Units.ToEMU(300.8));
  247. para.RemoveRun(0); // 移除原来的占位符文本
  248. await file.DisposeAsync();
  249. File.Delete(filePath);
  250. }
  251. else
  252. {
  253. para.ReplaceText(kvp.Key, kvp.Value);
  254. }
  255. }
  256. }
  257. }
  258. await using MemoryStream wordStream = new MemoryStream();
  259. doc.Write(wordStream);
  260. var bytes = wordStream.ToArray();
  261. doc.Close();
  262. var wordFile = new FileContentResult(bytes, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  263. return await Task.FromResult(wordFile);
  264. }
  265. /// <summary>
  266. /// 获取智慧课堂报告
  267. /// </summary>
  268. /// <returns></returns>
  269. private async Task<FileContentResult> GetZHKT(ReportExportInput input)
  270. {
  271. var warnGroup = _simpleCacheService
  272. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  273. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  274. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  275. return await Task.FromResult(wordFile);
  276. }
  277. /// <summary>
  278. /// 获取学生归寝报告
  279. /// </summary>
  280. /// <returns></returns>
  281. private async Task<FileContentResult> GetXSGQ(ReportExportInput input)
  282. {
  283. var warnGroup = _simpleCacheService
  284. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  285. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  286. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  287. return await Task.FromResult(wordFile);
  288. }
  289. /// <summary>
  290. /// 获取校园防霸凌报告
  291. /// </summary>
  292. /// <returns></returns>
  293. private async Task<FileContentResult> GetXYFBL(ReportExportInput input)
  294. {
  295. var warnGroup = _simpleCacheService
  296. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  297. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  298. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  299. return await Task.FromResult(wordFile);
  300. }
  301. /// <summary>
  302. /// 获取校园安全报告
  303. /// </summary>
  304. /// <returns></returns>
  305. private async Task<FileContentResult> GetXYAQ(ReportExportInput input)
  306. {
  307. var warnGroup = _simpleCacheService
  308. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  309. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  310. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  311. return await Task.FromResult(wordFile);
  312. }
  313. /// <summary>
  314. /// 获取区域管控报告
  315. /// </summary>
  316. /// <returns></returns>
  317. private async Task<FileContentResult> GetQYGK(ReportExportInput input)
  318. {
  319. var warnGroup = _simpleCacheService
  320. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  321. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  322. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  323. return await Task.FromResult(wordFile);
  324. }
  325. /// <summary>
  326. /// 获取安保巡逻报告
  327. /// </summary>
  328. /// <returns></returns>
  329. private async Task<FileContentResult> GetABXL(ReportExportInput input)
  330. {
  331. var warnGroup = _simpleCacheService
  332. .Get<List<WarnGroupInfo>>(SafeCampusConst.WarnGroup)
  333. .Where(x => x.Code == input.GroupCode).FirstOrDefault();
  334. var wordFile = new FileContentResult(new byte[] { }, "application/octet-stream") { FileDownloadName = $"{warnGroup.Name}场景分析报告.docx" };
  335. return await Task.FromResult(wordFile);
  336. }
  337. }