private bool CheckDiff(VkTask task, VkGoodsItem oldItem) { var result = task.IsDeleted != oldItem.IsDeleted || !task.Description.Equals(oldItem.Description) || !task.Header.Equals(oldItem.Name) || task.Price != oldItem.Price; return(result); }
public VkGoodsItem AddOrEditVkGoods(VkTask task, VkGoodsItem oldItem) { const int category = 4; using (var imageWorker = new VkImageWorker()) { if (oldItem == null || oldItem.VkId == 0) { if (_countAdd > MaxAdd) { _logger.Warn("Добавлено больше дневного лимита товаров"); return(null); } long idMainPhoto = 0; var dict = new Dictionary <long, string>(); var textAnswer = ""; foreach (var image in task.ImageList) { try { var wc = new WebClient(); wc.DownloadFile(new Uri(image), imageWorker.Filename); var serverInfo = _vk.Photo.GetMarketUploadServer(Config.GetConfig().IdGroup, idMainPhoto == 0); var nvc = new NameValueCollection(); textAnswer = Common.HttpUploadFile(serverInfo.UploadUrl, imageWorker.Filename, "file", "image/jpeg", nvc); if (_vk.UserId != null) { var loadPhoto = _vk.Photo.SaveMarketPhoto(Config.GetConfig().IdGroup, textAnswer); var id = loadPhoto[0].Id; if (id != null) { if (idMainPhoto == 0) { idMainPhoto = id.Value; } else { dict.Add(id.Value, image); } } else { throw new NullReferenceException("Пришел нулл в ответе от сервера ВК при загрузке фото"); } } else { throw new NullReferenceException("_vk.UserId = null"); } } catch (Exception e) { _logger.Error(e, "Ошибка при загрузке файла " + image + " на стену в ВК"); _logger.Error(textAnswer); if (textAnswer.Contains("ERR_UPLOAD_BAD_IMAGE_SIZE: market photo min size 400x400")) { _logger.Warn($"Изображение {image} слишком маленькое"); } else { throw; } } } if (idMainPhoto != 0) { var id = _vk.Markets.Add(new MarketProductParams() { CategoryId = category, Deleted = task.IsDeleted, Description = task.Description, MainPhotoId = idMainPhoto, Name = task.Header, OwnerId = Config.GetConfig().IdGroup * -1, Price = task.Price, PhotoIds = dict.Keys }); _countAdd++; var result = new VkGoodsItem() { Name = task.Header, Description = task.Description, Price = task.Price, IsDeleted = task.IsDeleted, IdImageList = dict.Keys.ToList(), MainImageId = idMainPhoto, SKU = task.SKU, VkId = id }; return(result); } const string text = "Ошибка при загрузке товара. Идентификатор основного фото равен 0"; _logger.Error(text); throw new Exception(text); } if (!CheckDiff(task, oldItem)) { return(oldItem); } _vk.Markets.Edit(new MarketProductParams() { Name = task.Header, Description = task.Description, Price = task.Price, Deleted = task.IsDeleted, ItemId = oldItem.VkId, PhotoIds = oldItem.IdImageList, MainPhotoId = oldItem.MainImageId, OwnerId = Config.GetConfig().IdGroup * -1, CategoryId = category }); oldItem.Description = task.Description; oldItem.Name = task.Header; oldItem.IsDeleted = task.IsDeleted; oldItem.Price = task.Price; return(oldItem); } }