TdevDevOpeAccountController.cs 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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. }
  389. else if (tabs[j].Name == "Date")
  390. {
  391. cels[j] = Convert.ToDateTime(itemVal).ToString("yyyy-MM-dd");
  392. }
  393. else
  394. {
  395. if (DateTime.TryParse(itemVal, out DateTime dt) && itemVal.Length >= 10)
  396. {
  397. cels[j] = dt.ToString("yyyy-MM-dd HH:mm:ss");
  398. }
  399. else
  400. {
  401. cels[j] = itemVal;
  402. }
  403. }
  404. }
  405. }
  406. list.Add(cels);
  407. }
  408. pages = new PagesModel<object>(list, searchModel);
  409. //var tabArray = tabs.Select(t => t.Label).ToArray();
  410. List<DevOpeAccountConfigTableHeadeModel> tabList = new List<DevOpeAccountConfigTableHeadeModel>();
  411. for (int i = 0; i < tabs.Count; i++)
  412. {
  413. var vals = tabs[i].Label.ToString();
  414. var name = tabs[i].Name.ToString();
  415. if (vals.Contains("@"))
  416. {
  417. string[] tmp = vals.Split('@');//按,分割字符串
  418. var TableHeadeModel = tabList.Find(t => t.prop == tmp[0].ToString());
  419. if (TableHeadeModel != null)
  420. {
  421. if (TableHeadeModel.children != null && TableHeadeModel.children.Count > 0)
  422. {
  423. TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null });
  424. }
  425. else
  426. {
  427. TableHeadeModel.children = new List<DevOpeAccountConfigTableHeadeModel>();
  428. TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null });
  429. }
  430. }
  431. else
  432. {
  433. var cc = new DevOpeAccountConfigTableHeadeModel { prop = tmp[0].ToString(), label = tmp[0].ToString(), Level = 1, children = new List<DevOpeAccountConfigTableHeadeModel>() };
  434. cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null });
  435. tabList.Add(cc);
  436. }
  437. }
  438. else
  439. {
  440. tabList.Add(new DevOpeAccountConfigTableHeadeModel { prop = vals, label = vals, Level = 1, children = null });
  441. }
  442. }
  443. var data = new
  444. {
  445. DevOpeAccountConfigCode = confCode,
  446. tabName = tabs.Select(t => t.Label).ToArray(),
  447. tabArray = tabList,
  448. pages
  449. };
  450. return new ApiResult<object>(data);
  451. }
  452. catch (Exception ex)
  453. {
  454. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  455. }
  456. }
  457. /// <summary>
  458. /// 查询运行台账-【返回配置集合】
  459. /// </summary>
  460. /// <param name="searchModel"></param>
  461. /// <returns></returns>
  462. [HttpPost("GetDevOpeAccountConfingAsync")]
  463. [AllowAnonymous]
  464. public async Task<ApiResult> GetDevOpeAccountConfingAsync(TdevDevOpeAccountSearchModel searchModel)
  465. {
  466. if (searchModel == null)
  467. {
  468. return new ApiResult(ReturnCode.ArgsError);
  469. }
  470. try
  471. {
  472. PagesModel<object> pages = null;
  473. List<DevOpeAccountConfigSelect> select = await _TdevDevOpeAccountService.GetDevOpeAccountConfigSelectAsync(searchModel);
  474. pages = new PagesModel<object>(select, searchModel);
  475. var data = new
  476. {
  477. pages
  478. };
  479. return new ApiResult<object>(data);
  480. }
  481. catch (Exception ex)
  482. {
  483. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  484. }
  485. }
  486. /// <summary>
  487. /// 创建业主设备运行台账
  488. /// </summary>
  489. /// <param name="content"></param>
  490. /// <returns></returns>
  491. [HttpPost("CreateDevOpeAccountAsync")]
  492. public async Task<ApiResult> CreateDevOpeAccountAsync(TdevDevOpeAccountViewModel content)
  493. {
  494. if (content == null)
  495. {
  496. return new ApiResult(ReturnCode.ArgsError);
  497. }
  498. try
  499. {
  500. await _TdevDevOpeAccountService.CreateOneAsync(content);
  501. }
  502. catch (Exception ex)
  503. {
  504. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  505. }
  506. return new ApiResult(ReturnCode.Success);
  507. }
  508. /// <summary>
  509. /// 删除业主设备运行台账
  510. /// </summary>
  511. /// <param name="id"></param>
  512. /// <returns></returns>
  513. [HttpDelete("DeleteDevOpeAccountAsync/{id}")]
  514. public async Task<ApiResult> DeleteDevOpeAccountAsync(string id)
  515. {
  516. if (string.IsNullOrEmpty(id))
  517. {
  518. return new ApiResult(ReturnCode.GeneralError);
  519. }
  520. try
  521. {
  522. await _TdevDevOpeAccountService.DeleteAsync(id);
  523. }
  524. catch (Exception ex)
  525. {
  526. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  527. }
  528. return new ApiResult(ReturnCode.Success);
  529. }
  530. /// <summary>
  531. /// 更新业主设备运行台账
  532. /// </summary>
  533. /// <param name="id"></param>
  534. /// <param name="updateModel"></param>
  535. /// <returns></returns>
  536. [HttpPut("UpdateDevOpeAccountAsync/{id}")]
  537. public async Task<ApiResult> UpdateDevOpeAccountAsync(string id, TdevDevOpeAccountUpdateModel updateModel)
  538. {
  539. if (string.IsNullOrEmpty(id))
  540. {
  541. return new ApiResult(ReturnCode.GeneralError);
  542. }
  543. try
  544. {
  545. await _TdevDevOpeAccountService.UpdateAsync(id, updateModel);
  546. }
  547. catch (Exception ex)
  548. {
  549. return new ApiResult(ReturnCode.GeneralError, ex.Message);
  550. }
  551. return new ApiResult(ReturnCode.Success);
  552. }
  553. /// <summary>
  554. /// 发送邮件(附件为台帐)
  555. /// </summary>
  556. /// <param name="searchModel"></param>
  557. /// <returns></returns>
  558. [HttpPost("SendDevOpeAccountToEmail")]
  559. [AllowAnonymous]
  560. public async Task SendDevOpeAccountToEmail(SendDevOpeAccountToEmailModel searchModel)
  561. {
  562. if (searchModel == null)
  563. {
  564. return;
  565. }
  566. try
  567. {
  568. var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
  569. IWorkbook workbook = new XSSFWorkbook();
  570. ISheet sheet = workbook.CreateSheet();
  571. #region
  572. //标题加粗居中.
  573. IFont font = workbook.CreateFont();
  574. font.IsBold = true;
  575. font.FontHeightInPoints = 25;
  576. font.FontName = "宋体";
  577. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  578. titleCellStyle.SetFont(font);
  579. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  580. //边框
  581. titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  582. titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  583. titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  584. titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  585. titleCellStyle.WrapText = true;
  586. #endregion
  587. #region
  588. //标题加粗居中.
  589. IFont font1 = workbook.CreateFont();
  590. font1.IsBold = true;
  591. font1.FontHeightInPoints = 14;
  592. font1.FontName = "宋体";
  593. ICellStyle headCellStyle = workbook.CreateCellStyle();
  594. headCellStyle.SetFont(font1);
  595. headCellStyle.Alignment = HorizontalAlignment.Center; //字体左右居中
  596. headCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
  597. //边框
  598. headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  599. headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  600. headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  601. headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  602. headCellStyle.WrapText = true;
  603. #endregion
  604. #region
  605. //标题加粗居中.
  606. IFont font2 = workbook.CreateFont();
  607. font2.IsBold = false;
  608. font2.FontHeightInPoints = 12;
  609. font2.FontName = "宋体";
  610. ICellStyle rowCellStyle = workbook.CreateCellStyle();
  611. rowCellStyle.SetFont(font2);
  612. rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  613. //边框
  614. rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  615. rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  616. rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  617. rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  618. rowCellStyle.WrapText = true;
  619. #endregion
  620. IRow title = sheet.CreateRow(0);
  621. title.Height = 30 * 25;
  622. title.CreateCell(0).SetCellValue("VOCs治理设施基本信息与维保管理信息表");
  623. foreach (var item in title.Cells)
  624. {
  625. item.CellStyle = titleCellStyle;
  626. }
  627. if (devOpeAccountList == null || devOpeAccountList.Count() == 0)
  628. {
  629. IRow content1 = sheet.CreateRow(1);
  630. content1.CreateCell(0).SetCellValue("暂无数据");
  631. content1.Cells[0].CellStyle = headCellStyle;
  632. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15));
  633. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15));
  634. }
  635. else
  636. {
  637. List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
  638. var confCode = searchModel.C_DevOpeAccountConfigCode;
  639. IRow head = sheet.CreateRow(1);
  640. IRow head2 = sheet.CreateRow(2);
  641. IRow head3 = sheet.CreateRow(3);
  642. head.Height = 30 * 20;
  643. head2.Height = 30 * 20;
  644. head3.Height = 30 * 20;
  645. if (!string.IsNullOrEmpty(confCode))
  646. {
  647. head.CreateCell(0).SetCellValue("日期");
  648. tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date", BReadDevSpot = false });
  649. sheet.AddMergedRegion(new CellRangeAddress(1, 3, 0, 0));
  650. int cel = 1;
  651. var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
  652. if (content != null && !string.IsNullOrEmpty(content.C_Config))
  653. {
  654. var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
  655. List<RunSpotConfigName> CtabColumns = new List<RunSpotConfigName>();
  656. int headerNum = 1;
  657. foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
  658. {
  659. if (item != null)
  660. {
  661. headerNum = item.Label.Split("@").Count();
  662. if (item.BReadDevSpot)
  663. {
  664. headerNum++;
  665. tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  666. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  667. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  668. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  669. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  670. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  671. }
  672. else
  673. {
  674. tabs.Add(item);
  675. CtabColumns.Add(item);
  676. }
  677. }
  678. }
  679. bool IsVal = false; int merge = 0;
  680. bool IsVal2 = false; int merge2 = 0;
  681. foreach (var tab in CtabColumns)
  682. {
  683. if (tab != null)
  684. {
  685. var vals = tab.Label;
  686. if (vals.Contains("@"))
  687. {
  688. string[] tmp = vals.Split('@');//按,分割字符串
  689. if (tmp.Count() == 3)
  690. {
  691. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  692. var getCel2 = sheet.GetRow(2).GetCell(cel - 1 - merge2).ToString();
  693. if (getCel == tmp[0].ToString())
  694. {
  695. head.CreateCell(cel).SetCellValue("");
  696. merge++;
  697. IsVal = true;
  698. }
  699. else
  700. {
  701. IsVal = false;
  702. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  703. }
  704. if (getCel2 == tmp[1].ToString())
  705. {
  706. head2.CreateCell(cel).SetCellValue("");
  707. merge2++;
  708. IsVal2 = true;
  709. }
  710. else
  711. {
  712. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  713. IsVal2 = false;
  714. }
  715. head3.CreateCell(cel).SetCellValue(tmp[2].ToString());
  716. }
  717. else
  718. {
  719. IsVal2 = false;
  720. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  721. if (getCel == tmp[0].ToString())
  722. {
  723. head.CreateCell(cel).SetCellValue("");
  724. merge++;
  725. IsVal = true;
  726. }
  727. else
  728. {
  729. IsVal = false;
  730. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  731. }
  732. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  733. head3.CreateCell(cel).SetCellValue("");
  734. sheet.AddMergedRegion(new CellRangeAddress(2, 3, cel, cel));
  735. }
  736. }
  737. else
  738. {
  739. IsVal = false;
  740. IsVal2 = false;
  741. head.CreateCell(cel).SetCellValue(vals);
  742. head2.CreateCell(cel).SetCellValue("");
  743. head3.CreateCell(cel).SetCellValue("");
  744. sheet.AddMergedRegion(new CellRangeAddress(1, 3, cel, cel));
  745. }
  746. if (!IsVal && merge > 0)
  747. {
  748. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  749. merge = 0;
  750. }
  751. if (!IsVal2 && merge2 > 0)
  752. {
  753. sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
  754. merge2 = 0;
  755. }
  756. cel++;
  757. }
  758. }
  759. if (cel == CtabColumns.Count + 1 && merge > 0 && IsVal)
  760. {
  761. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  762. merge = 0; IsVal = false;
  763. }
  764. if (cel == CtabColumns.Count + 1 && merge2 > 0 && IsVal2)
  765. {
  766. sheet.AddMergedRegion(new CellRangeAddress(1, 2, cel - merge2 - 1, cel - 1));
  767. merge2 = 0; IsVal2 = false;
  768. }
  769. }
  770. }
  771. foreach (var item in head.Cells)
  772. {
  773. item.CellStyle = headCellStyle;
  774. }
  775. foreach (var item in head2.Cells)
  776. {
  777. item.CellStyle = headCellStyle;
  778. }
  779. foreach (var item in head3.Cells)
  780. {
  781. item.CellStyle = headCellStyle;
  782. }
  783. int rowNumber = 4;
  784. var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
  785. for (int i = 0; i < confData.Count; i++)
  786. {
  787. IRow content = sheet.CreateRow(rowNumber);
  788. Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
  789. int forCount = dict.Count + 1;
  790. if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; }
  791. for (int j = 0; j < forCount; j++)
  792. {
  793. if (j < tabs.Count)
  794. {
  795. string celVal = "";
  796. if (dict.ContainsKey(tabs[j].Name))
  797. {
  798. celVal = dict[tabs[j].Name]?.ToString();
  799. }
  800. if (tabs[j].Name == "RunWhetherNormal")
  801. {
  802. celVal = celVal.ToLower() == "true" ? "异常" : "正常";
  803. content.CreateCell(j).SetCellValue(celVal);
  804. }
  805. else if (tabs[j].Name == "Date")
  806. {
  807. content.CreateCell(j).SetCellValue(Convert.ToDateTime(celVal).ToString("yyyy-MM-dd"));
  808. }
  809. else if (DateTime.TryParse(celVal, out DateTime dt) && celVal.Length >= 10)
  810. {
  811. content.CreateCell(j).SetCellValue(dt.ToString("yyyy-MM-dd HH:mm:ss"));
  812. }
  813. else
  814. {
  815. content.CreateCell(j).SetCellValue(celVal);
  816. }
  817. }
  818. }
  819. foreach (var item in content.Cells)
  820. {
  821. item.CellStyle = rowCellStyle;
  822. }
  823. rowNumber++;
  824. }
  825. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, tabs.Count - 1));
  826. for (int i = 0; i <= tabs.Count; i++)
  827. {
  828. sheet.SetColumnWidth(i, 5000);
  829. }
  830. }
  831. using (var stream = new NpoiMemoryStream())
  832. {
  833. workbook.Write(stream);
  834. stream.Seek(0, SeekOrigin.Begin);
  835. string emailName = $"{searchModel.DevName} 设备台账记录报表 {searchModel.D_Start?.ToString("yyyy-MM-dd HH:mm:ss")}至{searchModel.D_End?.ToString("yyyy-MM-dd HH:mm:ss")}";
  836. EmailHelper.SendEmail(searchModel.Mails, emailName, "", "报表见附件", $"{emailName}.xlsx", "application/vnd.ms-excel", stream);
  837. }
  838. }
  839. catch (Exception ex)
  840. {
  841. throw;
  842. }
  843. }
  844. /// <summary>
  845. /// 查询运行台账-导出-Excel
  846. /// </summary>
  847. /// <param name="searchModel"></param>
  848. /// <returns></returns>
  849. [HttpPost("DevOpeAccountContentExportExcel")]
  850. [AllowAnonymous]
  851. public async Task<IActionResult> DevOpeAccountContentExportExcel(TdevDevOpeAccountSearchModel searchModel)
  852. {
  853. try
  854. {
  855. var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
  856. IWorkbook workbook = new XSSFWorkbook();
  857. ISheet sheet = workbook.CreateSheet();
  858. #region
  859. //标题加粗居中.
  860. IFont font = workbook.CreateFont();
  861. font.IsBold = true;
  862. font.FontHeightInPoints = 25;
  863. font.FontName = "宋体";
  864. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  865. titleCellStyle.SetFont(font);
  866. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  867. //边框
  868. titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  869. titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  870. titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  871. titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  872. titleCellStyle.WrapText = true;
  873. #endregion
  874. #region
  875. //标题加粗居中.
  876. IFont font1 = workbook.CreateFont();
  877. font1.IsBold = true;
  878. font1.FontHeightInPoints = 14;
  879. font1.FontName = "宋体";
  880. ICellStyle headCellStyle = workbook.CreateCellStyle();
  881. headCellStyle.SetFont(font1);
  882. headCellStyle.Alignment = HorizontalAlignment.Center; //字体左右居中
  883. headCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
  884. //边框
  885. headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  886. headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  887. headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  888. headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  889. headCellStyle.WrapText = true;
  890. #endregion
  891. #region
  892. //标题加粗居中.
  893. IFont font2 = workbook.CreateFont();
  894. font2.IsBold = false;
  895. font2.FontHeightInPoints = 12;
  896. font2.FontName = "宋体";
  897. ICellStyle rowCellStyle = workbook.CreateCellStyle();
  898. rowCellStyle.SetFont(font2);
  899. rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  900. //边框
  901. rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  902. rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  903. rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  904. rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  905. rowCellStyle.WrapText = true;
  906. #endregion
  907. IRow title = sheet.CreateRow(0);
  908. title.Height = 30 * 25;
  909. title.CreateCell(0).SetCellValue("VOCs治理设施基本信息与维保管理信息表");
  910. foreach (var item in title.Cells)
  911. {
  912. item.CellStyle = titleCellStyle;
  913. }
  914. if (devOpeAccountList == null || devOpeAccountList.Count() == 0)
  915. {
  916. IRow content1 = sheet.CreateRow(1);
  917. content1.CreateCell(0).SetCellValue("暂无数据");
  918. content1.Cells[0].CellStyle = headCellStyle;
  919. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15));
  920. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15));
  921. }
  922. else
  923. {
  924. List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
  925. var confCode = searchModel.C_DevOpeAccountConfigCode;
  926. IRow head = sheet.CreateRow(1);
  927. IRow head2 = sheet.CreateRow(2);
  928. IRow head3 = sheet.CreateRow(3);
  929. head.Height = 30 * 20;
  930. head2.Height = 30 * 20;
  931. head3.Height = 30 * 20;
  932. if (!string.IsNullOrEmpty(confCode))
  933. {
  934. head.CreateCell(0).SetCellValue("日期");
  935. tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date",BReadDevSpot=false });
  936. sheet.AddMergedRegion(new CellRangeAddress(1, 3, 0, 0));
  937. int cel = 1;
  938. var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
  939. if (content != null && !string.IsNullOrEmpty(content.C_Config))
  940. {
  941. var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
  942. List<RunSpotConfigName> CtabColumns = new List<RunSpotConfigName>();
  943. int headerNum = 1;
  944. foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
  945. {
  946. if (item != null)
  947. {
  948. headerNum= item.Label.Split("@").Count();
  949. if (item.BReadDevSpot)
  950. {
  951. headerNum++;
  952. tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  953. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  954. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  955. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  956. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  957. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  958. }
  959. else
  960. {
  961. tabs.Add(item);
  962. CtabColumns.Add(item);
  963. }
  964. }
  965. }
  966. bool IsVal = false;int merge = 0;
  967. bool IsVal2 = false; int merge2 = 0;
  968. foreach (var tab in CtabColumns)
  969. {
  970. if (tab != null)
  971. {
  972. var vals = tab.Label;
  973. if (vals.Contains("@"))
  974. {
  975. string[] tmp = vals.Split('@');//按,分割字符串
  976. if (tmp.Count()==3)
  977. {
  978. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  979. var getCel2 = sheet.GetRow(2).GetCell(cel - 1 - merge2).ToString();
  980. if (getCel == tmp[0].ToString())
  981. {
  982. head.CreateCell(cel).SetCellValue("");
  983. merge++;
  984. IsVal = true;
  985. }
  986. else
  987. {
  988. IsVal = false;
  989. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  990. }
  991. if (getCel2 == tmp[1].ToString())
  992. {
  993. head2.CreateCell(cel).SetCellValue("");
  994. merge2++;
  995. IsVal2 = true;
  996. }
  997. else
  998. {
  999. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  1000. IsVal2 = false;
  1001. }
  1002. head3.CreateCell(cel).SetCellValue(tmp[2].ToString());
  1003. }
  1004. else
  1005. {
  1006. IsVal2 = false;
  1007. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  1008. if (getCel == tmp[0].ToString())
  1009. {
  1010. head.CreateCell(cel).SetCellValue("");
  1011. merge++;
  1012. IsVal = true;
  1013. }
  1014. else
  1015. {
  1016. IsVal = false;
  1017. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  1018. }
  1019. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  1020. head3.CreateCell(cel).SetCellValue("");
  1021. sheet.AddMergedRegion(new CellRangeAddress(2, 3, cel, cel));
  1022. }
  1023. }
  1024. else
  1025. {
  1026. IsVal = false;
  1027. IsVal2 = false;
  1028. head.CreateCell(cel).SetCellValue(vals);
  1029. head2.CreateCell(cel).SetCellValue("");
  1030. head3.CreateCell(cel).SetCellValue("");
  1031. sheet.AddMergedRegion(new CellRangeAddress(1, 3, cel, cel));
  1032. }
  1033. if (!IsVal && merge > 0)
  1034. {
  1035. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  1036. merge = 0;
  1037. }
  1038. if (!IsVal2 && merge2 > 0)
  1039. {
  1040. sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
  1041. merge2 = 0;
  1042. }
  1043. cel++;
  1044. }
  1045. }
  1046. if (cel== CtabColumns.Count+1&& merge>0&& IsVal)
  1047. {
  1048. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  1049. merge = 0; IsVal = false;
  1050. }
  1051. if (cel == CtabColumns.Count + 1 && merge2 > 0 && IsVal2)
  1052. {
  1053. sheet.AddMergedRegion(new CellRangeAddress(1, 2, cel - merge2 - 1, cel - 1));
  1054. merge2 = 0; IsVal2 = false;
  1055. }
  1056. }
  1057. }
  1058. foreach (var item in head.Cells)
  1059. {
  1060. item.CellStyle = headCellStyle;
  1061. }
  1062. foreach (var item in head2.Cells)
  1063. {
  1064. item.CellStyle = headCellStyle;
  1065. }
  1066. foreach (var item in head3.Cells)
  1067. {
  1068. item.CellStyle = headCellStyle;
  1069. }
  1070. int rowNumber = 4;
  1071. var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
  1072. for (int i = 0; i < confData.Count; i++)
  1073. {
  1074. IRow content = sheet.CreateRow(rowNumber);
  1075. Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
  1076. int forCount = dict.Count + 1;
  1077. if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; }
  1078. for (int j = 0; j < forCount; j++)
  1079. {
  1080. if (j < tabs.Count)
  1081. {
  1082. string celVal = "";
  1083. if (dict.ContainsKey(tabs[j].Name))
  1084. {
  1085. celVal = dict[tabs[j].Name]?.ToString();
  1086. }
  1087. if (tabs[j].Name == "RunWhetherNormal")
  1088. {
  1089. celVal = celVal.ToLower() == "true" ? "异常" : "正常";
  1090. content.CreateCell(j).SetCellValue(celVal);
  1091. }else if (tabs[j].Name == "Date")
  1092. {
  1093. content.CreateCell(j).SetCellValue(Convert.ToDateTime(celVal).ToString("yyyy-MM-dd"));
  1094. }
  1095. else if(DateTime.TryParse(celVal, out DateTime dt) && celVal.Length >= 10)
  1096. {
  1097. content.CreateCell(j).SetCellValue(dt.ToString("yyyy-MM-dd HH:mm:ss"));
  1098. }
  1099. else
  1100. {
  1101. content.CreateCell(j).SetCellValue(celVal);
  1102. }
  1103. }
  1104. }
  1105. foreach (var item in content.Cells)
  1106. {
  1107. item.CellStyle = rowCellStyle;
  1108. }
  1109. rowNumber++;
  1110. }
  1111. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, tabs.Count-1));
  1112. for (int i = 0; i <= tabs.Count; i++)
  1113. {
  1114. sheet.SetColumnWidth(i, 5000);
  1115. }
  1116. }
  1117. using (MemoryStream stream = new MemoryStream())
  1118. {
  1119. workbook.Write(stream);
  1120. byte[] data = stream.ToArray();
  1121. return File(data, "application/vnd.ms-excel", "设备运行台账.xlsx");
  1122. }
  1123. }
  1124. catch (Exception ex)
  1125. {
  1126. throw;
  1127. }
  1128. }
  1129. }
  1130. }