using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Com.FirmLib;
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 MaintainerBll : BaseHttpBll
    {
        private MaintainerBll() : base(@"/maintainance")
        {
        }
        /// <summary>
        /// 增加维修商
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Add(MaintainerItem item)
        {
            JObject jObject = new JObject();
            jObject.Add("name", item.Name);
            jObject.Add("website", item.Website);
            var result = PostJsonAdd(jObject);
            return HandlerResult(result, (jo) => { item.Id = jo["id"].ToString(); }, jo => CatchErrorMessage(jo));   
        }

        /// <summary>
        /// 获取所有维修商
        /// </summary>
        /// <returns></returns>
        public List<MaintainerItem> GetAllItems()
        {
            List<MaintainerItem> items = new List<MaintainerItem>();
            JObject jObject = new JObject();
            jObject.Add("orderDesc", true);
            var result = PostJsonQuery(jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<MaintainerItem>>(listStr);
            });
            return items;
        }
        /// <summary>
        /// 根据Id获取厂商信息
        /// </summary>
        /// <param name="venderId"></param>
        /// <returns></returns>
        public MaintainerItem GetItem(string venderId)
        {
            return GetAllItems().FirstOrDefault(c => c.Id == venderId);
        }
        /// <summary>
        /// 修改维修商
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Update(MaintainerItem 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="venderId"></param>
        /// <returns></returns>
        public List<ProjectItem> GetProjectItems(string venderId)
        {
            string uri = "/project/query";
            List<ProjectItem> items = new List<ProjectItem>();
            JObject jObject = new JObject();
            jObject.Add("venderId", venderId);
            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>
        /// <param name="expire">是否过期</param>
        /// <returns></returns>
        public List<MaintainerAssetItem> GetProjectAssetItems(string venderId, string projectId)
        {
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + this.m_ExtensionUri + @"/property/query";
            List<MaintainerAssetItem> items = new List<MaintainerAssetItem>();
            JObject jObject = new JObject();
            jObject.Add("venderId", venderId);
            jObject.Add("projectId", projectId);
           // jObject.Add("expire", expire.ToString());
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<MaintainerAssetItem>>(listStr);
            });
            items.ForEach(i => i.Asset.DpMaintainerId = venderId);
            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="manufacturerId"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public List<MaintainerAssetItem> GetCurrentProjectAssetItems(string manufacturerId, string projectId)
        {
            return GetProjectAssetItems(manufacturerId, projectId);
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="manufacturerId"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public List<MaintainerAssetItem> GetHistoryProjectAssetItems(string manufacturerId, string projectId)
        {
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + this.m_ExtensionUri + @"/property/history/query";
            List<MaintainerAssetItem> items = new List<MaintainerAssetItem>();
            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<MaintainerAssetItem>>(listStr);
            });
            return items;
        }
    }
}
