using PoJun.Dapper;
using ASHermed.SinoInterface.Api.IRepository.Test;
using ASHermed.SinoInterface.Entity.Test;
using ASHermed.SinoInterface.Repository.MySql;
using ASHermed.SinoInterface.Tools;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.IRepository;
using ASHermed.SinoInterface.Api.ContractModel;

namespace ASHermed.SinoInterface.Api.MySqlRepository
{

    public class DicDoctorRepository : BaseRepositoryToASHermed_SinoInterface<Dic_Doctor>, IDicDoctorRepository
    {

        public List<Dic_Doctor> GetAllDoctor()
        {
            var selectSql = new StringBuilder();

            selectSql.Append(@"select * from dic_doctor where IS_DELETED = false");

            var parms = new Dictionary<string, object>();
            var result = Query<Dic_Doctor>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<DoctorWithHosDeptByLanguage> SelectDetailByLanguage(int hosId, int deptId, int language, int source)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select distinct a.ID,a.LIST_IMAGE as ListImage,a.DOCNAME,a.SORT,a.SORT_INDEX as sortIndex,a.APT_TYPE as AptType,c.ID as hosId, c.CN_ABBREVIATED_ADDR as HosName,d.ID as DeptId,d.CN_NAME as DeptName,f.CARE_PROV_ID as CareProveId ");
            }
            else
            {
                selectSql.Append(@"select distinct a.ID,a.LIST_IMAGE as ListImage,a.DOCNAME,a.SORT,a.SORT_INDEX as sortIndex,a.APT_TYPE as AptType,c.ID as hosId, c.EN_ABBREVIATED_ADDR as HosName,d.ID as DeptId,d.EN_NAME as DeptName,f.CARE_PROV_ID as CareProveId ");
            }
            selectSql.Append(@" from(select a.ID, a.LIST_IMAGE, ");
            if (language == 1)
            {
                selectSql.Append(@" a.CN_NAME as DOCNAME, ");
            }
            else
            {
                selectSql.Append(@" a.EN_NAME as DOCNAME, ");
            }
            selectSql.Append(@" a.SORT,a.SORT_INDEX,a.APT_TYPE,GROUP_CONCAT(e.IMAGE) as NATION 
                        from dic_doctor a LEFT JOIN dic_nationality e on POSITION(E.id in a.NATIONALITY_ID) 
                        where a.IS_DELETED = 0 GROUP BY a.ID) a JOIN dic_map_dept_doctor b on b.IS_DELETED = 0 and b.APT_DOCTOR_ID = a.ID and b.USE_OCCASION = 0 ");
            if (hosId != 0)
            {
                selectSql.Append(@"  and b.APT_HOS_ID = @hosId ");
            }
            if (deptId != 0)
            {
                selectSql.Append(@"  and b.APT_DEPT_ID = @deptId ");
            }
            selectSql.Append(@" JOIN dic_hos c on c.IS_DELETED = 0 and c.ID = b.APT_HOS_ID
                                    JOIN dic_dept d on d.IS_DELETED = 0 and d.ID = b.APT_DEPT_ID and d.DEPT_TYPE = 0
                                    LEFT JOIN dic_map_his_doctor f on f.APT_DOCTOR_ID = a.ID and f.IS_DELETED = false
                                    where a.APT_TYPE != 1
                                    ORDER BY a.SORT ASC, a.SORT_INDEX DESC ");
            var parms = new Dictionary<string, object>();
            parms.Add("@hosId", hosId);
            parms.Add("@deptId", deptId);
            var result = Query<DoctorWithHosDeptByLanguage>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<AptDoctorByLanguage> SelectDeptDoctor(int hosId, int deptId, int language)
        {

            var selectSql = new StringBuilder();

            selectSql.Append(@" select distinct a.ID as DOCTOR_ID,c.ID as HOS_ID,d.ID as DEPT_ID,a.LIST_IMAGE,(select STR_TO_DATE(MAX(APT_TIME), '%Y-%m-%d') from out_apt_info apt where apt.DOC_ID = a.ID) as APT_TIME, ");
            if (language == 1)
            {
                selectSql.Append(@" a.CN_NAME as DOCTOR_NAME,c.CN_NAME as HOS_NAME,d.CN_NAME as DEPT_NAME ");
            }
            else
            {
                selectSql.Append(@" a.EN_NAME as DOCTOR_NAME,c.EN_NAME as HOS_NAME,d.EN_NAME as DEPT_NAME ");
            }
            selectSql.Append(@" from dic_doctor a JOIN dic_map_dept_doctor b on b.IS_DELETED = 0 and b.APT_DOCTOR_ID = a.ID ");
            if (hosId != 0)
            {
                selectSql.Append(@"  and b.APT_HOS_ID = @hosId ");
            }
            if (deptId != 0)
            {
                selectSql.Append(@"  and b.APT_DEPT_ID = @deptId ");
            }
            selectSql.Append(@" JOIN dic_hos c on c.IS_DELETED = 0 and c.ID = b.APT_HOS_ID
                                JOIN dic_dept d on d.IS_DELETED = 0 and d.ID = b.APT_DEPT_ID
                                where a.IS_DELETED = 0 and a.APT_TYPE != 1
                                ORDER BY a.SORT ASC, a.SORT_INDEX DESC ");
            var parms = new Dictionary<string, object>();
            parms.Add("@hosId", hosId);
            parms.Add("@deptId", deptId);
            var result = Query<AptDoctorByLanguage>(selectSql.ToString(), parms);
            return result?.ToList();

        }

        public List<VideoDoctor> GetVideoDoctorByDept(int deptId, int docId, int language, string key)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select de.CN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.CN_NAME as DocName,d.CN_DESC as DocDesc,d.SORT ");
            }
            else
            {
                selectSql.Append(@"select de.EN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.EN_NAME as DocName,d.EN_DESC as DocDesc,d.SORT ");
            }
            selectSql.Append(@"  from dic_video_dept_docs v INNER JOIN dic_doctor d ON v.DocId=d.ID INNER JOIN dic_dept de ON v.deptid=de.ID where de.IS_DELETED = 0 AND v.isdelete=0 AND d.IS_DELETED = 0 ");

            if (deptId != 0)
            {
                selectSql.Append(@" AND v.deptid=@deptId ");
                parms.Add("@deptId", deptId);
            }
            if (docId != 0)
            {
                selectSql.Append(@" AND v.DocId=@DocId ");
                parms.Add("@DocId", docId);
            }
            if (!string.IsNullOrEmpty(key))
            {
                selectSql.Append(@" AND (d.CN_NAME like CONCAT('%',@NAME,'%') or d.EN_NAME like CONCAT('%',@NAME,'%'))  ");
                parms.Add("@NAME", key);
            }
            var result = Query<VideoDoctor>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<InsurDoctor> GetInsurDocList(int type, int deptId, int language, string searchWords)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select de.CN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.CN_NAME as DocName,d.CN_DESC as DocDesc,d.SORT ");
            }
            else
            {
                selectSql.Append(@"select de.EN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.EN_NAME as DocName,d.EN_DESC as DocDesc,d.SORT ");
            }
            selectSql.Append(@"  from dic_insurance_dept_docs v INNER JOIN dic_doctor d ON v.DocId=d.ID INNER JOIN dic_dept de ON v.deptid=de.ID where de.IS_DELETED = 0 AND v.isdelete=0 AND d.IS_DELETED = 0 and v.InsuranceType=@InsuranceType ");
            parms.Add("@InsuranceType", type);
            if (deptId != 0)
            {
                selectSql.Append(@" AND v.deptid=@deptId ");
                parms.Add("@deptId", deptId);
            }

