< Summary

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

Metrics

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

File(s)

/opt/dev/sveta_api_build/WinSolutions.Sveta.Server/Data/DataLoading/Loaders/DepartmentGoodSettingLoader.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 DepartmentGoodSettingLoader<TParser> : BaseRecordsLoader<TParser, ExternalDepartmentGoodSetting> where 
 21    {
 022        public DepartmentGoodSettingLoader(long currentUserId) : base(currentUserId)
 023        {
 024        }
 25
 026        public long DepartmentId { get; set; }
 27
 028        public IGoodService GoodService { get; set; }
 29
 30        public override string GetTemplateDescription()
 031        {
 032            return "Шаблон загрузки сеттингов (* - поле обязательно для заполнения, ** - одно из полей обязательно для з
 033        }
 34
 35        protected override IEnumerable<BaseRecord> Load(SvetaDbContext context, IEnumerable<BaseRecord> list)
 36        {
 37            var goodUniqueCodes = new List<string>();
 38            var goodVendorCodes = new List<string>();
 39            var goodBarCodes = new List<string>();
 40            var goodNames = new List<string>();
 41
 42            var goodIds = new List<long>();
 43            var rests = list.Cast<ExternalDepartmentGoodSetting>().ToList();
 44
 45            foreach (var r in rests)
 46            {
 47                r.UniqueCode = r.UniqueCode.NormalizeName();
 48                r.VendorCode = r.VendorCode.NormalizeName();
 49                r.BarCode = r.BarCode.NormalizeName();
 50                r.GoodName = r.GoodName.NormalizeName();
 51                r.HasVendorCodeOnly = !string.IsNullOrEmpty(r.VendorCode)
 52                    && string.IsNullOrEmpty(r.UniqueCode)
 53                    && string.IsNullOrEmpty(r.BarCode)
 54                    && string.IsNullOrEmpty(r.GoodName);
 55
 56                if (!string.IsNullOrEmpty(r.UniqueCode))
 57                {
 58                    goodUniqueCodes.Add(r.UniqueCode.ToLower());
 59                }
 60                else if (!string.IsNullOrEmpty(r.BarCode))
 61                {
 62                    goodBarCodes.Add(r.BarCode.ToLower());
 63                }
 64                else if (!string.IsNullOrEmpty(r.GoodName))
 65                {
 66                    goodNames.Add(r.GoodName.ToLower());
 67                }
 68                if (!string.IsNullOrEmpty(r.VendorCode))
 69                {
 70                    goodVendorCodes.Add(r.VendorCode.ToLower());
 71                }
 72            }
 73
 74            var goodsByUniqueCodes = GoodService.FindGoodsByUniqueCodes(goodUniqueCodes).Result
 075                .ToDictionary(x => x.UniqueCode.ToLower(), x => new List<Good> { x });
 76            var goodsByVendorCodes = GoodService.FindGoodsByVendorsCodes(goodVendorCodes, DepartmentId).Result
 077                .GroupBy(x => x.GetActualVendorCode(DepartmentId))
 078                .ToDictionary(x => x.Key.ToLower(), x => x.ToList());
 79            var goodsByBarCodes = GoodService.FindGoodsByBarCodes(goodBarCodes).Result
 080                .GroupBy(x => x.GetActualBarCode())
 081                .ToDictionary(x => x.Key.ToLower(), x => x.ToList());
 82            var goodsByNames = GoodService.FindGoodsByNames(goodNames).Result
 083                .GroupBy(x => x.Name)
 084                .ToDictionary(x => x.Key.ToLower(), x => x.ToList());
 85
 86            foreach (var r in rests)
 87            {
 88                try
 89                {
 90                    if(string.IsNullOrEmpty(r.UniqueCode)
 91                        && string.IsNullOrEmpty(r.VendorCode)
 92                        && string.IsNullOrEmpty(r.BarCode)
 93                        && string.IsNullOrEmpty(r.GoodName))
 94                    {
 95                        throw new ArgumentException($"Не заданы UniqueCode, VendorCode, BarCode, GoodName. Должно быть з
 96                    }
 97
 98                    if (r.MinQuantity <= 0)
 99                    {
 100                        throw new ArgumentException("MinQuantity должно быть больше нуля");
 101                    }
 102                    if (r.PickingQuantum <= 0)
 103                    {
 104                        throw new ArgumentException("PickingQuantum должно быть больше нуля");
 105                    }
 106                    if (r.MinQuantity % r.PickingQuantum != 0)
 107                    {
 108                        throw new ArgumentException("MinQuantity не кратно PickingQuantum");
 109                    }
 110
 111                    List<Good> good = null;
 112                    if (!String.IsNullOrEmpty(r.UniqueCode))
 113                    {
 114                        goodsByUniqueCodes.TryGetValue(r.UniqueCode.ToLower(), out good);
 115                        if (good == null || good.Count == 0) throw new ArgumentException($"Товар не найден");
 116                    }
 117                    else if (!String.IsNullOrEmpty(r.VendorCode))
 118                    {
 119                        goodsByVendorCodes.TryGetValue(r.VendorCode.ToLower(), out good);
 120                        if ((good == null || good.Count == 0) && r.HasVendorCodeOnly) throw new ArgumentException($"Това
 121                    }
 122
 123                    if (good == null || good.Count == 0)
 124                    {
 125                        if (!String.IsNullOrEmpty(r.BarCode))
 126                        {
 127                            goodsByBarCodes.TryGetValue(r.BarCode.ToLower(), out good);
 128                            if (good == null || good.Count == 0) throw new ArgumentException($"Товар не найден");
 129                        }
 130                        else if (!String.IsNullOrEmpty(r.GoodName))
 131                        {
 132                            goodsByNames.TryGetValue(r.GoodName.ToLower(), out good);
 133                            if (good == null || good.Count == 0) throw new ArgumentException($"Товар не найден");
 134                        }
 135                    }
 136
 137                    if (good == null || good.Count == 0)
 138                    {
 139                        r.Exception = new ArgumentException($"Товар не найден");
 140                        continue;
 141                    }
 142                    if(good.Count > 1)
 143                    {
 144                        r.Exception = new ArgumentException($"Найден более чем один товар");
 145                        continue;
 146                    }
 147
 0148                    if (goodIds.Any(x => x == good.Single().Id))
 149                    {
 150                        r.Exception = new ArgumentException($"Сеттинги по данному товару уже найдены в загружаемом файле
 151                        continue;
 152                    }
 153                    else
 154                    {
 155                        r.GoodId = good.Single().Id;
 156                        goodIds.Add(good.Single().Id);
 157                    }
 158                }
 159                catch(Exception ex)
 160                {
 161                    r.Exception = ex;
 162                }
 163            }
 164
 165            var settingsDic = new Dictionary<long, DepartmentGoodSetting>(
 166                context.DepartmentGoodSetting
 167                    .Include(x => x.Good)
 168                        .Where(x => x.DepartmentId == DepartmentId)
 169                        .Select(x => new KeyValuePair<long, DepartmentGoodSetting>(x.GoodId, x)));
 170
 0171            foreach (var r in rests.Where(x => x.Exception == null))
 172            {
 173                DepartmentGoodSetting setting = null;
 174                bool isNewRecord = false;
 175                try
 176                {
 0177                    var st = settingsDic.Values.FirstOrDefault(x => x.GetActualVendorCode().ToLower() == r.VendorCode.To
 178                    if (st != null)
 179                    {
 180                        r.Exception = new ArgumentException($"Товар {st.Good.UniqueCode} с таким артикулом уже существуе
 181                        continue;
 182                    }
 183
 184                    if (!settingsDic.TryGetValue(r.GoodId, out setting))
 185                    {
 186                        setting = new DepartmentGoodSetting
 187                        {
 188                            GoodId = r.GoodId,
 189                            DepartmentId = DepartmentId
 190                        };
 191                        AddNew(context, setting);
 192                        isNewRecord = true;
 193                    }
 194                    setting.MinQuantity = r.MinQuantity;
 195                    setting.PickingQuantum = r.PickingQuantum;
 196                    setting.VendorCode = string.IsNullOrEmpty(r.VendorCode) ? setting.VendorCode : r.VendorCode;
 197                    setting.ShowcaseVisible = r.ShowcaseVisible;
 198                    setting.IsDeleted = false;
 199                    if (isNewRecord)
 200                        settingsDic.Add(r.GoodId, setting); //добавляем ко всем товарам, чтобы учесть, если в файле есть
 201                }
 202                catch (Exception ex)
 203                {
 204                    r.Exception = ex;
 205                    continue;
 206                }
 207
 208                yield return setting;
 209            }
 210        }
 211    }
 212}