123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155 |
- 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;
- 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(1, 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 (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(1, 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;
- }
- }
-
- }
- }
|