using ASHermed.SinoInterface.Api.ContractModel;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.ContractModel.Framework.Login;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.IService;
using ASHermed.SinoInterface.Code;
using ASHermed.SinoInterface.ContractModel;
using ASHermed.SinoInterface.Exception;
using ASHermed.SinoInterface.IFramework;
using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Tools;
using JWT;
using JWT.Algorithms;
using JWT.Serializers;
using Newtonsoft.Json.Linq;
using PoJun.Util;
using PoJun.Util.Maps;
using Renci.SshNet.Security;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace ASHermed.SinoInterface.Api.Service
{
    /// <summary>
	/// 三方服务
	/// </summary>
	public class TCPatInfoService : BaseService, ITCPatInfoService
    {
        IPubPatInfoRepository _pubPatInfoRepository;
        ITCService _tCService;
        /// <summary>
        /// 三方服务
        /// </summary>
        public TCPatInfoService(IPubPatInfoRepository pubPatInfoRepository, ITCService tCService)
        {
            _pubPatInfoRepository = pubPatInfoRepository;
            _tCService = tCService;

        }

        /// <summary>
        /// 保存患者的mrn(不是P开的编码)
        /// </summary>
        /// <param name="patId"></param>
        /// <returns></returns>
        public bool SavePatMrn(int patId)
        {
            try
            {
                var pat = _pubPatInfoRepository.SelectById(patId);
                if (pat != null)
                {
                    if (string.IsNullOrEmpty(pat.Pat_MRN))
                    {
                        var tcPats = _tCService.GetPatient("", pat.Phone_NO, pat.Birthday);
                        if (tcPats != null && tcPats.Count > 0)
                        {
                            var tcPat = tcPats.Find(p => p.PAPERName == pat.Last_Name && p.PAPERName2 == pat.First_Name);
                            if (tcPat != null)
                            {
                                var res = _pubPatInfoRepository.SavePatTcInfo(pat.Id, tcPat.PAPERRowId, tcPat.PAPMINo, tcPat.MRN);
                                if (res > 0)
                                {
                                    return true;
                                }
                            }
                        }
                    }
                    else
                    {
                        var tcPats = _tCService.GetPatient(pat.Pat_MRN, "", "");
                        if (tcPats != null && tcPats.Count > 0 && !string.IsNullOrEmpty(tcPats[0].MRN))
                        {
                            var res = _pubPatInfoRepository.SavePatMrn(pat.Id, tcPats[0].MRN);
                            if (res > 0)
                            {
                                return true;
                            }
                        }
                    }
                }
                return false;
            }
            catch (System.Exception e)
            {
                return false;
            }
        }

    }
}
