using Com.FirmLib.Common;
using Com.FirmLib.Entity;
using FirmHttpDao;
using FWindSoft.DataFramework;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.FirmLib.CacheData;

namespace Com.FirmLib.Bll
{
    /// <summary>
    /// 岗位厂商信息查询
    /// </summary>
    public class StationVenderBll : BaseHttpBll
    {
        private StationVenderBll() : base(@"")
        {

        }

        public List<ProductTypeDetailItem> GetProductItems(string familyCode)
        {
            //接口 http://<host>:<port>/venders/manufacturer/specification/page_query
            string tempUrlExtension = BllHttpSetting.Instance.EndUri + @"/manufacturer/specification/page_query";
            List<ProductTypeDetailItem> results = new List<ProductTypeDetailItem>();
            JObject jObject = new JObject();
            jObject.Add("pageIndex", 1);
            jObject.Add("pageSize", 100000000);
            jObject.Add("familyCode", familyCode);//与给定api不符合
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                JArray array = JArray.Parse(listStr);
                foreach (var item in array)
                {
                    JObject tempJo = (JObject)item;
                    if (tempJo != null)
                    {
                        ProductTypeDetailItem detailItem = new ProductTypeDetailItem();
                        detailItem.VenderId = tempJo["vender"]["venderId"].ToString();
                        detailItem.VenderName = tempJo["vender"]["name"].ToString();
                        detailItem.BrandId = tempJo["brand"]["brandId"].ToString();
                        detailItem.BrandName = tempJo["brand"]["name"].ToString();
                        detailItem.ProductId = tempJo["product"]["productId"].ToString();
                        detailItem.ProductName = tempJo["product"]["name"].ToString();
                        detailItem.ProductTypeId = tempJo["specification"]["specificationId"].ToString();
                        detailItem.ProductTypeName = tempJo["specification"]["name"].ToString();
                        detailItem.FamilyCode = familyCode;
                        // jObject.ConvertToParameterItems()
                        var prameters = JObject.Parse(tempJo["specification"]["infos"].ToString());
                        detailItem.Parameters = prameters.ConvertToParameterItems();
                        results.Add(detailItem);
                    }
                }
               // results = JsonConvert.DeserializeObject<List<ProductTypeDetailItem>>(listStr);
            });

            #region 备用方案
            //var venders = BllFactory<ManufacturerBll>.Instance.GetAllItems();
            //foreach (var vender in venders)
            //{
            //    var brands = BllFactory<BrandBll>.Instance.GetAllItems(vender.Id) ?? new List<BrandItem>();
            //    foreach (var brand in brands)
            //    {
            //        var products = BllFactory<ProductBll>.Instance.GetAllItems(brand.Id);
            //        foreach (var product in products)
            //        {
            //            if (product.FamilyCode != familyCode)
            //                continue;
            //            var productTypes = BllFactory<ProductTypeBll>.Instance.GetAllItems(product.Id);
            //            foreach (var productType in productTypes)
            //            {
            //                ProductTypeDetailItem detailItem = new ProductTypeDetailItem();
            //                detailItem.VenderId = vender.Id;
            //                detailItem.VenderName = vender.Name;
            //                detailItem.BrandId = brand.Id;
            //                detailItem.BrandName = brand.Name;

            //                detailItem.ProdcutId = product.Id;
            //                detailItem.ProductName = product.Name;

            //                detailItem.ProductTypeId = productType.Id;
            //                detailItem.ProductTypeName = productType.Name;

