using Com.FirmLib.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Com.FirmLib.UI
{
    /// <summary>
    /// 产品过滤条件
    /// </summary>
    public class ProdcutTypeCondition
    {
        public ProdcutTypeCondition()
        {
            
        }
        public ProdcutTypeCondition(string venderId)
        {
            this.VenderId = venderId;
        }
        public ProdcutTypeCondition(List<ProductTypeDetailItem> data)
        {
            this.CatchData = data;
        }
        /// <summary>
        /// 设备族
        /// </summary>
        public string FamilyCode { get; set; }
        /// <summary>
        ///生产厂商Id
        /// </summary>
        public string VenderId { get; set; }
        /// <summary>
        /// 品牌Id
        /// </summary>
        public string BrandId { get; set; }
        /// <summary>
        /// 产品Id
        /// </summary>
        public string  ProductId { get; set; }

        /// <summary>
        /// 缓存数据
        /// </summary>
        public List<ProductTypeDetailItem> CatchData { get; set; }

        /// <summary>
        /// 创建过滤条件
        /// </summary>
        /// <returns></returns>
        public Func<ProductTypeDetailItem,bool> CreateFilter()
        {
            List<Predicate<ProductTypeDetailItem>> predicates = new List<Predicate<ProductTypeDetailItem>>();
            if (!string.IsNullOrEmpty(VenderId))
            {
                predicates.Add(p => p.VenderId == VenderId);
            }
            if (!string.IsNullOrEmpty(FamilyCode))
            {
                predicates.Add(p => p.FamilyCode == FamilyCode);
            }
            if (!string.IsNullOrEmpty(BrandId))
            {
                predicates.Add(p => p.BrandId == BrandId);
            }
            if (!string.IsNullOrEmpty(ProductId))
            {
                predicates.Add(p => p.ProductId == ProductId);
            }
            return new Func<ProductTypeDetailItem, bool>(p => predicates.All(filter => filter(p)));
        }

        /// <summary>
        /// 根据过滤条件过滤
        /// </summary>
        /// <returns></returns>
        public List<ProductTypeDetailItem> ToList()
        {
            var dataSource = CatchData??new List<ProductTypeDetailItem>() ;
            var products = dataSource.Where(CreateFilter()).ToList();
            return products;
        }
    }
}
