using System;
using System.Collections.Generic;
using System.Linq;
using Com.FirmLib.Common;
using Com.FirmLib.Entity;
using FirmHttpDao;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Com.FirmLib.Bll
{
    public class ManufacturerBll: BaseHttpBll
    {
        private ManufacturerBll():base(@"/manufacturer")
        {
            
        }
        /// <summary>
        /// 增加生产厂商
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Add(ManufacturerItem item)
        {
            JObject jObject=new JObject();
            jObject.Add("name",item.Name);
            //jObject.Add("url", "www.saaa.cn");//假的
            var result = PostJsonAdd(jObject);       
            return HandlerResult(result, (jo) => { item.Id = jo["id"].ToString(); }, jo => CatchErrorMessage(jo));               
        }

        /// <summary>
        /// 获取所有生产厂家
        /// </summary>
        /// <returns></returns>
        public List<ManufacturerItem> GetAllItems()
        {
            List<ManufacturerItem> items=new List<ManufacturerItem>();          
            JObject jObject = new JObject();
            jObject.Add("orderDesc", true);
            var result = PostJsonQuery(jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<ManufacturerItem>>(listStr);
            });
            return items;
        }
        /// <summary>
        /// 根据Id获取厂商信息
        /// </summary>
        /// <param name="venderId"></param>
        /// <returns></returns>
        public ManufacturerItem GetItem(string venderId)
        {
            return GetAllItems().FirstOrDefault(c => c.Id == venderId);
        }
        /// <summary>
        /// 修改生产厂家
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Update(ManufacturerItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("venderId", item.Id);
            jObject.Add("name", item.Name);
            var result = PostJsonUpdate(jObject);
            return HandlerResult(result, null, jo => CatchErrorMessage(jo));          
        }

        /// <summary>
        /// 删除生产厂家
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(string id)
        {
            JObject jObject = new JObject();
            jObject.Add("venderId", id);
            var result = PostJsonDelete(jObject);
            return HandlerResult(result, null, jo => CatchErrorMessage(jo));       
        }


        /// <summary>
        /// 获取项目信息
        /// </summary>
        /// <param name="manufacturerId"></param>
        /// <returns></returns>
        public List<ProjectItem> GetProjectItems(string manufacturerId)
        {
            string uri = "/project/query";
            List<ProjectItem> items = new List<ProjectItem>();
            JObject jObject = new JObject();
            jObject.Add("venderId", manufacturerId);
            var result = PostJsonAsync(new Uri(GetEndUri() + uri, UriKind.RelativeOrAbsolute), jObject);
             HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items= JsonConvert.DeserializeObject<List<ProjectItem>>(listStr);
            }, jo => CatchErrorMessage(jo));

            return items;
        }

        /// <summary>
        /// 获取项目资产信息
        /// </summary>
        /// <param name="manufacturerId"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public List<ManufacturerAssetItem> GetProjectAssetItems(string manufacturerId,string projectId)
        {
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + this.m_ExtensionUri+ @"/property/query";
            List<ManufacturerAssetItem> items = new List<ManufacturerAssetItem>();
            JObject jObject = new JObject();
            jObject.Add("venderId", manufacturerId);
            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<ManufacturerAssetItem>>(listStr);
            });
            return items;
        }

        /// <summary>
        /// 获取关联项目信息
        /// </summary>
        /// <param name="manufacturerId"></param>
        /// <returns></returns>
        public Tuple<List<RefVenderItem>,List<RefVenderItem>> GetRefVenderItems(string manufacturerId)
        {
            List<RefVenderItem> supplierIems = new List<RefVenderItem>();
            List<RefVenderItem> maintainerIems = new List<RefVenderItem>();
            string tempUrlExtension = BllHttpSetting.Instance.EndUri + @"/specification/query";
            JObject jObject = new JObject();
            jObject.Add("venderId", manufacturerId);
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var tempJobject =(JObject) jo["content"];
                if (tempJobject.TryGetValue("supplier", out JToken supply))
                {
                     supplierIems = JsonConvert.DeserializeObject<List<RefVenderItem>>(supply.ToString());
                }
                if (tempJobject.TryGetValue("maintainer", out JToken maintainer))
                {
                    maintainerIems = JsonConvert.DeserializeObject<List<RefVenderItem>>(maintainer.ToString());
                }

            });
            return new Tuple<List<RefVenderItem>, List<RefVenderItem>>(supplierIems,maintainerIems);
        }
    }
}
