EquipmentDocService.cs
7.78 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using HHECS.Dal.Repository;
using HHECS.Model.Entities;
using System;
using System.Linq.Expressions;
using System.Linq;
using System.IO;
using HHECS.Infrastructure.Json;
using System.Data;
using HHECS.Dal;
namespace HHECS.Application.Service
{
public class EquipmentDocService : BaseService
{
/// <summary>
/// 文件存储路径
/// </summary>
private readonly string FileNamePath = "\\wwwroot\\Document";
public string GetEquipmentDoc(EquipmentDoc entity, int page, int limit)
{
return Execute(() =>
{
using var docRepository = new EquipmentDocRepository();
var query = docRepository.Where(GetExpression(entity));
var result = query.OrderByDescending(x => x.Created).Page(page, limit).Include(x => x.EquipmentType)
.ToList(x => new EquipmentDoc
{
Id = x.Id,
DocName = x.DocName,
Version = x.Version,
EquipmentTypeId = x.EquipmentTypeId,
EquipmentTypeName = x.EquipmentType.Name,
Url = x.Url,
DocType = x.DocType,
Remark = x.Remark,
Size = x.Size,
Created = x.Created,
CreatedBy = x.CreatedBy,
Updated = x.Updated,
UpdatedBy = x.UpdatedBy,
}).ToList();
Response.Result = result;
Response.Count = query.Count();
return Response.ToJson();
});
}
private Expression<Func<EquipmentDoc, bool>> GetExpression(EquipmentDoc entity)
{
Expression<Func<EquipmentDoc, bool>> filter = t => true;
if (entity.EquipmentTypeId != 0)
{
filter = filter.And(x => x.EquipmentTypeId.Equals(entity.EquipmentTypeId));
}
if (!string.IsNullOrWhiteSpace(entity.DocName))
{
filter = filter.And(x => x.DocName.Contains(entity.DocName));
}
return filter;
}
public string AddOrUpdate(EquipmentDoc entity, User user)
{
return Execute(() =>
{
if (entity.EquipmentTypeId == 0)
{
return Response.ResponseError("适用的设备类型不能为空!").ToJson();
}
using EquipmentDocRepository docRepository = new EquipmentDocRepository();
var UploadPath = $"{Directory.GetCurrentDirectory()}{FileNamePath}";
if (!Directory.Exists(UploadPath)) Directory.CreateDirectory(UploadPath);
var fileType = entity.File?.FileName.Split(".").LastOrDefault();
var fileName = entity.File?.FileName.Replace($".{fileType}", "");
if (string.IsNullOrWhiteSpace(fileName))
{
fileName = Guid.NewGuid().ToString().Substring(0, 8);
}
var newFileName = $"{fileName}_{Guid.NewGuid():N}.{fileType}";
//更新文件和数据,删除原始文件,上传新文件
if (entity.EditFlag && entity.File != null)
{
DeleteDocument(entity.Url);
using var fs = File.Create($"{UploadPath}\\{newFileName}");
entity.File.CopyTo(fs);
fs.Flush();
var size = fs.Length / 1024;
entity.Size = size > 1024 ? $"{size / 1024:N1}M" : $"{size:N1}Kb";
entity.DocType ??= fileType;
entity.UpdatedBy = user.UserCode;
entity.Updated = DateTime.Now;
}
//新增文件和数据
else if (!entity.EditFlag && entity.File != null)
{
using var fs = File.Create($"{UploadPath}\\{newFileName}");
entity.File.CopyTo(fs);
fs.Flush();
var size = fs.Length / 1024;
entity.Size = size > 1024 ? $"{size / 1024:N1}M" : $"{size:N1}Kb";
entity.DocType ??= fileType;
entity.CreatedBy = user.UserCode;
entity.Created = DateTime.Now;
}
else if (!entity.EditFlag && entity.File == null)
{
return Response.ResponseError("新增文件数据时,文件不能为空!").ToJson();
}
entity.DocName ??= fileName;
entity.Url = newFileName;
var result = docRepository.InsertOrUpdate(entity);
return Response.ResponseSuccess().ToJson();
});
}
public FileStream GetFileStreamByName(string fileName, out string docName)
{
using EquipmentDocRepository docRepository = new EquipmentDocRepository();
var doc = docRepository.Where(x => x.Url.Equals(fileName)).First();
docName = $"{doc.DocName}.{doc.DocType}";
try
{
string filePath = $"{Directory.GetCurrentDirectory()}{FileNamePath}\\{fileName}";
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
return fileStream;
}
catch (Exception)
{
return null;
}
}
public string Update(EquipmentDoc entity, User user)
{
return Execute(() =>
{
using EquipmentDocRepository docRepository = new EquipmentDocRepository();
entity.CreatedBy = user.UserCode;
entity.Created = DateTime.Now;
var result = docRepository.Update(entity);
return Response.ResponseSuccess().ToJson();
});
}
public string DeleteByIds(int[] ids)
{
return Execute(() =>
{
using EquipmentDocRepository docRepository = new EquipmentDocRepository();
var equipmentDocs = docRepository.Where(x => ids.Contains(x.Id)).ToList();
equipmentDocs.ForEach((item) =>
{
DeleteDocument(item.Url);
});
var result = docRepository.Delete(x => ids.Contains(x.Id));
return Response.ToJson();
});
}
private void DeleteDocument(string fileName)
{
string path = $"{Directory.GetCurrentDirectory()}{FileNamePath}\\{fileName}";
if (File.Exists(path))
{
File.Delete(path);
}
}
/// <summary>
/// /左侧列表 仓库+设备类型
/// </summary>
public DataTable GetTreeList()
{
return Execute(() =>
{
string sql = $@"
select
id = '1',
name = '根节点',
keys = '00000000-0000-0000-0000-000000000000',
parentId = null,
isok = '',
Code = ''
union all
select
id,
name = Name,
keys = Code,
parentId = '00000000-0000-0000-0000-000000000000',
isok = 'true',
e.Code
from equipmenttype e";
var dt = DALHelper.GetFreeSql().Ado.ExecuteDataTable(sql);
return dt == null || dt.Rows.Count == 0 ? null : dt;
});
}
}
}