using APT.BaseData.Domain.Enums;
using APT.Infrastructure.Core;
using APT.BaseData.Domain.ApiModel;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices.FM;
using System;
using System.Linq;
using APT.Infrastructure.Api;
namespace APT.BaseData.Services.Services.FM
{
    public class ExtConnConfigService : CommonService, IExtConnConfigService
	{
		public ExtConnConfigService(IRepository repository)
			: base(repository)
		{

		}
		/// <summary>
		/// 获取外部连接配置参数
		/// </summary>
		/// <param name="orgId"></param>
		/// <param name="destType"></param>
		/// <param name="connType"></param>
		/// <returns></returns>
		public ExtConnParamModel GetConfigParam(Guid orgId, FMExtConnDestTypeEnum destType, FMExtConnTypeEnum connType)
		{
            int intDestType = (int)destType, intConnType = (int)connType;
            ExtConnParamModel param = null;
			var conConfig= this.GetEntity<T_FM_EXT_CONN_CONFIG>(t => t.CONN_DEST_TYPE == intDestType && t.CONN_TYPE == intConnType
                    && t.ENABLE_STATUS==(int)FMEnableStatusEnum.启用,new BaseFilter(orgId), new string[] { "Nav_Details" });
			if (conConfig != null)
			{
				param = new ExtConnParamModel();
                param.OrgId = conConfig.ORG_ID;  
                param.BaseConfig= this.GetEntity<T_FM_BASE_CONFIG>(t => t.ORG_ID == conConfig.ORG_ID);
                if (conConfig.Nav_Details != null && conConfig.Nav_Details.Any())
                {
                    conConfig.Nav_Details.ForEach(t =>
                    {
                        if (!string.IsNullOrEmpty(t.KEY))
                            param.AddParam(t.KEY, t.VALUE);
                    });
                }
			}
			return param;
		}

        /// <summary>
		/// 获取外部连接配置参数
		/// </summary>
		/// <param name="orgId"></param>
		/// <param name="code"></param>
		/// <returns></returns>
		public ExtConnParamModel GetConfigParam(Guid orgId,string code)
        {
            ExtConnParamModel param = new ExtConnParamModel();
            var conConfig = this.GetEntity<T_FM_EXT_CONN_CONFIG>(t => t.CODE==code,
                new BaseFilter(orgId), new string[] { "Nav_Details" });
            if(conConfig!=null)
                param.OrgId = conConfig.ORG_ID;
            if (conConfig != null)
            {
                param.BaseConfig = this.GetEntity<T_FM_BASE_CONFIG>(t => t.ORG_ID == conConfig.ORG_ID);
                if (conConfig.Nav_Details != null && conConfig.Nav_Details.Any())
                {
                    conConfig.Nav_Details.ForEach(t =>
                    {
                        if (!string.IsNullOrEmpty(t.KEY))
                            param.AddParam(t.KEY, t.VALUE);
                    });
                }
            }
            return param;
        }
    }
}
