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.ContractModel;
using ASHermed.SinoInterface.IFramework.SingleSignOn;
using ASHermed.SinoInterface.Tools;
using Newtonsoft.Json;
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 DoctorService : BaseService, IDoctorService
    {

        IDicDoctorRepository _dicDoctorRepository;
        IDicMapHisDoctorRepository _dicMapHisDoctorRepository;
        IDicNationalityRepository _dicNationalityRepository;
        IDicHosRepository _dicHosRepository;
        IDicDeptRepository _dicDeptRepository;
        IDicMapDeptDoctorRepository _dicMapDeptDoctorRepository;
        RedisHelperClient _redisHelper;
        ITCService _tCService;

        #region 初始化
        /// <summary>
        /// 医生
        /// </summary>
        public DoctorService(IDicDoctorRepository dicDoctorRepository, IDicMapHisDoctorRepository dicMapHisDoctorRepository, IDicNationalityRepository dicNationalityRepository, IDicHosRepository dicHosRepository, IDicDeptRepository dicDeptRepository, IDicMapDeptDoctorRepository dicMapDeptDoctorRepository, RedisHelperClient redisHelper, ITCService tCService)
        {
            _dicDoctorRepository = dicDoctorRepository;
            _dicMapHisDoctorRepository = dicMapHisDoctorRepository;
            _dicNationalityRepository = dicNationalityRepository;
            _dicHosRepository = dicHosRepository;
            _dicDeptRepository = dicDeptRepository;
            _dicMapDeptDoctorRepository = dicMapDeptDoctorRepository;
            _redisHelper = redisHelper;
            _tCService = tCService;
        }
        #endregion
        public DoctorInfoByLanguage FindDoctorDetailByLanguage(int doctorId, int language, string userAgent)
        {
            Dic_Doctor doctor = _dicDoctorRepository.initLinq().Where(p => p.Id == doctorId).Single();

            if (doctor != null)
            {
                //Dic_Map_His_Doctor mapHisDoctor = dicMapHisDoctorRepository.initLinq().Where(p => p.APT_Doctor_Id== doctorId).Single();
                List<HosByLanguage> hos = _dicMapHisDoctorRepository.SelectByDoctorByLanguage(doctorId, language, 0);//pp端
                List<DeptByLanguage> dicDept = _dicMapHisDoctorRepository.SelectByDoctorByLanguage(doctorId, language);
                List<Dic_Nationality> nationalityList = new List<Dic_Nationality>();
                if (!string.IsNullOrEmpty(doctor.Nationality_Id))
                {
                    var nationList = _dicNationalityRepository.initLinq().Where("ID in (" + doctor.Nationality_Id + ")").Select().ToList();
                    nationalityList.AddRange(nationList);
                    //for (String nationId : doctor.getNationalityId().split(","))
                    //{
                    //    DicNationality nationality = dicNationalityMapper.selectByPrimaryKey(Integer.parseInt(nationId));
                    //    if (nationality != null)
                    //    {
                    //        nationalityList.add(nationality);
                    //    }
                    //}
                }
                var videoList = _dicDoctorRepository.GetVideoDoctorByDept(0, doctor.Id, language, "");
                var revisitlist = _dicDoctorRepository.GetReVisitDoctorByDept(0, doctor.Id, language, "");
                if (userAgent == "cigna")
                {
                    revisitlist = _dicDoctorRepository.GetInsurDoctorByDept(3, 0, doctor.Id, language, "");
                }
                DoctorInfoByLanguage doctorInfoByLanguage = new DoctorInfoByLanguage();
                doctorInfoByLanguage.Id = doctor.Id;
                doctorInfoByLanguage.CareProveId = doctor.Id;
                string doctorName = language == 1 ? doctor.CN_Name : doctor.EN_Name;
                doctorInfoByLanguage.DocName = doctorName;
                doctorInfoByLanguage.ListImage = doctor.Detail_Image;
                doctorInfoByLanguage.Language = language == 1 ? doctor.CN_Language : doctor.EN_Language;
                doctorInfoByLanguage.Dept = dicDept;
                doctorInfoByLanguage.Hos = hos;
                doctorInfoByLanguage.Nation = nationalityList;
                doctorInfoByLanguage.AptType = doctor.APT_Type;
                string desc = language == 1 ? doctor.CN_Desc : doctor.EN_Desc;
                string title = language == 1 ? doctor.CN_Title : doctor.EN_Title;
                string exper = language == 1 ? doctor.CN_Exper : doctor.EN_Exper;
                string education = language == 1 ? doctor.CN_Education : doctor.EN_Education;
                doctorInfoByLanguage.Desc = desc;
                doctorInfoByLanguage.Title = title;
                doctorInfoByLanguage.Exper = exper;
                doctorInfoByLanguage.Education = education;
                if (videoList != null && videoList.Count > 0)
                {
                    var video = videoList.FirstOrDefault();
                    doctorInfoByLanguage.IsVideoDoc = true;
                    doctorInfoByLanguage.VideoPrice = video.Price;
                    doctorInfoByLanguage.OnlineDeptName = video.DeptName;
                }
                if (revisitlist != null && revisitlist.Count > 0)
                {
                    var revisit = revisitlist.FirstOrDefault();
                    doctorInfoByLanguage.IsRevisitDoc = true;
                    doctorInfoByLanguage.RevisitPrice = revisit.Price;
                    doctorInfoByLanguage.OnlineDeptName = revisit.DeptName;
                }

                return doctorInfoByLanguage;

            }
            return null;
        }

        public async Task<List<AptDocListInfo>> GetDocList(int hosId, string filter, int language)
        {
            List<AptDocListInfo> resList = new List<AptDocListInfo>();
            List<AptDocList> docList = _dicDoctorRepository.GetAllConditionDoctor(hosId, language);
            if (docList?.Count > 0)
            {
                resList = docList.GroupBy(p => new { p.DeptId, p.DocId, p.DeptName, p.DocImg, p.DocName, p.Initial, p.NamePinYin, p.DocNameEn })
                    .Select(q => new AptDocListInfo()
                    {
                        DeptId = q.Key.DeptId,
                        DeptName = q.Key.DeptName,
                        DocId = q.Key.DocId,
                        DocImg = q.Key.DocImg,
                        DocName = q.Key.DocName,
                        DocNameEn = q.Key.DocNameEn,
                        Initial = q.Key.Initial,
                        NamePinYin = q.Key.NamePinYin,
                        HosName = q.Select(i => i.HosName).ToList()
                    }
                    ).ToList();
            }
            if (!string.IsNullOrEmpty(filter))
            {
                bool isCh = filter.IsChinese();
                if (!isCh)
                {
                    //转换为数组
                    string[] filterPinyinArr = filter.ToLower().Replace("  ", " ").Split(' ');
                    resList = GetContainList(resList, filterPinyinArr, filter);
                }
                else
                {
                    resList = resList.FindAll(p => p.DocName.Contains(filter) || p.DocNameEn.Contains(filter));
                }
            }
            return resList;
        }

        private List<AptDocListInfo> GetContainList(List<AptDocListInfo> list, string[] b,string filter)
        {
            List<AptDocListInfo> resList = new List<AptDocListInfo>();
            if (list == null || b == null)
            {
                return resList;
            }
            foreach (var item in list)
            {
                string[] a = item.NamePinYin?.ToLower().Split(',');
                if (ContainArray(a, b))
                {
                    resList.Add(item);
                }
                else
                {
                    a = item.DocNameEn?.ToLower().Replace("  ", " ").Split(' ');
                    if (Common.ContainArray(a, b))
                    {
                        resList.Add(item);
                    }
                    else
                    {
                        if (item.DocName != null && item.DocName.ToLower().Contains(filter.ToLower()))
                        {
                            resList.Add(item);
                        }
                    }
                }
            }
            return resList;
        }

        private bool ContainArray(string[] a, string[] b)
        {
            if (a == null || b == null)
            {
                return false;
            }
            Array.Sort(a);
            Array.Sort(b);

            int start = 0;
            int match = 0;
            for (int i = 0; i < b.Length; i++)
            {
                for (int j = start; j < a.Length; j++)
                {
                    if (b[i] == (a[j]))
                    {
                        start = j + 1;
                        match++;
                        break;
                    }
                }
            }
            return match == b.Length;
        }

        public List<DoctorInfoByLanguage> FindListWithHosDeptPairByLanguage(string filter, int language)
        {
            List<Dic_Doctor> list = _dicDoctorRepository.GetAllDoctor();
            List<DoctorInfoByLanguage> docList = new List<DoctorInfoByLanguage>();

            if (list != null && list.Count > 0)
            {
                bool isCh = !string.IsNullOrEmpty(filter) ? filter.IsChinese() : false;

                foreach (var doc in list)
                {
                    if (doc.APT_Type == 1)//不可预约
                    {
                        continue;
                    }
                    List<HosByLanguage> hos = _dicHosRepository.SelectByDoctorByLanguage(doc.Id, language, 0);//0:PP端
                    List<DeptByLanguage> dept = _dicDeptRepository.SelectByDoctorByLanguage(doc.Id, language);

                    if (!string.IsNullOrEmpty(filter))
                    {
                        if (!isCh)
                        {
                            //转换为数组
                            string[] filterPinyinArr = filter.ToLower().Replace("  ", " ").Split(' ');
                            //获取医生名称拼音
                            string[] doctorPinyinArr = doc.Name_Pinyin?.ToLower().Split(',');
                            //匹配拼音
                            if (Common.ContainArray(doctorPinyinArr, filterPinyinArr))
                            {
                                docList.Add(SetListDoctor(doc, hos, dept, language));
                            }
                            else
                            {
                                doctorPinyinArr = doc.EN_Name?.ToLower().Replace("  ", " ").Split(' ');
                                if (Common.ContainArray(doctorPinyinArr, filterPinyinArr))
                                {
                                    docList.Add(SetListDoctor(doc, hos, dept, language));
                                }
                                else
                                {
                                    doctorPinyinArr = doc.CN_Name?.Replace("  ", " ").Split(' ');
                                    if (Common.ContainArray(doctorPinyinArr, filterPinyinArr))
                                    {
                                        docList.Add(SetListDoctor(doc, hos, dept, language));
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (doc.CN_Name.ToLower().Contains(filter.ToLower()))
                            {
                                docList.Add(SetListDoctor(doc, hos, dept, language));
                            }
                            else if (doc.EN_Name.ToLower().Contains(filter.ToLower()))
                            {
                                docList.Add(SetListDoctor(doc, hos, dept, language));
                            }
                        }
                    }
                    else
                    {
                        docList.Add(SetListDoctor(doc, hos, dept, language));
                    }
                }
                return docList;
            }
            return null;
        }

        public DoctorInfoByLanguage SetListDoctor(Dic_Doctor doc, List<HosByLanguage> hos, List<DeptByLanguage> dept, int language)
        {
            DoctorInfoByLanguage doctorInfoByLanguage = new DoctorInfoByLanguage();

            string doctorName = language == 1 ? doc.CN_Name : doc.EN_Name;
            string initial = language == 1 ? doc.CN_Initial : doc.EN_Initial;
            string desc = language == 1 ? doc.CN_Desc : doc.EN_Desc;
            string title = language == 1 ? doc.CN_Title : doc.EN_Title;
            string exper = language == 1 ? doc.CN_Exper : doc.EN_Exper;
            string education = language == 1 ? doc.CN_Education : doc.EN_Education;
            doctorInfoByLanguage.Id = doc.Id;
            doctorInfoByLanguage.ListImage = doc.List_Image;
            doctorInfoByLanguage.DocName = doctorName;
            doctorInfoByLanguage.Initial = initial;
            doctorInfoByLanguage.AptType = doc.APT_Type;
            doctorInfoByLanguage.Dept = dept;
            doctorInfoByLanguage.Hos = hos;
            doctorInfoByLanguage.NamePinyin = doc.Name_Pinyin;
            doctorInfoByLanguage.Desc = desc;
            doctorInfoByLanguage.Title = title;
            doctorInfoByLanguage.Exper = exper;
            doctorInfoByLanguage.Education = education;

            return doctorInfoByLanguage;
        }

        public List<DoctorWithHosDeptByLanguage> FindDetailListByLanguage(int hosId, int deptId, int language, int aptType)
        {
            List<DoctorWithHosDeptByLanguage> list = _dicDoctorRepository.SelectDetailByLanguage(hosId, deptId, language, 0);
            if (aptType != 0)
            {
                //list = list.stream().filter( o-> (o.getAptType() & aptType) > 0 && o.getAptType() <= aptType).collect(Collectors.toList());
                list = list.FindAll(p => (p.AptType & aptType) > 0 && p.AptType <= aptType);
            }
            return list;
        }

        public List<AptDoctorByLanguage> DeptDoctor(int hosId, int deptId, int language)
        {
            List<AptDoctorByLanguage> list = _dicDoctorRepository.SelectDeptDoctor(hosId, deptId, language);
            return list;
        }

        public List<VideoDoctor> GetVideoDoctorByDept(int deptId, int language, string searchWords)
        {
            var videoList = _dicDoctorRepository.GetVideoDoctorByDept(deptId, 0, language, searchWords);
            var revisitlist = _dicDoctorRepository.GetReVisitDoctorByDept(deptId, 0, language, searchWords);
            foreach (var item in videoList)
            {
                item.IsVideoDoc = true;
                item.IsRevisitDoc = revisitlist.Find(p => p.DocId == item.DocId) != null ? true : false;
            }
            return videoList;
        }

        public List<DeptModel> GetVideoDept(int language)
        {
            List<DeptModel> list = new List<DeptModel>();
            if (language == 1)
            {
                list.Add(new DeptModel() { Id = 0, DeptName = "全部科室" });
            }
            else
            {
                list.Add(new DeptModel() { Id = 0, DeptName = "All" });
            }

            var videoList = _dicDoctorRepository.GetVideoDoctorByDept(0, 0, language, "");
            foreach (var item in videoList)
            {
                if (list.Find(p => p.Id == item.DeptId) == null)
                {
                    list.Add(new DeptModel() { Id = item.Id, DeptName = item.DeptName });
                }
            }
            return list;
        }
        public List<RevisitDoctor> GetRevisitDoctorByDept(int deptId, int language, string searchWords)
        {
            var revisitlist = _dicDoctorRepository.GetReVisitDoctorByDept(deptId, 0, language, searchWords);
            var videoList = _dicDoctorRepository.GetVideoDoctorByDept(deptId, 0, language, searchWords);
            foreach (var item in revisitlist)
            {
                item.IsRevisitDoc = true;
                item.IsVideoDoc = videoList.Find(p => p.DocId == item.DocId) != null ? true : false;
            }
            return revisitlist;
        }
        public List<DeptModel> GetRevisitDept(int language)
        {
            List<DeptModel> list = new List<DeptModel>();
            if (language == 1)
            {
                list.Add(new DeptModel() { Id = 0, DeptName = "全部科室" });
            }
            else
            {
                list.Add(new DeptModel() { Id = 0, DeptName = "All" });
            }
            var visitList = _dicDoctorRepository.GetReVisitDoctorByDept(0, 0, language, "");
            foreach (var item in visitList)
            {
                if (list.Find(p => p.Id == item.DeptId) == null)
                {
                    list.Add(new DeptModel() { Id = item.Id, DeptName = item.DeptName });
                }
            }
            return list;
        }

        public List<WorkDoctor> GetAllOnlineDoctors(int language, string key)
        {
            List<WorkDoctor> workDoctors = new List<WorkDoctor>();
            var onlineDocs = _dicDoctorRepository.GetAllOnlineDoctors(language);
            if (onlineDocs != null && onlineDocs.Count > 0)
            {
                if (!string.IsNullOrEmpty(key))
                {
                    onlineDocs = onlineDocs.FindAll(p => p.DocName.ToLower().Contains(key.ToLower()) || p.DocNameOrher.ToLower().Contains(key.ToLower()));
                }
                WorkDoctor workDoctor = null;
                foreach (var item in onlineDocs)
                {
                    var doc = workDoctors.Find(p => p.DocId == item.DocId);
                    if (doc != null)
                    {
                        continue;
                    }
                    workDoctor = new WorkDoctor();
                    workDoctor.DeptId = item.DeptId;
                    List<string> deptNames = new List<string>();
                    deptNames.AddRange(onlineDocs.FindAll(p => p.DocId == item.DocId).Select(p => p.DeptName).ToList());
                    workDoctor.DeptNames = string.Join(" ", deptNames.Distinct().ToArray());
                    workDoctor.DocDesc = item.DocDesc;
                    workDoctor.DocId = item.DocId;
                    workDoctor.DocName = item.DocName;
                    workDoctor.ListImage = item.ListImage;
                    workDoctors.Add(workDoctor);
                }
            }
            return workDoctors.Distinct().ToList();
        }

        public List<WorkDoctor> FindWorkDocList(bool isIndex, int deptId, int language, string key)
        {
            List<WorkDoctor> list = new List<WorkDoctor>();
            List<RevisitDoctor> allRevisitlist = _dicDoctorRepository.GetReVisitDoctorByDept(deptId, 0, language, key);
            List<VideoDoctor> allVideoList = _dicDoctorRepository.GetVideoDoctorByDept(deptId, 0, language, key);
            var docIds = _redisHelper.Get("DocSlot_" + DateTime.Now.ToString("MMdd"));
            var docIdList = new List<int>();
            if (string.IsNullOrEmpty(docIds))
            {
                //这里一般跑不到,RedisSchedulingJob会提前缓存
                var onlineDepts = _dicDoctorRepository.GetAllOnlineDeptDocInfo();
                Scheduling scheduling = new Scheduling();
                foreach (var item in onlineDepts)
                {
                    var docSlot = _dicDoctorRepository.GetDocSlot(item.DocId);
                    if (docSlot != null && docSlot.Count > 0)
                    {
                        scheduling = _tCService.GetSchedulingNew(DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("yyyy-MM-dd"), docSlot[0].HisDeptId + "", docSlot[0].HisServId + "", docSlot[0].DocProvId + "");
                    }

                    if (scheduling != null && scheduling.SlotDateList != null && scheduling.SlotDateList.Count > 0)
                    {
                        var slot = scheduling.SlotDateList.Find(p => p.ResourceList != null && p.ResourceList.Length > 0);
                        if (slot != null)
                        {
                            docIdList.Add(item.DocId);
                        }
                    }
                }
                _redisHelper.StringSet("DocSlot_" + DateTime.Now.ToString("MMdd"), docIdList.ToJson(), TimeSpan.FromDays(1));
            }
            else
            {
                docIdList = JsonConvert.DeserializeObject<List<int>>(docIds);
            }

            WorkDoctor doc = null;
            foreach (var item in docIdList)
            {
                var vid = allVideoList.Find(p => p.DocId == item);
                if (vid != null)
                {
                    doc = new WorkDoctor();
                    doc.DeptId = vid.DeptId;
                    List<string> deptNames = new List<string>();
                    deptNames.AddRange(allRevisitlist.FindAll(p => p.DocId == vid.DocId).Select(p => p.DeptName).ToList());
                    deptNames.AddRange(allVideoList.FindAll(p => p.DocId == vid.DocId).Select(p => p.DeptName).ToList());
                    doc.DeptNames = string.Join(" ", deptNames.Distinct().ToArray());
                    doc.DocDesc = vid.DocDesc;
                    doc.DocId = vid.DocId;
                    doc.DocName = vid.DocName;
                    doc.ListImage = vid.ListImage;
                    doc.IsVideoDoc = true;
                    doc.IsRevisitDoc = allRevisitlist.Find(p => p.DocId == vid.DocId) != null;
                    list.Add(doc);
                }
                else
                {
                    var rev = allRevisitlist.Find(p => p.DocId == item);
                    if (rev != null)
                    {
                        doc = new WorkDoctor();
                        doc.DeptId = rev.DeptId;
                        List<string> deptNames = new List<string>();
                        deptNames.AddRange(allRevisitlist.FindAll(p => p.DocId == rev.DocId).Select(p => p.DeptName).ToList());
                        deptNames.AddRange(allVideoList.FindAll(p => p.DocId == rev.DocId).Select(p => p.DeptName).ToList());
                        doc.DeptNames = string.Join(" ", deptNames.Distinct().ToArray());
                        doc.DocDesc = rev.DocDesc;
                        doc.DocId = rev.DocId;
                        doc.DocName = rev.DocName;
                        doc.ListImage = rev.ListImage;
                        doc.IsVideoDoc = allVideoList.Find(p => p.DocId == rev.DocId) != null;
                        doc.IsRevisitDoc = true;
                        list.Add(doc);
                    }
                }
            }

            list = list.OrderBy(p => p.DocName).ToList();//根据名字排序,我这里大胆的预测一下,以后会改成根据评价高低排序
            if (isIndex)
            {//首页取3个医生返回
                if (list != null && list.Count > 3)
                {
                    list = list.Take(3).ToList();
                }
            }
            return list;
        }

        public List<WorkDoctor> FindTeamDocList(bool isIndex, int deptId, int language)
        {
            List<WorkDoctor> list = new List<WorkDoctor>();
            List<RevisitDoctor> allRevisitlist = _dicDoctorRepository.GetReVisitDoctorByDept(0, 0, language, "");
            List<VideoDoctor> allVideoList = _dicDoctorRepository.GetVideoDoctorByDept(0, 0, language, "");
            WorkDoctor doc = null;
            if (isIndex)
            {//首页取3个医生返回
                if (allVideoList != null && allVideoList.Count > 3)
                {
                    allVideoList = allVideoList.OrderBy(p => p.Sort)?.Take(3).ToList();
                }
                foreach (var item in allVideoList)
                {
                    doc = new WorkDoctor();
                    doc.DeptId = item.DeptId;
                    List<string> deptNames = new List<string>();
                    deptNames.AddRange(allRevisitlist.FindAll(p => p.DocId == item.DocId).Select(p => p.DeptName).ToList());
                    deptNames.AddRange(allVideoList.FindAll(p => p.DocId == item.DocId).Select(p => p.DeptName).ToList());
                    doc.DeptNames = string.Join(" ", deptNames.Distinct().ToArray());
                    doc.DocDesc = item.DocDesc;
                    doc.DocId = item.DocId;
                    doc.DocName = item.DocName;
                    doc.ListImage = item.ListImage;
                    doc.IsVideoDoc = true;
                    doc.IsRevisitDoc = allRevisitlist.Find(p => p.DocId == item.DocId) != null;
                    list.Add(doc);
                }
            }
            else
            {
                List<VideoDoctor> videoList = null;
                List<RevisitDoctor> revisitlist = null;
                if (deptId != 0)
                {
                    videoList = allVideoList.FindAll(p => p.DeptId == deptId);
                    revisitlist = allRevisitlist.FindAll(p => p.DeptId == deptId);
                }
                else
                {
                    videoList = allVideoList;
                    revisitlist = allRevisitlist;
                }
                foreach (var item in videoList)
                {
                    if (list.Exists(p => p.DocId == item.DocId))
                    {
                        continue;
                    }
                    doc = new WorkDoctor();
                    doc.DeptId = item.DeptId;
                    List<string> deptNames = new List<string>();
                    deptNames.AddRange(allRevisitlist.FindAll(p => p.DocId == item.DocId).Select(p => p.DeptName).ToList());
                    deptNames.AddRange(allVideoList.FindAll(p => p.DocId == item.DocId).Select(p => p.DeptName).ToList());
                    doc.DeptNames = string.Join(" ", deptNames.Distinct().ToArray());
                    doc.DocDesc = item.DocDesc;
                    doc.DocId = item.DocId;
                    doc.DocName = item.DocName;
                    doc.ListImage = item.ListImage;
                    doc.IsVideoDoc = true;
                    doc.IsRevisitDoc = allRevisitlist.Find(p => p.DocId == item.DocId) != null;
                    list.Add(doc);
                }
                foreach (var item in revisitlist)
                {
                    if (list.Exists(p => p.DocId == item.DocId))
                    {
                        continue;
                    }
                    doc = new WorkDoctor();
                    doc.DeptId = item.DeptId;
                    List<string> deptNames = new List<string>();
                    deptNames.AddRange(allRevisitlist.FindAll(p => p.DocId == item.DocId).Select(p => p.DeptName).ToList());
                    deptNames.AddRange(allVideoList.FindAll(p => p.DocId == item.DocId).Select(p => p.DeptName).ToList());
                    doc.DeptNames = string.Join(" ", deptNames.Distinct().ToArray());
                    doc.DocDesc = item.DocDesc;
                    doc.DocId = item.DocId;
                    doc.DocName = item.DocName;
                    doc.ListImage = item.ListImage;
                    doc.IsVideoDoc = allVideoList.Find(p => p.DocId == item.DocId) != null;
                    doc.IsRevisitDoc = true;
                    list.Add(doc);
                }
            }

            return list;
        }

        public List<WorkDoctor> FindInsurTeamDocList(int type, int deptId, int language)
        {
            List<WorkDoctor> list = new List<WorkDoctor>();
            var docList = _dicDoctorRepository.GetInsurDoctorByDept(type, deptId, 0, language, "");
            if (docList != null && docList.Count > 0)
            {
                WorkDoctor doc = null;
                foreach (var item in docList)
                {
                    doc = new WorkDoctor();
                    doc.DeptId = item.DeptId;
                    doc.DeptNames = item.DeptName;
                    doc.DocDesc = item.DocDesc;
                    doc.DocId = item.DocId;
                    doc.DocName = item.DocName;
                    doc.ListImage = item.ListImage;
                    doc.IsRevisitDoc = true;
                    list.Add(doc);
                }
            }
            return list;
        }

        public List<DoctorBelongTo> GetDocHosIds(int doctorId)
        {
            return _dicMapDeptDoctorRepository.GetDocHosIds(doctorId);
        }

        public DoctorInfoByLanguage FindDoctorDetailCityByLanguage(int doctorId, int language)
        {
            Dic_Doctor doctor = _dicDoctorRepository.SelectByPrimaryKey(doctorId);
            if (doctor != null)
            {
                Dic_Map_His_Doctor mapHisDoctor = _dicMapHisDoctorRepository.SelectByDoctor(doctorId);
                List<HosByLanguage> hos = _dicHosRepository.SelectByDoctorByLanguage(doctorId, language, 0);
                List<HosByCity> hosByCityList = _dicHosRepository.SelectCity(language);
                foreach (var hosByCity in hosByCityList)
                {
                    hosByCity.HosByLanguages = new List<HosByLanguage>();
                    foreach (var hosInfo in hos)
                    {
                        if (hosInfo.City == hosByCity.City)
                        {
                            hosByCity.HosByLanguages.Add(hosInfo);
                        }
                    }
                }

                List<DeptByLanguage> dicDept = _dicDeptRepository.SelectByDoctorByLanguage(doctorId, language);
                List<Dic_Nationality> nationalityList = new List<Dic_Nationality>();
                if (!string.IsNullOrEmpty(doctor.Nationality_Id))
                {
                    var arrIds = doctor.Nationality_Id.Split(',');
                    foreach (var nationId in arrIds)
                    {
                        Dic_Nationality nationality = _dicNationalityRepository.SelectByPrimaryKey(Convert.ToInt32(nationId));
                        if (nationality != null)
                        {
                            nationalityList.Add(nationality);
                        }
                    }
                }
                DoctorInfoByLanguage doctorInfoByLanguage = new DoctorInfoByLanguage();
                doctorInfoByLanguage.Id = doctor.Id;
                doctorInfoByLanguage.CareProveId = doctor.Id;
                string doctorName = language == 1 ? doctor.CN_Name : doctor.EN_Name;
                doctorInfoByLanguage.DocName = doctorName;
                doctorInfoByLanguage.ListImage = doctor.List_Image;

                doctorInfoByLanguage.Dept = dicDept;
                doctorInfoByLanguage.HosCity = hosByCityList.FindAll(p => p.HosByLanguages != null && p.HosByLanguages.Count > 0);
                doctorInfoByLanguage.Nation = nationalityList;
                doctorInfoByLanguage.AptType = doctor.APT_Type;

                return doctorInfoByLanguage;

            }
            return null;
        }

        public DoctorInfoModel FindDoctorDetail(int doctorId, int cityId, int language)
        {
            var doctor = _dicDoctorRepository.SelectByPrimaryKey(doctorId);
            if (doctor == null)
            {
                return null;
            }
            DoctorInfoModel model = new DoctorInfoModel();
            var mapHisDoctor = _dicMapHisDoctorRepository.SelectByDoctor(doctorId);
            List<Dic_Hos> hos = _dicHosRepository.SelectByDoctor(doctorId, 0, cityId);
            List<Dic_Dept> dicDept = _dicDeptRepository.SelectByDoctor(doctorId);
            List<Dic_Nationality> nationalityList = new List<Dic_Nationality>();
            if (doctor != null && !string.IsNullOrEmpty(doctor.Nationality_Id))
            {
                foreach (var item in doctor.Nationality_Id.Split(','))
                {
                    Dic_Nationality nationality = _dicNationalityRepository.SelectByPrimaryKey(Convert.ToInt32(item));
                    if (nationality != null)
                    {
                        nationalityList.Add(nationality);
                    }
                }
            }
            model.Dept = dicDept;
            model.Doctor = doctor;
            model.Hos = hos;
            model.MapHisDoctor = mapHisDoctor;
            model.Nation = nationalityList;
            return model;
        }

        public List<InsurDoctor> GetInsurDocList(int type, int deptId, int language, string searchWords)
        {
            var docList = _dicDoctorRepository.GetInsurDocList(type, deptId, language, searchWords);
            return docList;
        }









    }
}