            if (!string.IsNullOrEmpty(searchWords))
            {
                selectSql.Append(@" AND (d.CN_NAME like CONCAT('%',@NAME,'%') or d.EN_NAME like CONCAT('%',@NAME,'%'))  ");
                parms.Add("@NAME", searchWords);
            }
            var result = Query<InsurDoctor>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<RevisitDoctor> GetInsurDoctorByDept(int type, int deptId, int docId, int language, string key)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select de.CN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.CN_NAME as DocName,d.CN_DESC as DocDesc,d.SORT ");
            }
            else
            {
                selectSql.Append(@"select de.EN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.EN_NAME as DocName,d.EN_DESC as DocDesc,d.SORT ");
            }
            selectSql.Append(@"  from dic_insurance_dept_docs v INNER JOIN dic_doctor d ON v.DocId=d.ID INNER JOIN dic_dept de ON v.deptid=de.ID where de.IS_DELETED = 0 AND v.isdelete=0 AND d.IS_DELETED = 0 and v.InsuranceType=@InsuranceType ");
            parms.Add("@InsuranceType", type);
            if (deptId != 0)
            {
                selectSql.Append(@" AND v.deptid=@deptId ");
                parms.Add("@deptId", deptId);
            }
            if (docId != 0)
            {
                selectSql.Append(@" AND v.DocId=@DocId ");
                parms.Add("@DocId", docId);
            }
            if (!string.IsNullOrEmpty(key))
            {
                selectSql.Append(@" AND (d.CN_NAME like CONCAT('%',@NAME,'%') or d.EN_NAME like CONCAT('%',@NAME,'%'))  ");
                parms.Add("@NAME", key);
            }
            var result = Query<RevisitDoctor>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<RevisitDoctor> GetReVisitDoctorByDept(int deptId, int docId, int language, string key)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select de.CN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.CN_NAME as DocName,d.CN_DESC as DocDesc,d.SORT ");
            }
            else
            {
                selectSql.Append(@"select de.EN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.EN_NAME as DocName,d.EN_DESC as DocDesc,d.SORT ");
            }
            selectSql.Append(@"  from dic_revisit_dept_docs v INNER JOIN dic_doctor d ON v.DocId=d.ID INNER JOIN dic_dept de ON v.deptid=de.ID where de.IS_DELETED = 0 AND v.isdelete=0 AND d.IS_DELETED = 0 ");

            if (deptId != 0)
            {
                selectSql.Append(@" AND v.deptid=@deptId ");
                parms.Add("@deptId", deptId);
            }
            if (docId != 0)
            {
                selectSql.Append(@" AND v.DocId=@DocId ");
                parms.Add("@DocId", docId);
            }
            if (!string.IsNullOrEmpty(key))
            {
                selectSql.Append(@" AND (d.CN_NAME like CONCAT('%',@NAME,'%') or d.EN_NAME like CONCAT('%',@NAME,'%'))  ");
                parms.Add("@NAME", key);
            }
            var result = Query<RevisitDoctor>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<VideoDoctor> GetAllWorkDoctorByDept(int deptId, int language)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select de.CN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.CN_NAME as DocName,d.CN_DESC as DocDesc ");
            }
            else
            {
                selectSql.Append(@"select de.EN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.EN_NAME as DocName,d.EN_DESC as DocDesc ");
            }
            selectSql.Append(@"  from dic_video_dept_docs v INNER JOIN dic_doctor d ON v.DocId=d.ID INNER JOIN dic_dept de ON v.deptid=de.ID where de.IS_DELETED = 0 AND v.isdelete=0 AND d.IS_DELETED = 0 ");

            if (deptId != 0)
            {
                selectSql.Append(@" AND v.deptid=@deptId ");
                parms.Add("@deptId", deptId);
            }

            var result = Query<VideoDoctor>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<AllDoctorInfo> GetAllOnlineDoctors(int language)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select de.CN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.CN_NAME as DocName,d.EN_NAME as DocNameOrher,d.CN_DESC as DocDesc ");
            }
            else
            {
                selectSql.Append(@"select de.EN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.EN_NAME as DocName,d.CN_NAME as DocNameOrher,d.EN_DESC as DocDesc ");
            }
            selectSql.Append(@"  from dic_video_dept_docs v INNER JOIN dic_doctor d ON v.DocId=d.ID INNER JOIN dic_dept de ON v.deptid=de.ID where de.IS_DELETED = 0 AND v.isdelete=0 AND d.IS_DELETED = 0 ");
            if (language == 1)
            {
                selectSql.Append(@"UNION select de.CN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.CN_NAME as DocName,d.EN_NAME as DocNameOrher,d.CN_DESC as DocDesc ");
            }
            else
            {
                selectSql.Append(@"UNION select de.EN_NAME as DeptName,v.Id,v.DeptId,v.Price,d.Id as DocId,d.LIST_IMAGE as ListImage,d.EN_NAME as DocName,d.CN_NAME as DocNameOrher,d.EN_DESC as DocDesc ");
            }
            selectSql.Append(@"  from dic_revisit_dept_docs v INNER JOIN dic_doctor d ON v.DocId=d.ID INNER JOIN dic_dept de ON v.deptid=de.ID where de.IS_DELETED = 0 AND v.isdelete=0 AND d.IS_DELETED = 0 ");
            var result = Query<AllDoctorInfo>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public Dic_Doctor SelectByPrimaryKey(int doctorId)
        {
            var selectSql = new StringBuilder();

            selectSql.Append(@"select ID, LIST_IMAGE, DETAIL_IMAGE, HTML_URL, CN_NAME, EN_NAME, CN_TITLE, EN_TITLE, CN_NATIONALITY,
                        EN_NATIONALITY, CN_LANGUAGE, EN_LANGUAGE, IS_STAR, SORT, SORT_INDEX, SELECTED_LANGUAGE,
                        CREATE_USER, CREATE_TIME, UPDATE_USER, UPDATE_TIME, IS_DELETED, APT_TYPE, CN_INITIAL,
                        EN_INITIAL, CN_DESC, EN_DESC, CN_EXPER, EN_EXPER, CN_EDUCATION, EN_EDUCATION, NATIONALITY_ID,NAME_PINYIN
                            from dic_doctor
                            where ID = @ID
                            and IS_DELETED = false");

            var parms = new Dictionary<string, object>();
            parms.Add("@ID", doctorId);
            var result = Query<Dic_Doctor>(selectSql.ToString(), parms);
            return result?.FirstOrDefault();
        }

        public Dic_Doctor SelectById(int id, int language)
        {
            var sql = @" select ";
            if (language == 1)
            {
                sql += " CN_DESC,CN_EXPER,CN_EDUCATION,CN_NAME,CN_TITLE,CN_LANGUAGE ";
            }
            else
            {
                sql += " EN_DESC,EN_EXPER,EN_EDUCATION,EN_NAME,EN_TITLE,EN_LANGUAGE ";
            }
            sql += " ,LIST_IMAGE from dic_doctor where ID=@ID ";
            return Query<Dic_Doctor>(sql, new { ID = id }).FirstOrDefault();
        }

        public List<DocDept> GetAllOnlineDeptDocInfo()
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select d.ID as DocId,d.CN_NAME AS DocNameCn,d.EN_NAME as DocNameEn,d.LIST_IMAGE as DocImage,M.ID AS DeptId,M.CN_NAME as DeptNameCn,m.EN_NAME as DeptNameEn from dic_video_dept_docs vd INNER JOIN dic_doctor d on vd.DocId = d.ID INNER JOIN dic_dept m on vd.DeptId = m.ID  WHERE D.IS_DELETED =0 AND vd.IsDelete = 0
                UNION
select d.ID as DocId,d.CN_NAME AS DocNameCn,d.EN_NAME as DocNameEn,d.LIST_IMAGE as DocImage,M.ID AS DeptId,M.CN_NAME as DeptNameCn,m.EN_NAME as DeptNameEn from dic_revisit_dept_docs vd INNER JOIN dic_doctor d on vd.DocId = d.ID INNER JOIN dic_dept m on vd.DeptId = m.ID WHERE D.IS_DELETED =0 AND vd.IsDelete = 0");

            var result = Query<DocDept>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<DocDept> GetAllOnlineDeptInfo()
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select deptid from dic_revisit_dept where IsDelete = 0
                UNION
                select deptid from dic_video_dept where IsDelete = 0");

            var result = Query<DocDept>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public int GetDocHisCareProvId(int doctorId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@"select CARE_PROV_ID from dic_map_his_doctor where APT_DOCTOR_ID =@DocId");
            var parms = new Dictionary<string, object>();
            parms.Add("@DocId", doctorId);
            var result = Query<int>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }

        public string GetAptDocPhone(int docId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@"select PHONE from sys_user where DOCID = @DocId");
            var parms = new Dictionary<string, object>();
            parms.Add("@DocId", docId);
            var result = Query<string>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }
        public List<Online_Doctor_Slot> GetDocSlot(int docId)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select * from online_doctor_slot where IsDelete = 0 and DocId=@DocId");
            parms.Add("@DocId", docId);
            var result = Query<Online_Doctor_Slot>(selectSql.ToString(), parms);
            return result?.ToList();
        }
        /// <summary>
        /// 弃用
        /// </summary>
        /// <returns></returns>
        public Pub_Doctor_User_Info SelectDocListByHealthClub()
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" select *
                                        from pub_doctor_user_info
                                        where IS_HEALTH_CLUB = true
                                          and `STATUS` = 1");
            var parms = new Dictionary<string, object>();
            var result = Query<Pub_Doctor_User_Info>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }

        public Sys_User GetSysUserByHealthClub()
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" select *
                                        from Sys_User
                                        where IS_HEALTH_CLUB = true
                                          and `STATUS` = 1");
            var parms = new Dictionary<string, object>();
            var result = Query<Sys_User>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }
        public Sys_User GetSysUserById(int doctorId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" select *
                                        from Sys_User
                                        where id = @id");
            var parms = new Dictionary<string, object>();
            parms.Add("@id", doctorId);
            var result = Query<Sys_User>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }
        /// <summary>
        /// 弃用
        /// </summary>
        /// <param name="doctorId"></param>
        /// <returns></returns>
        public Pub_Doctor_User_Info GetDoctorUserInfo(int doctorId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" select *
                                        from pub_doctor_user_info
                                        where id = @id");
            var parms = new Dictionary<string, object>();
            parms.Add("@id", doctorId);
            var result = Query<Pub_Doctor_User_Info>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }

        public DoctorUserInfoResp SelectDoctorWithStatusByImId(string docImId)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" select d.ID,
               d.Sex as GENDER,
               d.NICKNAME,
               d.UserImg as HEADIMG,
               d.ImId,
               s.CN_DESCRIPTION as ONLINESTATUS,
               s.STATUS_ICON    as STATUSICON,
               ds.PERSONALIZED_SIGNATURE as PERSONALIZEDSIGNATURE,
               2                as USERTYPE
        from sys_user d
                 left JOIN out_doctor_status_info ds on ds.DOCTOR_ID = d.ID and ds.`STATUS` = 1
                 left JOIN dic_doctor_status s on s.ID = ds.ONLINE_STATUS and s.`STATUS` = 1
        where d.`STATUS` = 1
          and (d.IMID = @docImid or d.ID = @docImid)
        union
        select d.id,
               d.GENDER,
               d.NICK_NAME as NICKNAME,
               d.HEADIMG,
               d.IM_ID as ImId,
               null as ONLINESTATUS,
               null as STATUSICON,
               null as PERSONALIZEDSIGNATURE,
               1    as USERTYPE
        from pub_user_info d
        where d.IS_DELETED = 0
          and (d.IM_ID = @docImid)");
            var parms = new Dictionary<string, object>();
            parms.Add("@docImid", docImId);
            var result = Query<DoctorUserInfoResp>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }

        public OutMapDocPat SelectByDocIdWithPatId(int userId, int Id)
        {
            var selectSql = new StringBuilder();
            selectSql.Append(@" select id,DOCTOR_ID as DOCTORID,PAT_ID as PATID,IS_STAR as ISSTAR,IS_TOPPING as ISTOPPING,`STATUS`,CREATE_TIME as CREATETIME,UPDATE_TIME as UPDATETIME,REMARK,REMARKS,DESCRIPTION
        from out_map_doc_pat
        where `STATUS` = 1
          and DOCTOR_ID = @doctorId
          and PAT_ID = @patId");
            var parms = new Dictionary<string, object>();
            parms.Add("@doctorId", Id);
            parms.Add("@patId", userId);
            var result = Query<OutMapDocPat>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }

        public List<Dic_Doctor> GetDocForHisDocId(List<string> hisDocIds, int deptId, int hosId)
        {
            var sql = @" select DISTINCT h.ID,h.CN_NAME,h.EN_NAME,h.CareProvID,h.APT_TYPE,h.LIST_IMAGE,h.CN_LANGUAGE,h.EN_LANGUAGE from ( select b.*,a.CARE_PROV_ID AS CareProvID from dic_map_his_doctor  a
                        INNER JOIN dic_doctor b ON a.APT_DOCTOR_ID = b.ID and b.IS_DELETED = FALSE 
	                    INNER JOIN dic_map_dept_doctor c on b.id = c.APT_DOCTOR_ID and c.IS_DELETED = FALSE 
                        where c.APT_DEPT_ID = @DeptId AND c.APT_HOS_ID = @HosId AND (b.APT_TYPE=2 OR b.APT_TYPE=4) ) h ";
            return Query<Dic_Doctor>(sql, new { HisDocId = hisDocIds, DeptId = deptId, HosId = hosId }).ToList();
        }
        public List<DicDoc> GetDocForHisDocIds(List<string> hisDocIds, int deptId, int language, int hosId)
        {
            var sql = @" select DISTINCT h.ID,";
            if (language == 1)
            {
                sql += "h.CN_NAME,h.CN_LANGUAGE,";
            }
            else
            {
                sql += "h.EN_NAME as CN_NAME,h.EN_LANGUAGE as CN_LANGUAGE,";
            }
            sql += @" h.CareProvID,h.APT_TYPE,h.LIST_IMAGE from ( select b.*,a.CARE_PROV_ID AS CareProvID from dic_map_his_doctor  a
                        INNER JOIN dic_doctor b ON a.APT_DOCTOR_ID = b.ID and b.IS_DELETED=0
	                    INNER JOIN dic_map_dept_doctor c on b.id = c.APT_DOCTOR_ID and c.IS_DELETED = FALSE 
                        where c.APT_DEPT_ID = @DeptId AND c.APT_HOS_ID = @HosId AND (b.APT_TYPE=2 OR b.APT_TYPE=4) ) h ";
            return Query<DicDoc>(sql, new { HisDocId = hisDocIds, DeptId = deptId, HosId = hosId }).ToList();
        }

        public async Task<AptDocInfo> GetAptDocInfo(int docId, int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select LIST_IMAGE as DocImg,CN_NAME as DocName,CN_LANGUAGE as DocLanguage ");
            }
            else
            {
                selectSql.Append(@"select LIST_IMAGE as DocImg,EN_NAME as DocName,EN_LANGUAGE as DocLanguage ");
            }
            selectSql.Append(@" from dic_doctor where IS_DELETED = false ");
            if (docId > 0)//CN_DESC,CN_EXPER,CN_EDUCATION,CN_NAME,CN_TITLE,CN_LANGUAGE
            {
                selectSql.Append(@" and id=@id ");
            }
            var parms = new Dictionary<string, object>();
            parms.Add("@id", docId);
            var result = QueryAsync<AptDocInfo>(selectSql.ToString(), parms);
            return result.Result?.FirstOrDefault();
        }

        public async Task<List<DocHosDept>> GetDocDepts(int doctorId, int deptId, int language)
        {
            var sql = new StringBuilder();
            if (language == 1)
            {
                sql.Append(@"select CN_ABBREVIATED_ADDR as HosName,ds.CN_NAME AS ServName,dt.CN_NAME as deptName,");
            }
            else
            {
                sql.Append(@"select EN_ABBREVIATED_ADDR as HosName,ds.EN_NAME AS ServName,dt.EN_NAME as deptName,");
            }
            sql.Append(@"dd.APT_HOS_ID,h.HIS_HOS_ID,h.CN_ABBREVIATED_ADDR,h.LONGITUDE,h.LATITUDE,h.SORT,h.PICTURE,dd.APT_DEPT_ID,dd.APT_DOCTOR_ID,d.APT_TYPE,hd.HIS_DEPT_ID,ds.HIS_SERV_ID,hdoc.CARE_PROV_ID,ds.Id as AptProj  
                    from dic_map_dept_doctor dd INNER JOIN dic_hos h on dd.APT_HOS_ID = h.id 
                    INNER JOIN dic_doctor d on dd.APT_DOCTOR_ID = d.ID 
                    INNER JOIN dic_map_his_dept hd on dd.APT_DEPT_ID = hd.APT_DEPT_ID and h.HIS_HOS_ID = hd.HIS_HOS_ID
                    INNER JOIN dic_map_dept_serv ds on hd.ID = ds.DEPT_MAP_ID 
                    INNER JOIN dic_map_his_doctor hdoc on hdoc.APT_DOCTOR_ID = d.ID
                    INNER JOIN dic_dept dt on dt.id = dd.APT_DEPT_ID
                    where dd.APT_DOCTOR_ID = @doctorId and dd.IS_DELETED = 0 and h.IS_DELETED = 0 and dd.APT_DEPT_ID = @deptId and h.IS_DELETED = 0 and hd.IS_DELETED = 0 and ds.IS_DELETED = 0 and hdoc.IS_DELETED = 0 and dt.IS_DELETED = 0 and d.APT_TYPE in (2,4)");
            return QueryAsync<DocHosDept>(sql.ToString(), new { doctorId = doctorId, deptId = deptId }).Result?.ToList();
        }

        public List<AptDocList> GetAllConditionDoctor(int hosId, int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select APT_HOS_ID as hosId,h.CN_ABBREVIATED_ADDR as hosName,APT_DEPT_ID as deptId, d.CN_NAME as deptname,APT_DOCTOR_ID as docId, doc.CN_NAME as DocName,doc.LIST_IMAGE as DocImg,doc.CN_Initial as Initial,d.NAME_PINYIN as NamePinYin,doc.EN_NAME as DocNameEn ");
            }
            else
            {
                selectSql.Append(@"select APT_HOS_ID as hosId,h.EN_ABBREVIATED_ADDR as hosName,APT_DEPT_ID as deptId, d.EN_NAME as deptname,APT_DOCTOR_ID as docId, doc.EN_NAME as DocName,doc.LIST_IMAGE as DocImg,doc.EN_Initial as Initial,d.NAME_PINYIN as NamePinYin,doc.CN_NAME as DocNameEn ");
            }
            selectSql.Append(@" from dic_map_dept_doctor dd LEFT JOIN dic_hos h on dd.APT_HOS_ID = h.id 
                                    LEFT JOIN dic_dept d on dd.APT_DEPT_ID = d.id 
                                    LEFT JOIN dic_doctor doc on dd.APT_DOCTOR_ID = doc.ID
                                    where dd.IS_DELETED = 0 and h.IS_DELETED = 0 and d.IS_DELETED = 0 and doc.IS_DELETED = 0 and doc.APT_TYPE > 1  ");
            if (hosId > 0)
            {
                selectSql.Append(@"  and APT_HOS_ID = @hosId ");
            }
            var parms = new Dictionary<string, object>();
            parms.Add("@hosId", hosId);
            var result = Query<AptDocList>(selectSql.ToString(), parms);
            return result?.ToList();
        }
    }
}
