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

namespace Com.FirmLib.Bll
{
    /// <summary>
    /// 供应商相关操作
    /// </summary>
   public  class SellerBll : BaseHttpBll
    {
        private SellerBll() : base(@"/supplier")
        {
        }
        /// <summary>
        /// 增加供应商
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Add(SellerItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("name", item.Name);
            jObject.Add("website", item.Website);
            var result = PostJsonAdd(jObject);
            var flag=HandlerResult(result, (jo) => { item.Id = jo["id"].ToString(); },(jo) =>{ this.ErrorMessage = jo["resultMsg"].ToString(); });
            return flag;
        }

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

        }

        /// <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) => { this.ErrorMessage = jo["resultMsg"].ToString(); });
        }
        /// <summary>
        /// 获取项目信息
        /// </summary>
        /// <param name="sellerId"></param>
        /// <returns></returns>
        public List<ProjectItem> GetProjectItems(string sellerId)
        {
            string uri = "/project/query";
            List<ProjectItem> items = new List<ProjectItem>();
            JObject jObject = new JObject();
            jObject.Add("venderId", sellerId);
            var result = PostJsonAsync(new Uri(GetEndUri() + uri, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<ProjectItem>>(listStr);
            });
            return items;
        }
        /// <summary>
        /// 获取项目联系人
        /// </summary>
        /// <param name="venderId"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public LinkerItem GetLinker(string venderId, string projectId)
        {
            string uri = "/link/query";
            LinkerItem item = new LinkerItem();
            JObject jObject = new JObject();
            jObject.Add("venderId", venderId);
            jObject.Add("projectId", projectId);
            var result = PostJsonAsync(new Uri(GetEndUri() + uri, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                item = JsonConvert.DeserializeObject<LinkerItem>(listStr);
                item.ProjectId = projectId;
                item.VenderId = venderId;
            });
            return item;         
        }

        /// <summary>
        /// 修改联系人
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool UpdateLinker(LinkerItem item)
        {
            string uri = "/link/create";
            JObject jObject = new JObject();
            #region jobject初始化
            jObject.Add("venderId", item.VenderId);
            jObject.Add("projectId", item.ProjectId);
            jObject.AddCheckNull("phone", item.Phone);
            jObject.AddCheckNull("name", item.Name);
            jObject.AddCheckNull("fax", item.Fax);
            jObject.AddCheckNull("email", item.Email);
            #endregion
            var result = PostJsonAsync(new Uri(GetEndUri() + uri, UriKind.RelativeOrAbsolute), jObject);
            return HandlerResult(result, null, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); });

        }

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

        public List<string> GetProjectContracts(string venderId, string projectId)
        {
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + this.m_ExtensionUri + @"/contract/query";
            List<string> items = new List<string>();
            JObject jObject = new JObject();
            jObject.Add("venderId", venderId);
            jObject.Add("projectId", projectId);
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["contractId"].ToString();
                items = JsonConvert.DeserializeObject<List<string>>(listStr);
            });
            return items;
        }
    }
}
