TdevDevOpeAccountController.cs 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. using FluentEmail.Core;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Logging;
  5. using Newtonsoft.Json;
  6. using NPOI.HPSF;
  7. using NPOI.SS.Formula.Functions;
  8. using NPOI.SS.UserModel;
  9. using NPOI.SS.Util;
  10. using NPOI.XSSF.UserModel;
  11. using Ropin.Inspection.Api.Common;
  12. using Ropin.Inspection.Api.Controllers;
  13. using Ropin.Inspection.Common.Helper;
  14. using Ropin.Inspection.Model;
  15. using Ropin.Inspection.Model.Common;
  16. using Ropin.Inspection.Model.ViewModel.DEV;
  17. using Ropin.Inspection.Service;
  18. using Ropin.Inspection.Service.DEV.Interface;
  19. using System;
  20. using System.Collections;
  21. using System.Collections.Generic;
  22. using System.Data;
  23. using System.Drawing.Printing;
  24. using System.IO;
  25. using System.IO.Compression;
  26. using System.Linq;
  27. using System.Net;
  28. using System.Net.Http;
  29. using System.Runtime.ConstrainedExecution;
  30. using System.Threading.Tasks;
  31. namespace Ropin.Inspection.Api
  32. {
  33. public class TdevDevOpeAccountController : BaseController
  34. {
  35. public ILogger<TdevDevOpeAccountController> _logger { get; }
  36. private readonly ITdevDevOpeAccountService _TdevDevOpeAccountService;
  37. private readonly IDevDevOpeAccountConfigService _devDevOpeAccountConfigService;
  38. /// <summary>
  39. /// 构造函数
  40. /// </summary>
  41. /// <param name="TdevDevOpeAccountService"></param>
  42. /// <param name="logger"></param>
  43. public TdevDevOpeAccountController(ITdevDevOpeAccountService TdevDevOpeAccountService, IDevDevOpeAccountConfigService devDevOpeAccountConfigService, ILogger<TdevDevOpeAccountController> logger)
  44. {
  45. _TdevDevOpeAccountService = TdevDevOpeAccountService;
  46. _logger = logger;
  47. _devDevOpeAccountConfigService = devDevOpeAccountConfigService;
  48. }
  49. /// <summary>
  50. /// 通过id获取业主设备运行台账信息
  51. /// </summary>
  52. /// <param name="id"></param>
  53. /// <returns></returns>
  54. [HttpGet("GetDevOpeAccountAsync/{id}")]
  55. public async Task<ApiResult> GetDevOpeAccountAsync(string id)
  56. {
  57. if (string.IsNullOrEmpty(id))
  58. {
  59. return new ApiResult(ReturnCode.GeneralError);
  60. }
  61. try
  62. {
  63. var content = await _TdevDevOpeAccountService.GetConditionAsync(new TdevDevOpeAccountSearchModel { C_ID = id });
  64. return new ApiResult<TdevDevOpeAccountViewModel>(content.FirstOrDefault());
  65. }
  66. catch (Exception ex)
  67. {
  68. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  69. }
  70. }
  71. /// <summary>
  72. /// 获取所有业主设备运行台账
  73. /// </summary>
  74. /// <returns></returns>
  75. [HttpGet("GetDevOpeAccountsAsync")]
  76. public async Task<ApiResult> GetDevOpeAccountsAsync()
  77. {
  78. try
  79. {
  80. var contentList = await _TdevDevOpeAccountService.GetAllAsync();
  81. return new ApiResult<IEnumerable<TdevDevOpeAccountViewModel>>(contentList);
  82. }
  83. catch (Exception ex)
  84. {
  85. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  86. }
  87. }
  88. /// <summary>
  89. /// 通过业主设备运行台账名称条件查询
  90. /// </summary>
  91. /// <param name="searchModel"></param>
  92. /// <returns></returns>
  93. [HttpPost("GetDevOpeAccountsByAsync")]
  94. public async Task<ApiResult> GetDevOpeAccountsByAsync(TdevDevOpeAccountSearchModel searchModel)
  95. {
  96. if (searchModel == null)
  97. {
  98. return new ApiResult(ReturnCode.ArgsError);
  99. }
  100. searchModel.IsPagination = false;
  101. try
  102. {
  103. var contentList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
  104. return new ApiResult<PagesModel<TdevDevOpeAccountViewModel>>(new PagesModel<TdevDevOpeAccountViewModel>(contentList, searchModel));
  105. }
  106. catch (Exception ex)
  107. {
  108. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  109. }
  110. }
  111. /// <summary>
  112. /// 查询运行台账
  113. /// </summary>
  114. /// <param name="searchModel"></param>
  115. /// <returns></returns>
  116. [HttpPost("GetDevOpeAccountContentByAsync")]
  117. [AllowAnonymous]
  118. public async Task<ApiResult> GetDevOpeAccountContentByAsync(TdevDevOpeAccountSearchModel searchModel)
  119. {
  120. if (searchModel == null)
  121. {
  122. return new ApiResult(ReturnCode.ArgsError);
  123. }
  124. try
  125. {
  126. var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
  127. if (devOpeAccountList==null|| devOpeAccountList.Count()==0)
  128. {
  129. return new ApiResult<object>(new
  130. {
  131. tabName = new string[0],
  132. pages = new PagesModel<object>(null, searchModel)
  133. });
  134. }
  135. List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
  136. PagesModel<object> pages = null;
  137. var confCode = searchModel.C_DevOpeAccountConfigCode;
  138. if (!string.IsNullOrEmpty(confCode))
  139. {
  140. tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date", BReadDevSpot=false });
  141. var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
  142. if (content != null && !string.IsNullOrEmpty(content.C_Config))
  143. {
  144. var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
  145. foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
  146. {
  147. if (item!=null)
  148. {
  149. if (item.BReadDevSpot)
  150. {
  151. //string labVal = "";string labVal1 ="";
  152. //if (item.Label.Contains('(')|| item.Label.Contains('('))
  153. //{
  154. // labVal = item.Label.Replace("(","(").Replace("(","最大值(");
  155. // labVal1 = item.Label.Replace("(", "(").Replace("(", "最小值(");
  156. //}
  157. //else
  158. //{
  159. // labVal = item.Label + "最大值";
  160. // labVal1 = item.Label + "最小值";
  161. //}
  162. tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  163. tabs.Add(new RunSpotConfigName { Label = item.Label+ "@最大值", Name = item.Name+ "_Max", BReadDevSpot = item.BReadDevSpot });
  164. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  165. }
  166. else
  167. {
  168. tabs.Add(item);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
  175. List<object> list = new List<object>();
  176. for (int i = 0; i < confData.Count; i++)
  177. {
  178. Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
  179. if (dict.ContainsKey("RunWhetherNormal"))
  180. {
  181. var RunWhetherNormal = dict["RunWhetherNormal"].ToString();
  182. dict["RunWhetherNormal"] = RunWhetherNormal.ToLower() == "true" ? "是" : "否";
  183. }
  184. var trueList = tabs?.Where(t => t.BReadDevSpot == true);
  185. foreach (var item in trueList)
  186. {
  187. if (item!=null)
  188. {
  189. if (!dict.ContainsKey(item.Name))
  190. {
  191. dict.Add(item.Name, "0");
  192. }
  193. }
  194. }
  195. foreach (var item in dict)
  196. {
  197. if (item.Key?.ToString()== "Date")
  198. {
  199. dict[item.Key] = Convert.ToDateTime(item.Value).ToString("yyyy-MM-dd");
  200. }
  201. else
  202. {
  203. if (item.Value != null)
  204. {
  205. if (DateTime.TryParse(item.Value.ToString(), out DateTime dt) && item.Value.ToString().Length >= 10)
  206. {
  207. dict[item.Key] = dt.ToString("yyyy-MM-dd HH:mm:ss");
  208. }
  209. }
  210. }
  211. }
  212. list.Add(dict);
  213. }
  214. pages = new PagesModel<object>(list, searchModel);
  215. //var tabArray = tabs.Select(t => t.Label).ToArray();
  216. List<DevOpeAccountConfigTableHeadeModel> tabList = new List<DevOpeAccountConfigTableHeadeModel>();
  217. for (int i = 0; i < tabs.Count; i++)
  218. {
  219. var vals = tabs[i].Label.ToString();
  220. var name = tabs[i].Name.ToString();
  221. if (vals.Contains("@"))
  222. {
  223. string[] tmp = vals.Split('@');//按,分割字符串
  224. var TableHeadeModel = tabList.Find(t => t.label == tmp[0].ToString());
  225. if (TableHeadeModel!=null)
  226. {
  227. if (TableHeadeModel.children!=null&& TableHeadeModel.children.Count>0)
  228. {
  229. if (tmp.Count() == 2)
  230. {
  231. TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[1].ToString(), Level = 2, children = null });
  232. }
  233. if (tmp.Count() == 3)
  234. {
  235. var childModel = TableHeadeModel.children.Find(x => x.label == tmp[1].ToString());
  236. if (childModel!=null)
  237. {
  238. if (childModel.children==null)
  239. {
  240. childModel.children = new List<DevOpeAccountConfigTableHeadeModel>();
  241. }
  242. childModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children = null });
  243. }
  244. else
  245. {
  246. List<DevOpeAccountConfigTableHeadeModel> chile = null;
  247. chile = new List<DevOpeAccountConfigTableHeadeModel>();
  248. chile.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children = null });
  249. TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[1].ToString(), Level = 2, children = chile });
  250. }
  251. }
  252. }
  253. else
  254. {
  255. TableHeadeModel.children = new List<DevOpeAccountConfigTableHeadeModel>();
  256. if (tmp.Count() == 2)
  257. {
  258. TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[1].ToString(), Level = 2, children = null });
  259. }
  260. if (tmp.Count() == 3)
  261. {
  262. List<DevOpeAccountConfigTableHeadeModel> chile = null;
  263. chile = new List<DevOpeAccountConfigTableHeadeModel>();
  264. chile.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children = null });
  265. TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[1].ToString(), Level = 2, children = chile });
  266. }
  267. }
  268. }
  269. else
  270. {
  271. var cc = new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[0].ToString(),Level=1, children = new List<DevOpeAccountConfigTableHeadeModel>()};
  272. if (tmp.Count() == 2)
  273. {
  274. cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[1].ToString(), Level = 2, children = null });
  275. }
  276. if (tmp.Count()==3)
  277. {
  278. List<DevOpeAccountConfigTableHeadeModel> chile = null;
  279. chile = new List<DevOpeAccountConfigTableHeadeModel>();
  280. chile.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children =null});
  281. cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[1].ToString(), Level = 2, children = chile });
  282. }
  283. tabList.Add(cc);
  284. }
  285. }
  286. else
  287. {
  288. tabList.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label=vals, Level = 1, children = null});
  289. }
  290. }
  291. var data = new
  292. {
  293. DevOpeAccountConfigCode= confCode,
  294. tabName = tabs.Select(t => t.Label).ToArray(),
  295. tabArray= tabList,
  296. pages
  297. };
  298. return new ApiResult<object>(data);
  299. }
  300. catch (Exception ex)
  301. {
  302. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  303. }
  304. }
  305. /// <summary>
  306. /// 查询运行台账-导出
  307. /// </summary>
  308. /// <param name="searchModel"></param>
  309. /// <returns></returns>
  310. [HttpPost("GetDevOpeAccountContentExportByAsync")]
  311. [AllowAnonymous]
  312. public async Task<ApiResult> GetDevOpeAccountContentExportByAsync(TdevDevOpeAccountSearchModel searchModel)
  313. {
  314. if (searchModel == null)
  315. {
  316. return new ApiResult(ReturnCode.ArgsError);
  317. }
  318. try
  319. {
  320. var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
  321. if (devOpeAccountList == null || devOpeAccountList.Count() == 0)
  322. {
  323. return new ApiResult<object>(new
  324. {
  325. tabName = new string[0],
  326. pages = new PagesModel<object>(null, searchModel)
  327. });
  328. }
  329. List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
  330. PagesModel<object> pages = null;
  331. var confCode = searchModel.C_DevOpeAccountConfigCode;
  332. if (!string.IsNullOrEmpty(confCode))
  333. {
  334. tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date" ,BReadDevSpot=false });
  335. var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
  336. if (content != null && !string.IsNullOrEmpty(content.C_Config))
  337. {
  338. var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
  339. foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
  340. {
  341. if (item != null)
  342. {
  343. if (item.BReadDevSpot)
  344. {
  345. //string labVal = ""; string labVal1 = "";
  346. //if (item.Label.Contains('(') || item.Label.Contains('('))
  347. //{
  348. // labVal = item.Label.Replace("(", "(").Replace("(", "最大值(");
  349. // labVal1 = item.Label.Replace("(", "(").Replace("(", "最小值(");
  350. //}
  351. //else
  352. //{
  353. // labVal = item.Label + "最大值";
  354. // labVal1 = item.Label + "最小值";
  355. //}
  356. tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  357. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  358. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  359. }
  360. else
  361. {
  362. tabs.Add(item);
  363. }
  364. }
  365. }
  366. }
  367. }
  368. var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
  369. List<object> list = new List<object>();
  370. for (int i = 0; i < confData.Count; i++)
  371. {
  372. Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
  373. string[] cels = new string[tabs.Count];
  374. int forCount = dict.Count + 1;
  375. if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; }
  376. for (int j = 0; j < forCount; j++)
  377. {
  378. if (j < tabs.Count)
  379. {
  380. string itemVal = "";
  381. if (dict.ContainsKey(tabs[j].Name))
  382. {
  383. itemVal = dict[tabs[j].Name]?.ToString();
  384. }
  385. if (tabs[j].Name== "RunWhetherNormal")
  386. {
  387. cels[j] = itemVal?.ToLower() == "true" ? "是" : "否";
  388. }else if (tabs[j].Name == "Date")
  389. {
  390. cels[j] = Convert.ToDateTime(itemVal).ToString("yyyy-MM-dd");
  391. }
  392. else
  393. {
  394. if (DateTime.TryParse(itemVal, out DateTime dt) && itemVal.Length >= 10)
  395. {
  396. cels[j] = dt.ToString("yyyy-MM-dd HH:mm:ss");
  397. }
  398. else
  399. {
  400. cels[j] = itemVal;
  401. }
  402. }
  403. }
  404. }
  405. list.Add(cels);
  406. }
  407. pages = new PagesModel<object>(list, searchModel);
  408. //var tabArray = tabs.Select(t => t.Label).ToArray();
  409. List<DevOpeAccountConfigTableHeadeModel> tabList = new List<DevOpeAccountConfigTableHeadeModel>();
  410. for (int i = 0; i < tabs.Count; i++)
  411. {
  412. var vals = tabs[i].Label.ToString();
  413. var name = tabs[i].Name.ToString();
  414. if (vals.Contains("@"))
  415. {
  416. string[] tmp = vals.Split('@');//按,分割字符串
  417. var TableHeadeModel = tabList.Find(t => t.prop == tmp[0].ToString());
  418. if (TableHeadeModel != null)
  419. {
  420. if (TableHeadeModel.children != null && TableHeadeModel.children.Count > 0)
  421. {
  422. TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null });
  423. }
  424. else
  425. {
  426. TableHeadeModel.children = new List<DevOpeAccountConfigTableHeadeModel>();
  427. TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null });
  428. }
  429. }
  430. else
  431. {
  432. var cc = new DevOpeAccountConfigTableHeadeModel { prop = tmp[0].ToString(), label = tmp[0].ToString(), Level = 1, children = new List<DevOpeAccountConfigTableHeadeModel>() };
  433. cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null });
  434. tabList.Add(cc);
  435. }
  436. }
  437. else
  438. {
  439. tabList.Add(new DevOpeAccountConfigTableHeadeModel { prop = vals, label = vals, Level = 1, children = null });
  440. }
  441. }
  442. var data = new
  443. {
  444. DevOpeAccountConfigCode = confCode,
  445. tabName = tabs.Select(t => t.Label).ToArray(),
  446. tabArray = tabList,
  447. pages
  448. };
  449. return new ApiResult<object>(data);
  450. }
  451. catch (Exception ex)
  452. {
  453. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  454. }
  455. }
  456. /// <summary>
  457. /// 查询运行台账-【返回配置集合】
  458. /// </summary>
  459. /// <param name="searchModel"></param>
  460. /// <returns></returns>
  461. [HttpPost("GetDevOpeAccountConfingAsync")]
  462. [AllowAnonymous]
  463. public async Task<ApiResult> GetDevOpeAccountConfingAsync(TdevDevOpeAccountSearchModel searchModel)
  464. {
  465. if (searchModel == null)
  466. {
  467. return new ApiResult(ReturnCode.ArgsError);
  468. }
  469. try
  470. {
  471. PagesModel<object> pages = null;
  472. List<DevOpeAccountConfigSelect> select = await _TdevDevOpeAccountService.GetDevOpeAccountConfigSelectAsync(searchModel);
  473. pages = new PagesModel<object>(select, searchModel);
  474. var data = new
  475. {
  476. pages
  477. };
  478. return new ApiResult<object>(data);
  479. }
  480. catch (Exception ex)
  481. {
  482. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  483. }
  484. }
  485. /// <summary>
  486. /// 创建业主设备运行台账
  487. /// </summary>
  488. /// <param name="content"></param>
  489. /// <returns></returns>
  490. [HttpPost("CreateDevOpeAccountAsync")]
  491. public async Task<ApiResult> CreateDevOpeAccountAsync(TdevDevOpeAccountViewModel content)
  492. {
  493. if (content == null)
  494. {
  495. return new ApiResult(ReturnCode.ArgsError);
  496. }
  497. try
  498. {
  499. await _TdevDevOpeAccountService.CreateOneAsync(content);
  500. }
  501. catch (Exception ex)
  502. {
  503. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  504. }
  505. return new ApiResult(ReturnCode.Success);
  506. }
  507. /// <summary>
  508. /// 删除业主设备运行台账
  509. /// </summary>
  510. /// <param name="id"></param>
  511. /// <returns></returns>
  512. [HttpDelete("DeleteDevOpeAccountAsync/{id}")]
  513. public async Task<ApiResult> DeleteDevOpeAccountAsync(string id)
  514. {
  515. if (string.IsNullOrEmpty(id))
  516. {
  517. return new ApiResult(ReturnCode.GeneralError);
  518. }
  519. try
  520. {
  521. await _TdevDevOpeAccountService.DeleteAsync(id);
  522. }
  523. catch (Exception ex)
  524. {
  525. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  526. }
  527. return new ApiResult(ReturnCode.Success);
  528. }
  529. /// <summary>
  530. /// 更新业主设备运行台账
  531. /// </summary>
  532. /// <param name="id"></param>
  533. /// <param name="updateModel"></param>
  534. /// <returns></returns>
  535. [HttpPut("UpdateDevOpeAccountAsync/{id}")]
  536. public async Task<ApiResult> UpdateDevOpeAccountAsync(string id, TdevDevOpeAccountUpdateModel updateModel)
  537. {
  538. if (string.IsNullOrEmpty(id))
  539. {
  540. return new ApiResult(ReturnCode.GeneralError);
  541. }
  542. try
  543. {
  544. await _TdevDevOpeAccountService.UpdateAsync(id, updateModel);
  545. }
  546. catch (Exception ex)
  547. {
  548. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  549. }
  550. return new ApiResult(ReturnCode.Success);
  551. }
  552. /// <summary>
  553. /// 发送邮件(附件为台帐)
  554. /// </summary>
  555. /// <param name="searchModel"></param>
  556. /// <returns></returns>
  557. [HttpPost("SendDevOpeAccountToEmail")]
  558. [AllowAnonymous]
  559. public async Task SendDevOpeAccountToEmail(SendDevOpeAccountToEmailModel searchModel)
  560. {
  561. if (searchModel == null)
  562. {
  563. return;
  564. }
  565. try
  566. {
  567. var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
  568. var contentList = from c in devOpeAccountList.Select(t => t.C_Content)
  569. select JsonConvert.DeserializeObject<TdevDevOpeContentViewModel>(c);
  570. IWorkbook workbook = new XSSFWorkbook();
  571. ISheet sheet = workbook.CreateSheet("sheet1");
  572. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 12));
  573. var titleRow = sheet.CreateRow(0);
  574. titleRow.Height = 20 * 25;
  575. ICell titleCell = titleRow.CreateCell(0);
  576. titleCell.SetCellValue("设各台账记录");
  577. //第一行字体样式
  578. IFont font = workbook.CreateFont();
  579. font.IsBold = true;
  580. font.FontHeightInPoints = 16;
  581. font.FontName = "宋体";
  582. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  583. titleCellStyle.SetFont(font);
  584. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  585. //边框
  586. titleCellStyle.BorderBottom = BorderStyle.Thin;
  587. titleCellStyle.BorderLeft = BorderStyle.Thin;
  588. titleCellStyle.BorderRight = BorderStyle.Thin;
  589. titleCellStyle.BorderTop = BorderStyle.Thin;
  590. titleCell.CellStyle = titleCellStyle;
  591. var headRow = sheet.CreateRow(1);
  592. //headRow.CreateCell(0).SetCellValue("序 号");
  593. headRow.CreateCell(0).SetCellValue("日期");
  594. headRow.CreateCell(1).SetCellValue("排污口编号");
  595. headRow.CreateCell(2).SetCellValue("设备名称");
  596. headRow.CreateCell(3).SetCellValue("参数名称");
  597. headRow.CreateCell(4).SetCellValue("设计值");
  598. headRow.CreateCell(5).SetCellValue("单位");
  599. headRow.CreateCell(6).SetCellValue("污染因子");
  600. headRow.CreateCell(7).SetCellValue("数据来源");
  601. headRow.CreateCell(8).SetCellValue("烟气筒高度(m)");
  602. headRow.CreateCell(9).SetCellValue("排放温度(°C)");
  603. headRow.CreateCell(10).SetCellValue("压力");
  604. headRow.CreateCell(11).SetCellValue("开机时间");
  605. headRow.CreateCell(12).SetCellValue("关机时间");
  606. //第二行,列名
  607. IFont font1 = workbook.CreateFont();
  608. font1.IsBold = true;
  609. font1.FontHeightInPoints = 12;
  610. font1.FontName = "宋体";
  611. ICellStyle headCellStyle = workbook.CreateCellStyle();
  612. headCellStyle.SetFont(font1);
  613. //边框
  614. headCellStyle.BorderBottom = BorderStyle.Thin;
  615. headCellStyle.BorderLeft = BorderStyle.Thin;
  616. headCellStyle.BorderRight = BorderStyle.Thin;
  617. headCellStyle.BorderTop = BorderStyle.Thin;
  618. foreach (var item in headRow.Cells)
  619. {
  620. item.CellStyle = headCellStyle;
  621. }
  622. int start = 2;
  623. IFont font3 = workbook.CreateFont();
  624. font3.FontHeightInPoints = 9;
  625. font3.FontName = "宋体";
  626. ICellStyle contentCellStyle = workbook.CreateCellStyle();
  627. contentCellStyle.SetFont(font3);
  628. //边框
  629. contentCellStyle.BorderBottom = BorderStyle.Thin;
  630. contentCellStyle.BorderLeft = BorderStyle.Thin;
  631. contentCellStyle.BorderRight = BorderStyle.Thin;
  632. contentCellStyle.BorderTop = BorderStyle.Thin;
  633. foreach (var item in contentList)
  634. {
  635. var row = sheet.CreateRow(start);
  636. row.CreateCell(0).SetCellValue(item.Date.ToString("yyyy-MM-dd hh:mm:ss"));
  637. row.CreateCell(1).SetCellValue(item.DrainOutletNumber);
  638. row.CreateCell(2).SetCellValue(item.DevStoreName);
  639. row.CreateCell(3).SetCellValue(item.DevStoreType);
  640. row.CreateCell(4).SetCellValue(item.SpecificationsParameterName);
  641. row.CreateCell(5).SetCellValue(item.SpecificationsDesignValue);
  642. row.CreateCell(6).SetCellValue(item.PollutionFactor);
  643. row.CreateCell(7).SetCellValue(item.DataSources);
  644. row.CreateCell(8).SetCellValue(item.HeightOfFlue);
  645. row.CreateCell(9).SetCellValue(item.DischargeTemperature);
  646. row.CreateCell(10).SetCellValue(item.Pressure);
  647. row.CreateCell(11).SetCellValue(item.RunStartTime.ToString("yyyy-MM-dd hh:mm:ss"));
  648. row.CreateCell(12).SetCellValue(item.RunEndTime.ToString("yyyy-MM-dd hh:mm:ss"));
  649. start++;
  650. foreach (var cell in row.Cells)
  651. {
  652. cell.CellStyle = contentCellStyle;
  653. }
  654. }
  655. // 自适应单元格
  656. for (int i = 0; i < sheet.LastRowNum; i++)
  657. {
  658. sheet.AutoSizeRow(i);
  659. }
  660. for (int i = 0; i < 12; i++)
  661. {
  662. sheet.AutoSizeColumn(i, true);
  663. }
  664. using (var stream = new NpoiMemoryStream())
  665. {
  666. workbook.Write(stream);
  667. stream.Seek(0, SeekOrigin.Begin);
  668. string emailName = $"{searchModel.DevName} 设备台账记录报表 {searchModel.D_Start?.ToString("yyyy-MM-dd HH:mm:ss")}至{searchModel.D_End?.ToString("yyyy-MM-dd HH:mm:ss")}";
  669. EmailHelper.SendEmail(searchModel.Mails, emailName, "", "报表见附件", $"{emailName}.xlsx", "application/vnd.ms-excel", stream);
  670. }
  671. }
  672. catch (Exception ex)
  673. {
  674. throw;
  675. }
  676. }
  677. /// <summary>
  678. /// 查询运行台账-导出-Excel
  679. /// </summary>
  680. /// <param name="searchModel"></param>
  681. /// <returns></returns>
  682. [HttpPost("DevOpeAccountContentExportExcel")]
  683. [AllowAnonymous]
  684. public async Task<IActionResult> DevOpeAccountContentExportExcel(TdevDevOpeAccountSearchModel searchModel)
  685. {
  686. try
  687. {
  688. var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
  689. IWorkbook workbook = new XSSFWorkbook();
  690. ISheet sheet = workbook.CreateSheet();
  691. #region
  692. //标题加粗居中.
  693. IFont font = workbook.CreateFont();
  694. font.IsBold = true;
  695. font.FontHeightInPoints = 25;
  696. font.FontName = "宋体";
  697. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  698. titleCellStyle.SetFont(font);
  699. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  700. //边框
  701. titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  702. titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  703. titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  704. titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  705. titleCellStyle.WrapText = true;
  706. #endregion
  707. #region
  708. //标题加粗居中.
  709. IFont font1 = workbook.CreateFont();
  710. font1.IsBold = true;
  711. font1.FontHeightInPoints = 14;
  712. font1.FontName = "宋体";
  713. ICellStyle headCellStyle = workbook.CreateCellStyle();
  714. headCellStyle.SetFont(font1);
  715. headCellStyle.Alignment = HorizontalAlignment.Center; //字体左右居中
  716. headCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
  717. //边框
  718. headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  719. headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  720. headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  721. headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  722. headCellStyle.WrapText = true;
  723. #endregion
  724. #region
  725. //标题加粗居中.
  726. IFont font2 = workbook.CreateFont();
  727. font2.IsBold = false;
  728. font2.FontHeightInPoints = 12;
  729. font2.FontName = "宋体";
  730. ICellStyle rowCellStyle = workbook.CreateCellStyle();
  731. rowCellStyle.SetFont(font2);
  732. rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  733. //边框
  734. rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  735. rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  736. rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  737. rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  738. rowCellStyle.WrapText = true;
  739. #endregion
  740. IRow title = sheet.CreateRow(0);
  741. title.Height = 30 * 25;
  742. title.CreateCell(0).SetCellValue("VOCs治理设施基本信息与维保管理信息表");
  743. foreach (var item in title.Cells)
  744. {
  745. item.CellStyle = titleCellStyle;
  746. }
  747. if (devOpeAccountList == null || devOpeAccountList.Count() == 0)
  748. {
  749. IRow content1 = sheet.CreateRow(1);
  750. content1.CreateCell(0).SetCellValue("暂无数据");
  751. content1.Cells[0].CellStyle = headCellStyle;
  752. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15));
  753. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15));
  754. }
  755. else
  756. {
  757. List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
  758. var confCode = searchModel.C_DevOpeAccountConfigCode;
  759. IRow head = sheet.CreateRow(1);
  760. IRow head2 = sheet.CreateRow(2);
  761. IRow head3 = sheet.CreateRow(3);
  762. head.Height = 30 * 20;
  763. head2.Height = 30 * 20;
  764. head3.Height = 30 * 20;
  765. if (!string.IsNullOrEmpty(confCode))
  766. {
  767. head.CreateCell(0).SetCellValue("日期");
  768. tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date",BReadDevSpot=false });
  769. sheet.AddMergedRegion(new CellRangeAddress(1, 3, 0, 0));
  770. int cel = 1;
  771. var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
  772. if (content != null && !string.IsNullOrEmpty(content.C_Config))
  773. {
  774. var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
  775. List<RunSpotConfigName> CtabColumns = new List<RunSpotConfigName>();
  776. int headerNum = 1;
  777. foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
  778. {
  779. if (item != null)
  780. {
  781. headerNum= item.Label.Split("@").Count();
  782. if (item.BReadDevSpot)
  783. {
  784. headerNum++;
  785. tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  786. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  787. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  788. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  789. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  790. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  791. }
  792. else
  793. {
  794. tabs.Add(item);
  795. CtabColumns.Add(item);
  796. }
  797. }
  798. }
  799. bool IsVal = false;int merge = 0;
  800. bool IsVal2 = false; int merge2 = 0;
  801. foreach (var tab in CtabColumns)
  802. {
  803. if (tab != null)
  804. {
  805. var vals = tab.Label;
  806. if (vals.Contains("@"))
  807. {
  808. string[] tmp = vals.Split('@');//按,分割字符串
  809. if (tmp.Count()==3)
  810. {
  811. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  812. var getCel2 = sheet.GetRow(2).GetCell(cel - 1 - merge2).ToString();
  813. if (getCel == tmp[0].ToString())
  814. {
  815. head.CreateCell(cel).SetCellValue("");
  816. merge++;
  817. IsVal = true;
  818. }
  819. else
  820. {
  821. IsVal = false;
  822. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  823. }
  824. if (getCel2 == tmp[1].ToString())
  825. {
  826. head2.CreateCell(cel).SetCellValue("");
  827. merge2++;
  828. IsVal2 = true;
  829. }
  830. else
  831. {
  832. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  833. IsVal2 = false;
  834. }
  835. head3.CreateCell(cel).SetCellValue(tmp[2].ToString());
  836. }
  837. else
  838. {
  839. IsVal2 = false;
  840. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  841. if (getCel == tmp[0].ToString())
  842. {
  843. head.CreateCell(cel).SetCellValue("");
  844. merge++;
  845. IsVal = true;
  846. }
  847. else
  848. {
  849. IsVal = false;
  850. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  851. }
  852. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  853. head3.CreateCell(cel).SetCellValue("");
  854. sheet.AddMergedRegion(new CellRangeAddress(2, 3, cel, cel));
  855. }
  856. }
  857. else
  858. {
  859. IsVal = false;
  860. IsVal2 = false;
  861. head.CreateCell(cel).SetCellValue(vals);
  862. head2.CreateCell(cel).SetCellValue("");
  863. head3.CreateCell(cel).SetCellValue("");
  864. sheet.AddMergedRegion(new CellRangeAddress(1, 3, cel, cel));
  865. }
  866. if (!IsVal && merge > 0)
  867. {
  868. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  869. merge = 0;
  870. }
  871. if (!IsVal2 && merge2 > 0)
  872. {
  873. sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
  874. merge2 = 0;
  875. }
  876. cel++;
  877. }
  878. }
  879. if (cel== CtabColumns.Count+1&& merge>0&& IsVal)
  880. {
  881. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  882. merge = 0; IsVal = false;
  883. }
  884. if (cel == CtabColumns.Count + 1 && merge2 > 0 && IsVal2)
  885. {
  886. sheet.AddMergedRegion(new CellRangeAddress(1, 2, cel - merge2 - 1, cel - 1));
  887. merge2 = 0; IsVal2 = false;
  888. }
  889. }
  890. }
  891. foreach (var item in head.Cells)
  892. {
  893. item.CellStyle = headCellStyle;
  894. }
  895. foreach (var item in head2.Cells)
  896. {
  897. item.CellStyle = headCellStyle;
  898. }
  899. foreach (var item in head3.Cells)
  900. {
  901. item.CellStyle = headCellStyle;
  902. }
  903. int rowNumber = 4;
  904. var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
  905. for (int i = 0; i < confData.Count; i++)
  906. {
  907. IRow content = sheet.CreateRow(rowNumber);
  908. Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
  909. int forCount = dict.Count + 1;
  910. if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; }
  911. for (int j = 0; j < forCount; j++)
  912. {
  913. if (j < tabs.Count)
  914. {
  915. string celVal = "";
  916. if (dict.ContainsKey(tabs[j].Name))
  917. {
  918. celVal = dict[tabs[j].Name]?.ToString();
  919. }
  920. if (tabs[j].Name == "RunWhetherNormal")
  921. {
  922. celVal = celVal.ToLower() == "true" ? "是" : "否";
  923. content.CreateCell(j).SetCellValue(celVal);
  924. }else if (tabs[j].Name == "Date")
  925. {
  926. content.CreateCell(j).SetCellValue(Convert.ToDateTime(celVal).ToString("yyyy-MM-dd"));
  927. }
  928. else if(DateTime.TryParse(celVal, out DateTime dt) && celVal.Length >= 10)
  929. {
  930. content.CreateCell(j).SetCellValue(dt.ToString("yyyy-MM-dd HH:mm:ss"));
  931. }
  932. else
  933. {
  934. content.CreateCell(j).SetCellValue(celVal);
  935. }
  936. }
  937. }
  938. foreach (var item in content.Cells)
  939. {
  940. item.CellStyle = rowCellStyle;
  941. }
  942. rowNumber++;
  943. }
  944. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, tabs.Count-1));
  945. for (int i = 0; i <= tabs.Count; i++)
  946. {
  947. sheet.SetColumnWidth(i, 5000);
  948. }
  949. }
  950. using (MemoryStream stream = new MemoryStream())
  951. {
  952. workbook.Write(stream);
  953. byte[] data = stream.ToArray();
  954. return File(data, "application/vnd.ms-excel", "设备运行台账.xlsx");
  955. }
  956. }
  957. catch (Exception ex)
  958. {
  959. throw;
  960. }
  961. }
  962. }
  963. }