
using System;
using System.Collections.Generic;
using System.Linq;
using Com.FirmLib.Bll;
using Com.FirmLib.Entity;
using Com.FirmLib.UI.BllCommon;
using Com.FirmLib.UI.Common;
using Com.FirmLib.UI.Manufacturer.Model;
using FWindSoft.DataFramework;
using FWindSoft.Tools;
using FWindSoft.Wpf;


namespace Com.FirmLib.UI
{
    /// <summary>
    /// 型号相关查询
    /// </summary>
    public class ProductTypeManager
    {
        #region 接口数据转换
        public static List<ProductTypeDetailItem> ConverToList(List<BrandTreeItem> brandTreeItems)
        {
            List<ProductTypeDetailItem> result = new List<ProductTypeDetailItem>();
            foreach (var brand in brandTreeItems)
            {
                foreach (var product in brand.Products)
                {
                    foreach (var productType in product.ProductTypes)
                    {
                        #region 数据转换
                        ProductTypeDetailItem item = new ProductTypeDetailItem();
                        item.BrandId = brand.Id;
                        item.BrandName = brand.Name;
                        item.ProductId = product.Id;
                        item.ProductName = product.Name;
                        item.FamilyCode = product.FamilyCode;
                        item.ProductTypeId = productType.Id;
                        item.ProductTypeName = productType.Name;
                        result.Add(item); 
                        #endregion
                    }
                }
            }

            return result;
        }

        #endregion

        #region 查询接口
        /// <summary>
        /// 查询设备族信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<ShowDataItem> GetFamilys(ProdcutTypeCondition condition)
        {
            List<ShowDataItem> result = new List<ShowDataItem>();
            var products = condition.ToList();
            var comparer = new TComparer<ShowDataItem>((s1, s2) => s1.Id == s2.Id);

            var groups = products.GroupBy(c => new ShowDataItem() { Name = EquipmentFamilyManager.GetFamilyName(c.FamilyCode), Id = c.FamilyCode, FamilyCode = c.FamilyCode }, comparer);
            foreach (var group1 in groups)
            {
                result.Add(group1.Key);
            }
            //设备族 展现名字；
            return result;
        }
        /// <summary>
        /// 查询品牌信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<ShowDataItem> GetBrands(ProdcutTypeCondition condition)
        {
            List<ShowDataItem> result = new List<ShowDataItem>();
            var dataSource = condition.ToList();
            var comparer = new TComparer<ShowDataItem>((s1, s2) => s1.Id == s2.Id);
            var groups = dataSource.GroupBy(c => new ShowDataItem() { Name = c.BrandName, Id = c.BrandId }, comparer);
            foreach (var group1 in groups)
            {
                result.Add(group1.Key);
            }
            return result;
        }
        /// <summary>
        /// 查询产品信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<ShowDataItem> GetProducts(ProdcutTypeCondition condition)
        {
            List<ShowDataItem> result = new List<ShowDataItem>();
            var products = condition.ToList();
            var comparer = new TComparer<ShowDataItem>((s1, s2) => s1.Id == s2.Id);
            var groups = products.GroupBy(c => new ShowDataItem() { Name = c.ProductName, Id = c.ProductId, FamilyCode = c.FamilyCode }, comparer);
            foreach (var group1 in groups)
            {
                result.Add(group1.Key);
            }
            return result;
        }
        /// <summary>
        /// 查询类型信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<ShowDataItem> GetProductTypes(ProdcutTypeCondition condition)
        {
            List<ShowDataItem> result = new List<ShowDataItem>();
            var products = condition.ToList();
            var comparer = new TComparer<ShowDataItem>((s1, s2) => s1.Id == s2.Id);
            var groups = products.GroupBy(c => new ShowDataItem() { Name = c.ProductTypeName, Id = c.ProductTypeId, FamilyCode = c.FamilyCode }, comparer);
            foreach (var group1 in groups)
            {
                result.Add(group1.Key);
            }
            return result;
        }
        #endregion

