| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using SVETA.Api.Data.DTO.Showcase; |
| | | 6 | | using SVETA.Api.Services.Interfaces; |
| | | 7 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using AutoMapper; |
| | | 10 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 11 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 12 | | using SVETA.Api.Data.DTO; |
| | | 13 | | using WinSolutions.Sveta.Server.Data.DataModel.Extensions; |
| | | 14 | | using Microsoft.Extensions.Options; |
| | | 15 | | using SVETA.Api.Data.Domain; |
| | | 16 | | using Newtonsoft.Json; |
| | | 17 | | using WinSolutions.Sveta.Common.Extensions; |
| | | 18 | | using Clave.Expressionify; |
| | | 19 | | using System.Diagnostics; |
| | | 20 | | using Microsoft.EntityFrameworkCore.Internal; |
| | | 21 | | |
| | | 22 | | namespace SVETA.Api.Services.Implements |
| | | 23 | | { |
| | | 24 | | public class ShowcaseWorker : IShowcaseWorker |
| | | 25 | | { |
| | | 26 | | private readonly ILogger<ShowcaseWorker> _logger; |
| | | 27 | | private readonly IGoodService _goodService; |
| | | 28 | | private readonly IDepartmentService _departmentService; |
| | | 29 | | private readonly IContragentService _contragentService; |
| | | 30 | | private readonly IDiscountColorService _discountColorService; |
| | | 31 | | private readonly ImagesSettings _imageSettings; |
| | | 32 | | List<DiscountColor> discountColors; |
| | | 33 | | |
| | | 34 | | |
| | 0 | 35 | | public ShowcaseWorker(ILogger<ShowcaseWorker> logger, IGoodService goodService, |
| | 0 | 36 | | IDepartmentService departmentService, IContragentService contragentService, |
| | 0 | 37 | | IDiscountColorService discountColorService, IOptions<ImagesSettings> options) |
| | 0 | 38 | | { |
| | 0 | 39 | | _logger = logger; |
| | 0 | 40 | | _goodService = goodService; |
| | 0 | 41 | | _departmentService = departmentService; |
| | 0 | 42 | | _contragentService = contragentService; |
| | 0 | 43 | | _discountColorService = discountColorService; |
| | 0 | 44 | | _imageSettings = options.Value; |
| | | 45 | | |
| | 0 | 46 | | discountColors = _discountColorService.GetDiscountColors(0, int.MaxValue, null, null).Result.Result; |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | Department CheckDepartment(Department department) |
| | 0 | 50 | | { |
| | 0 | 51 | | if(department == null) |
| | 0 | 52 | | { |
| | 0 | 53 | | throw new ArgumentException("Не найден магазин"); |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | if(department.Cluster == null || department.Cluster.IsDeleted) |
| | 0 | 57 | | { |
| | 0 | 58 | | throw new ArgumentException("Не найден кластер"); |
| | | 59 | | } |
| | | 60 | | |
| | 0 | 61 | | if (department.Cluster.Warehouse == null || department.Cluster.Warehouse.IsDeleted) |
| | 0 | 62 | | { |
| | 0 | 63 | | throw new ArgumentException("Не найден склад-владелец"); |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | return department; |
| | 0 | 67 | | } |
| | | 68 | | |
| | | 69 | | public async Task<IQueryable<Good>> GetShowcaseGoodsBaseQuery(long? categoryId, long departmentId, bool showNA) |
| | 0 | 70 | | { |
| | 0 | 71 | | var dep = CheckDepartment(await _departmentService.GetDepartment(departmentId)); |
| | 0 | 72 | | return _goodService.GetShowcaseGoodsBaseQuery(categoryId, dep.Cluster, showNA); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | public List<ShowcaseGoodDTO> GetShowcaseGoods(long? categoryId, long departmentId, bool showNA, int page, int li |
| | | 76 | | out int count, out int filteredCount, string labelname = default) |
| | 0 | 77 | | { |
| | 0 | 78 | | var dep = CheckDepartment(_departmentService.GetDepartment(departmentId).Result); |
| | | 79 | | |
| | 0 | 80 | | var ratio = GetContractRatio(dep).Result; |
| | 0 | 81 | | if (ratio == 0 || dep.Cluster.RatioForCalculations == 0) |
| | 0 | 82 | | { |
| | 0 | 83 | | count = filteredCount = 0; |
| | 0 | 84 | | return new List<ShowcaseGoodDTO>(); |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | // запрос для всех товаров заданной категории |
| | 0 | 88 | | var allGoods = _goodService.GetShowcaseGoodsBaseQuery(categoryId, dep.Cluster, showNA); |
| | 0 | 89 | | count = filteredCount = allGoods.Count(); |
| | | 90 | | |
| | | 91 | | // отфильтрованный запрос товаров заданной категории |
| | 0 | 92 | | var sidebarFilter = ShowcaseSidebarFilter.FromJson(sidebarFilterJson); |
| | 0 | 93 | | if (sidebarFilter != null || !string.IsNullOrWhiteSpace(filter) || !string.IsNullOrEmpty(labelname)) |
| | 0 | 94 | | { |
| | 0 | 95 | | allGoods = _goodService.GetFilteredShowcaseGoodsQuery(allGoods, dep.Cluster, ratio, |
| | 0 | 96 | | filter, sidebarFilter?.brands, sidebarFilter?.country, sidebarFilter?.minQuantity, |
| | 0 | 97 | | sidebarFilter?.price?.priceFrom, sidebarFilter?.price?.priceTo, labelname); |
| | 0 | 98 | | filteredCount = allGoods.Count(); |
| | 0 | 99 | | } |
| | | 100 | | |
| | | 101 | | // применяем сотировку и пагинацию |
| | 0 | 102 | | var pagedGoods = _goodService.GetPagedShowcaseGoodsQuery(allGoods, dep.Cluster, page, limit, sort).ToList(); |
| | 0 | 103 | | pagedGoods.ForEach(g => g.Photos.SetPhotoUrl(_imageSettings)); |
| | | 104 | | |
| | 0 | 105 | | var result = ToGoodDtoMapper(dep, ratio, discountColors, _imageSettings) |
| | 0 | 106 | | .Map<List<ShowcaseGoodDTO>>(pagedGoods) |
| | 0 | 107 | | .ToList(); |
| | 0 | 108 | | result.ForEach(x => PrepareGoodDTO(x)); |
| | | 109 | | |
| | 0 | 110 | | return result; |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | void PrepareGoodDTO(ShowcaseGoodDTO dto) |
| | 0 | 114 | | { |
| | 0 | 115 | | if(dto.OldPrice.GetValueOrDefault(0) <= dto.Price) |
| | 0 | 116 | | { |
| | 0 | 117 | | dto.OldPrice = null; |
| | 0 | 118 | | dto.Discount = null; |
| | 0 | 119 | | dto.LabelColor = null; |
| | 0 | 120 | | } |
| | 0 | 121 | | } |
| | | 122 | | |
| | | 123 | | public async Task<decimal> GetContractRatio(Department buyerStore) |
| | 0 | 124 | | { |
| | 0 | 125 | | var buyer = await _contragentService.GetContragentWithContracts(buyerStore.Contragent.Id); |
| | 0 | 126 | | var seller = (await _departmentService.GetDepartment(buyerStore.Cluster.Warehouse.Id)).Contragent; |
| | 0 | 127 | | var contract = buyer.ContractsAsBuyer |
| | 0 | 128 | | .Where(x => !x.IsDeleted |
| | 0 | 129 | | && x.Seller.Id == seller.Id |
| | 0 | 130 | | && x.BeginDate <= DateTime.UtcNow |
| | 0 | 131 | | && x.EndDate >= DateTime.UtcNow) |
| | 0 | 132 | | .FirstOrDefault(); |
| | 0 | 133 | | if(contract == null) |
| | 0 | 134 | | { |
| | 0 | 135 | | throw new ArgumentException("Контракт не найден"); |
| | | 136 | | } |
| | 0 | 137 | | return contract.RatioForCalculations; |
| | 0 | 138 | | } |
| | | 139 | | |
| | | 140 | | public async Task<ShowcaseGoodDTO> GetShowcaseGood(long goodId, long departmentId) |
| | 0 | 141 | | { |
| | 0 | 142 | | var dep = CheckDepartment(await _departmentService.GetDepartment(departmentId)); |
| | 0 | 143 | | var ratio = await GetContractRatio(dep); |
| | | 144 | | |
| | 0 | 145 | | var good = await _goodService.GetShowcaseGood(goodId, dep.Cluster); |
| | 0 | 146 | | if(good == null) |
| | 0 | 147 | | { |
| | 0 | 148 | | throw new ArgumentException("Не найден товар"); |
| | | 149 | | } |
| | | 150 | | //установим урлы для картинок товара |
| | 0 | 151 | | good.Photos.SetPhotoUrl(_imageSettings); |
| | | 152 | | |
| | 0 | 153 | | var result = ToGoodDtoMapper(dep, ratio, discountColors, _imageSettings).Map<ShowcaseGoodDTO>(good); |
| | 0 | 154 | | PrepareGoodDTO(result); |
| | 0 | 155 | | return result; |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | private static IMapper ToGoodDtoMapper(Department department, decimal contractRatio, List<DiscountColor> discoun |
| | 0 | 159 | | { |
| | 0 | 160 | | var config = new MapperConfiguration(cfg => |
| | 0 | 161 | | { |
| | 0 | 162 | | cfg.CreateMap<Good, ShowcaseGoodDTO>() |
| | 0 | 163 | | .ForMember(d => d.VendorCode, e => e.MapFrom(x => x.GetActualVendorCode(department.Cluster.Warehouse |
| | 0 | 164 | | .ForMember(d => d.Price, e => e.MapFrom(x => x.CurrentPrice(department.Cluster, contractRatio))) |
| | 0 | 165 | | .ForMember(d => d.OldPrice, e => e.MapFrom(x => x.OldPrice(department.Cluster, contractRatio))) |
| | 0 | 166 | | .ForMember(d => d.Discount, |
| | 0 | 167 | | e => e.MapFrom(x => x.Prices.Actual(department.Cluster.WarehouseId).DiscountForLabelDisplay())) |
| | 0 | 168 | | .ForMember(d => d.LabelColor, |
| | 0 | 169 | | e => e.MapFrom(x => GetDiscountColor(x.Prices.Actual(department.Cluster.WarehouseId).DiscountFor |
| | 0 | 170 | | .ForMember(d => d.TextColor, |
| | 0 | 171 | | e => e.MapFrom(x => GetDiscountTextColor(x.Prices.Actual(department.Cluster.WarehouseId).Discoun |
| | 0 | 172 | | .ForMember(d => d.Transparency, |
| | 0 | 173 | | e => e.MapFrom(x => GetDiscountTransparency(x.Prices.Actual(department.Cluster.WarehouseId).Disc |
| | 0 | 174 | | .ForMember(d => d.RestQuantity, e => e.MapFrom(x => x.Rests.ActualQuantity(department.Cluster.Wareho |
| | 0 | 175 | | .ForMember(d => d.MinQuantity, |
| | 0 | 176 | | e => e.MapFrom(x => x.DepartmentGoodSettings.ActualMinQuantity(department.Cluster.WarehouseId))) |
| | 0 | 177 | | .ForMember(d => d.MainBarcode, e => e.MapFrom( |
| | 0 | 178 | | s => new BarCodeDTO |
| | 0 | 179 | | { |
| | 0 | 180 | | Id = s.GoodBarcodes.FirstOrDefault(b => b.IsPrimary) != null ? |
| | 0 | 181 | | s.GoodBarcodes.FirstOrDefault(b => b.IsPrimary).BarCode.Id |
| | 0 | 182 | | : s.DefaultBarCode.Id, |
| | 0 | 183 | | Code = s.GoodBarcodes.FirstOrDefault(b => b.IsPrimary) != null ? |
| | 0 | 184 | | s.GoodBarcodes.FirstOrDefault(b => b.IsPrimary).BarCode.Code |
| | 0 | 185 | | : s.DefaultBarCode.Code |
| | 0 | 186 | | })) |
| | 0 | 187 | | .ForMember(d => d.Barcodes, e => e.MapFrom( |
| | 0 | 188 | | s => s.GoodBarcodes.Where(b => !b.IsPrimary).Count() > 0 ? s.GoodBarcodes.Where(b => !b.IsPrimar |
| | 0 | 189 | | { |
| | 0 | 190 | | Code = b.BarCode != null ? b.BarCode.Code : default |
| | 0 | 191 | | }) |
| | 0 | 192 | | : new List<BarCodeDTO>() |
| | 0 | 193 | | )) |
| | 0 | 194 | | .ForMember(d => d.Labels, e => e.MapFrom( |
| | 0 | 195 | | s => s.DepartmentGoodSettings |
| | 0 | 196 | | .Where(x => !x.IsDeleted && x.ShowcaseVisible && x.DepartmentId == department.Cluster.Wareho |
| | 0 | 197 | | .SelectMany(x => x.GoodSettingsLabels) |
| | 0 | 198 | | .Where(x => !x.GoodLabel.IsDeleted && x.GoodLabel.ShowcaseVisible) |
| | 0 | 199 | | .Select(x => new ShowcaseGoodLabelDTO(x.GoodLabel)) |
| | 0 | 200 | | .OrderBy(x => x.Priority) |
| | 0 | 201 | | .ToList() |
| | 0 | 202 | | )) |
| | 0 | 203 | | .ForMember(d => d.PickingQuantum, |
| | 0 | 204 | | e => e.MapFrom(x => x.DepartmentGoodSettings.ActualPickingQuantum(department.Cluster.WarehouseId |
| | 0 | 205 | | .ForMember(d => d.VatKind, |
| | 0 | 206 | | e => e.MapFrom(s => new EnumDB_DTO |
| | 0 | 207 | | {Id = s.VatsKind.Id, Code = s.VatsKind.Code, Name = s.VatsKind.Name})) |
| | 0 | 208 | | .ForMember(d => d.UnitKind, |
| | 0 | 209 | | e => e.MapFrom(s => new EnumDB_DTO |
| | 0 | 210 | | {Id = s.UnitsKind.Id, Code = s.UnitsKind.Code, Name = s.UnitsKind.Name})); |
| | 0 | 211 | | cfg.CreateMap<Photo, PhotoDTO>(); |
| | 0 | 212 | | cfg.CreateMap<Category, CategoryForFeedDTO>() |
| | 0 | 213 | | .ForMember(d => d.Id, e => e.MapFrom(s => s.Id)) |
| | 0 | 214 | | .ForMember(d => d.Name, e => e.MapFrom(s => s.Name)) |
| | 0 | 215 | | .ForMember(d => d.ExcludeFromGoogleFeed, e => e.MapFrom(s => s.ExcludeFromGoogleFeed)) |
| | 0 | 216 | | .ForMember(d => d.ExcludeFromYandexFeed, e => e.MapFrom(s => s.ExcludeFromYandexFeed)) |
| | 0 | 217 | | .ForMember(d => d.GoogleProductCategoryCode, e => e.MapFrom(s => s.GoogleProductCategoryCode)); |
| | 0 | 218 | | cfg.CreateMap<Contragent, IdNameDTO>() |
| | 0 | 219 | | .ForMember(d => d.Id, e => e.MapFrom(s => s.Id)) |
| | 0 | 220 | | .ForMember(d => d.Name, e => e.MapFrom(s => s.ShortName)); |
| | 0 | 221 | | cfg.CreateMap<Brand, IdNameDTO>() |
| | 0 | 222 | | .ForMember(d => d.Id, e => e.MapFrom(s => s.Id)) |
| | 0 | 223 | | .ForMember(d => d.Name, e => e.MapFrom(s => s.Name)); |
| | 0 | 224 | | cfg.CreateMap<BarCode, BarCodeDTO>(); |
| | 0 | 225 | | cfg.CreateMap<Country, IdNameDTO>(); |
| | 0 | 226 | | }); |
| | 0 | 227 | | var mapper = config.CreateMapper(); |
| | 0 | 228 | | return mapper; |
| | 0 | 229 | | } |
| | | 230 | | |
| | | 231 | | static string GetDiscountColor(int? discount, List<DiscountColor> discountColors) |
| | 0 | 232 | | { |
| | 0 | 233 | | return discount.HasValue |
| | 0 | 234 | | ? discountColors.Where(x => x.DiscountLevel <= discount.Value).OrderByDescending(x => x.DiscountLevel).S |
| | 0 | 235 | | : null; |
| | 0 | 236 | | } |
| | | 237 | | |
| | | 238 | | static string GetDiscountTextColor(int? discount, List<DiscountColor> discountColors) |
| | 0 | 239 | | { |
| | 0 | 240 | | return discount.HasValue |
| | 0 | 241 | | ? discountColors.Where(x => x.DiscountLevel <= discount.Value).OrderByDescending(x => x.DiscountLevel).F |
| | 0 | 242 | | : null; |
| | 0 | 243 | | } |
| | | 244 | | |
| | | 245 | | static decimal GetDiscountTransparency(int? discount, List<DiscountColor> discountColors) |
| | 0 | 246 | | { |
| | 0 | 247 | | var color = discount.HasValue |
| | 0 | 248 | | ? discountColors.Where(x => x.DiscountLevel <= discount.Value).OrderByDescending(x => x.DiscountLevel).F |
| | 0 | 249 | | : null; |
| | 0 | 250 | | return color != null ? color.Transparency : 0; |
| | 0 | 251 | | } |
| | | 252 | | } |
| | | 253 | | |
| | | 254 | | class ShowcaseSidebarPriceFilter |
| | | 255 | | { |
| | | 256 | | public string price_from { get; set; } |
| | | 257 | | |
| | | 258 | | public string price_to { get; set; } |
| | | 259 | | |
| | | 260 | | public decimal? priceFrom { get; set; } |
| | | 261 | | |
| | | 262 | | public decimal? priceTo { get; set; } |
| | | 263 | | } |
| | | 264 | | |
| | | 265 | | class ShowcaseSidebarFilter |
| | | 266 | | { |
| | | 267 | | public List<long> brands { get; set; } |
| | | 268 | | |
| | | 269 | | public List<long> country { get; set; } |
| | | 270 | | |
| | | 271 | | public List<decimal> minQuantity { get; set; } |
| | | 272 | | |
| | | 273 | | public ShowcaseSidebarPriceFilter price { get; set; } |
| | | 274 | | |
| | | 275 | | public static ShowcaseSidebarFilter FromJson(string json) |
| | | 276 | | { |
| | | 277 | | ShowcaseSidebarFilter result = null; |
| | | 278 | | if (!string.IsNullOrWhiteSpace(json)) |
| | | 279 | | { |
| | | 280 | | try |
| | | 281 | | { |
| | | 282 | | result = JsonConvert.DeserializeObject<ShowcaseSidebarFilter>(json); |
| | | 283 | | if (result.price != null) |
| | | 284 | | { |
| | | 285 | | if (!string.IsNullOrEmpty(result.price.price_from)) |
| | | 286 | | { |
| | | 287 | | result.price.priceFrom = result.price.price_from.ToDecimal(); |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | if (!string.IsNullOrEmpty(result.price.price_to)) |
| | | 291 | | { |
| | | 292 | | result.price.priceTo = result.price.price_to.ToDecimal(); |
| | | 293 | | } |
| | | 294 | | } |
| | | 295 | | else |
| | | 296 | | { |
| | | 297 | | result.price = new ShowcaseSidebarPriceFilter(); |
| | | 298 | | } |
| | | 299 | | } |
| | | 300 | | catch |
| | | 301 | | { |
| | | 302 | | throw new ArgumentException("Некорректный sidebarFilterJson"); |
| | | 303 | | } |
| | | 304 | | } |
| | | 305 | | return result; |
| | | 306 | | } |
| | | 307 | | } |
| | | 308 | | } |