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.App.Entity;

namespace ASHermed.SinoInterface.Api.MySqlRepository
{

    public class DicDeptRepository : BaseRepositoryToASHermed_SinoInterface<Dic_Dept>, IDicDeptRepository
    {
        public List<DeptByLanguage> SelectByLanguage(int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select ID, CN_NAME as NAME,LIST_IMAGE as ListImage,ICON,NAME_PINYIN as NamePinyin,EN_NAME as YName ");
            }
            else
            {
                selectSql.Append(@"select ID, EN_NAME as NAME,LIST_IMAGE as ListImage,ICON,NAME_PINYIN as NamePinyin,CN_NAME as YName ");
            }
            selectSql.Append(@",CN_NAME as NameCn, EN_NAME as NameEn from dic_dept where IS_DELETED = false and DEPT_TYPE=0 ");
            if (language == 1)
            {
                selectSql.Append(@" order by CN_INITIAL");
            }
            else
            {
                selectSql.Append(@" order by EN_INITIAL");
            }

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

        public List<DeptByLanguage> GetOnlineDepts(int type, int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select DISTINCT d.ID, CN_NAME as NAME,LIST_IMAGE as ListImage,ICON,NAME_PINYIN as NamePinyin,EN_NAME as YName ");
            }
            else
            {
                selectSql.Append(@"select DISTINCT d.ID, EN_NAME as NAME,LIST_IMAGE as ListImage,ICON,NAME_PINYIN as NamePinyin,CN_NAME as YName ");
            }

            if (type == 1)
            {
                selectSql.Append(@" from dic_video_dept ");
            }
            else
            {
                selectSql.Append(@" from dic_revisit_dept ");
            }

            selectSql.Append(@" dd INNER JOIN dic_dept d on dd.DeptId = d.ID where IS_DELETED = false and DEPT_TYPE=0 and IsDelete=0 ");
            if (language == 1)
            {
                selectSql.Append(@" order by CN_INITIAL");
            }
            else
            {
                selectSql.Append(@" order by EN_INITIAL");
            }
            var result = Query<DeptByLanguage>(selectSql.ToString(), null);
            return result?.ToList();
        }

        public List<DeptByLanguage> GetInsurDept(int type, int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select DISTINCT d.ID, CN_NAME as NAME,LIST_IMAGE as ListImage,ICON,NAME_PINYIN as NamePinyin,EN_NAME as YName ");
            }
            else
            {
                selectSql.Append(@"select DISTINCT d.ID, EN_NAME as NAME,LIST_IMAGE as ListImage,ICON,NAME_PINYIN as NamePinyin,CN_NAME as YName ");
            }
            selectSql.Append(@" from dic_insurance_dept_docs ");
            selectSql.Append(@" dd INNER JOIN dic_dept d on dd.DeptId = d.ID where IS_DELETED = false and DEPT_TYPE=0 and IsDelete=0 and dd.InsuranceType =@InsuranceType");
            if (language == 1)
            {
                selectSql.Append(@" order by CN_INITIAL");
            }
            else
            {
                selectSql.Append(@" order by EN_INITIAL");
            }
            var parms = new Dictionary<string, object>();
            parms.Add("@InsuranceType", type);
            var result = Query<DeptByLanguage>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public MaterialDept GetOnlineDeptByDoc(int docId)
        {
            var selectSql = new StringBuilder();

            selectSql.Append(@" select DeptId,d.EN_NAME as NameEn from dic_video_dept_docs dd INNER JOIN dic_dept d on d.ID = dd.DeptId where DocId = @DocId and IsDelete = 0");
            var parms = new Dictionary<string, object>();
            parms.Add("@DocId", docId);
            var result = Query<MaterialDept>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }

