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 InsuerBll:BaseHttpBll
    {
        private InsuerBll() : base(@"/insurance")
        {
        }
        /// <summary>
        /// 增加保险商
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Add(InsuerItem 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<InsuerItem> GetAllItems()
        {
            List<InsuerItem> items = new List<InsuerItem>();
            JObject jObject = new JObject();
            jObject.Add("orderDesc", true);
            var result = PostJsonQuery(jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<InsuerItem>>(listStr);
            });
            return items;
        }
        /// <summary>
        /// 根据Id获取厂商信息
        /// </summary>
        /// <param name="venderId"></param>
        /// <returns></returns>
        public InsuerItem GetItem(string venderId)
        {
            return GetAllItems().FirstOrDefault(c => c.Id == venderId);
        }
        /// <summary>
        /// 修改保险商
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool Update(InsuerItem 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="insuerId"></param>
        /// <returns></returns>
        public List<InsuerProjectItem> GetProjectItems(string insuerId)
        {
            string uri = "/project/query";
            List<InsuerProjectItem> items = new List<InsuerProjectItem>();
            JObject jObject = new JObject();
            jObject.Add("venderId", insuerId);
            var result = PostJsonAsync(new Uri(GetEndUri() + uri, UriKind.RelativeOrAbsolute), jObject);

            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<InsuerProjectItem>>(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.AddCheckNull("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<InsuerAssetItem> GetAllAssetItems(string venderId, string projectId)
        {
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + this.m_ExtensionUri + @"/project/query";
            List<InsuerAssetItem> items = new List<InsuerAssetItem>();
            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<InsuerAssetItem>>(listStr);
            });
            return items;
        }
        /// <summary>
        /// 获取项目中所有保单
        /// </summary>
        /// <param name="venderId"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public List<InsuerContractStatisticsItem> GetAllPolicyItems(string venderId, string projectId)
        {
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + this.m_ExtensionUri + @"/contract/query";
            List<InsuerContractStatisticsItem> items = new List<InsuerContractStatisticsItem>();
            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<InsuerContractStatisticsItem>>(listStr);
            });
            return items;
        }
        /// <summary>
        /// 获取指定保单总的资产
        /// </summary>
        /// <param name="venderId"></param>
        /// <param name="projectId"></param>
        /// <param name="polcyId"></param>
        /// <returns></returns>
        public List<InsuerAssetItem> GetPolicyAssetItems(string venderId, string projectId,string polcyId)
        {
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + this.m_ExtensionUri + @"/contract/property/query";
            List<InsuerAssetItem> items = new List<InsuerAssetItem>();
            JObject jObject = new JObject();
            jObject.Add("venderId", venderId);
            jObject.Add("projectId", projectId);
            jObject.Add("insuranceNo", polcyId);
            var result = PostJsonAsync(new Uri(tempUrlExtension, UriKind.RelativeOrAbsolute), jObject);
            HandlerResult(result, (jo) =>
            {
                var listStr = jo["content"].ToString();
                items = JsonConvert.DeserializeObject<List<InsuerAssetItem>>(listStr);
            });
            return items;
        }

        #region 保单相关信息维护，增，改
        /// <summary>
        /// 添加保单信息
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        public bool AddPolicy(InsuerContractItem item)
        {
            string uri = "/contract/create";
            JObject jObject = new JObject();
            #region jobject初始化
            jObject.Add("venderId", item.VenderId);
            jObject.Add("projectId", item.ProjectId);
            jObject.AddCheckNull("insuranceNo", item.InsuranceNo);
            jObject.AddCheckNull("expireDate", item.ExpireDate);
            jObject.AddCheckNull("contractFile", item.InsuranceFile);
            #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="item"></param>
        /// <returns></returns>
        public bool UpdatePolicy(InsuerContractItem item)
        {
            string uri = "/contract/update";
            JObject jObject = new JObject();
            #region jobject初始化
            jObject.Add("venderId", item.VenderId);
            jObject.Add("projectId", item.ProjectId);
            //jObject.AddCheckNull("insuranceNo", item.InsuranceNo);
            jObject.AddCheckNull("expireDate", item.ExpireDate);
            jObject.AddCheckNull("contractFile", item.InsuranceFile);
            jObject.AddCheckNull("oldInsuranceNo", item.InsuranceNo);
            //jObject.AddCheckNull("newInsuranceNo", item.NewInsuranceNo);
            #endregion
            var result = PostJsonAsync(new Uri(GetEndUri() + uri, UriKind.RelativeOrAbsolute), jObject);
            return HandlerResult(result, null, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); });

        }
        #endregion

        #region 保单信息
        /// <summary>
        /// 检查保单是否存在
        /// </summary>
        /// <param name="insuranceNo"></param>
        /// <returns></returns>
        public string CheckInsuranceNo(string insuranceNo)
        {
            string uri = "/contract/check";
            JObject jObject = new JObject();
            jObject.Add("insuranceNo", insuranceNo);
            List<string> list = new List<string>();
            var result = PostJsonAsync(new Uri(GetEndUri() + uri, UriKind.RelativeOrAbsolute), jObject);
            string refVenderId = string.Empty;
            var isExist= !HandlerResult(result, null, (jo) => { this.ErrorMessage = jo["resultMsg"].ToString(); refVenderId = jo["venderId"].ToString(); });
            if (isExist)
            {
                // isExist = isExist ;
                //if (venderId != refVenderId)
                //{
                //    throw new Exception($"该保单已被其他项目关联");
                //}
            }
            return refVenderId;
        }

        /// <summary>
        /// 获取指定单号的保单信息
        /// </summary>
        /// <param name="venderId"></param>
        /// <param name="projectId"></param>
        /// <param name="policyNo"></param>
        /// <returns></returns>
        public InsuerContractItem GetPolicyItem(string venderId, string projectId,string policyNo)
        {
            string tempUrlExtension = BllHttpSetting.Instance.DpUri + this.m_ExtensionUri + @"/contract/query";
            List<InsuerContractItem> items = new List<InsuerContractItem>();
            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<InsuerContractItem>>(listStr);
            });
            var policyItem = items.FirstOrDefault(c => c.InsuranceNo == policyNo);
            return policyItem;
        }
        #endregion
    }
}
