| | | 1 | | using AutoMapper; |
| | | 2 | | using Microsoft.Extensions.Logging; |
| | | 3 | | using SVETA.Api.Data.DTO; |
| | | 4 | | using SVETA.Api.Services.Interfaces; |
| | | 5 | | using System; |
| | | 6 | | using System.Collections.Concurrent; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Data; |
| | | 10 | | using System.Linq; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 13 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 14 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 15 | | using SVETA.Api.Data.DTO.Prices.PriceTrend; |
| | | 16 | | using Newtonsoft.Json; |
| | | 17 | | using System.Globalization; |
| | | 18 | | using DocumentFormat.OpenXml.Office2010.ExcelAc; |
| | | 19 | | using Microsoft.EntityFrameworkCore; |
| | | 20 | | using SVETA.Api.Helpers.Authorize; |
| | | 21 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 22 | | using WinSolutions.Sveta.Server.Data.DataModel.Extensions; |
| | | 23 | | using WinSolutions.Sveta.Server.Data.DataModel.Contexts; |
| | | 24 | | using WinSolutions.Sveta.Common; |
| | | 25 | | using Clave.Expressionify; |
| | | 26 | | |
| | | 27 | | namespace SVETA.Api.Services.Implements |
| | | 28 | | { |
| | | 29 | | public class PriceWorker : IPriceWorker |
| | | 30 | | { |
| | | 31 | | private readonly IPriceTrendService _priceTrendService; |
| | | 32 | | private readonly IDepartmentService _departmentService; |
| | | 33 | | private readonly ICategoryRatioService _departmentCategoryRatioService; |
| | | 34 | | private readonly IClusterService _clusterService; |
| | | 35 | | private readonly ISupplyContractService _suplContractService; |
| | | 36 | | private readonly IDirectoriesService _dirService; |
| | | 37 | | private readonly IGoodService _goodService; |
| | | 38 | | private readonly IExchangeTokenService _exchangeTokenService; |
| | | 39 | | private readonly IEventService _eventService; |
| | | 40 | | private readonly ILogger<PriceWorker> _logger; |
| | | 41 | | SvetaDbContext _db; |
| | | 42 | | IAuthenticationService _authenticationService; |
| | | 43 | | |
| | | 44 | | RecordsState activeRecordState; |
| | | 45 | | Dictionary<string, PriceCurrent> currentPrices; |
| | | 46 | | |
| | 0 | 47 | | public PriceWorker(IPriceTrendService priceTrendService, |
| | 0 | 48 | | IDepartmentService departmentService, |
| | 0 | 49 | | ICategoryRatioService departmentCategoryRatioService, |
| | 0 | 50 | | IClusterService clusterService, |
| | 0 | 51 | | ISupplyContractService suplContractService, |
| | 0 | 52 | | IGoodService goodService, |
| | 0 | 53 | | IDirectoriesService dirService, |
| | 0 | 54 | | IExchangeTokenService exchangeTokenService, |
| | 0 | 55 | | IUserService userService, |
| | 0 | 56 | | IEventService eventService, |
| | 0 | 57 | | SvetaDbContext db, |
| | 0 | 58 | | IAuthenticationService authenticationService, |
| | 0 | 59 | | ILogger<PriceWorker> logger) |
| | 0 | 60 | | { |
| | 0 | 61 | | _priceTrendService = priceTrendService; |
| | 0 | 62 | | _dirService = dirService; |
| | 0 | 63 | | _departmentCategoryRatioService = departmentCategoryRatioService; |
| | 0 | 64 | | _clusterService = clusterService; |
| | 0 | 65 | | _suplContractService = suplContractService; |
| | 0 | 66 | | _departmentService = departmentService; |
| | 0 | 67 | | _exchangeTokenService = exchangeTokenService; |
| | 0 | 68 | | _goodService = goodService; |
| | 0 | 69 | | _logger = logger; |
| | 0 | 70 | | _eventService = eventService; |
| | 0 | 71 | | _db = db; |
| | 0 | 72 | | _authenticationService = authenticationService; |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public async Task<PriceTrend> ChangePrice(PriceTrendRequestDTO priceIn) |
| | 0 | 76 | | { |
| | | 77 | | /* Department dep = await _departmentService.GetDepartment(priceIn.DepartmentId); |
| | | 78 | | if (dep == null) |
| | | 79 | | { |
| | | 80 | | throw new ArgumentException($"Подразделение #{priceIn.DepartmentId} не существует"); |
| | | 81 | | } |
| | | 82 | | Good good = await _goodService.GetGood(priceIn.GoodId); |
| | | 83 | | if (good == null) |
| | | 84 | | { |
| | | 85 | | throw new ArgumentException($"Товар #{priceIn.GoodId} не существует"); |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | return await ChangePriceInternal(null, dep, good, priceIn.PriceNew);*/ |
| | 0 | 89 | | return null; |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | public async Task<List<Dictionary<string, string>>> ExchangeLoadPrice(Department department, ExchangePriceTrendR |
| | 0 | 93 | | { |
| | 0 | 94 | | var vendorCodes = new List<string>(); |
| | 0 | 95 | | var barCodes = new List<string>(); |
| | 0 | 96 | | requestDto.Prices.ForEach(x => |
| | 0 | 97 | | { |
| | 0 | 98 | | x.VendorCode = x.VendorCode.NormalizeName(); |
| | 0 | 99 | | x.BarCode = x.BarCode.NormalizeName(); |
| | 0 | 100 | | |
| | 0 | 101 | | if(!string.IsNullOrEmpty(x.VendorCode)) |
| | 0 | 102 | | { |
| | 0 | 103 | | vendorCodes.Add(x.VendorCode.ToLower()); |
| | 0 | 104 | | } |
| | 0 | 105 | | if (!string.IsNullOrEmpty(x.BarCode)) |
| | 0 | 106 | | { |
| | 0 | 107 | | barCodes.Add(x.BarCode.ToLower()); |
| | 0 | 108 | | } |
| | 0 | 109 | | }); |
| | | 110 | | |
| | 0 | 111 | | _authenticationService.SwitchToServiceUser(); |
| | 0 | 112 | | ConcurrentBag<Dictionary<string, string>> errors = new ConcurrentBag<Dictionary<string, string>>(); |
| | 0 | 113 | | if (requestDto.BeginDate.ToUniversalTime() <= DateTime.UtcNow) |
| | 0 | 114 | | { |
| | 0 | 115 | | errors.Add(new Dictionary<string, string>() |
| | 0 | 116 | | { |
| | 0 | 117 | | {"", "Дата начала действия цены меньше текущей даты"} |
| | 0 | 118 | | }); |
| | 0 | 119 | | return errors.ToList(); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | var priceTrend = new PriceTrend |
| | 0 | 123 | | { |
| | 0 | 124 | | BeginDate = requestDto.BeginDate.Date, |
| | 0 | 125 | | SupplierDepartment = department |
| | 0 | 126 | | }; |
| | | 127 | | |
| | 0 | 128 | | await _priceTrendService.CreatePriceTrend(priceTrend); |
| | | 129 | | |
| | | 130 | | try |
| | 0 | 131 | | { |
| | 0 | 132 | | Dictionary<string, long> goodsByVendorCode = _db.Goods |
| | 0 | 133 | | .Include(x => x.DepartmentGoodSettings) |
| | 0 | 134 | | .Expressionify() |
| | 0 | 135 | | .Where(d => !d.IsDeleted && vendorCodes.Contains(d.GetActualVendorCode(department.Id).ToLower())) |
| | 0 | 136 | | .AsNoTracking() |
| | 0 | 137 | | .ToList() |
| | 0 | 138 | | .Select(d => new KeyValuePair<string, long>(d.GetActualVendorCode(department.Id).ToString(), d.Id)) |
| | 0 | 139 | | .ToDictionary(pair => pair.Key.ToLower(), pair => pair.Value); |
| | | 140 | | |
| | 0 | 141 | | Dictionary<string, long> goodsByBarCode = _db.Goods |
| | 0 | 142 | | .Include(d => d.GoodBarcodes).ThenInclude(barcode => barcode.BarCode) |
| | 0 | 143 | | .Include(d => d.DefaultBarCode) |
| | 0 | 144 | | .Expressionify() |
| | 0 | 145 | | .Where(d => !d.IsDeleted && barCodes.Contains(d.GetActualBarCode())) |
| | 0 | 146 | | .AsNoTracking() |
| | 0 | 147 | | .ToList().GroupBy(x => x.GetActualBarCode()).Select(x => x.FirstOrDefault()) //убираем дубли ШК, остав |
| | 0 | 148 | | .Select(d => new KeyValuePair<string, long>(d.GetActualBarCode(), d.Id)) |
| | 0 | 149 | | .ToDictionary(pair => pair.Key.ToLower(), pair => pair.Value); |
| | | 150 | | |
| | 0 | 151 | | ConcurrentBag<PriceTrendDetail> prices = new ConcurrentBag<PriceTrendDetail>(); |
| | | 152 | | |
| | 0 | 153 | | Parallel.ForEach(requestDto.Prices, trend => |
| | 0 | 154 | | { |
| | 0 | 155 | | var innerError = new List<string>(); |
| | 0 | 156 | | if (string.IsNullOrWhiteSpace(trend.VendorCode) || !goodsByVendorCode.TryGetValue(trend.VendorCode.T |
| | 0 | 157 | | { |
| | 0 | 158 | | if (string.IsNullOrWhiteSpace(trend.BarCode) || !goodsByBarCode.TryGetValue(trend.BarCode.ToLowe |
| | 0 | 159 | | { |
| | 0 | 160 | | errors.Add(new Dictionary<string, string> |
| | 0 | 161 | | { |
| | 0 | 162 | | {"VendorCode", trend.VendorCode}, {"BarCode", trend.BarCode}, {"Error", "Товар не найден |
| | 0 | 163 | | }); |
| | 0 | 164 | | return; |
| | 0 | 165 | | } |
| | 0 | 166 | | } |
| | 0 | 167 | | |
| | 0 | 168 | | if (trend.CurrentPrice <= 0) |
| | 0 | 169 | | { |
| | 0 | 170 | | innerError.Add("Текущая цена товара не может быть меньше или равна 0"); |
| | 0 | 171 | | } |
| | 0 | 172 | | if (trend.OldPrice < 0) |
| | 0 | 173 | | { |
| | 0 | 174 | | innerError.Add("Старая цена товара не может быть меньше 0"); |
| | 0 | 175 | | } |
| | 0 | 176 | | |
| | 0 | 177 | | if (innerError.Count > 0) |
| | 0 | 178 | | { |
| | 0 | 179 | | var dir = new Dictionary<string, string>() |
| | 0 | 180 | | { |
| | 0 | 181 | | {"VendorCode", trend.VendorCode}, {"BarCode", trend.BarCode}, {"Errors", string.Join(";", in |
| | 0 | 182 | | }; |
| | 0 | 183 | | errors.Add(dir); |
| | 0 | 184 | | return; |
| | 0 | 185 | | } |
| | 0 | 186 | | |
| | 0 | 187 | | if (prices.Any(x => x.GoodId == good)) |
| | 0 | 188 | | { |
| | 0 | 189 | | var dir = new Dictionary<string, string>() |
| | 0 | 190 | | { |
| | 0 | 191 | | {"VendorCode", trend.VendorCode}, {"BarCode", trend.BarCode}, {"Errors", $"Товар с Id={good |
| | 0 | 192 | | }; |
| | 0 | 193 | | errors.Add(dir); |
| | 0 | 194 | | return; |
| | 0 | 195 | | } |
| | 0 | 196 | | var priceTrendDetail = new PriceTrendDetail |
| | 0 | 197 | | { |
| | 0 | 198 | | GoodId = good, |
| | 0 | 199 | | PriceNew = trend.CurrentPrice, |
| | 0 | 200 | | PriceOld = trend.OldPrice, |
| | 0 | 201 | | PriceTrend = priceTrend |
| | 0 | 202 | | }; |
| | 0 | 203 | | priceTrendDetail.Discount = priceTrendDetail.Discount(); |
| | 0 | 204 | | prices.Add(priceTrendDetail); |
| | 0 | 205 | | |
| | 0 | 206 | | }); |
| | 0 | 207 | | if (prices.Count > 0) |
| | 0 | 208 | | { |
| | 0 | 209 | | _db.PriceTrendDetails.AddRange(prices); |
| | 0 | 210 | | await _db.SaveChangesAsync(_authenticationService.UserId); |
| | 0 | 211 | | } |
| | 0 | 212 | | return errors.ToList(); |
| | | 213 | | } |
| | 0 | 214 | | catch (Exception e) |
| | 0 | 215 | | { |
| | 0 | 216 | | _logger.LogError("Ошибка загрузки цен из 1С"); |
| | 0 | 217 | | _logger.LogError(e.Message); |
| | 0 | 218 | | throw; |
| | | 219 | | } |
| | 0 | 220 | | } |
| | | 221 | | |
| | | 222 | | public async Task<List<PriceTrendFromFileResultDTO>> Load(long priceTrendId, List<PriceTrendFromFileDTO> data) |
| | | 223 | | { |
| | | 224 | | var priceTrend = await _priceTrendService.GetPriceTrend(priceTrendId, null); |
| | | 225 | | if(priceTrend == null) |
| | | 226 | | { |
| | | 227 | | throw new ArgumentException($"PriceTrend {priceTrendId} not found"); |
| | | 228 | | } |
| | | 229 | | |
| | | 230 | | var goodUniqueCodes = new List<string>(); |
| | | 231 | | var goodVendorCodes = new List<string>(); |
| | | 232 | | var goodBarCodes = new List<string>(); |
| | | 233 | | var goodNames = new List<string>(); |
| | | 234 | | |
| | | 235 | | data.ForEach(x => |
| | 0 | 236 | | { |
| | 0 | 237 | | x.UniqueCode = x.UniqueCode.NormalizeName(); |
| | 0 | 238 | | x.VendorCode = x.VendorCode.NormalizeName(); |
| | 0 | 239 | | x.BarCode = x.BarCode.NormalizeName(); |
| | 0 | 240 | | x.GoodName = x.GoodName.NormalizeName(); |
| | | 241 | | |
| | 0 | 242 | | if (!string.IsNullOrEmpty(x.UniqueCode)) |
| | 0 | 243 | | { |
| | 0 | 244 | | goodUniqueCodes.Add(x.UniqueCode.ToLower()); |
| | 0 | 245 | | } |
| | 0 | 246 | | else if (!string.IsNullOrEmpty(x.VendorCode)) |
| | 0 | 247 | | { |
| | 0 | 248 | | goodVendorCodes.Add(x.VendorCode.ToLower()); |
| | 0 | 249 | | } |
| | 0 | 250 | | else if (!string.IsNullOrEmpty(x.BarCode)) |
| | 0 | 251 | | { |
| | 0 | 252 | | goodBarCodes.Add(x.BarCode.ToLower()); |
| | 0 | 253 | | } |
| | 0 | 254 | | else if (!string.IsNullOrEmpty(x.GoodName)) |
| | 0 | 255 | | { |
| | 0 | 256 | | goodNames.Add(x.GoodName.ToLower()); |
| | 0 | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | x.Price = x.Price.NormalizeName(); |
| | 0 | 260 | | x.OldPrice = x.OldPrice.NormalizeName(); |
| | 0 | 261 | | }); |
| | | 262 | | activeRecordState = await _dirService.GetRecordState((long)RecordState.Active); |
| | | 263 | | |
| | | 264 | | var goodsByUniqueCodes = _goodService.FindGoodsByUniqueCodes(goodUniqueCodes).Result |
| | 0 | 265 | | .ToDictionary(x => x.UniqueCode.ToLower(), x => new List<Good> { x }); |
| | | 266 | | var goodsByVendorCodes = _goodService.FindGoodsByVendorsCodes(goodVendorCodes, priceTrend.SupplierDepartment |
| | 0 | 267 | | .GroupBy(x => x.GetActualVendorCode(priceTrend.SupplierDepartmentId)) |
| | 0 | 268 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 269 | | var goodsByBarCodes = _goodService.FindGoodsByBarCodes(goodBarCodes).Result |
| | 0 | 270 | | .GroupBy(x => x.GetActualBarCode()) |
| | 0 | 271 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 272 | | var goodsByNames = _goodService.FindGoodsByNames(goodNames).Result |
| | 0 | 273 | | .GroupBy(x => x.Name) |
| | 0 | 274 | | .ToDictionary(x => x.Key.ToLower(), x => x.ToList()); |
| | | 275 | | |
| | | 276 | | var priceTrendsDic = new Dictionary<long, PriceTrend>(); |
| | | 277 | | var goodsAdded = new List<long>(); |
| | | 278 | | |
| | | 279 | | var result = new List<PriceTrendFromFileResultDTO>(); |
| | | 280 | | foreach (var row in data) |
| | | 281 | | { |
| | | 282 | | if (string.IsNullOrEmpty(row.UniqueCode) |
| | | 283 | | && string.IsNullOrEmpty(row.VendorCode) |
| | | 284 | | && string.IsNullOrEmpty(row.BarCode) |
| | | 285 | | && string.IsNullOrEmpty(row.GoodName)) |
| | | 286 | | { |
| | | 287 | | result.Add(new PriceTrendFromFileResultDTO(row, "Не задан товар")); |
| | | 288 | | continue; |
| | | 289 | | } |
| | | 290 | | |
| | | 291 | | decimal priceDec; |
| | | 292 | | try |
| | | 293 | | { |
| | | 294 | | priceDec = row.Price.ToDecimal(); |
| | | 295 | | if(priceDec <= 0) |
| | | 296 | | { |
| | | 297 | | result.Add(new PriceTrendFromFileResultDTO(row, "Цена должна быть больше нуля")); |
| | | 298 | | continue; |
| | | 299 | | } |
| | | 300 | | } |
| | | 301 | | catch |
| | | 302 | | { |
| | | 303 | | result.Add(new PriceTrendFromFileResultDTO(row, "Некорректный формат цены")); |
| | | 304 | | continue; |
| | | 305 | | } |
| | | 306 | | |
| | | 307 | | decimal? priceOld = null; |
| | | 308 | | try |
| | | 309 | | { |
| | | 310 | | if (!string.IsNullOrEmpty(row.OldPrice)) |
| | | 311 | | { |
| | | 312 | | priceOld = row.OldPrice.ToDecimal(); |
| | | 313 | | if (priceOld <= 0) |
| | | 314 | | { |
| | | 315 | | result.Add(new PriceTrendFromFileResultDTO(row, "Старая цена должна быть больше нуля")); |
| | | 316 | | continue; |
| | | 317 | | } |
| | | 318 | | } |
| | | 319 | | } |
| | | 320 | | catch |
| | | 321 | | { |
| | | 322 | | result.Add(new PriceTrendFromFileResultDTO(row, "Некорректный формат старой цены")); |
| | | 323 | | continue; |
| | | 324 | | } |
| | | 325 | | |
| | | 326 | | List<Good> good = null; |
| | | 327 | | if (!String.IsNullOrEmpty(row.UniqueCode)) |
| | | 328 | | { |
| | | 329 | | goodsByUniqueCodes.TryGetValue(row.UniqueCode.ToLower(), out good); |
| | | 330 | | } |
| | | 331 | | else if (!String.IsNullOrEmpty(row.VendorCode)) |
| | | 332 | | { |
| | | 333 | | goodsByVendorCodes.TryGetValue(row.VendorCode.ToLower(), out good); |
| | | 334 | | } |
| | | 335 | | else if (!String.IsNullOrEmpty(row.BarCode)) |
| | | 336 | | { |
| | | 337 | | goodsByBarCodes.TryGetValue(row.BarCode.ToLower(), out good); |
| | | 338 | | } |
| | | 339 | | else if (!String.IsNullOrEmpty(row.GoodName)) |
| | | 340 | | { |
| | | 341 | | goodsByNames.TryGetValue(row.GoodName.ToLower(), out good); |
| | | 342 | | } |
| | | 343 | | else |
| | | 344 | | { |
| | | 345 | | result.Add(new PriceTrendFromFileResultDTO(row, "Не заданы UniqueCode, VendorCode, BarCode, GoodName |
| | | 346 | | continue; |
| | | 347 | | } |
| | | 348 | | |
| | | 349 | | if (good == null || good.Count == 0) |
| | | 350 | | { |
| | | 351 | | result.Add(new PriceTrendFromFileResultDTO(row, "Товар не найден")); |
| | | 352 | | continue; |
| | | 353 | | } |
| | | 354 | | if (good.Count > 1) |
| | | 355 | | { |
| | | 356 | | result.Add(new PriceTrendFromFileResultDTO(row, "Найден более чем один товар")); |
| | | 357 | | continue; |
| | | 358 | | } |
| | 0 | 359 | | if (goodsAdded.Any(x => x == good.Single().Id)) |
| | | 360 | | { |
| | | 361 | | result.Add(new PriceTrendFromFileResultDTO(row, "Переоценка для данного товара уже добавлена")); |
| | | 362 | | continue; |
| | | 363 | | } |
| | | 364 | | |
| | | 365 | | // добавляем туда собственно переоценку |
| | | 366 | | var trendDetail = new PriceTrendDetail() |
| | | 367 | | { |
| | | 368 | | PriceNew = priceDec, |
| | | 369 | | PriceOld = priceOld, |
| | | 370 | | PriceTrend = priceTrend, |
| | | 371 | | GoodId = good.Single().Id |
| | | 372 | | }; |
| | | 373 | | trendDetail.Discount = trendDetail.Discount(); |
| | | 374 | | _db.PriceTrendDetails.Add(trendDetail); |
| | | 375 | | goodsAdded.Add(good.Single().Id); |
| | | 376 | | } |
| | | 377 | | await _db.SaveChangesAsync(_authenticationService.UserId); |
| | | 378 | | |
| | | 379 | | return result; |
| | | 380 | | } |
| | | 381 | | } |
| | | 382 | | } |