Esempio n. 1
0
        public static Item Match(Item target, IEnumerable <Item> set, bool isLazada = false)
        {
            var matched =
                set.Where(s => target.Name.GetName() == s.Name.GetName()).ToList();

            var matched_targetLonger =
                set.Where(s => target.Name.GetName().Contains(s.Name.GetName())).ToList();

            var matched_setLonger =
                set.Where(s => s.Name.GetName().Contains(target.Name.GetName())).ToList();

            if (!matched.Any() && matched_targetLonger.Any() &&
                (matched_targetLonger.Count > matched.Count))
            {
                matched = matched_targetLonger;
            }

            if (!matched.Any() && matched_setLonger.Any() &&
                (matched_setLonger.Count > matched.Count))
            {
                matched = matched_setLonger;
            }

            if (!matched.Any())
            {
                // 90% matched
                matched = set.Where(s => CompareHelper.Compare(s.Name.GetName(), target.Name.GetName()) < target.Name.Length * 0.1).ToList();
            }

            if (!matched.Any())
            {
                // 80% matched
                matched = set.Where(s => CompareHelper.Compare(s.Name.GetName(), target.Name.GetName()) < target.Name.Length * 0.2).ToList();
            }

            // Not found
            if (!matched.Any())
            {
                target.Matched = false;
                return(target);
            }

            // One only no altname
            if (matched.Count == 1)
            {
                var firstMatch = matched.First();
                if (string.IsNullOrEmpty(firstMatch.Model))
                {
                    firstMatch.Matched = true;
                    return(firstMatch);
                }
            }

            target.SKU = target.SKU ?? target.Model ?? "";

            // Name and altname same [Manualy add cost]
            var manualyMatched = set.Where(s =>
                                           !string.IsNullOrEmpty(s.Model) &&
                                           target.Name.Equals(s.Name, StringComparison.InvariantCultureIgnoreCase) &&
                                           target.SKU.Equals(s.Model, StringComparison.InvariantCultureIgnoreCase))
                                 .FirstOrDefault();

            if (manualyMatched != null)
            {
                manualyMatched.Matched = true;
                return(manualyMatched);
            }

            // Duplicate matched
            var targetSize = target.SKU;

            if (string.IsNullOrEmpty(targetSize) && matched.Any())
            {
                var dupMatched = matched.OrderBy(s => s.Price).First();
                dupMatched.Matched = true;
                return(dupMatched);
            }

            // Multiple matched [Get sizing]
            if (!string.IsNullOrEmpty(targetSize) && matched.Any())
            {
                // Duplicate matched but target has sku

                if (matched.All(x => string.IsNullOrEmpty(x.Model)))
                {
                    var moreMath = matched.OrderByDescending(x => x.Stock).First();
                    moreMath.Matched = true;
                    return(moreMath);
                }

                foreach (var alt in matched.Where(s => s.Model != null))
                {
                    alt.Model = alt.Model;
                }

                var sizeMatched = matched.Where(s => s.Model != null).OrderBy(s => CompareHelper.Compare(targetSize.GetName(), s.Model.GetName())).First();

                // Price so differance
                var targetPrice  = isLazada ? target.Price * 95 / 100 : target.Price;
                var matchedPrice = sizeMatched.Price;

                if (Math.Abs(targetPrice - matchedPrice) > 100)
                {
                    sizeMatched = CompareHelper.GetClosedPrice(targetPrice, matched);
                }

                sizeMatched.MultiStocks = string.Join(", ", matched.Select(x => x.Model + " : " + x.Stock));
                sizeMatched.MultiPrices = string.Join(", ", matched.Select(x => x.Model + " : " + x.Price));

                sizeMatched.Matched = true;
                return(sizeMatched);
            }

            target.Matched = false;
            return(target);
        }
