From 70233af5426b0d1c343ebe87183303a34a9aaa58 Mon Sep 17 00:00:00 2001 From: hutongqing <hutongqing@hnkhzn.com> Date: 星期三, 20 十一月 2024 09:05:38 +0800 Subject: [PATCH] WIDESEAWCS_QuartzJob更新版本 --- WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/DeviceProtocolService.cs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 files changed, 58 insertions(+), 1 deletions(-) diff --git a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/DeviceProtocolService.cs b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/DeviceProtocolService.cs index 0fab091..f9c4ae8 100644 --- a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/DeviceProtocolService.cs +++ b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/Service/DeviceProtocolService.cs @@ -15,14 +15,18 @@ *----------------------------------------------------------------*/ #endregion << 鐗� 鏈� 娉� 閲� >> +using Magicodes.ExporterAndImporter.Core.Models; +using Magicodes.ExporterAndImporter.Excel; +using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using WIDESEAWCS_Core; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.BaseServices; - +using WIDESEAWCS_Core.Helper; using WIDESEAWCS_QuartzJob.Models; using WIDESEAWCS_QuartzJob.Repository; @@ -35,5 +39,58 @@ { _unitOfWorkManage = unitOfWorkManage; } + + /// <summary> + /// 璇诲彇瀵煎叆鏂囦欢鐨勬暟鎹繑鍥炲埌鍓嶇 + /// </summary> + /// <param name="fileInput">鏂囦欢</param> + /// <returns>杩斿洖璇诲彇缁撴灉锛屾垚鍔熻繑鍥炴暟鎹紝澶辫触杩斿洖閿欒淇℃伅</returns> + public WebResponseContent GetImportData(List<IFormFile> fileInput) + { + try + { + // 鍒ゆ柇涓婁紶鐨勬枃浠舵槸鍚︿负绌� + if (fileInput == null || fileInput.Count == 0) + return new WebResponseContent { Status = true, Message = "璇烽�夋嫨涓婁紶鐨勬枃浠�" }; + // 鑾峰彇涓婁紶鐨勬枃浠� + Microsoft.AspNetCore.Http.IFormFile formFile = fileInput[0]; + // 鑾峰彇鏂囦欢淇濆瓨璺緞 + string dicPath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelImprot/{DateTime.Now.ToString("yyyMMdd")}/{typeof(Dt_DeviceProtocol).Name}/"; + // 鍒ゆ柇璺緞鏄惁瀛樺湪锛屼笉瀛樺湪鍒欏垱寤� + if (!Directory.Exists(dicPath)) Directory.CreateDirectory(dicPath); + // 鐢熸垚鏂囦欢鍚� + string fileName = $"{Guid.NewGuid()}_{formFile.FileName}"; + // 鑾峰彇鏂囦欢淇濆瓨璺緞 + dicPath = $"{dicPath}{fileName}"; + // 灏嗘枃浠朵繚瀛樺埌鎸囧畾璺緞 + using (FileStream stream = new FileStream(dicPath, FileMode.Create)) + { + formFile.CopyTo(stream); + } + // 鍒涘缓Excel瀵煎叆鍣� + ExcelImporter importer = new ExcelImporter(); + // 瀵煎叆Excel鏂囦欢 + ImportResult<Dt_DeviceProtocol> importResult = importer.Import<Dt_DeviceProtocol>(dicPath, "").Result; + // 鍒ゆ柇瀵煎叆缁撴灉鏄惁鏈夐敊璇� + if (importResult.HasError) + { + // 杩斿洖閿欒淇℃伅 + return WebResponseContent.Instance.Error(importResult.TemplateErrors.Serialize()); + } + // 杩斿洖瀵煎叆缁撴灉 + return WebResponseContent.Instance.OK(data: importResult.Data); + } + catch (Exception ex) + { + // 杩斿洖寮傚父淇℃伅 + return WebResponseContent.Instance.Error(ex.Message); + } + } + + public override WebResponseContent AddData(SaveModel saveModel) + { + //saveModel.MainData[""] + return base.AddData(saveModel); + } } } -- Gitblit v1.9.3