| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.IO; |
| | | 4 | | using System.Text; |
| | | 5 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 6 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 7 | | using WinSolutions.Sveta.Server.Data.DataLoading.Parsers; |
| | | 8 | | using System.Linq; |
| | | 9 | | using WinSolutions.Sveta.Server.Data.DataLoading.Records; |
| | | 10 | | using System.Diagnostics.CodeAnalysis; |
| | | 11 | | using System.Globalization; |
| | | 12 | | using ClosedXML.Excel; |
| | | 13 | | using WinSolutions.Sveta.Common; |
| | | 14 | | |
| | | 15 | | namespace WinSolutions.Sveta.Server.Data.DataLoading |
| | | 16 | | { |
| | | 17 | | public class LoaderException:Exception |
| | | 18 | | { |
| | | 19 | | public LoaderException(string msg):base(msg) |
| | | 20 | | { |
| | | 21 | | } |
| | | 22 | | } |
| | | 23 | | |
| | | 24 | | public class LoadingResult |
| | | 25 | | { |
| | | 26 | | public long UploadId{get;set;} |
| | | 27 | | |
| | | 28 | | public List<string> TemplateHeader { get; set; } |
| | | 29 | | |
| | | 30 | | public IEnumerable<ParsingError> Errors{get;set;} |
| | | 31 | | |
| | | 32 | | public int AffectedCount {get;set;} |
| | | 33 | | |
| | | 34 | | public byte[] GetResultFile() |
| | | 35 | | { |
| | | 36 | | var result = new List<string[]>(); |
| | | 37 | | |
| | | 38 | | if (TemplateHeader != null && TemplateHeader.Any()) |
| | | 39 | | { |
| | | 40 | | var line = new List<string>(TemplateHeader[0].Split(';')) { "" }; |
| | | 41 | | result.Add(line.ToArray()); |
| | | 42 | | |
| | | 43 | | line = new List<string>(TemplateHeader[1].Split(';')) { "Ошибка" }; |
| | | 44 | | result.Add(line.ToArray()); |
| | | 45 | | |
| | | 46 | | line = new List<string>(TemplateHeader[2].Split(';')) { "Error" }; |
| | | 47 | | result.Add(line.ToArray()); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | if (Errors != null) |
| | | 51 | | { |
| | | 52 | | foreach (var e in Errors) |
| | | 53 | | { |
| | | 54 | | var line = new List<string>(e.Line) { e.Exception.Message }; |
| | | 55 | | result.Add(line.ToArray()); |
| | | 56 | | } |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | using (MemoryStream ms = new MemoryStream()) |
| | | 60 | | { |
| | | 61 | | CsvUtil.ToExcelStream(result).CopyTo(ms); |
| | | 62 | | return ms.ToArray(); |
| | | 63 | | } |
| | | 64 | | } |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | public abstract class BaseRecordsLoader<TParser,TRecord>: IDataLoader where TParser: IParser, new() where TRecord: |
| | | 68 | | { |
| | | 69 | | long currentUserId; |
| | | 70 | | |
| | 0 | 71 | | public BaseRecordsLoader(long currentUserId) |
| | 0 | 72 | | { |
| | 0 | 73 | | this.currentUserId = currentUserId; |
| | 0 | 74 | | } |
| | | 75 | | |
| | | 76 | | public abstract string GetTemplateDescription(); |
| | | 77 | | |
| | | 78 | | public List<string> GetTemplateHeader(bool csvFormat) |
| | 0 | 79 | | { |
| | 0 | 80 | | var result = new List<string>(); |
| | 0 | 81 | | result.Add(GetTemplateDescription()); |
| | | 82 | | |
| | 0 | 83 | | var map = new TRecord().GetMapping().ToList(); |
| | | 84 | | |
| | 0 | 85 | | var line = ""; |
| | 0 | 86 | | for (int i = 0; i < map.Count; i++) |
| | 0 | 87 | | { |
| | 0 | 88 | | line += (csvFormat ? $"\"{map[i].NameRus}\"" : map[i].NameRus); |
| | 0 | 89 | | if (i < map.Count - 1) |
| | 0 | 90 | | { |
| | 0 | 91 | | line += ";"; |
| | 0 | 92 | | } |
| | 0 | 93 | | } |
| | 0 | 94 | | result.Add(line); |
| | | 95 | | |
| | 0 | 96 | | line = ""; |
| | 0 | 97 | | for (int i = 0; i < map.Count; i++) |
| | 0 | 98 | | { |
| | 0 | 99 | | line += (csvFormat ? $"\"{map[i].PropertyInfo.Name}\"" : map[i].PropertyInfo.Name); |
| | 0 | 100 | | if (i < map.Count - 1) |
| | 0 | 101 | | { |
| | 0 | 102 | | line += ";"; |
| | 0 | 103 | | } |
| | 0 | 104 | | } |
| | 0 | 105 | | result.Add(line); |
| | | 106 | | |
| | 0 | 107 | | return result; |
| | 0 | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | public Action<BaseRecord> BeforeAddRecordAction {get;set;} |
| | | 111 | | |
| | | 112 | | public void GetTemplate(SvetaDbContext context, Stream stream, bool csvFormat = true) |
| | 0 | 113 | | { |
| | 0 | 114 | | var template = GetTemplateInternal(csvFormat); |
| | | 115 | | |
| | 0 | 116 | | if (csvFormat) |
| | 0 | 117 | | { |
| | 0 | 118 | | using (var writer = new StreamWriter(stream, encoding: Encoding.UTF8, leaveOpen: true)) |
| | 0 | 119 | | { |
| | 0 | 120 | | template.ForEach(x => writer.WriteLine(string.Join(';', x))); |
| | 0 | 121 | | } |
| | 0 | 122 | | } |
| | | 123 | | else |
| | 0 | 124 | | { |
| | 0 | 125 | | using (var book = CsvUtil.ToExcel(template)) |
| | 0 | 126 | | { |
| | 0 | 127 | | book.SaveAs(stream); |
| | 0 | 128 | | } |
| | 0 | 129 | | } |
| | 0 | 130 | | } |
| | | 131 | | |
| | | 132 | | List<string[]> GetTemplateInternal(bool csvFormat) |
| | 0 | 133 | | { |
| | 0 | 134 | | var map = new TRecord().GetMapping().ToList(); |
| | 0 | 135 | | var writer = new List<string[]>(); |
| | | 136 | | |
| | 0 | 137 | | GetTemplateHeader(csvFormat).ForEach(x => |
| | 0 | 138 | | { |
| | 0 | 139 | | writer.Add(x.Split(';')); |
| | 0 | 140 | | }); |
| | | 141 | | |
| | 0 | 142 | | var nf = new NumberFormatInfo(); |
| | 0 | 143 | | var rnd = new Random(); |
| | 0 | 144 | | for (int x = 0; x < 4; x++) |
| | 0 | 145 | | { |
| | 0 | 146 | | var line = new List<string>(); |
| | 0 | 147 | | for (int i = 0; i < map.Count; i++) |
| | 0 | 148 | | { |
| | 0 | 149 | | var val = String.Format(nf, map[i].SampleData, rnd.Next(0, 1000)); |
| | | 150 | | |
| | 0 | 151 | | if (csvFormat && map[i].PropertyInfo.PropertyType == typeof(string)) |
| | 0 | 152 | | { |
| | 0 | 153 | | val = $"\"{val}\""; |
| | 0 | 154 | | } |
| | | 155 | | |
| | 0 | 156 | | line.Add(val); |
| | 0 | 157 | | } |
| | | 158 | | |
| | 0 | 159 | | writer.Add(line.ToArray()); |
| | 0 | 160 | | } |
| | | 161 | | |
| | 0 | 162 | | return writer; |
| | 0 | 163 | | } |
| | | 164 | | |
| | | 165 | | protected int SaveChanges(SvetaDbContext context) |
| | 0 | 166 | | { |
| | 0 | 167 | | var task = context.SaveChangesAsync(currentUserId); |
| | 0 | 168 | | task.Wait(); |
| | 0 | 169 | | return task.Result; |
| | 0 | 170 | | } |
| | | 171 | | |
| | 0 | 172 | | List<BaseRecord> added = null; |
| | | 173 | | public LoadingResult Load(SvetaDbContext context, Stream stream) |
| | 0 | 174 | | { |
| | 0 | 175 | | var upload = new Upload |
| | 0 | 176 | | { |
| | 0 | 177 | | Items = new List<UploadItem>(), |
| | 0 | 178 | | SourceFileName = stream is FileStream ? (stream as FileStream).Name : null |
| | 0 | 179 | | }; |
| | | 180 | | |
| | 0 | 181 | | LoadingResult res = new LoadingResult(); |
| | | 182 | | |
| | | 183 | | try |
| | 0 | 184 | | { |
| | 0 | 185 | | added = new List<BaseRecord>(); |
| | | 186 | | |
| | | 187 | | |
| | | 188 | | |
| | 0 | 189 | | var parser = new TParser(); |
| | 0 | 190 | | var list = parser.Parse(stream).ToList(); |
| | | 191 | | |
| | 0 | 192 | | var list2 = list.Where(x => |
| | 0 | 193 | | { |
| | 0 | 194 | | if (x is IExternalRecord r) |
| | 0 | 195 | | return r.Exception == null; |
| | 0 | 196 | | |
| | 0 | 197 | | return true; |
| | 0 | 198 | | }).ToList(); |
| | | 199 | | |
| | 0 | 200 | | var res2 = Load(context, list2); |
| | 0 | 201 | | var pl = res2.ToList(); |
| | | 202 | | |
| | | 203 | | |
| | | 204 | | |
| | 0 | 205 | | if(pl.Any()) |
| | 0 | 206 | | res.AffectedCount = SaveChanges(context); |
| | | 207 | | |
| | | 208 | | |
| | 0 | 209 | | res.Errors = list.Where(x => |
| | 0 | 210 | | { |
| | 0 | 211 | | if(x is IExternalRecord r) |
| | 0 | 212 | | return r.Exception != null; |
| | 0 | 213 | | |
| | 0 | 214 | | return true; |
| | 0 | 215 | | }) |
| | 0 | 216 | | .Cast<IExternalRecord>() |
| | 0 | 217 | | .Select(x => new ParsingError |
| | 0 | 218 | | { |
| | 0 | 219 | | LineNumber = x.LineNumber, |
| | 0 | 220 | | Line = x.Line, |
| | 0 | 221 | | Exception = x.Exception |
| | 0 | 222 | | }); |
| | | 223 | | |
| | 0 | 224 | | res.TemplateHeader = GetTemplateHeader(false); |
| | | 225 | | |
| | 0 | 226 | | upload.Items = new List<UploadItem> |
| | 0 | 227 | | ( |
| | 0 | 228 | | added.Select(x => new UploadItem |
| | 0 | 229 | | { |
| | 0 | 230 | | EndityId = x.Id, |
| | 0 | 231 | | EntityName = x.GetType().Name |
| | 0 | 232 | | }) |
| | 0 | 233 | | ); |
| | | 234 | | |
| | 0 | 235 | | upload.ResultFile = res.GetResultFile(); |
| | 0 | 236 | | upload.UploadedRecordCount = res.AffectedCount; |
| | 0 | 237 | | } |
| | 0 | 238 | | catch(Exception ex) |
| | 0 | 239 | | { |
| | 0 | 240 | | res.Errors = new List<ParsingError>(new []{new ParsingError{Exception = ex} }); |
| | 0 | 241 | | var lines = new List<string[]>(); |
| | 0 | 242 | | lines.Add(new string[] { "Ошибка загрузки. Обратитесь к администратору системы, приложив данный файл с о |
| | 0 | 243 | | lines.Add(new string[] { ex.Message }); |
| | 0 | 244 | | lines.Add(new string[] { ex.StackTrace }); |
| | 0 | 245 | | var ms = new MemoryStream(); |
| | 0 | 246 | | CsvUtil.ToExcelStream(lines).CopyTo(ms); |
| | 0 | 247 | | upload.ResultFile = ms.ToArray(); |
| | 0 | 248 | | } |
| | | 249 | | |
| | 0 | 250 | | context.Uploads.Add(upload); |
| | 0 | 251 | | SaveChanges(context); |
| | 0 | 252 | | res.UploadId = upload.Id; |
| | | 253 | | |
| | 0 | 254 | | AfterLoad(); |
| | 0 | 255 | | SaveChanges(context); |
| | | 256 | | |
| | 0 | 257 | | return res; |
| | 0 | 258 | | } |
| | | 259 | | |
| | | 260 | | protected virtual void AfterLoad() |
| | 0 | 261 | | { |
| | | 262 | | |
| | 0 | 263 | | } |
| | | 264 | | |
| | | 265 | | class TypeComparer : IComparer<Type> |
| | | 266 | | { |
| | | 267 | | public int Compare([AllowNull] Type x, [AllowNull] Type y) |
| | 0 | 268 | | { |
| | 0 | 269 | | var px = x.GetProperties().Where(x => x.PropertyType.IsSubclassOf(typeof(BaseRecord))).ToList(); |
| | 0 | 270 | | var py = y.GetProperties().Where(x => x.PropertyType.IsSubclassOf(typeof(BaseRecord))).ToList(); |
| | | 271 | | |
| | 0 | 272 | | if(px.Count == 0 && py.Count == 0) |
| | 0 | 273 | | return 0; |
| | | 274 | | |
| | 0 | 275 | | foreach (var p in px) |
| | 0 | 276 | | { |
| | 0 | 277 | | if(p.PropertyType == y || p.PropertyType.IsSubclassOf(y)) |
| | 0 | 278 | | return 1; |
| | 0 | 279 | | } |
| | | 280 | | |
| | 0 | 281 | | foreach (var p in py) |
| | 0 | 282 | | { |
| | 0 | 283 | | if(p.PropertyType == x || p.PropertyType.IsSubclassOf(x)) |
| | 0 | 284 | | return -1; |
| | 0 | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | return 0; |
| | 0 | 288 | | } |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | public void Rollback(SvetaDbContext context,long uploadId) |
| | 0 | 292 | | { |
| | 0 | 293 | | if(!context.Uploads.Any(x => x.Id == uploadId)) |
| | 0 | 294 | | throw new LoaderException($"Загрузка с Id = {uploadId} не найдена."); |
| | | 295 | | |
| | | 296 | | |
| | 0 | 297 | | var status = context.Uploads.Where(x => x.Id == uploadId) |
| | 0 | 298 | | .Select(x => x.Status) |
| | 0 | 299 | | .FirstOrDefault(); |
| | | 300 | | |
| | 0 | 301 | | if(status == UploadStatus.Cancelled) |
| | 0 | 302 | | throw new LoaderException($"Загрузка с Id = {uploadId} уже отменена."); |
| | | 303 | | |
| | 0 | 304 | | var types = new Dictionary<string,Type>(); |
| | | 305 | | |
| | | 306 | | |
| | 0 | 307 | | var added = context.UploadItems |
| | 0 | 308 | | .Where(x => x.UploadId == uploadId) |
| | 0 | 309 | | .OrderBy(x => x.Id) |
| | 0 | 310 | | .ToList(); |
| | | 311 | | |
| | 0 | 312 | | foreach (var typeName in added.GroupBy(x => x.EntityName).Select(x => x.Key).ToList()) |
| | 0 | 313 | | { |
| | 0 | 314 | | types[typeName] = Type.GetType($"WinSolutions.Sveta.Server.Data.DataModel.Entities.{typeName}",true); |
| | 0 | 315 | | } |
| | | 316 | | |
| | | 317 | | |
| | 0 | 318 | | var typeOrder = types.OrderBy(x => x.Value,new TypeComparer()).ToList(); |
| | 0 | 319 | | typeOrder.Reverse(); |
| | | 320 | | |
| | 0 | 321 | | foreach (var t in typeOrder) |
| | 0 | 322 | | { |
| | 0 | 323 | | foreach (var r in added.Where(x => x.EntityName == t.Key).ToList()) |
| | 0 | 324 | | { |
| | | 325 | | |
| | 0 | 326 | | var rec = (BaseRecord)context.Find(t.Value,r.EndityId); |
| | | 327 | | |
| | 0 | 328 | | if(rec != null) |
| | 0 | 329 | | { |
| | 0 | 330 | | context.Remove(rec); |
| | | 331 | | //context.Entry(rec).State = Microsoft.EntityFrameworkCore.EntityState.Deleted; |
| | 0 | 332 | | } |
| | 0 | 333 | | } |
| | | 334 | | |
| | 0 | 335 | | SaveChanges(context); |
| | 0 | 336 | | } |
| | | 337 | | |
| | 0 | 338 | | var upload = new Upload |
| | 0 | 339 | | { |
| | 0 | 340 | | Id = uploadId, |
| | 0 | 341 | | Status = UploadStatus.Cancelled |
| | 0 | 342 | | }; |
| | | 343 | | |
| | 0 | 344 | | context.Uploads.Attach(upload); |
| | 0 | 345 | | context.Entry(upload).Property(x => x.Status).IsModified = true; |
| | 0 | 346 | | SaveChanges(context); |
| | 0 | 347 | | } |
| | | 348 | | |
| | | 349 | | |
| | | 350 | | public void Commit(SvetaDbContext context,long uploadId) |
| | 0 | 351 | | { |
| | 0 | 352 | | if(!context.Uploads.Any(x => x.Id == uploadId)) |
| | 0 | 353 | | throw new LoaderException($"Загрузка с Id = {uploadId} не найдена."); |
| | | 354 | | |
| | | 355 | | |
| | 0 | 356 | | var status = context.Uploads.Where(x => x.Id == uploadId) |
| | 0 | 357 | | .Select(x => x.Status) |
| | 0 | 358 | | .FirstOrDefault(); |
| | | 359 | | |
| | 0 | 360 | | if(status == UploadStatus.Cancelled) |
| | 0 | 361 | | throw new LoaderException($"Загрузка с Id = {uploadId} уже отменена."); |
| | | 362 | | |
| | 0 | 363 | | if(status == UploadStatus.Commited) |
| | 0 | 364 | | throw new LoaderException($"Загрузка с Id = {uploadId} уже подтверждена."); |
| | | 365 | | |
| | 0 | 366 | | var types = new Dictionary<string,Type>(); |
| | | 367 | | |
| | | 368 | | |
| | 0 | 369 | | var added = context.UploadItems |
| | 0 | 370 | | .Where(x => x.UploadId == uploadId) |
| | 0 | 371 | | .OrderBy(x => x.Id) |
| | 0 | 372 | | .ToList(); |
| | | 373 | | |
| | 0 | 374 | | foreach (var typeName in added.GroupBy(x => x.EntityName).Select(x => x.Key).ToList()) |
| | 0 | 375 | | { |
| | 0 | 376 | | types[typeName] = Type.GetType($"WinSolutions.Sveta.Server.Data.DataModel.Entities.{typeName}",true); |
| | 0 | 377 | | } |
| | | 378 | | |
| | | 379 | | |
| | | 380 | | |
| | 0 | 381 | | var activeState = context.refRecordsState.Where(x => x.Code == "Active").Single(); |
| | | 382 | | |
| | 0 | 383 | | foreach (var r in added) |
| | 0 | 384 | | { |
| | 0 | 385 | | var type = types[r.EntityName]; |
| | | 386 | | |
| | 0 | 387 | | var rec = (BaseRecord)context.Find(type,r.EndityId); |
| | | 388 | | |
| | 0 | 389 | | if(rec != null) |
| | 0 | 390 | | { |
| | 0 | 391 | | rec.RecState = activeState; |
| | 0 | 392 | | } |
| | 0 | 393 | | } |
| | | 394 | | |
| | 0 | 395 | | SaveChanges(context); |
| | | 396 | | |
| | 0 | 397 | | var upload = new Upload |
| | 0 | 398 | | { |
| | 0 | 399 | | Id = uploadId, |
| | 0 | 400 | | Status = UploadStatus.Commited |
| | 0 | 401 | | }; |
| | | 402 | | |
| | 0 | 403 | | context.Uploads.Attach(upload); |
| | 0 | 404 | | context.Entry(upload).Property(x => x.Status).IsModified = true; |
| | 0 | 405 | | SaveChanges(context); |
| | 0 | 406 | | } |
| | | 407 | | |
| | | 408 | | protected abstract IEnumerable<BaseRecord> Load(SvetaDbContext context,IEnumerable<BaseRecord> list); |
| | | 409 | | |
| | 0 | 410 | | RecordsState inactiveState = null; |
| | | 411 | | void SetState(SvetaDbContext context, BaseRecord rec) |
| | 0 | 412 | | { |
| | 0 | 413 | | if(inactiveState == null) |
| | 0 | 414 | | inactiveState = context.refRecordsState.Where(x => x.Code == "Inactive").Single(); |
| | | 415 | | |
| | 0 | 416 | | rec.RecState = inactiveState; |
| | 0 | 417 | | } |
| | | 418 | | |
| | | 419 | | protected virtual void AddNew(SvetaDbContext context,BaseRecord rec) |
| | 0 | 420 | | { |
| | 0 | 421 | | BeforeAddRecordAction?.Invoke(rec); |
| | 0 | 422 | | SetState(context,rec); |
| | 0 | 423 | | context.Add(rec); |
| | 0 | 424 | | added.Add(rec); |
| | 0 | 425 | | } |
| | | 426 | | } |
| | | 427 | | } |