Esempio n. 2
0
        public static Item Match_Stable(Item target, IEnumerable <Item> set)
        {
            var matched =
                set.Where(s => target.Name.GetName() == s.Name.GetName()).ToList();

            var matched_targetLonger =
                set.Where(s => target.Name.GetName().Contains(s.Name.GetName())).ToList();

            var matched_setLonger =
                set.Where(s => s.Name.GetName().Contains(target.Name.GetName())).ToList();

            if (!matched.Any() && matched_targetLonger.Any() &&
                (matched_targetLonger.Count > matched.Count))
            {
                matched = matched_targetLonger;
            }

            if (!matched.Any() && matched_setLonger.Any() &&
                (matched_setLonger.Count > matched.Count))
            {
                matched = matched_setLonger;
            }

            if (!matched.Any())
            {
                // 90% matched
                matched = set.Where(s => CompareHelper.Compare(s.Name.GetName(), target.Name.GetName()) < target.Name.Length * 0.1).ToList();
            }

            if (!matched.Any())
            {
                // 80% matched
                matched = set.Where(s => CompareHelper.Compare(s.Name.GetName(), target.Name.GetName()) < target.Name.Length * 0.2).ToList();
            }

            // Not found
            if (!matched.Any())
            {
                target.Matched = false;
                return(target);
            }

            // One only no altname
            if (matched.Count == 1)
            {
                var firstMatch = matched.First();
                if (string.IsNullOrEmpty(firstMatch.Model))
                {
                    firstMatch.Matched = true;
                    return(firstMatch);
                }
            }

            target.SKU = target.SKU ?? target.Model ?? "";

            // Name and altname same [Manualy add cost]
            var manualyMatched = set.Where(s =>
                                           !string.IsNullOrEmpty(s.Model) &&
                                           target.Name.Equals(s.Name, StringComparison.InvariantCultureIgnoreCase) &&
                                           target.SKU.Equals(s.Model, StringComparison.InvariantCultureIgnoreCase))
                                 .FirstOrDefault();

            if (manualyMatched != null)
            {
                manualyMatched.Matched = true;
                return(manualyMatched);
            }

            // Duplicate matched
            var targetSize = string.IsNullOrEmpty(target.SKU) ? null : Regex.Match(target.SKU, @"\d+").Value;

            if (string.IsNullOrEmpty(targetSize) && matched.Any())
            {
                var dupMatched = matched.OrderBy(s => s.Price).First();
                dupMatched.Matched = true;
                return(dupMatched);
            }

            // Multiple matched [Get sizing]
            if (!string.IsNullOrEmpty(targetSize) && matched.Any())
            {
                // Duplicate matched but target has sku

                if (matched.All(x => string.IsNullOrEmpty(x.Model)))
                {
                    var moreMath = matched.OrderByDescending(x => x.Stock).First();
                    moreMath.Matched = true;
                    return(moreMath);
                }

                foreach (var alt in matched.Where(s => s.Model != null))
                {
                    alt.Model = Regex.Match(alt.Model, @"\d+").Value;
                }

                var sizeMatched = matched.Where(s => s.Model != null).OrderBy(s => CompareHelper.Compare(targetSize.GetName(), s.Model.GetName())).First();
                sizeMatched.Matched = true;
                return(sizeMatched);
            }

            target.Matched = false;
            return(target);
        }
Esempio n. 3
0
        private void WriteLazada(string outputPath)
        {
            var results = new List <Item>();

            foreach (var source in _sources)
            {
                var descs = _descs.FirstOrDefault(s => string.Compare(s.Name, source.Name, StringComparison.CurrentCultureIgnoreCase) == 0);
                if (descs == null)
                {
                    var lazName     = source.Name;
                    var match90Name = _descs.FirstOrDefault(s => CompareHelper.Compare(s.Name.ToLower(), source.Name.ToLower()) < source.Name.Length * 0.1)?.Name;
                    var match80Name = match90Name == null?_descs.FirstOrDefault(s => CompareHelper.Compare(s.Name.ToLower(), source.Name.ToLower()) < source.Name.Length * 0.2)?.Name : null;

                    var match70Name = match90Name == null && match80Name == null?_descs.FirstOrDefault(s => CompareHelper.Compare(s.Name.ToLower(), source.Name.ToLower()) < source.Name.Length * 0.3)?.Name : null;

                    results.Add(new Item()
                    {
                        LazName = lazName, Matched90Name = match90Name, Matched80Name = match80Name, Matched70Name = match70Name
                    });
                }
            }

            using (FileStream stream = new FileStream(outputPath, FileMode.CreateNew, FileAccess.Write))
            {
                var workbook = new XSSFWorkbook();
                var sheet    = workbook.CreateSheet("sheet1");
                var row      = 0;

                var headerRow = sheet.CreateRow(row);
                headerRow.CreateCell(0).SetCellValue("lazName");
                headerRow.CreateCell(1).SetCellValue("Matched90Name");
                headerRow.CreateCell(2).SetCellValue("Matched80Name");
                headerRow.CreateCell(3).SetCellValue("Matched70Name");

                foreach (var result in results.OrderByDescending(x => x.Matched90Name).ThenByDescending(x => x.Matched80Name).ThenByDescending(x => x.Matched70Name))
                {
                    var rowtemp = sheet.CreateRow(++row);
                    rowtemp.CreateCell(0).SetCellValue(result.LazName);
                    rowtemp.CreateCell(1).SetCellValue(result.Matched90Name);
                    rowtemp.CreateCell(2).SetCellValue(result.Matched80Name);
                    rowtemp.CreateCell(3).SetCellValue(result.Matched70Name);
                }

                workbook.Write(stream);
            }
        }