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.ContractModel;
using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Tools;
using PoJun.Util.Maps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.Api.Service
{
    /// <summary>
	/// 门诊
	/// </summary>
	public class HosService : BaseService, IHosService
    {
        IDicHosRepository _dicHosRepository;
        IDicDeptRepository _dicDeptRepository;
        /// <summary>
        /// 门诊
        /// </summary>
        public HosService(IDicHosRepository dicHosRepository, IDicDeptRepository dicDeptRepository)
        {
            _dicHosRepository = dicHosRepository;
            _dicDeptRepository = dicDeptRepository;
        }

        public List<HosByLanguage> SelectListByLanguage(int language, double lon, double lat)
        {
            List<HosByLanguage> list = _dicHosRepository.SelectListByLanguage(language, 0);
            foreach (var item in list)
            {
                item.DistanceDouble = Common.GetDistance(lat, lon, item.Latitude, item.Longitude);
                item.Distance = item.DistanceDouble + "";
            }
            return list.OrderBy(o => o.SORT).ToList();
        }

        public List<Dic_Map_Dept_Doctor> GetForDept(int deptId)
        {
            return _dicHosRepository.GetForDept(deptId);
        }

        public List<HosInfoByLanguage> FindAll(int language)
        {
            List<HosInfoByLanguage> list = _dicHosRepository.SelectHosWithLanguage(language);
            return list;
        }
        public HosInfoByLanguage Find(int language, int hosId)
        {
            HosInfoByLanguage info = _dicHosRepository.SelectHosByIdWithLanguage(hosId, language);
            return info;
        }

        public HosByDeptLanguage FindByDeptLanguage(int deptId, int language, double lon, double lat)
        {
            List<HosByLanguage> hosByLanguageList = _dicHosRepository.SelectByDeptLanguage(deptId, language, 0);//pp端
            foreach (var hos in hosByLanguageList)
            {
                hos.Distance = Common.GetDistance(lat, lon, hos.Latitude, hos.Longitude) + "";
            }
            DeptByLanguage deptByLanguage = _dicDeptRepository.SelectByIdLanguage(language, deptId);
            HosByDeptLanguage hosByDeptLanguage = new HosByDeptLanguage();
            hosByDeptLanguage.HosByLanguages = hosByLanguageList;
            hosByDeptLanguage.DeptByLanguage = deptByLanguage;
            return hosByDeptLanguage;
        }

        public HosByDeptLanguage FindByDeptLanguageGroupCity(int deptId, int language, double? lon, double? lat)
        {
            List<HosByLanguage> hosByLanguageList = _dicHosRepository.SelectByDeptLanguage(deptId, language, 0);
            List<HosByCity> hosByCityList = _dicHosRepository.SelectCity(language);
            foreach (var hosByCity in hosByCityList)
            {
                hosByCity.HosByLanguages = new List<HosByLanguage>();
                foreach (var hos in hosByLanguageList)
                {
                    if (lat != null && lon != null)
                    {
                        hos.Distance = Common.GetDistance(Convert.ToDouble(lat), Convert.ToDouble(lon), hos.Latitude, hos.Longitude) + "";
                    }

                    if (hos.City == hosByCity.City)
                    {
                        hosByCity.HosByLanguages.Add(hos);
                    }
                }
            }

            DeptByLanguage deptByLanguage = _dicDeptRepository.SelectByIdLanguage(language, deptId);
            HosByDeptLanguage hosByDeptLanguage = new HosByDeptLanguage();
            hosByDeptLanguage.HosByCity = hosByCityList;
            hosByDeptLanguage.DeptByLanguage = deptByLanguage;
            return hosByDeptLanguage;
        }

        public List<HosByCity> FindCityAll(int language)
        {
            List<HosByCity> hosByCityList = _dicHosRepository.SelectCity(language);
            List<HosInfoByLanguage> list = _dicHosRepository.SelectHosWithLanguage(language);
            foreach (var hosByCity in hosByCityList)
            {
                hosByCity.HosByLanguagesNew = new List<HosInfoByLanguage>();
                foreach (var hos in list)
                {
                    if (hos.City == hosByCity.City)
                    {
                        hosByCity.HosByLanguagesNew.Add(hos);
                    }
                }
            }
            return hosByCityList;
        }



    }
}
