TdevDevOpeAccountController.cs 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  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. if (!string.IsNullOrEmpty(confCode))
  640. {
  641. IRow head = sheet.CreateRow(1);
  642. IRow head2 = sheet.CreateRow(2);
  643. IRow head3 = sheet.CreateRow(3);
  644. head.Height = 30 * 20;
  645. head2.Height = 30 * 20;
  646. head3.Height = 30 * 20;
  647. if (!string.IsNullOrEmpty(confCode))
  648. {
  649. head.CreateCell(0).SetCellValue("日期");
  650. tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date", BReadDevSpot = false });
  651. sheet.AddMergedRegion(new CellRangeAddress(1, 3, 0, 0));
  652. int cel = 1;
  653. var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
  654. if (content != null && !string.IsNullOrEmpty(content.C_Config))
  655. {
  656. var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
  657. List<RunSpotConfigName> CtabColumns = new List<RunSpotConfigName>();
  658. int headerNum = 1;
  659. foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
  660. {
  661. if (item != null)
  662. {
  663. headerNum = item.Label.Split("@").Count();
  664. if (item.BReadDevSpot)
  665. {
  666. headerNum++;
  667. tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  668. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  669. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  670. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  671. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  672. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  673. }
  674. else
  675. {
  676. tabs.Add(item);
  677. CtabColumns.Add(item);
  678. }
  679. }
  680. }
  681. bool IsVal = false; int merge = 0;
  682. bool IsVal2 = false; int merge2 = 0;
  683. foreach (var tab in CtabColumns)
  684. {
  685. if (tab != null)
  686. {
  687. var vals = tab.Label;
  688. if (vals.Contains("@"))
  689. {
  690. string[] tmp = vals.Split('@');//按,分割字符串
  691. if (tmp.Count() == 3)
  692. {
  693. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  694. var getCel2 = sheet.GetRow(2).GetCell(cel - 1 - merge2).ToString();
  695. if (getCel == tmp[0].ToString())
  696. {
  697. head.CreateCell(cel).SetCellValue("");
  698. merge++;
  699. IsVal = true;
  700. }
  701. else
  702. {
  703. IsVal = false;
  704. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  705. }
  706. if (getCel2 == tmp[1].ToString())
  707. {
  708. head2.CreateCell(cel).SetCellValue("");
  709. merge2++;
  710. IsVal2 = true;
  711. }
  712. else
  713. {
  714. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  715. IsVal2 = false;
  716. }
  717. head3.CreateCell(cel).SetCellValue(tmp[2].ToString());
  718. }
  719. else
  720. {
  721. IsVal2 = false;
  722. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  723. if (getCel == tmp[0].ToString())
  724. {
  725. head.CreateCell(cel).SetCellValue("");
  726. merge++;
  727. IsVal = true;
  728. }
  729. else
  730. {
  731. IsVal = false;
  732. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  733. }
  734. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  735. head3.CreateCell(cel).SetCellValue("");
  736. sheet.AddMergedRegion(new CellRangeAddress(2, 3, cel, cel));
  737. }
  738. }
  739. else
  740. {
  741. IsVal = false;
  742. IsVal2 = false;
  743. head.CreateCell(cel).SetCellValue(vals);
  744. head2.CreateCell(cel).SetCellValue("");
  745. head3.CreateCell(cel).SetCellValue("");
  746. sheet.AddMergedRegion(new CellRangeAddress(1, 3, cel, cel));
  747. }
  748. if (!IsVal && merge > 0)
  749. {
  750. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  751. merge = 0;
  752. }
  753. if (!IsVal2 && merge2 > 0)
  754. {
  755. sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
  756. merge2 = 0;
  757. }
  758. cel++;
  759. }
  760. }
  761. if (cel == CtabColumns.Count + 1 && merge > 0 && IsVal)
  762. {
  763. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  764. merge = 0; IsVal = false;
  765. }
  766. if (cel == CtabColumns.Count + 1 && merge2 > 0 && IsVal2)
  767. {
  768. sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
  769. merge2 = 0; IsVal2 = false;
  770. }
  771. }
  772. }
  773. foreach (var item in head.Cells)
  774. {
  775. item.CellStyle = headCellStyle;
  776. }
  777. foreach (var item in head2.Cells)
  778. {
  779. item.CellStyle = headCellStyle;
  780. }
  781. foreach (var item in head3.Cells)
  782. {
  783. item.CellStyle = headCellStyle;
  784. }
  785. int rowNumber = 4;
  786. var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
  787. for (int i = 0; i < confData.Count; i++)
  788. {
  789. IRow content = sheet.CreateRow(rowNumber);
  790. Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
  791. int forCount = dict.Count + 1;
  792. if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; }
  793. for (int j = 0; j < forCount; j++)
  794. {
  795. if (j < tabs.Count)
  796. {
  797. string celVal = "";
  798. if (dict.ContainsKey(tabs[j].Name))
  799. {
  800. celVal = dict[tabs[j].Name]?.ToString();
  801. }
  802. if (tabs[j].Name == "RunWhetherNormal")
  803. {
  804. celVal = celVal.ToLower() == "true" ? "异常" : "正常";
  805. content.CreateCell(j).SetCellValue(celVal);
  806. }
  807. else if (tabs[j].Name == "Date")
  808. {
  809. content.CreateCell(j).SetCellValue(Convert.ToDateTime(celVal).ToString("yyyy-MM-dd"));
  810. }
  811. else if (DateTime.TryParse(celVal, out DateTime dt) && celVal.Length >= 10)
  812. {
  813. content.CreateCell(j).SetCellValue(dt.ToString("yyyy-MM-dd HH:mm:ss"));
  814. }
  815. else
  816. {
  817. content.CreateCell(j).SetCellValue(celVal);
  818. }
  819. }
  820. }
  821. foreach (var item in content.Cells)
  822. {
  823. item.CellStyle = rowCellStyle;
  824. }
  825. rowNumber++;
  826. }
  827. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, tabs.Count - 1));
  828. for (int i = 0; i <= tabs.Count; i++)
  829. {
  830. sheet.SetColumnWidth(i, 5000);
  831. }
  832. }
  833. else
  834. {
  835. IRow content1 = sheet.CreateRow(1);
  836. content1.CreateCell(0).SetCellValue("获取数据失败,没有选择台账配置!");
  837. content1.Cells[0].CellStyle = headCellStyle;
  838. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15));
  839. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15));
  840. }
  841. }
  842. using (var stream = new NpoiMemoryStream())
  843. {
  844. workbook.Write(stream);
  845. stream.Seek(0, SeekOrigin.Begin);
  846. string emailName = $"{searchModel.DevName} 设备台账记录报表 {searchModel.D_Start?.ToString("yyyy-MM-dd HH:mm:ss")}至{searchModel.D_End?.ToString("yyyy-MM-dd HH:mm:ss")}";
  847. EmailHelper.SendEmail(searchModel.Mails, emailName, "", "报表见附件", $"{emailName}.xlsx", "application/vnd.ms-excel", stream);
  848. }
  849. }
  850. catch (Exception ex)
  851. {
  852. throw;
  853. }
  854. }
  855. /// <summary>
  856. /// 查询运行台账-导出-Excel
  857. /// </summary>
  858. /// <param name="searchModel"></param>
  859. /// <returns></returns>
  860. [HttpPost("DevOpeAccountContentExportExcel")]
  861. [AllowAnonymous]
  862. public async Task<IActionResult> DevOpeAccountContentExportExcel(TdevDevOpeAccountSearchModel searchModel)
  863. {
  864. try
  865. {
  866. var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
  867. IWorkbook workbook = new XSSFWorkbook();
  868. ISheet sheet = workbook.CreateSheet();
  869. #region
  870. //标题加粗居中.
  871. IFont font = workbook.CreateFont();
  872. font.IsBold = true;
  873. font.FontHeightInPoints = 25;
  874. font.FontName = "宋体";
  875. ICellStyle titleCellStyle = workbook.CreateCellStyle();
  876. titleCellStyle.SetFont(font);
  877. titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  878. //边框
  879. titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  880. titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  881. titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  882. titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  883. titleCellStyle.WrapText = true;
  884. #endregion
  885. #region
  886. //标题加粗居中.
  887. IFont font1 = workbook.CreateFont();
  888. font1.IsBold = true;
  889. font1.FontHeightInPoints = 14;
  890. font1.FontName = "宋体";
  891. ICellStyle headCellStyle = workbook.CreateCellStyle();
  892. headCellStyle.SetFont(font1);
  893. headCellStyle.Alignment = HorizontalAlignment.Center; //字体左右居中
  894. headCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
  895. //边框
  896. headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  897. headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  898. headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  899. headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  900. headCellStyle.WrapText = true;
  901. #endregion
  902. #region
  903. //标题加粗居中.
  904. IFont font2 = workbook.CreateFont();
  905. font2.IsBold = false;
  906. font2.FontHeightInPoints = 12;
  907. font2.FontName = "宋体";
  908. ICellStyle rowCellStyle = workbook.CreateCellStyle();
  909. rowCellStyle.SetFont(font2);
  910. rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
  911. //边框
  912. rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
  913. rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
  914. rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
  915. rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
  916. rowCellStyle.WrapText = true;
  917. #endregion
  918. IRow title = sheet.CreateRow(0);
  919. title.Height = 30 * 25;
  920. title.CreateCell(0).SetCellValue("VOCs治理设施基本信息与维保管理信息表");
  921. foreach (var item in title.Cells)
  922. {
  923. item.CellStyle = titleCellStyle;
  924. }
  925. if (devOpeAccountList == null || devOpeAccountList.Count() == 0)
  926. {
  927. IRow content1 = sheet.CreateRow(1);
  928. content1.CreateCell(0).SetCellValue("暂无数据");
  929. content1.Cells[0].CellStyle = headCellStyle;
  930. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15));
  931. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15));
  932. }
  933. else
  934. {
  935. List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
  936. var confCode = searchModel.C_DevOpeAccountConfigCode;
  937. IRow head = sheet.CreateRow(1);
  938. IRow head2 = sheet.CreateRow(2);
  939. IRow head3 = sheet.CreateRow(3);
  940. head.Height = 30 * 20;
  941. head2.Height = 30 * 20;
  942. head3.Height = 30 * 20;
  943. if (!string.IsNullOrEmpty(confCode))
  944. {
  945. head.CreateCell(0).SetCellValue("日期");
  946. tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date",BReadDevSpot=false });
  947. sheet.AddMergedRegion(new CellRangeAddress(1, 3, 0, 0));
  948. int cel = 1;
  949. var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
  950. if (content != null && !string.IsNullOrEmpty(content.C_Config))
  951. {
  952. var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
  953. List<RunSpotConfigName> CtabColumns = new List<RunSpotConfigName>();
  954. int headerNum = 1;
  955. foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
  956. {
  957. if (item != null)
  958. {
  959. headerNum= item.Label.Split("@").Count();
  960. if (item.BReadDevSpot)
  961. {
  962. headerNum++;
  963. tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  964. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  965. tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  966. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
  967. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
  968. CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
  969. }
  970. else
  971. {
  972. tabs.Add(item);
  973. CtabColumns.Add(item);
  974. }
  975. }
  976. }
  977. bool IsVal = false;int merge = 0;
  978. bool IsVal2 = false; int merge2 = 0;
  979. foreach (var tab in CtabColumns)
  980. {
  981. if (tab != null)
  982. {
  983. var vals = tab.Label;
  984. if (vals.Contains("@"))
  985. {
  986. string[] tmp = vals.Split('@');//按,分割字符串
  987. if (tmp.Count()==3)
  988. {
  989. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  990. var getCel2 = sheet.GetRow(2).GetCell(cel - 1 - merge2).ToString();
  991. if (getCel == tmp[0].ToString())
  992. {
  993. head.CreateCell(cel).SetCellValue("");
  994. merge++;
  995. IsVal = true;
  996. }
  997. else
  998. {
  999. IsVal = false;
  1000. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  1001. }
  1002. if (getCel2 == tmp[1].ToString())
  1003. {
  1004. head2.CreateCell(cel).SetCellValue("");
  1005. merge2++;
  1006. IsVal2 = true;
  1007. }
  1008. else
  1009. {
  1010. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  1011. IsVal2 = false;
  1012. }
  1013. head3.CreateCell(cel).SetCellValue(tmp[2].ToString());
  1014. }
  1015. else
  1016. {
  1017. IsVal2 = false;
  1018. var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
  1019. if (getCel == tmp[0].ToString())
  1020. {
  1021. head.CreateCell(cel).SetCellValue("");
  1022. merge++;
  1023. IsVal = true;
  1024. }
  1025. else
  1026. {
  1027. IsVal = false;
  1028. head.CreateCell(cel).SetCellValue(tmp[0].ToString());
  1029. }
  1030. head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
  1031. head3.CreateCell(cel).SetCellValue("");
  1032. sheet.AddMergedRegion(new CellRangeAddress(2, 3, cel, cel));
  1033. }
  1034. }
  1035. else
  1036. {
  1037. IsVal = false;
  1038. IsVal2 = false;
  1039. head.CreateCell(cel).SetCellValue(vals);
  1040. head2.CreateCell(cel).SetCellValue("");
  1041. head3.CreateCell(cel).SetCellValue("");
  1042. sheet.AddMergedRegion(new CellRangeAddress(1, 3, cel, cel));
  1043. }
  1044. if (!IsVal && merge > 0)
  1045. {
  1046. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  1047. merge = 0;
  1048. }
  1049. if (!IsVal2 && merge2 > 0)
  1050. {
  1051. sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
  1052. merge2 = 0;
  1053. }
  1054. cel++;
  1055. }
  1056. }
  1057. if (cel== CtabColumns.Count+1&& merge>0&& IsVal)
  1058. {
  1059. sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
  1060. merge = 0; IsVal = false;
  1061. }
  1062. if (cel == CtabColumns.Count + 1 && merge2 > 0 && IsVal2)
  1063. {
  1064. sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
  1065. merge2 = 0; IsVal2 = false;
  1066. }
  1067. }
  1068. }
  1069. foreach (var item in head.Cells)
  1070. {
  1071. item.CellStyle = headCellStyle;
  1072. }
  1073. foreach (var item in head2.Cells)
  1074. {
  1075. item.CellStyle = headCellStyle;
  1076. }
  1077. foreach (var item in head3.Cells)
  1078. {
  1079. item.CellStyle = headCellStyle;
  1080. }
  1081. int rowNumber = 4;
  1082. var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
  1083. for (int i = 0; i < confData.Count; i++)
  1084. {
  1085. IRow content = sheet.CreateRow(rowNumber);
  1086. Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
  1087. int forCount = dict.Count + 1;
  1088. if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; }
  1089. for (int j = 0; j < forCount; j++)
  1090. {
  1091. if (j < tabs.Count)
  1092. {
  1093. string celVal = "";
  1094. if (dict.ContainsKey(tabs[j].Name))
  1095. {
  1096. celVal = dict[tabs[j].Name]?.ToString();
  1097. }
  1098. if (tabs[j].Name == "RunWhetherNormal")
  1099. {
  1100. celVal = celVal.ToLower() == "true" ? "异常" : "正常";
  1101. content.CreateCell(j).SetCellValue(celVal);
  1102. }else if (tabs[j].Name == "Date")
  1103. {
  1104. content.CreateCell(j).SetCellValue(Convert.ToDateTime(celVal).ToString("yyyy-MM-dd"));
  1105. }
  1106. else if(DateTime.TryParse(celVal, out DateTime dt) && celVal.Length >= 10)
  1107. {
  1108. content.CreateCell(j).SetCellValue(dt.ToString("yyyy-MM-dd HH:mm:ss"));
  1109. }
  1110. else
  1111. {
  1112. content.CreateCell(j).SetCellValue(celVal);
  1113. }
  1114. }
  1115. }
  1116. foreach (var item in content.Cells)
  1117. {
  1118. item.CellStyle = rowCellStyle;
  1119. }
  1120. rowNumber++;
  1121. }
  1122. sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, tabs.Count-1));
  1123. for (int i = 0; i <= tabs.Count; i++)
  1124. {
  1125. sheet.SetColumnWidth(i, 5000);
  1126. }
  1127. }
  1128. using (MemoryStream stream = new MemoryStream())
  1129. {
  1130. workbook.Write(stream);
  1131. byte[] data = stream.ToArray();
  1132. return File(data, "application/vnd.ms-excel", "设备运行台账.xlsx");
  1133. }
  1134. }
  1135. catch (Exception ex)
  1136. {
  1137. throw;
  1138. }
  1139. }
  1140. }
  1141. }