using AllBase;
using AllBase.Model.Result.PDDResult;
using AllBase.Model.Result;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using Work.Store.Api;
namespace Work.Services;
///
/// 图片缓存服务
///
public class ImageCacheService
{
///
/// 获取图片缓存
///
///
public static Dictionary GetImageCaches(PDDProductInfo product)
{
Dictionary wxImgMapDic = new Dictionary(StringComparer.OrdinalIgnoreCase);
try
{
List imageUrls = new List();
imageUrls.AddRange(product.detail.galleries.Where(g => g.type == 1).Take(9).Select(a => a.url));
var temp_detailGalleries = product.detail.galleries.Where(g => g.type == 2).ToList();
if (temp_detailGalleries.Count == 0)
temp_detailGalleries.Add(new gallery_item() { type = 2, url = product.detail.galleries.FirstOrDefault(g => g.type == 1)?.url });
imageUrls.AddRange(temp_detailGalleries.Select(a => a.url));
if (product.detail.sku.FirstOrDefault()?.spec?.Count > 0)
{
imageUrls.AddRange(product.detail.sku.Select(a => a.thumb_url));
}
List imageUrlMd5s = imageUrls.Select(a => BaseClass.GetMd5(a)).ToList();
var caches = StoreApiClient.Instance.ImageCache.GetImageCaches(imageUrlMd5s);
foreach (var item in caches)
{
item.IsCache = true;
wxImgMapDic[item.Md5] = item;
}
}
catch(Exception ex)
{
}
return wxImgMapDic;
}
///
/// 保存缓存图片
///
///
///
public static void SaveImageCaches(PlatformType platform, IEnumerable images)
{
try
{
var pending = images.Where(image => !image.IsCache).ToList();
var failed = StoreApiClient.Instance.ImageCache.SaveImageCaches((int)platform, pending);
foreach (var item in pending)
{
item.IsCache = !failed.Contains(item.Md5, StringComparer.OrdinalIgnoreCase);
}
}
catch
{
}
}
}
public class ImageCache
{
public string Md5 { get; set; }
public int Platform { get; set; }
[JsonProperty("platform_url")]
public string PlatformUrl { get; set; }
public bool IsCache { get; set; }
}