        #region 产品类型信息点呈现相关
        /// <summary>
        /// 获取指定设备族相关信息
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static List<PropertyDefinition> GetInfoPointDefinitions(string code)
        {
            
            List<PropertyDefinition> result = new List<PropertyDefinition>();
            if (string.IsNullOrEmpty(code))
            {
                return result;
            }
            var definitions = EquipmentFamilyManager.GetInfoPointDefinitions(code);
            List<string> sort = new List<string>() { "MaintainPeriod", "ServiceLife" };
            foreach (EquipmentInfoPointItem definition in definitions)
            {
                if (definition.Category != "技术参数" && !sort.Contains(definition.CodeName))
                    continue;
                PropertyDefinition item = new PropertyDefinition() { IsVisible = true };
                item.Id = definition.CodeName;
                item.Name = definition.Name;
                item.IsVisible = definition.IsShow;
                item.ExtendObject = definition.Clone();
                result.Add(item);
            }

            for (int i = 0; i < sort.Count; i++)
            {
                string flag = sort[i];
                var currentItem = result.FirstOrDefault(c => c.Id == flag);
                if (currentItem != null)
                {
                    result.Remove(currentItem);
                    result.Insert(i, currentItem);
                }
            }
            return result;
        }
        /// <summary>
        /// 获取型号详细信息
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public static List<TypeShowItem> GetProdcutTypeInfoItems(ProdcutTypeCondition condition)
        {
            string productId = condition.ProductId;
            var familyCode = condition.FamilyCode;
            var definitions = GetInfoPointDefinitions(familyCode);
            return GetProdcutTypeInfoItems(productId, definitions);
        }
        /// <summary>
        /// 获取型号详细信息
        /// </summary>
        /// <param name="productid"></param>
        /// <param name="definitions"></param>
        /// <returns></returns>
        public static List<TypeShowItem> GetProdcutTypeInfoItems(string productid,List<PropertyDefinition> definitions)
        {
            string productId = productid;
            var typeItems = new List<TypeShowItem>();
            var dataItems = BllFactory<ProductTypeBll>.Instance.GetAllItems(productId);
            var inDefinitions = definitions??new List<PropertyDefinition>();
            foreach (var dataItem in dataItems)
            {
                #region 类型转换
                var tempItem = new TypeShowItem();
                tempItem.ProductTypeId = dataItem.Id;
                tempItem.TypeName = dataItem.Name;
                tempItem.Parameters = PropertyCollection.CreateCollection(inDefinitions);
                foreach (var item in dataItem.Parameters)
                {
                    var definitonPara = tempItem.Parameters[item.Code];
                    if (definitonPara != null)
                    {
                        definitonPara.Value = item.Value;
                    }
                }
                typeItems.Add(tempItem);
                #endregion
            }
            return typeItems;
        }
        #endregion


        #region 缓存产品信息相关
        /// <summary>
        /// 获取产品信息
        /// </summary>
        /// <returns></returns>
        public static  Dictionary<string, ProductTypeDetailItem> GetProductTypes()
        {
            return MemoryCacheUtil.GetCacheItem<Dictionary<string,ProductTypeDetailItem>>("ProdcutTypes",()=>
            {
                Dictionary<string, ProductTypeDetailItem> dic = new Dictionary<string, ProductTypeDetailItem>();
                var items = ProductTypeManager.ConverToList(BllFactory<GeneralBll>.Instance
                               .GetProductTypeItems());
                foreach (var productTypeDetailItem in items)
                {
                    dic[productTypeDetailItem.ProductTypeId] = productTypeDetailItem;
                }
                return dic;
            }, new TimeSpan(0,0,300)) ?? new Dictionary<string, ProductTypeDetailItem>();
        }
        /// <summary>
        /// 获取类型的产品名[暂时不用字典存储]
        /// </summary>
        /// <param name="productTypeId"></param>
        /// <returns></returns>
        public static string GetProductName(string productTypeId)
        {
            var items = GetProductTypes();
            items.TryGetValue(productTypeId, out ProductTypeDetailItem item);
            if (item != null)
                return item.ProductName;
            return string.Empty;
        }
        #endregion
    }
}