            //                detailItem.Parameters = new List<ParameterItem>(productType.Parameters);
            //                results.Add(detailItem);
            //            }
            //        }
            //    }
            //} 
            #endregion
            return results;
        }
        /// <summary>
        /// 获取维修商联系人信息
        /// </summary>
        /// <returns></returns>
        public List<VenderLinkItem> GetMaintainerLinkerItems(string projectId)
        {
            List<VenderLinkItem> results = GetVenderLinkerItems(projectId, "maintainance");
            var venders = BllFactory<MaintainerBll>.Instance.GetAllItems();
            foreach (var vender in venders)
            {
                if (results.Any(v => v.VenderId == vender.Id))
                {
                    continue;
                }
                VenderLinkItem venderLinkItem = new VenderLinkItem() { VenderId = vender.Id, VenderName = vender.Name };
                results.Add(venderLinkItem);
            }

            return results;
        }
        /// <summary>
        /// 获取供应商及联系人地址
        /// </summary>
        /// <returns></returns>
        public List<VenderLinkItem> GetSellerLinkerItems(string projectId)
        {
            List<VenderLinkItem> results =GetVenderLinkerItems(projectId,"supplier");
            var venders = BllFactory<SellerBll>.Instance.GetAllItems();
            foreach (var vender in venders)
            {
                if (results.Any(v => v.VenderId == vender.Id))
                {
                    continue;
                }
                VenderLinkItem venderLinkItem = new VenderLinkItem() { VenderId = vender.Id, VenderName = vender.Name };
                results.Add(venderLinkItem);             
            }
            return results; 
        }
        /// <summary>
        /// 获取保险商及联系人地址
        /// </summary>
        /// <returns></returns>
        public List<VenderLinkItem> GetInsuerLinkerItems(string projectId)
        {
            List<VenderLinkItem> results = GetVenderLinkerItems(projectId, "insurance");
            var venders = BllFactory<InsuerBll>.Instance.GetAllItems();
            foreach (var vender in venders)
            {
                if (results.Any(v => v.VenderId == vender.Id))
                {
                    continue;
                }
                VenderLinkItem venderLinkItem = new VenderLinkItem() { VenderId = vender.Id, VenderName = vender.Name };
                results.Add(venderLinkItem);
            }
            return results;
        }

        public List<VenderLinkItem> GetVenderLinkerItems(string projectId, string venderType)
        {
            List<VenderLinkItem> items = new List<VenderLinkItem>();
            string tempUrlExtension = BllHttpSetting.Instance.EndUri + @"/auxiliary/link/query";
            JObject jObject = new JObject();
            jObject.Add("projectId", projectId);
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<VenderLinkItem>>(listStr);
            });
            return items.Where(i=>i.VenderType==venderType).ToList();
        }

        /// <summary>
        /// 获取供应合同编号
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="venderId"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        public List<SupplyContractItem> GetSupplyContractItems(string projectId, string venderId,string secret)
        {
            //作废输入值
            secret = PlatformProjectManager.GetProjectPassword(projectId);
            List<SupplyContractItem> items = new List<SupplyContractItem>();
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + $@"/getSupplierContractID?projectId={projectId}&secret={secret}";
            JObject jObject = new JObject();
            jObject.Add("DPSupplierID", venderId);
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<SupplyContractItem>>(listStr);
            });
            return items;
        }
        /// <summary>
        /// 获取保单相关信息
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="venderId"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        public List<InsuerPolicyItem> GetInsuerContractItems(string projectId, string venderId, string secret)
        {
            //作废输入值
            secret = PlatformProjectManager.GetProjectPassword(projectId);
            List<InsuerPolicyItem> items = new List<InsuerPolicyItem>();
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + $@"/getInsuranceNum?projectId={projectId}&secret={secret}";
            JObject jObject = new JObject();
            jObject.Add("DPInsurerID", venderId);
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<InsuerPolicyItem>>(listStr);
            });
            #region 补充厂商库维护的表单
            try
            {
                var dpItems = BllFactory<InsuerBll>.Instance.GetAllPolicyItems(venderId, projectId);
                foreach (var insuerContractItem in dpItems)
                {
                    var tempItem = items.FirstOrDefault(i => i.ContranctId == insuerContractItem.InsuranceNo);
                    if (tempItem == null)
                    {
                        items.Add(new InsuerPolicyItem() { ContranctId = insuerContractItem.InsuranceNo, ContranctFileKey = insuerContractItem.InsuranceFile });
                    }
                }
            }
            catch (Exception)
            {
            } 
            #endregion
            return items;
        }
    }
}
