wanshenmean
2025-04-15 21cd52c5592aad3687be74599a932012d9dd77a4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 命名空间:WIDESEAWCS_QuartzJob
 * 创建者:胡童庆
 * 创建时间:2024/8/2 16:13:36
 * 版本:V1.0.0
 * 描述:设备协议业务实现层
 *
 * ----------------------------------------------------------------
 * 修改人:
 * 修改时间:
 * 版本:V1.0.1
 * 修改说明:
 * 
 *----------------------------------------------------------------*/
#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;
 
namespace WIDESEAWCS_QuartzJob.Service
{
    /// <summary>
    /// 设备协议信息业务层
    /// </summary>
    public class DeviceProtocolService : ServiceBase<Dt_DeviceProtocol, IDeviceProtocolRepository>, IDeviceProtocolService
    {
        private readonly IUnitOfWorkManage _unitOfWorkManage;
 
        /// <summary>
        /// 设备协议信息业务层
        /// </summary>
        public DeviceProtocolService(IDeviceProtocolRepository BaseDal, IUnitOfWorkManage unitOfWorkManage) : base(BaseDal)
        {
            _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);
            }
        }
 
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
        public override WebResponseContent AddData(SaveModel saveModel)
        {
            //saveModel.MainData[""]
            return base.AddData(saveModel);
        }
    }
}