using Muchinfo.MTPClient.Data.Model;
using Muchinfo.MTPClient.Infrastructure.Cache;
using Muchinfo.MTPClient.Infrastructure.Users;
using Muchinfo.MTPClient.IService;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Muchinfo.PC.Common.Extensions;
namespace Muchinfo.MTPClient.Service
{
public class FavoriteService : IFavoriteService
{
///
/// 获取自选商品列表
///
/// The favorite identifier.
/// ObservableCollection{QuoteGoods}.
public ObservableCollection GetMyFavoriteGoodses(uint favoriteId)
{
var list = FavoriteManager.GetMyFavorites();
if (list == null) return null;
var item = list.FirstOrDefault(z => z.Id == favoriteId);
if (item == null) return null;
var goodsList = item.FavoriteGoodses;
if (goodsList == null || !goodsList.Any()) return null;
var result = new ObservableCollection();
var cacheGoodsList = CacheManager.CacheGoodsBaseInfos;
if (cacheGoodsList == null || !cacheGoodsList.Any()) return null;
var sortList = goodsList.OrderBy(z => z.ShowIndex);
foreach (var localGoods in sortList)
{
if (localGoods == null) continue;
var goods = cacheGoodsList.FirstOrDefault(z => z.Symbol.ToUpper() == localGoods.Symbol.ToUpper());
if (goods == null) continue;
//goods.IsSelected = true;
result.Add(goods);
}
//默认按商品代码排序
return result.OrderBy(z=>z.GoodsCode).ToObservableCollection();
}
///
/// 添加商品到我的自选
///
/// The favorite identifier.
/// The goods.
public void AddToMyFavorite(uint favoriteId, QuoteGoods goods)
{
var list = FavoriteManager.GetMyFavorites();
if (list == null) return;
var item = list.FirstOrDefault(z => z.Id == favoriteId);
if (item == null) return;
var localGoodsList = item.FavoriteGoodses == null ? new List() : item.FavoriteGoodses.ToList();
var quoteItem = localGoodsList.FirstOrDefault(z => z.Symbol.ToUpper() == goods.Symbol.ToUpper());
if (quoteItem == null)
{
var showIndex = localGoodsList.Count > 0 ? localGoodsList.Max(z => z.ShowIndex) + 1 : 1;
localGoodsList.Add(new LocalGoods()
{
Symbol = goods.Symbol,
ShowIndex = showIndex
});
}
item.FavoriteGoodses = localGoodsList;
FavoriteManager.AddOrUpdateMyFavorites(item);
}
///
/// 从自选删除
///
/// The favorite identifier.
/// The goods.
public void RemoveFromMyFavorite(uint favoriteId, QuoteGoods goods)
{
var list = FavoriteManager.GetMyFavorites();
if (list == null) return;
var item = list.FirstOrDefault(z => z.Id == favoriteId);
if (item == null) return;
var localGoodsList = item.FavoriteGoodses == null ? new List() : item.FavoriteGoodses.ToList();
var quoteItem = localGoodsList.FirstOrDefault(z => z.Symbol.ToUpper() == goods.Symbol.ToUpper());
if (quoteItem == null) return;
localGoodsList.Remove(quoteItem);
item.FavoriteGoodses = localGoodsList;
FavoriteManager.AddOrUpdateMyFavorites(item);
}
///
/// 获取所属自选
///
/// The goods.
/// List{MyFavorite}.
public List GetParentFavorites(QuoteGoods goods)
{
var list = FavoriteManager.GetMyFavorites();
if (list == null || !list.Any()) return null;
var result = new List();
foreach (var myFavorite in list)
{
if (myFavorite == null || myFavorite.FavoriteGoodses == null || !myFavorite.FavoriteGoodses.Any()) continue;
var item = myFavorite.FavoriteGoodses.FirstOrDefault(z => z.Symbol.ToUpper() == goods.Symbol.ToUpper());
if (item == null) continue;
result.Add(myFavorite);
}
return result;
}
}
}