12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166 |
- using FluentEmail.Core;
- using Microsoft.AspNetCore.Authorization;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Extensions.Logging;
- using Newtonsoft.Json;
- using NPOI.HPSF;
- using NPOI.SS.Formula.Functions;
- using NPOI.SS.UserModel;
- using NPOI.SS.Util;
- using NPOI.XSSF.UserModel;
- using Ropin.Inspection.Api.Common;
- using Ropin.Inspection.Api.Controllers;
- using Ropin.Inspection.Common.Helper;
- using Ropin.Inspection.Model;
- using Ropin.Inspection.Model.Common;
- using Ropin.Inspection.Model.ViewModel.DEV;
- using Ropin.Inspection.Service;
- using Ropin.Inspection.Service.DEV.Interface;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Drawing.Printing;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Net;
- using System.Net.Http;
- using System.Runtime.ConstrainedExecution;
- using System.Threading.Tasks;
- namespace Ropin.Inspection.Api
- {
- public class TdevDevOpeAccountController : BaseController
- {
- public ILogger<TdevDevOpeAccountController> _logger { get; }
- private readonly ITdevDevOpeAccountService _TdevDevOpeAccountService;
- private readonly IDevDevOpeAccountConfigService _devDevOpeAccountConfigService;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="TdevDevOpeAccountService"></param>
- /// <param name="logger"></param>
- public TdevDevOpeAccountController(ITdevDevOpeAccountService TdevDevOpeAccountService, IDevDevOpeAccountConfigService devDevOpeAccountConfigService, ILogger<TdevDevOpeAccountController> logger)
- {
- _TdevDevOpeAccountService = TdevDevOpeAccountService;
- _logger = logger;
- _devDevOpeAccountConfigService = devDevOpeAccountConfigService;
- }
- /// <summary>
- /// 通过id获取业主设备运行台账信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [HttpGet("GetDevOpeAccountAsync/{id}")]
- public async Task<ApiResult> GetDevOpeAccountAsync(string id)
- {
- if (string.IsNullOrEmpty(id))
- {
- return new ApiResult(ReturnCode.GeneralError);
- }
- try
- {
- var content = await _TdevDevOpeAccountService.GetConditionAsync(new TdevDevOpeAccountSearchModel { C_ID = id });
- return new ApiResult<TdevDevOpeAccountViewModel>(content.FirstOrDefault());
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 获取所有业主设备运行台账
- /// </summary>
- /// <returns></returns>
- [HttpGet("GetDevOpeAccountsAsync")]
- public async Task<ApiResult> GetDevOpeAccountsAsync()
- {
- try
- {
- var contentList = await _TdevDevOpeAccountService.GetAllAsync();
- return new ApiResult<IEnumerable<TdevDevOpeAccountViewModel>>(contentList);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 通过业主设备运行台账名称条件查询
- /// </summary>
- /// <param name="searchModel"></param>
- /// <returns></returns>
- [HttpPost("GetDevOpeAccountsByAsync")]
- public async Task<ApiResult> GetDevOpeAccountsByAsync(TdevDevOpeAccountSearchModel searchModel)
- {
- if (searchModel == null)
- {
- return new ApiResult(ReturnCode.ArgsError);
- }
- searchModel.IsPagination = false;
- try
- {
- var contentList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
- return new ApiResult<PagesModel<TdevDevOpeAccountViewModel>>(new PagesModel<TdevDevOpeAccountViewModel>(contentList, searchModel));
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 查询运行台账
- /// </summary>
- /// <param name="searchModel"></param>
- /// <returns></returns>
- [HttpPost("GetDevOpeAccountContentByAsync")]
- [AllowAnonymous]
- public async Task<ApiResult> GetDevOpeAccountContentByAsync(TdevDevOpeAccountSearchModel searchModel)
- {
- if (searchModel == null)
- {
- return new ApiResult(ReturnCode.ArgsError);
- }
- try
- {
- var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
- if (devOpeAccountList==null|| devOpeAccountList.Count()==0)
- {
- return new ApiResult<object>(new
- {
- tabName = new string[0],
- pages = new PagesModel<object>(null, searchModel)
- });
- }
- List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
- PagesModel<object> pages = null;
- var confCode = searchModel.C_DevOpeAccountConfigCode;
- if (!string.IsNullOrEmpty(confCode))
- {
- tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date", BReadDevSpot=false });
- var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
- if (content != null && !string.IsNullOrEmpty(content.C_Config))
- {
- var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
- foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
- {
- if (item!=null)
- {
- if (item.BReadDevSpot)
- {
- //string labVal = "";string labVal1 ="";
- //if (item.Label.Contains('(')|| item.Label.Contains('('))
- //{
- // labVal = item.Label.Replace("(","(").Replace("(","最大值(");
- // labVal1 = item.Label.Replace("(", "(").Replace("(", "最小值(");
- //}
- //else
- //{
- // labVal = item.Label + "最大值";
- // labVal1 = item.Label + "最小值";
- //}
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
- tabs.Add(new RunSpotConfigName { Label = item.Label+ "@最大值", Name = item.Name+ "_Max", BReadDevSpot = item.BReadDevSpot });
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
- }
- else
- {
- tabs.Add(item);
- }
- }
- }
- }
- }
- var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
- List<object> list = new List<object>();
- for (int i = 0; i < confData.Count; i++)
- {
- Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
- if (dict.ContainsKey("RunWhetherNormal"))
- {
- var RunWhetherNormal = dict["RunWhetherNormal"].ToString();
- dict["RunWhetherNormal"] = RunWhetherNormal.ToLower() == "true" ? "异常" : "正常";
- }
- var trueList = tabs?.Where(t => t.BReadDevSpot == true);
- foreach (var item in trueList)
- {
- if (item!=null)
- {
- if (!dict.ContainsKey(item.Name))
- {
- dict.Add(item.Name, "0");
- }
- }
- }
- foreach (var item in dict)
- {
- if (item.Key?.ToString()== "Date")
- {
- dict[item.Key] = Convert.ToDateTime(item.Value).ToString("yyyy-MM-dd");
- }
- else
- {
- if (item.Value != null)
- {
- if (DateTime.TryParse(item.Value.ToString(), out DateTime dt) && item.Value.ToString().Length >= 10)
- {
- dict[item.Key] = dt.ToString("yyyy-MM-dd HH:mm:ss");
- }
- }
- }
- }
- list.Add(dict);
- }
- pages = new PagesModel<object>(list, searchModel);
- //var tabArray = tabs.Select(t => t.Label).ToArray();
- List<DevOpeAccountConfigTableHeadeModel> tabList = new List<DevOpeAccountConfigTableHeadeModel>();
- for (int i = 0; i < tabs.Count; i++)
- {
- var vals = tabs[i].Label.ToString();
- var name = tabs[i].Name.ToString();
- if (vals.Contains("@"))
- {
- string[] tmp = vals.Split('@');//按,分割字符串
- var TableHeadeModel = tabList.Find(t => t.label == tmp[0].ToString());
- if (TableHeadeModel!=null)
- {
- if (TableHeadeModel.children!=null&& TableHeadeModel.children.Count>0)
- {
- if (tmp.Count() == 2)
- {
- TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[1].ToString(), Level = 2, children = null });
- }
- if (tmp.Count() == 3)
- {
- var childModel = TableHeadeModel.children.Find(x => x.label == tmp[1].ToString());
- if (childModel!=null)
- {
- if (childModel.children==null)
- {
- childModel.children = new List<DevOpeAccountConfigTableHeadeModel>();
- }
- childModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children = null });
- }
- else
- {
- List<DevOpeAccountConfigTableHeadeModel> chile = null;
- chile = new List<DevOpeAccountConfigTableHeadeModel>();
- chile.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children = null });
- TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[1].ToString(), Level = 2, children = chile });
- }
- }
- }
- else
- {
- TableHeadeModel.children = new List<DevOpeAccountConfigTableHeadeModel>();
- if (tmp.Count() == 2)
- {
- TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[1].ToString(), Level = 2, children = null });
- }
- if (tmp.Count() == 3)
- {
- List<DevOpeAccountConfigTableHeadeModel> chile = null;
- chile = new List<DevOpeAccountConfigTableHeadeModel>();
- chile.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children = null });
- TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[1].ToString(), Level = 2, children = chile });
- }
- }
- }
- else
- {
- var cc = new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[0].ToString(),Level=1, children = new List<DevOpeAccountConfigTableHeadeModel>()};
- if (tmp.Count() == 2)
- {
- cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[1].ToString(), Level = 2, children = null });
- }
- if (tmp.Count()==3)
- {
- List<DevOpeAccountConfigTableHeadeModel> chile = null;
- chile = new List<DevOpeAccountConfigTableHeadeModel>();
- chile.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label = tmp[2].ToString(), Level = 3, children =null});
- cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = null, label = tmp[1].ToString(), Level = 2, children = chile });
- }
- tabList.Add(cc);
- }
- }
- else
- {
- tabList.Add(new DevOpeAccountConfigTableHeadeModel { prop = name, label=vals, Level = 1, children = null});
- }
- }
- var data = new
- {
- DevOpeAccountConfigCode= confCode,
- tabName = tabs.Select(t => t.Label).ToArray(),
- tabArray= tabList,
- pages
- };
- return new ApiResult<object>(data);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 查询运行台账-导出
- /// </summary>
- /// <param name="searchModel"></param>
- /// <returns></returns>
- [HttpPost("GetDevOpeAccountContentExportByAsync")]
- [AllowAnonymous]
- public async Task<ApiResult> GetDevOpeAccountContentExportByAsync(TdevDevOpeAccountSearchModel searchModel)
- {
- if (searchModel == null)
- {
- return new ApiResult(ReturnCode.ArgsError);
- }
- try
- {
- var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
- if (devOpeAccountList == null || devOpeAccountList.Count() == 0)
- {
- return new ApiResult<object>(new
- {
- tabName = new string[0],
- pages = new PagesModel<object>(null, searchModel)
- });
- }
- List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
- PagesModel<object> pages = null;
- var confCode = searchModel.C_DevOpeAccountConfigCode;
- if (!string.IsNullOrEmpty(confCode))
- {
- tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date" ,BReadDevSpot=false });
- var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
- if (content != null && !string.IsNullOrEmpty(content.C_Config))
- {
- var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
- foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
- {
- if (item != null)
- {
- if (item.BReadDevSpot)
- {
- //string labVal = ""; string labVal1 = "";
- //if (item.Label.Contains('(') || item.Label.Contains('('))
- //{
- // labVal = item.Label.Replace("(", "(").Replace("(", "最大值(");
- // labVal1 = item.Label.Replace("(", "(").Replace("(", "最小值(");
- //}
- //else
- //{
- // labVal = item.Label + "最大值";
- // labVal1 = item.Label + "最小值";
- //}
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
- }
- else
- {
- tabs.Add(item);
- }
- }
- }
- }
- }
- var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
- List<object> list = new List<object>();
- for (int i = 0; i < confData.Count; i++)
- {
- Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
- string[] cels = new string[tabs.Count];
- int forCount = dict.Count + 1;
- if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; }
- for (int j = 0; j < forCount; j++)
- {
- if (j < tabs.Count)
- {
- string itemVal = "";
- if (dict.ContainsKey(tabs[j].Name))
- {
- itemVal = dict[tabs[j].Name]?.ToString();
- }
- if (tabs[j].Name== "RunWhetherNormal")
- {
- cels[j] = itemVal?.ToLower() == "true" ? "异常" : "正常";
- }
- else if (tabs[j].Name == "Date")
- {
- cels[j] = Convert.ToDateTime(itemVal).ToString("yyyy-MM-dd");
- }
- else
- {
- if (DateTime.TryParse(itemVal, out DateTime dt) && itemVal.Length >= 10)
- {
- cels[j] = dt.ToString("yyyy-MM-dd HH:mm:ss");
- }
- else
- {
- cels[j] = itemVal;
- }
- }
- }
- }
- list.Add(cels);
- }
- pages = new PagesModel<object>(list, searchModel);
- //var tabArray = tabs.Select(t => t.Label).ToArray();
- List<DevOpeAccountConfigTableHeadeModel> tabList = new List<DevOpeAccountConfigTableHeadeModel>();
- for (int i = 0; i < tabs.Count; i++)
- {
- var vals = tabs[i].Label.ToString();
- var name = tabs[i].Name.ToString();
- if (vals.Contains("@"))
- {
- string[] tmp = vals.Split('@');//按,分割字符串
- var TableHeadeModel = tabList.Find(t => t.prop == tmp[0].ToString());
- if (TableHeadeModel != null)
- {
- if (TableHeadeModel.children != null && TableHeadeModel.children.Count > 0)
- {
- TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null });
- }
- else
- {
- TableHeadeModel.children = new List<DevOpeAccountConfigTableHeadeModel>();
- TableHeadeModel.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null });
- }
- }
- else
- {
- var cc = new DevOpeAccountConfigTableHeadeModel { prop = tmp[0].ToString(), label = tmp[0].ToString(), Level = 1, children = new List<DevOpeAccountConfigTableHeadeModel>() };
- cc.children.Add(new DevOpeAccountConfigTableHeadeModel { prop = tmp[1].ToString(), label = tmp[1].ToString(), Level = 2, children = null });
- tabList.Add(cc);
- }
- }
- else
- {
- tabList.Add(new DevOpeAccountConfigTableHeadeModel { prop = vals, label = vals, Level = 1, children = null });
- }
- }
- var data = new
- {
- DevOpeAccountConfigCode = confCode,
- tabName = tabs.Select(t => t.Label).ToArray(),
- tabArray = tabList,
- pages
- };
- return new ApiResult<object>(data);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 查询运行台账-【返回配置集合】
- /// </summary>
- /// <param name="searchModel"></param>
- /// <returns></returns>
- [HttpPost("GetDevOpeAccountConfingAsync")]
- [AllowAnonymous]
- public async Task<ApiResult> GetDevOpeAccountConfingAsync(TdevDevOpeAccountSearchModel searchModel)
- {
- if (searchModel == null)
- {
- return new ApiResult(ReturnCode.ArgsError);
- }
- try
- {
- PagesModel<object> pages = null;
- List<DevOpeAccountConfigSelect> select = await _TdevDevOpeAccountService.GetDevOpeAccountConfigSelectAsync(searchModel);
- pages = new PagesModel<object>(select, searchModel);
- var data = new
- {
- pages
- };
- return new ApiResult<object>(data);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- }
- /// <summary>
- /// 创建业主设备运行台账
- /// </summary>
- /// <param name="content"></param>
- /// <returns></returns>
- [HttpPost("CreateDevOpeAccountAsync")]
- public async Task<ApiResult> CreateDevOpeAccountAsync(TdevDevOpeAccountViewModel content)
- {
- if (content == null)
- {
- return new ApiResult(ReturnCode.ArgsError);
- }
- try
- {
- await _TdevDevOpeAccountService.CreateOneAsync(content);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- return new ApiResult(ReturnCode.Success);
- }
- /// <summary>
- /// 删除业主设备运行台账
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- [HttpDelete("DeleteDevOpeAccountAsync/{id}")]
- public async Task<ApiResult> DeleteDevOpeAccountAsync(string id)
- {
- if (string.IsNullOrEmpty(id))
- {
- return new ApiResult(ReturnCode.GeneralError);
- }
- try
- {
- await _TdevDevOpeAccountService.DeleteAsync(id);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- return new ApiResult(ReturnCode.Success);
- }
- /// <summary>
- /// 更新业主设备运行台账
- /// </summary>
- /// <param name="id"></param>
- /// <param name="updateModel"></param>
- /// <returns></returns>
- [HttpPut("UpdateDevOpeAccountAsync/{id}")]
- public async Task<ApiResult> UpdateDevOpeAccountAsync(string id, TdevDevOpeAccountUpdateModel updateModel)
- {
- if (string.IsNullOrEmpty(id))
- {
- return new ApiResult(ReturnCode.GeneralError);
- }
- try
- {
- await _TdevDevOpeAccountService.UpdateAsync(id, updateModel);
- }
- catch (Exception ex)
- {
- return new ApiResult(ReturnCode.GeneralError, ex.Message);
- }
- return new ApiResult(ReturnCode.Success);
- }
- /// <summary>
- /// 发送邮件(附件为台帐)
- /// </summary>
- /// <param name="searchModel"></param>
- /// <returns></returns>
- [HttpPost("SendDevOpeAccountToEmail")]
- [AllowAnonymous]
- public async Task SendDevOpeAccountToEmail(SendDevOpeAccountToEmailModel searchModel)
- {
- if (searchModel == null)
- {
- return;
- }
- try
- {
- var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
- IWorkbook workbook = new XSSFWorkbook();
- ISheet sheet = workbook.CreateSheet();
- #region
- //标题加粗居中.
- IFont font = workbook.CreateFont();
- font.IsBold = true;
- font.FontHeightInPoints = 25;
- font.FontName = "宋体";
- ICellStyle titleCellStyle = workbook.CreateCellStyle();
- titleCellStyle.SetFont(font);
- titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
- //边框
- titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- titleCellStyle.WrapText = true;
- #endregion
- #region
- //标题加粗居中.
- IFont font1 = workbook.CreateFont();
- font1.IsBold = true;
- font1.FontHeightInPoints = 14;
- font1.FontName = "宋体";
- ICellStyle headCellStyle = workbook.CreateCellStyle();
- headCellStyle.SetFont(font1);
- headCellStyle.Alignment = HorizontalAlignment.Center; //字体左右居中
- headCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
- //边框
- headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- headCellStyle.WrapText = true;
- #endregion
- #region
- //标题加粗居中.
- IFont font2 = workbook.CreateFont();
- font2.IsBold = false;
- font2.FontHeightInPoints = 12;
- font2.FontName = "宋体";
- ICellStyle rowCellStyle = workbook.CreateCellStyle();
- rowCellStyle.SetFont(font2);
- rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
- //边框
- rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- rowCellStyle.WrapText = true;
- #endregion
- IRow title = sheet.CreateRow(0);
- title.Height = 30 * 25;
- title.CreateCell(0).SetCellValue("VOCs治理设施基本信息与维保管理信息表");
- foreach (var item in title.Cells)
- {
- item.CellStyle = titleCellStyle;
- }
- if (devOpeAccountList == null || devOpeAccountList.Count() == 0)
- {
- IRow content1 = sheet.CreateRow(1);
- content1.CreateCell(0).SetCellValue("暂无数据");
- content1.Cells[0].CellStyle = headCellStyle;
- sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15));
- sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15));
- }
- else
- {
- List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
- var confCode = searchModel.C_DevOpeAccountConfigCode;
- if (!string.IsNullOrEmpty(confCode))
- {
- IRow head = sheet.CreateRow(1);
- IRow head2 = sheet.CreateRow(2);
- IRow head3 = sheet.CreateRow(3);
- head.Height = 30 * 20;
- head2.Height = 30 * 20;
- head3.Height = 30 * 20;
- if (!string.IsNullOrEmpty(confCode))
- {
- head.CreateCell(0).SetCellValue("日期");
- tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date", BReadDevSpot = false });
- sheet.AddMergedRegion(new CellRangeAddress(1, 3, 0, 0));
- int cel = 1;
- var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
- if (content != null && !string.IsNullOrEmpty(content.C_Config))
- {
- var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
- List<RunSpotConfigName> CtabColumns = new List<RunSpotConfigName>();
- int headerNum = 1;
- foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
- {
- if (item != null)
- {
- headerNum = item.Label.Split("@").Count();
- if (item.BReadDevSpot)
- {
- headerNum++;
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
- CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
- CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
- CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
- }
- else
- {
- tabs.Add(item);
- CtabColumns.Add(item);
- }
- }
- }
- bool IsVal = false; int merge = 0;
- bool IsVal2 = false; int merge2 = 0;
- foreach (var tab in CtabColumns)
- {
- if (tab != null)
- {
- var vals = tab.Label;
- if (vals.Contains("@"))
- {
- string[] tmp = vals.Split('@');//按,分割字符串
- if (tmp.Count() == 3)
- {
- var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
- var getCel2 = sheet.GetRow(2).GetCell(cel - 1 - merge2).ToString();
- if (getCel == tmp[0].ToString())
- {
- head.CreateCell(cel).SetCellValue("");
- merge++;
- IsVal = true;
- }
- else
- {
- IsVal = false;
- head.CreateCell(cel).SetCellValue(tmp[0].ToString());
- }
- if (getCel2 == tmp[1].ToString())
- {
- head2.CreateCell(cel).SetCellValue("");
- merge2++;
- IsVal2 = true;
- }
- else
- {
- head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
- IsVal2 = false;
- }
- head3.CreateCell(cel).SetCellValue(tmp[2].ToString());
- }
- else
- {
- IsVal2 = false;
- var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
- if (getCel == tmp[0].ToString())
- {
- head.CreateCell(cel).SetCellValue("");
- merge++;
- IsVal = true;
- }
- else
- {
- IsVal = false;
- head.CreateCell(cel).SetCellValue(tmp[0].ToString());
- }
- head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
- head3.CreateCell(cel).SetCellValue("");
- sheet.AddMergedRegion(new CellRangeAddress(2, 3, cel, cel));
- }
- }
- else
- {
- IsVal = false;
- IsVal2 = false;
- head.CreateCell(cel).SetCellValue(vals);
- head2.CreateCell(cel).SetCellValue("");
- head3.CreateCell(cel).SetCellValue("");
- sheet.AddMergedRegion(new CellRangeAddress(1, 3, cel, cel));
- }
- if (!IsVal && merge > 0)
- {
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
- merge = 0;
- }
- if (!IsVal2 && merge2 > 0)
- {
- sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
- merge2 = 0;
- }
- cel++;
- }
- }
- if (cel == CtabColumns.Count + 1 && merge > 0 && IsVal)
- {
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
- merge = 0; IsVal = false;
- }
- if (cel == CtabColumns.Count + 1 && merge2 > 0 && IsVal2)
- {
- sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
- merge2 = 0; IsVal2 = false;
- }
- }
- }
- foreach (var item in head.Cells)
- {
- item.CellStyle = headCellStyle;
- }
- foreach (var item in head2.Cells)
- {
- item.CellStyle = headCellStyle;
- }
- foreach (var item in head3.Cells)
- {
- item.CellStyle = headCellStyle;
- }
- int rowNumber = 4;
- var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
- for (int i = 0; i < confData.Count; i++)
- {
- IRow content = sheet.CreateRow(rowNumber);
- Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
- int forCount = dict.Count + 1;
- if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; }
- for (int j = 0; j < forCount; j++)
- {
- if (j < tabs.Count)
- {
- string celVal = "";
- if (dict.ContainsKey(tabs[j].Name))
- {
- celVal = dict[tabs[j].Name]?.ToString();
- }
- if (tabs[j].Name == "RunWhetherNormal")
- {
- celVal = celVal.ToLower() == "true" ? "异常" : "正常";
- content.CreateCell(j).SetCellValue(celVal);
- }
- else if (tabs[j].Name == "Date")
- {
- content.CreateCell(j).SetCellValue(Convert.ToDateTime(celVal).ToString("yyyy-MM-dd"));
- }
- else if (DateTime.TryParse(celVal, out DateTime dt) && celVal.Length >= 10)
- {
- content.CreateCell(j).SetCellValue(dt.ToString("yyyy-MM-dd HH:mm:ss"));
- }
- else
- {
- content.CreateCell(j).SetCellValue(celVal);
- }
- }
- }
- foreach (var item in content.Cells)
- {
- item.CellStyle = rowCellStyle;
- }
- rowNumber++;
- }
- sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, tabs.Count - 1));
- for (int i = 0; i <= tabs.Count; i++)
- {
- sheet.SetColumnWidth(i, 5000);
- }
- }
- else
- {
- IRow content1 = sheet.CreateRow(1);
- content1.CreateCell(0).SetCellValue("获取数据失败,没有选择台账配置!");
- content1.Cells[0].CellStyle = headCellStyle;
- sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15));
- sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15));
- }
- }
- using (var stream = new NpoiMemoryStream())
- {
- workbook.Write(stream);
- stream.Seek(0, SeekOrigin.Begin);
- string emailName = $"{searchModel.DevName} 设备台账记录报表 {searchModel.D_Start?.ToString("yyyy-MM-dd HH:mm:ss")}至{searchModel.D_End?.ToString("yyyy-MM-dd HH:mm:ss")}";
- EmailHelper.SendEmail(searchModel.Mails, emailName, "", "报表见附件", $"{emailName}.xlsx", "application/vnd.ms-excel", stream);
- }
- }
- catch (Exception ex)
- {
- throw;
- }
- }
- /// <summary>
- /// 查询运行台账-导出-Excel
- /// </summary>
- /// <param name="searchModel"></param>
- /// <returns></returns>
- [HttpPost("DevOpeAccountContentExportExcel")]
- [AllowAnonymous]
- public async Task<IActionResult> DevOpeAccountContentExportExcel(TdevDevOpeAccountSearchModel searchModel)
- {
- try
- {
- var devOpeAccountList = await _TdevDevOpeAccountService.GetConditionAsync(searchModel);
- IWorkbook workbook = new XSSFWorkbook();
- ISheet sheet = workbook.CreateSheet();
- #region
- //标题加粗居中.
- IFont font = workbook.CreateFont();
- font.IsBold = true;
- font.FontHeightInPoints = 25;
- font.FontName = "宋体";
- ICellStyle titleCellStyle = workbook.CreateCellStyle();
- titleCellStyle.SetFont(font);
- titleCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
- //边框
- titleCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- titleCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- titleCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- titleCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- titleCellStyle.WrapText = true;
- #endregion
- #region
- //标题加粗居中.
- IFont font1 = workbook.CreateFont();
- font1.IsBold = true;
- font1.FontHeightInPoints = 14;
- font1.FontName = "宋体";
- ICellStyle headCellStyle = workbook.CreateCellStyle();
- headCellStyle.SetFont(font1);
- headCellStyle.Alignment = HorizontalAlignment.Center; //字体左右居中
- headCellStyle.VerticalAlignment = VerticalAlignment.Center;//字体上下居中
- //边框
- headCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- headCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- headCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- headCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- headCellStyle.WrapText = true;
- #endregion
- #region
- //标题加粗居中.
- IFont font2 = workbook.CreateFont();
- font2.IsBold = false;
- font2.FontHeightInPoints = 12;
- font2.FontName = "宋体";
- ICellStyle rowCellStyle = workbook.CreateCellStyle();
- rowCellStyle.SetFont(font2);
- rowCellStyle.Alignment = HorizontalAlignment.Center; //字体居中
- //边框
- rowCellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
- rowCellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
- rowCellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
- rowCellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
- rowCellStyle.WrapText = true;
- #endregion
-
- IRow title = sheet.CreateRow(0);
- title.Height = 30 * 25;
- title.CreateCell(0).SetCellValue("VOCs治理设施基本信息与维保管理信息表");
- foreach (var item in title.Cells)
- {
- item.CellStyle = titleCellStyle;
- }
- if (devOpeAccountList == null || devOpeAccountList.Count() == 0)
- {
- IRow content1 = sheet.CreateRow(1);
- content1.CreateCell(0).SetCellValue("暂无数据");
- content1.Cells[0].CellStyle = headCellStyle;
- sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(1, 1, 0, 15));
- sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, 15));
- }
- else
- {
- List<RunSpotConfigName> tabs = new List<RunSpotConfigName>();
- var confCode = searchModel.C_DevOpeAccountConfigCode;
- IRow head = sheet.CreateRow(1);
- IRow head2 = sheet.CreateRow(2);
- IRow head3 = sheet.CreateRow(3);
- head.Height = 30 * 20;
- head2.Height = 30 * 20;
- head3.Height = 30 * 20;
- if (!string.IsNullOrEmpty(confCode))
- {
- head.CreateCell(0).SetCellValue("日期");
- tabs.Add(new RunSpotConfigName { Label = "日期", Name = "Date",BReadDevSpot=false });
- sheet.AddMergedRegion(new CellRangeAddress(1, 3, 0, 0));
- int cel = 1;
- var content = await _devDevOpeAccountConfigService.GetEntityByID(confCode);
- if (content != null && !string.IsNullOrEmpty(content.C_Config))
- {
- var RunSpotConfig = JsonConvert.DeserializeObject<TdevDevOpeAccountConfigNameViewModel>(content.C_Config);
- List<RunSpotConfigName> CtabColumns = new List<RunSpotConfigName>();
- int headerNum = 1;
- foreach (var item in RunSpotConfig?.RunSpotConfigList.ToList())
- {
- if (item != null)
- {
- headerNum= item.Label.Split("@").Count();
- if (item.BReadDevSpot)
- {
- headerNum++;
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
- tabs.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
- CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@平均值", Name = item.Name, BReadDevSpot = item.BReadDevSpot });
- CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最大值", Name = item.Name + "_Max", BReadDevSpot = item.BReadDevSpot });
- CtabColumns.Add(new RunSpotConfigName { Label = item.Label + "@最小值", Name = item.Name + "_Min", BReadDevSpot = item.BReadDevSpot });
- }
- else
- {
- tabs.Add(item);
- CtabColumns.Add(item);
- }
- }
- }
- bool IsVal = false;int merge = 0;
- bool IsVal2 = false; int merge2 = 0;
- foreach (var tab in CtabColumns)
- {
- if (tab != null)
- {
- var vals = tab.Label;
- if (vals.Contains("@"))
- {
- string[] tmp = vals.Split('@');//按,分割字符串
- if (tmp.Count()==3)
- {
- var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
- var getCel2 = sheet.GetRow(2).GetCell(cel - 1 - merge2).ToString();
- if (getCel == tmp[0].ToString())
- {
- head.CreateCell(cel).SetCellValue("");
- merge++;
- IsVal = true;
- }
- else
- {
- IsVal = false;
- head.CreateCell(cel).SetCellValue(tmp[0].ToString());
- }
- if (getCel2 == tmp[1].ToString())
- {
- head2.CreateCell(cel).SetCellValue("");
- merge2++;
- IsVal2 = true;
- }
- else
- {
- head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
- IsVal2 = false;
- }
- head3.CreateCell(cel).SetCellValue(tmp[2].ToString());
- }
- else
- {
- IsVal2 = false;
- var getCel = sheet.GetRow(1).GetCell(cel - 1 - merge).ToString();
- if (getCel == tmp[0].ToString())
- {
- head.CreateCell(cel).SetCellValue("");
- merge++;
- IsVal = true;
- }
- else
- {
- IsVal = false;
- head.CreateCell(cel).SetCellValue(tmp[0].ToString());
- }
- head2.CreateCell(cel).SetCellValue(tmp[1].ToString());
- head3.CreateCell(cel).SetCellValue("");
- sheet.AddMergedRegion(new CellRangeAddress(2, 3, cel, cel));
- }
- }
- else
- {
- IsVal = false;
- IsVal2 = false;
- head.CreateCell(cel).SetCellValue(vals);
- head2.CreateCell(cel).SetCellValue("");
- head3.CreateCell(cel).SetCellValue("");
- sheet.AddMergedRegion(new CellRangeAddress(1, 3, cel, cel));
- }
- if (!IsVal && merge > 0)
- {
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
- merge = 0;
- }
- if (!IsVal2 && merge2 > 0)
- {
- sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
- merge2 = 0;
- }
- cel++;
- }
- }
- if (cel== CtabColumns.Count+1&& merge>0&& IsVal)
- {
- sheet.AddMergedRegion(new CellRangeAddress(1, 1, cel - merge - 1, cel - 1));
- merge = 0; IsVal = false;
- }
- if (cel == CtabColumns.Count + 1 && merge2 > 0 && IsVal2)
- {
- sheet.AddMergedRegion(new CellRangeAddress(2, 2, cel - merge2 - 1, cel - 1));
- merge2 = 0; IsVal2 = false;
- }
- }
- }
- foreach (var item in head.Cells)
- {
- item.CellStyle = headCellStyle;
- }
- foreach (var item in head2.Cells)
- {
- item.CellStyle = headCellStyle;
- }
- foreach (var item in head3.Cells)
- {
- item.CellStyle = headCellStyle;
- }
- int rowNumber = 4;
- var confData = devOpeAccountList.Select(t => t.C_Content).ToList();
- for (int i = 0; i < confData.Count; i++)
- {
- IRow content = sheet.CreateRow(rowNumber);
- Dictionary<string, object> dict = JsonConvert.DeserializeObject<Dictionary<string, object>>(confData[i]);
- int forCount = dict.Count + 1;
- if (tabs.Count() > dict.Count) { forCount = tabs.Count() + 1; }
- for (int j = 0; j < forCount; j++)
- {
- if (j < tabs.Count)
- {
- string celVal = "";
- if (dict.ContainsKey(tabs[j].Name))
- {
- celVal = dict[tabs[j].Name]?.ToString();
- }
- if (tabs[j].Name == "RunWhetherNormal")
- {
- celVal = celVal.ToLower() == "true" ? "异常" : "正常";
- content.CreateCell(j).SetCellValue(celVal);
- }else if (tabs[j].Name == "Date")
- {
- content.CreateCell(j).SetCellValue(Convert.ToDateTime(celVal).ToString("yyyy-MM-dd"));
- }
- else if(DateTime.TryParse(celVal, out DateTime dt) && celVal.Length >= 10)
- {
- content.CreateCell(j).SetCellValue(dt.ToString("yyyy-MM-dd HH:mm:ss"));
- }
- else
- {
- content.CreateCell(j).SetCellValue(celVal);
- }
- }
- }
- foreach (var item in content.Cells)
- {
- item.CellStyle = rowCellStyle;
- }
- rowNumber++;
- }
- sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, tabs.Count-1));
- for (int i = 0; i <= tabs.Count; i++)
- {
- sheet.SetColumnWidth(i, 5000);
- }
- }
- using (MemoryStream stream = new MemoryStream())
- {
- workbook.Write(stream);
- byte[] data = stream.ToArray();
- return File(data, "application/vnd.ms-excel", "设备运行台账.xlsx");
- }
- }
- catch (Exception ex)
- {
- throw;
- }
- }
-
- }
- }
|