public void UpdateChannelData() { try { var createTime = DateTime.Now; var createBy = Constants.SystemUser; var skuList = GetSupplierSKUList(); var localItemList = GetAllItems(); var updateItemList = from sl in skuList join li in localItemList on sl.SKU.ToUpper() equals li.SKU.ToUpper() select new { li.ID, SKU = sl.SKU.ToUpper(), sl.Title, sl.InventoryQty, sl.Price, sl.Description, sl.IsBulkyItem, sl.RrpPrice, sl.Category, sl.Discontinued, sl.EANCode, sl.Brand, sl.MPN, sl.Weight, sl.Length, sl.Width, sl.Height, sl.VIC, sl.NSW, sl.SA, sl.QLD, sl.TAS, sl.WA, sl.NT, sl.Image1, sl.Image2, sl.Image3, sl.Image4, sl.Image5, sl.Image6, sl.Image7, sl.Image8, sl.Image9, sl.Image10, sl.Image11, sl.Image12, sl.Image13, sl.Image14, sl.Image15, LocalListing = li }; foreach (var updateItem in updateItemList) { var item = updateItem.LocalListing; if(!string.IsNullOrWhiteSpace(updateItem.Description)) item.Description = updateItem.Description; item.Cost = updateItem.Price; var postage = new List<decimal>() { updateItem.VIC.IsNumeric()? Convert.ToDecimal(updateItem.VIC):0, updateItem.NSW.IsNumeric()? Convert.ToDecimal(updateItem.NSW):0, updateItem.SA.IsNumeric()? Convert.ToDecimal(updateItem.SA):0, updateItem.QLD.IsNumeric()? Convert.ToDecimal(updateItem.QLD):0, updateItem.TAS.IsNumeric()? Convert.ToDecimal(updateItem.TAS):0, updateItem.WA.IsNumeric()? Convert.ToDecimal(updateItem.WA):0, updateItem.NT.IsNumeric()? Convert.ToDecimal(updateItem.NT):0 }.Max(); item.Price = (item.Cost+ postage) * Convert.ToDecimal(1.3); //UpdateItem(item); _itemRepository.Update(item,itm=> itm.Description,itm=>itm.Cost,itm=>itm.Price); } #region Add New Item var addItemList = from sl in skuList where !localItemList.Any(li => li.SKU.ToUpper().Equals(sl.SKU.ToUpper())) select sl; foreach (var additem in addItemList) { try { if (additem.SKU.Length > 23) LogManager.Instance.Info($"SKU {additem.SKU} length is larger than 23."); //throw new Exception($"SKU {additem.SKU} length is larger than 23."); var newItem = new D_Item(); newItem.SKU = additem.SKU; newItem.Name = additem.Title; newItem.Cost = additem.Price; newItem.Price= newItem.Cost * Convert.ToDecimal(1.3); newItem.Description = additem.Description; newItem.Type = ThirdStoreItemType.SINGLE.ToValue(); newItem.SupplierID = ThirdStoreSupplier.P.ToValue(); newItem.GrossWeight =(!string.IsNullOrWhiteSpace(additem.Weight) ? Convert.ToDecimal(additem.Weight):0); newItem.NetWeight = (!string.IsNullOrWhiteSpace(additem.Weight) ? Convert.ToDecimal(additem.Weight) : 0); newItem.Length =(!string.IsNullOrWhiteSpace(additem.Length) ? Convert.ToDecimal(additem.Length)/100 : 0); newItem.Width = (!string.IsNullOrWhiteSpace(additem.Width) ? Convert.ToDecimal(additem.Width) / 100 : 0); newItem.Height = (!string.IsNullOrWhiteSpace(additem.Height) ? Convert.ToDecimal(additem.Height) / 100 : 0); //newItem.GrossWeight = additem.Weight; //newItem.NetWeight = additem.Weight; //newItem.Length = additem.Length; //newItem.Width = additem.Width; //newItem.Height = additem.Height; newItem.Ref3 = additem.EANCode.Trim(); newItem.IsReadyForList =(additem.SKU.Length <= 23&&additem.Price<= Convert.ToDecimal(ThirdStoreConfig.Instance.SyncDSPriceBelow)? true :false) ; newItem.IsActive = true; newItem.CreateTime = createTime; newItem.CreateBy = createBy; newItem.EditTime = createTime; newItem.EditBy = createBy; newItem.FillOutNull(); DownloadItemImages(additem, newItem); #region Download and Save Item Images (Already moved to DownloadItemImages) //var imagesURL = new List<string>(); //if (!string.IsNullOrEmpty(additem.Image1)) // imagesURL.Add(additem.Image1); //if (!string.IsNullOrEmpty(additem.Image2)) // imagesURL.Add(additem.Image2); //if (!string.IsNullOrEmpty(additem.Image3)) // imagesURL.Add(additem.Image3); //if (!string.IsNullOrEmpty(additem.Image4)) // imagesURL.Add(additem.Image4); //if (!string.IsNullOrEmpty(additem.Image5)) // imagesURL.Add(additem.Image5); //if (!string.IsNullOrEmpty(additem.Image6)) // imagesURL.Add(additem.Image6); //if (!string.IsNullOrEmpty(additem.Image7)) // imagesURL.Add(additem.Image7); //if (!string.IsNullOrEmpty(additem.Image8)) // imagesURL.Add(additem.Image8); //if (!string.IsNullOrEmpty(additem.Image9)) // imagesURL.Add(additem.Image9); //if (!string.IsNullOrEmpty(additem.Image10)) // imagesURL.Add(additem.Image10); //if (!string.IsNullOrEmpty(additem.Image11)) // imagesURL.Add(additem.Image11); //if (!string.IsNullOrEmpty(additem.Image12)) // imagesURL.Add(additem.Image12); //if (!string.IsNullOrEmpty(additem.Image13)) // imagesURL.Add(additem.Image13); //if (!string.IsNullOrEmpty(additem.Image14)) // imagesURL.Add(additem.Image14); //if (!string.IsNullOrEmpty(additem.Image15)) // imagesURL.Add(additem.Image15); // //int i = 0; //using (var wc = new ThirdStoreWebClient()) //{ // foreach (var imageURL in imagesURL) // { // try // { // var imgBytes = wc.DownloadData(imageURL); // using (var stream = new MemoryStream(imgBytes)) // { // var fileName = additem.SKU + "-" + i.ToString().PadLeft(2, '0') + ".jpg"; // var imgObj = _imageService.SaveImage(stream, fileName); // newItem.ItemImages.Add(new M_ItemImage() // { // Image = imgObj, // DisplayOrder = i, // StatusID = 0,//TODO Get item active status id // CreateTime = createTime, // CreateBy = createBy, // EditTime = createTime, // EditBy = createBy // }); // } // } // catch (Exception ex) // { // LogManager.Instance.Error(imageURL + " download failed. " + ex.Message); // } // i++; // } //} #endregion //DirectoryInfo di = new DirectoryInfo(ThirdStoreConfig.Instance.ThirdStoreImagesPath + "\\" + additem.SKU + "\\"); //DirectoryInfo di = new DirectoryInfo(@"C:\Users\gdutj\Downloads\3rdStockSystem\DSZImages20191120\" + additem.SKU + "\\"); _itemRepository.Insert(newItem); } catch (Exception ex) { LogManager.Instance.Error(ex.Message); } } #endregion #region Persist DSZ Item Data //var fullFileName = ThirdStoreConfig.Instance.ThirdStoreDSZData+"\\"+CommonFunc.ToCSVFileName("DSZData"); //_csvContext.Write(skuList, fullFileName,_csvFileDescription); //_dszItemRepository.Clear(); //var insertItems = new List<D_DSZItem>(); //foreach(var item in skuList) //{ // var insertItem = AutoMapper.Mapper.Map<DSZSKUModel, D_DSZItem>(item); // insertItem.FillOutNull(); // insertItems.Add(insertItem); //} //_dszItemRepository.Insert(insertItems); #endregion } catch (Exception ex) { LogManager.Instance.Error(ex.Message); } }
public bool UpdateLocalItem() { try { var createTime = DateTime.Now; var createBy = Constants.SystemUser; var skuList = GetSupplierSKUList(); var localItemList = GetItemsBySupplier(1);//TODO Get Dropshipzone Supplier ID //var skuNullCount = skuList.Where(sl => sl==null).ToList(); #region Update Item var updateItemList = from sl in skuList join li in localItemList on sl.SKU.ToUpper() equals li.SKU.ToUpper() select new { li.ID, SKU = sl.SKU.ToUpper(), sl.Title, sl.InventoryQty, sl.Price, sl.Description, sl.IsBulkyItem, sl.RrpPrice, sl.Category, sl.Discontinued, sl.EANCode, sl.Brand, sl.MPN, sl.Weight, sl.Length, sl.Width, sl.Height, sl.VIC, sl.NSW, sl.SA, sl.QLD, sl.TAS, sl.WA, sl.NT, sl.Image1, sl.Image2, sl.Image3, sl.Image4, sl.Image5, sl.Image6, sl.Image7, sl.Image8, sl.Image9, sl.Image10, sl.Image11, sl.Image12, sl.Image13, sl.Image14, sl.Image15 }; foreach (var updateItem in updateItemList) { var item = localItemList.FirstOrDefault(li => li.ID == updateItem.ID); item.Title = updateItem.Title; item.Description = updateItem.Description.StripHTML(); item.InventoryQty = updateItem.InventoryQty; item.Price = updateItem.Price; item.Ref1 = updateItem.IsBulkyItem; item.Ref2 = updateItem.Weight; item.Ref3 = updateItem.Length; item.Ref4 = updateItem.Width; item.Ref5 = updateItem.Height; item.Ref6 = updateItem.RrpPrice; item.Ref7 = updateItem.Category; item.Ref8 = updateItem.Discontinued; item.Ref9 = updateItem.EANCode; item.Ref10 = updateItem.Brand; item.Ref11 = updateItem.MPN; item.StatusID = 1;//TODO Get item active status id //TODO: Redownload images if images changed if (_itemSettings.ReDownloadImage) { //item.ItemImages.ToList().ForEach(im => _imageService.DeleteImage(im.Image)); //DownloadNewImages(); } //postage rule var postageRuleName = updateItem.nameof(n => n.VIC) + ":" + updateItem.VIC + ";" + updateItem.nameof(n => n.NSW) + ":" + updateItem.NSW + ";" + updateItem.nameof(n => n.SA) + ":" + updateItem.SA + ";" + updateItem.nameof(n => n.QLD) + ":" + updateItem.QLD + ";" + updateItem.nameof(n => n.TAS) + ":" + updateItem.TAS + ";" + updateItem.nameof(n => n.WA) + ":" + updateItem.WA + ";" + updateItem.nameof(n => n.NT) + ":" + updateItem.NT; var existingRule = _postageRuleService.GetPostageRuleByName(postageRuleName); if (existingRule == null) { var newRule = new T_PostageRule(); newRule.Name = postageRuleName; newRule.Description = postageRuleName; newRule.CreateTime = createTime; newRule.CreateBy = createBy; newRule.EditTime = createTime; newRule.EditBy = createBy; newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "3000", PostcodeTo = "3999", Formula = updateItem.VIC, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "8000", PostcodeTo = "8999", Formula = updateItem.VIC, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "0200", PostcodeTo = "0299", Formula = updateItem.NSW, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "1000", PostcodeTo = "2999", Formula = updateItem.NSW, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "5000", PostcodeTo = "5999", Formula = updateItem.SA, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "4000", PostcodeTo = "4999", Formula = updateItem.QLD, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "9000", PostcodeTo = "9999", Formula = updateItem.QLD, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "7000", PostcodeTo = "7999", Formula = updateItem.TAS, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "6000", PostcodeTo = "6797", Formula = updateItem.WA, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "6800", PostcodeTo = "6999", Formula = updateItem.WA, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "0800", PostcodeTo = "0999", Formula = updateItem.NT, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); existingRule = _postageRuleService.InsertPostageRule(newRule); } item.PostageRuleID = existingRule.ID; _itemRepository.Update(item); } #endregion #region Delete Disabled Item var disableItemList = from li in localItemList where !skuList.Any(sl => sl.SKU.ToUpper().Equals(li.SKU.ToUpper())) && li.StatusID == 1 //TODO Get item active status id select li; foreach (var disableItem in disableItemList) { disableItem.StatusID = 2;//TODO Get item disable status id _itemRepository.Update(disableItem, i => i.StatusID); } #endregion #region Add New Item var addItemList = from sl in skuList where !localItemList.Any(li => li.SKU.ToUpper().Equals(sl.SKU.ToUpper())) select sl; foreach (var additem in addItemList) { try { var newItem = new D_Item(); newItem.SKU = additem.SKU; newItem.Title = additem.Title; newItem.Price = additem.Price; newItem.InventoryQty = additem.InventoryQty; newItem.Description = additem.Description; newItem.StatusID = 1; //TODO Get item active status id newItem.SupplierID = 1; //TODO Get Dropshipzone supplier id newItem.Ref1 = additem.IsBulkyItem; newItem.Ref2 = additem.Weight; newItem.Ref3 = additem.Length; newItem.Ref4 = additem.Width; newItem.Ref5 = additem.Height; newItem.Ref6 = additem.RrpPrice; newItem.Ref7 = additem.Category; newItem.Ref8 = additem.Discontinued; newItem.Ref9 = additem.EANCode; newItem.Ref10 = additem.Brand; newItem.Ref11 = additem.MPN; newItem.CreateTime = createTime; newItem.CreateBy = createBy; newItem.EditTime = createTime; newItem.EditBy = createBy; newItem.FillOutNull(); //images //var imagesURL = additem.Images.Split(';'); var imagesURL = new List <string>(); if (!string.IsNullOrEmpty(additem.Image1)) { imagesURL.Add(additem.Image1); } if (!string.IsNullOrEmpty(additem.Image2)) { imagesURL.Add(additem.Image2); } if (!string.IsNullOrEmpty(additem.Image3)) { imagesURL.Add(additem.Image3); } if (!string.IsNullOrEmpty(additem.Image4)) { imagesURL.Add(additem.Image4); } if (!string.IsNullOrEmpty(additem.Image5)) { imagesURL.Add(additem.Image5); } if (!string.IsNullOrEmpty(additem.Image6)) { imagesURL.Add(additem.Image6); } if (!string.IsNullOrEmpty(additem.Image7)) { imagesURL.Add(additem.Image7); } if (!string.IsNullOrEmpty(additem.Image8)) { imagesURL.Add(additem.Image8); } if (!string.IsNullOrEmpty(additem.Image9)) { imagesURL.Add(additem.Image9); } if (!string.IsNullOrEmpty(additem.Image10)) { imagesURL.Add(additem.Image10); } if (!string.IsNullOrEmpty(additem.Image11)) { imagesURL.Add(additem.Image11); } if (!string.IsNullOrEmpty(additem.Image12)) { imagesURL.Add(additem.Image12); } if (!string.IsNullOrEmpty(additem.Image13)) { imagesURL.Add(additem.Image13); } if (!string.IsNullOrEmpty(additem.Image14)) { imagesURL.Add(additem.Image14); } if (!string.IsNullOrEmpty(additem.Image15)) { imagesURL.Add(additem.Image15); } int i = 0; DirectoryInfo di = new DirectoryInfo(DropshipConfig.Instance.ImageFilesPath + additem.SKU + "\\"); if (!di.Exists) { di.Create(); } using (var wc = new DropshipWebClient()) { foreach (var imageURL in imagesURL) { try { var imageFileName = CommonFunc.GetImageFileName(additem.SKU, i); var saveImageFileFullName = Path.Combine(di.FullName, imageFileName); wc.DownloadFile(imageURL, saveImageFileFullName); newItem.ItemImages.Add(new M_ItemImage() { Image = _imageService.InsertImage(new D_Image() { ImagePath = additem.SKU + "\\" + imageFileName, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }), DisplayOrder = i + 1, StatusID = 5,//TODO Get item active status id CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); } catch (Exception ex) { LogManager.Instance.Error(imageURL + " download failed. " + ex.Message); } i++; } } //postage rule var postageRuleName = additem.nameof(n => n.VIC) + ":" + additem.VIC + ";" + additem.nameof(n => n.NSW) + ":" + additem.NSW + ";" + additem.nameof(n => n.SA) + ":" + additem.SA + ";" + additem.nameof(n => n.QLD) + ":" + additem.QLD + ";" + additem.nameof(n => n.TAS) + ":" + additem.TAS + ";" + additem.nameof(n => n.WA) + ":" + additem.WA + ";" + additem.nameof(n => n.NT) + ":" + additem.NT; var existingRule = _postageRuleService.GetPostageRuleByName(postageRuleName); if (existingRule == null) { var newRule = new T_PostageRule(); newRule.Name = postageRuleName; newRule.Description = postageRuleName; newRule.CreateTime = createTime; newRule.CreateBy = createBy; newRule.EditTime = createTime; newRule.EditBy = createBy; newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "3000", PostcodeTo = "3999", Formula = additem.VIC, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "8000", PostcodeTo = "8999", Formula = additem.VIC, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "0200", PostcodeTo = "0299", Formula = additem.NSW, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "1000", PostcodeTo = "2999", Formula = additem.NSW, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "5000", PostcodeTo = "5999", Formula = additem.SA, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "4000", PostcodeTo = "4999", Formula = additem.QLD, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "9000", PostcodeTo = "9999", Formula = additem.QLD, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "7000", PostcodeTo = "7999", Formula = additem.TAS, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "6000", PostcodeTo = "6797", Formula = additem.WA, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "6800", PostcodeTo = "6999", Formula = additem.WA, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); newRule.T_PostageRuleLine.Add(new T_PostageRuleLine() { PostcodeFrom = "0800", PostcodeTo = "0999", Formula = additem.NT, CreateTime = createTime, CreateBy = createBy, EditTime = createTime, EditBy = createBy }); existingRule = _postageRuleService.InsertPostageRule(newRule); } newItem.PostageRuleID = existingRule.ID; _itemRepository.Insert(newItem); } catch (Exception ex) { LogManager.Instance.Error(ex.Message); } } #endregion return(true); } catch (Exception ex) { LogManager.Instance.Error(ex.Message); return(false); } }
public void AddOrUpdateItem(IList<D_Item> items) { try { var createTime = DateTime.Now; var createBy = _workContext.CurrentUserName; var localItemList = GetAllItems(); #region Update Section var updateItemList = from sl in items join li in localItemList on sl.SKU.ToUpper() equals li.SKU.ToUpper() select new {updateData= sl, LocalItem= li }; var upds = new List<D_Item>(); foreach (var updateItem in updateItemList) { var item = updateItem.LocalItem; var updateData = updateItem.updateData; if (!string.IsNullOrWhiteSpace(updateData.Description)) item.Description = updateData.Description; item.Cost = updateData.Cost; item.Price = updateData.Price; upds.Add(item); } _itemRepository.Update(upds, itm => itm.Description, itm => itm.Cost, itm => itm.Price); #endregion #region Add Section var addItemList = from sl in items where !localItemList.Any(li => li.SKU.ToUpper().Equals(sl.SKU.ToUpper())) select sl; foreach (var additem in addItemList) { try { var newItem = new D_Item(); //newItem.SKU = additem.SKU; //newItem.Name = additem.Name; //newItem.Cost = additem.Cost; //newItem.Price = additem.Price; //newItem.Description = additem.Description; AutoMapper.Mapper.Map(additem,newItem); newItem.CreateTime = createTime; newItem.CreateBy = createBy; newItem.EditTime = createTime; newItem.EditBy = createBy; newItem.FillOutNull(); var imageURLs = newItem.Ref5.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries).ToList(); DownloadItemImages(imageURLs, newItem); //PreDownloadItemImages(imageURLs, newItem); //RestoreItemImages(imageURLs, newItem); _itemRepository.Insert(newItem); } catch(Exception ex) { LogManager.Instance.Error(ex.Message); } } #endregion } catch (Exception ex) { LogManager.Instance.Error(ex.Message); throw ex; } }