        public List<DeptByLanguage> SelectByDoctorByLanguage(int docId, int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select distinct t1.ID, t1.CN_NAME as NAME,t1.LIST_IMAGE ");
            }
            else
            {
                selectSql.Append(@"select distinct t1.ID, t1.EN_NAME as NAME,t1.LIST_IMAGE ");
            }
            selectSql.Append(@" from dic_dept t1, dic_map_dept_doctor t2
                                where t1.IS_DELETED = false and t1.DEPT_TYPE=0 AND t1.ID = t2.APT_DEPT_ID AND t2.IS_DELETED = false
                                AND t2.USE_OCCASION = 0
                                AND t2.APT_DOCTOR_ID = @doctorId ");
            var parms = new Dictionary<string, object>();
            parms.Add("@doctorId", docId);
            var result = Query<DeptByLanguage>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public DeptByLanguage SelectByIdLanguage(int language, int deptId)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select ID, CN_NAME as NAME,LIST_IMAGE as ListImage,CN_DESC as `Desc`,ICON,CN_PACKAGE as Packages ");
            }
            else
            {
                selectSql.Append(@"select ID, EN_NAME as NAME,LIST_IMAGE  as ListImage,EN_DESC as `Desc`,ICON,EN_PACKAGE as Packages ");
            }
            selectSql.Append(@" from dic_dept where IS_DELETED = false ");
            if (deptId != 0)
            {
                selectSql.Append(@" and ID = @deptId ");
                parms.Add("@deptId", deptId);
            }
            selectSql.Append(@" order by SORT, SORT_INDEX ");
            var result = Query<DeptByLanguage>(selectSql.ToString(), parms);
            return result?.FirstOrDefault();
        }

        public List<DeptModel> SelectIndexDeptByLanguage(int language)
        {
            var selectSql = new StringBuilder();
            //var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select ID, CN_NAME as DeptName,Icon ");
            }
            else
            {
                selectSql.Append(@"select ID, EN_NAME as DeptName,Icon ");
            }
            selectSql.Append(@" from dic_dept where IS_DELETED = false and IS_SHOW_INDEX = true order by Sort");
            var result = Query<DeptModel>(selectSql.ToString(), null);
            return result?.ToList();
        }
        public List<DeptModel> GetCignaDepts(int language, string keyWords)
        {
            var selectSql = new StringBuilder();
            //var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select d.ID, CN_NAME as DeptName,Icon,sort ");
            }
            else
            {
                selectSql.Append(@"select d.ID, EN_NAME as DeptName,Icon,sort ");
            }
            selectSql.Append(@" from dic_dept d INNER JOIN dic_insurance_dept rd on d.ID = rd.DeptId where IS_DELETED = false and IsDelete=0 ");
            if (!string.IsNullOrEmpty(keyWords))
            {
                if (language == 1)
                {
                    selectSql.AppendFormat(@" and CN_NAME like '%{0}%'", keyWords);
                }
                else
                {
                    selectSql.AppendFormat(@" and EN_NAME like '%{0}%'", keyWords);
                }
            }
            selectSql.Append(@" order by Sort");
            var result = Query<DeptModel>(selectSql.ToString(), null);
            return result?.ToList();
        }
        public DeptModel GetDeptById(int id, int language)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            if (language == 1)
            {
                selectSql.Append(@"select ID,CN_NAME as DeptName,CN_DESC as 'Desc',CN_PACKAGE as Package,LIST_IMAGE as ListImage,ICON,SORT,CN_DOCTOR_DESC as DocDesc ");
            }
            else
            {
                selectSql.Append(@"select ID,EN_NAME as DeptName,EN_DESC as 'Desc',EN_PACKAGE as Package,LIST_IMAGE as ListImage,ICON,SORT,EN_DOCTOR_DESC as DocDesc ");
            }
            selectSql.Append(@" from dic_dept where ID = @ID");
            parms.Add("@ID", id);
            var result = Query<DeptModel>(selectSql.ToString(), parms);
            return result?.FirstOrDefault();
        }
        public Dic_Dept GetDeptById(int id)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select * from dic_dept where ID = @ID");
            parms.Add("@ID", id);
            var result = Query<Dic_Dept>(selectSql.ToString(), parms);
            return result?.FirstOrDefault();
        }

        public List<int> GetDeptIdIds(int doctorId)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@DocId", doctorId);
            var selectSql = @"select DISTINCT(APT_DEPT_ID) from dic_map_dept_doctor where APT_DOCTOR_ID = @DocId";
            var result = Query<int>(selectSql, parms);
            return result?.ToList();
        }

        public HisDeptServ GetHisServ(int deptId)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@deptId", deptId);
            var selectSql = @"select hd.APT_DEPT_ID as AptDeptId,hd.HIS_DEPT_ID as HisDeptId,ds.HIS_SERV_ID as HisServId from dic_map_his_dept hd INNER JOIN dic_map_dept_serv ds on hd.ID = ds.DEPT_MAP_ID where HIS_SERV_NAME ='15 Mins Consultation' and hd.IS_DELETED=0 and ds.IS_DELETED=0 and hd.APT_DEPT_ID = @deptId";
            var result = Query<HisDeptServ>(selectSql, parms);
            return result?.FirstOrDefault();
        }

        public List<Dic_Map_Dept_Doctor> GetHosAllDepts(int hosId)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@hosId", hosId);
            var selectSql = @"select * from dic_map_dept_doctor where IS_DELETED = 0 and APT_HOS_ID = @hosId";
            var result = Query<Dic_Map_Dept_Doctor>(selectSql, parms);
            return result?.ToList();
        }

        public List<Dic_Dept> SelectByDoctor(int doctorId)
        {
            var parms = new Dictionary<string, object>();
            parms.Add("@doctorId", doctorId);
            var selectSql = @"select distinct t1.ID,
                        t1.PARENT_ID,
                        t1.CN_NAME,
                        t1.EN_NAME,
                        t1.SORT,
                        t1.SORT_INDEX,
                        t1.CREATE_USER,
                        t1.CREATE_TIME,
                        t1.UPDATE_USER,
                        t1.UPDATE_TIME,
                        t1.IS_DELETED,
                        -- t1.CN_DESC,
                        -- t1.EN_DESC,
                        t1.CN_DOCTOR_DESC,
                        t1.EN_DOCTOR_DESC,
                        t1.CN_PACKAGE,
                        t1.EN_PACKAGE,
                        t1.HOME_IMAGE,
                        t1.LIST_IMAGE,
                        t1.ICON,
                        t1.CN_ADDR,
                        t1.EN_ADDR
        from dic_dept t1,
             dic_map_dept_doctor t2
        where t1.IS_DELETED = false
          and t1.DEPT_TYPE = 0
          AND t1.ID = t2.APT_DEPT_ID
          AND t2.IS_DELETED = false
          AND t2.APT_DOCTOR_ID = @doctorId";
            var result = Query<Dic_Dept>(selectSql, parms);
            return result?.ToList();
        }

        public List<Dic_Dept> GetAllDept()
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select id,cn_name from dic_dept where IS_DELETED = 0");
            //parms.Add("@ID", id);
            var result = Query<Dic_Dept>(selectSql.ToString(), parms);
            return result?.ToList();
        }
        public List<Dic_Map_His_Dept> GetHisAllDept()
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"SELECT   ID,APT_DEPT_ID AS AptDeptID,
				                        HIS_HOS_ID AS HisHosID,
				                        HIS_DEPT_ID AS HisDeptID,
				                        HIS_DEPT_CODE AS HisDeptCode,
				                        APT_TYPE AS AptType,
				                        IS_DELETED AS ISDeleted,
				                        HIS_HOS_NAME AS HisHosName, 
				                        HIS_DEPT_NAME AS HisDeptName
                                FROM dic_map_his_dept where IS_DELETED = 0");
            //parms.Add("@ID", id);
            var result = Query<Dic_Map_His_Dept>(selectSql.ToString(), parms);
            return result?.ToList();
        }
    }
}
