< Summary

Class:WinSolutions.Sveta.Server.Data.DataLoading.Loaders.RestLoader`1
Assembly:WinSolutions.Sveta.Server
File(s):/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Data/DataLoading/Loaders/RestLoader.cs
Covered lines:0
Uncovered lines:16
Coverable lines:16
Total lines:166
Line coverage:0% (0 of 16)
Covered branches:0
Total branches:2
Branch coverage:0% (0 of 2)

Metrics

MethodLine coverage Branch coverage
.ctor(...)0%100%
GetTemplateDescription()0%100%
get_DepartmentId()0%100%
get_GoodService()0%100%

File(s)

/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Data/DataLoading/Loaders/RestLoader.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Text;
 4using WinSolutions.Sveta.Server.Data.DataModel.Contexts;
 5using WinSolutions.Sveta.Server.Data.DataModel.Entities;
 6using WinSolutions.Sveta.Server.Data.DataLoading.Parsers;
 7using WinSolutions.Sveta.Server.Data.DataLoading.Records;
 8using System.Linq;
 9using Microsoft.EntityFrameworkCore;
 10using WinSolutions.Sveta.Server.Utils;
 11using System.ComponentModel.DataAnnotations;
 12using WinSolutions.Sveta.Server.Data.DataModel.Kinds;
 13using System.Globalization;
 14using WinSolutions.Sveta.Common.Extensions;
 15using WinSolutions.Sveta.Server.Services.Interfaces;
 16using WinSolutions.Sveta.Server.Data.DataModel.Extensions;
 17
 18namespace WinSolutions.Sveta.Server.Data.DataLoading.Loaders
 19{
 20    public class RestLoader<TParser> : BaseRecordsLoader<TParser, ExternalRest> where TParser : IParser, new()
 21    {
 022        public RestLoader(long currentUserId) : base(currentUserId)
 023        {
 024        }
 25
 26        public override string GetTemplateDescription()
 027        {
 028            return "Шаблон загрузки остатков (* - поле обязательно для заполнения, ** - одно из полей обязательно для за
 029        }
 30
 031        public long DepartmentId { get; set; }
 32
 033        public IGoodService GoodService { get; set; }
 34
 35        protected override IEnumerable<BaseRecord> Load(SvetaDbContext context, IEnumerable<BaseRecord> list)
 36        {
 37            var goodIds = new LinkedList<long>();
 38            var rests = list.Cast<ExternalRest>().ToList();
 39
 40            var goodIdsAll = new List<long>();
 41            var goodUniqueCodes = new List<string>();
 42            var goodVendorCodes = new List<string>();
 43            var goodNames = new List<string>();
 44
 45            foreach (var r in rests)
 46            {
 47                r.GoodName = r.GoodName.NormalizeName();
 48                r.UniqueCode = r.UniqueCode.NormalizeName();
 49                r.VendorCode = r.VendorCode.NormalizeName();
 50
 51                if (!string.IsNullOrEmpty(r.UniqueCode))
 52                {
 53                    goodUniqueCodes.Add(r.UniqueCode.ToLower());
 54                }
 55                else if (long.TryParse(r.GoodID, out long goodId))
 56                {
 57                    goodIdsAll.Add(goodId);
 58                }
 59                else if (!string.IsNullOrEmpty(r.GoodName))
 60                {
 61                    goodNames.Add(r.GoodName.ToLower());
 62                }
 63                else if (!string.IsNullOrEmpty(r.VendorCode))
 64                {
 65                    goodVendorCodes.Add(r.VendorCode.ToLower());
 66                }
 67            }
 68
 69            var goodsByIds = GoodService.FindGoodsByIds(goodIdsAll).Result
 070                .GroupBy(x => x.Id)
 071                .ToDictionary(x => x.Key, x => x.ToList());
 72            var goodsByUniqueCodes = GoodService.FindGoodsByUniqueCodes(goodUniqueCodes).Result
 073                .ToDictionary(x => x.UniqueCode.ToLower(), x => new List<Good> { x });
 74            var goodsByVendorCodes = GoodService.FindGoodsByVendorsCodes(goodVendorCodes, DepartmentId).Result
 075                .GroupBy(x => x.GetActualVendorCode(DepartmentId))
 076                .ToDictionary(x => x.Key.ToLower(), x => x.ToList());
 77            var goodsByNames = GoodService.FindGoodsByNames(goodNames).Result
 078                .GroupBy(x => x.Name)
 079                .ToDictionary(x => x.Key.ToLower(), x => x.ToList());
 80
 81            var restsDic = new Dictionary<long, Rest>(
 82                context.Rests
 83                    .Where(x => x.DepartmentId == DepartmentId)
 84                    .Select(x => new KeyValuePair<long, Rest>(x.GoodId, x)));
 85
 86            foreach (var r in rests)
 87            {
 88                Rest rest = null;
 89                try
 90                {
 91                    List<Good> good = null;
 92                    if (!String.IsNullOrEmpty(r.UniqueCode))
 93                    {
 94                        goodsByUniqueCodes.TryGetValue(r.UniqueCode.ToLower(), out good);
 95                    }
 96                    else if (long.TryParse(r.GoodID, out long goodId))
 97                    {
 98                        goodsByIds.TryGetValue(goodId, out good);
 99                    }
 100                    else if (!String.IsNullOrEmpty(r.GoodName))
 101                    {
 102                        goodsByNames.TryGetValue(r.GoodName.ToLower(), out good);
 103                    }
 104                    else if (!String.IsNullOrEmpty(r.VendorCode))
 105                    {
 106                        goodsByVendorCodes.TryGetValue(r.VendorCode.ToLower(), out good);
 107                    }
 108                    else
 109                    {
 110                        r.Exception = new ArgumentException($"Не заданы UniqueCode, GoodID, GoodName, VendorCode. Должно
 111                        continue;
 112                    }
 113
 114                    if (good == null || good.Count == 0)
 115                    {
 116                        r.Exception = new ArgumentException($"Товар не найден");
 117                        continue;
 118                    }
 119                    if (good.Count > 1)
 120                    {
 121                        r.Exception = new ArgumentException($"Найден более чем один товар");
 122                        continue;
 123                    }
 124
 0125                    if (goodIds.Any(x => x == good.Single().Id))
 126                    {
 127                        r.Exception = new ArgumentException($"Остатки по данному товару уже найдены в загружаемом файле"
 128                        continue;
 129                    }
 130                    else
 131                    {
 132                        goodIds.AddLast(good.Single().Id);
 133                    }
 134
 135
 136                    try
 137                    {
 138                        if (!restsDic.TryGetValue(good.Single().Id, out rest))
 139                        {
 140                            rest = new Rest
 141                            {
 142                                GoodId = good.Single().Id,
 143                                DepartmentId = DepartmentId
 144                            };
 145                            AddNew(context, rest);
 146                        }
 147                        rest.Quantity = r.Quantity;
 148                        rest.IsDeleted = false;
 149                    }
 150                    catch (Exception ex)
 151                    {
 152                        r.Exception = ex;
 153                        continue;
 154                    }
 155                }
 156                catch (Exception ex)
 157                {
 158                    r.Exception = ex;
 159                    continue;
 160                }
 161
 162                yield return rest;
 163            }
 164        }
 165    }
 166}