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 DicHosRepository : BaseRepositoryToASHermed_SinoInterface<Dic_Hos>, IDicHosRepository
    {

        public List<HosByLanguage> SelectListByLanguage(int language, int useOccasionType)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select DISTINCT a.ID,a.CN_ABBREVIATED_ADDR as NAME,a.CN_ADDR as ADDR,PICTURE,CN_MOBILE as
            MOBILE,LONGITUDE,LATITUDE,CN_CITY as CITY, SORT ");
            }
            else
            {
                selectSql.Append(@"select DISTINCT a.ID,a.EN_ABBREVIATED_ADDR as NAME,a.EN_ADDR as ADDR,PICTURE,EN_MOBILE as
            MOBILE,LONGITUDE,LATITUDE,EN_CITY as CITY, SORT ");
            }
            selectSql.Append(@" from dic_hos a
                                    JOIN dic_map_hos_dept b
                                    ON a.ID = b.APT_HOS_ID and b.IS_DELETED = false and b.USE_OCCASION=@useOccasion
                                    where a.IS_DELETED = false");
            var parms = new Dictionary<string, object>();
            parms.Add("@useOccasion", useOccasionType);
            var result = Query<HosByLanguage>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public HosByLanguage SelectById(int id, int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select DISTINCT a.ID,a.CN_ABBREVIATED_ADDR as NAME,a.CN_ADDR as ADDR,PICTURE,CN_MOBILE as
            MOBILE,LONGITUDE,LATITUDE,CN_CITY as CITY ");
            }
            else
            {
                selectSql.Append(@"select DISTINCT a.ID,a.EN_ABBREVIATED_ADDR as NAME,a.EN_ADDR as ADDR,PICTURE,EN_MOBILE as
            MOBILE,LONGITUDE,LATITUDE,EN_CITY as CITY ");
            }
            selectSql.Append(@" from dic_hos a
                                    JOIN dic_map_hos_dept b
                                    ON a.ID = b.APT_HOS_ID and b.IS_DELETED = false and b.USE_OCCASION=0
                                    where a.ID=@ID");
            var parms = new Dictionary<string, object>();
            parms.Add("@ID", id);
            var result = Query<HosByLanguage>(selectSql.ToString(), parms);
            return result.FirstOrDefault();
        }

        public List<HosInfoByLanguage> SelectHosWithLanguage(int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"SELECT ID,PICTURE,HIS_HOS_CODE as HISHOSCODE,CN_NAME AS HNAME,CN_ADDR AS ADDR,CN_MOBILE AS MOBILE,
            CN_WORK_TIME AS WORK_TIME,CN_DESC AS HDESC,CN_MAP_IMAGE AS MAP_IMAGE,CN_CITY as CITY,
            CN_ABBREVIATED_ADDR as ShortName  ");
            }
            else
            {
                selectSql.Append(@"SELECT ID,PICTURE,HIS_HOS_CODE as HISHOSCODE,EN_NAME AS HNAME,EN_ADDR AS ADDR,EN_MOBILE AS MOBILE,
            EN_WORK_TIME AS WORK_TIME,EN_DESC AS HDESC,EN_MAP_IMAGE AS MAP_IMAGE,EN_CITY as CITY,
            EN_ABBREVIATED_ADDR as ShortName  ");
            }
            selectSql.Append(@" FROM dic_hos where IS_DELETED = false");
            var result = Query<HosInfoByLanguage>(selectSql.ToString(), null);
            return result?.ToList();
        }

        public HosInfoByLanguage SelectHosByIdWithLanguage(int hosId, int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"SELECT ID,PICTURE,LONGITUDE,LATITUDE,HIS_HOS_CODE as HISHOSCODE,CN_NAME AS HNAME,CN_ADDR AS ADDR,CN_MOBILE AS MOBILE,
            CN_WORK_TIME AS WORKTIME,CN_DESC AS HDESC,CN_MAP_IMAGE AS MAPIMAGE,CN_ABBREVIATED_ADDR as ShortName,P_ADDRESS_CN as ParkAddress,P_ADDRESS_URL as ParkAddressUrl,P_ADDRESS_LON as ParkLongitude,P_ADDRESS_LAT as ParkLatitude,P_REMARK_CN as ParkRemark,P_PIC_CN as ParkPic,P_COUNT as PCount ");
            }
            else
            {
                selectSql.Append(@"SELECT ID,PICTURE,LONGITUDE,LATITUDE,HIS_HOS_CODE as HISHOSCODE,EN_NAME AS HNAME,EN_ADDR AS ADDR,EN_MOBILE AS MOBILE,
            EN_WORK_TIME AS WORKTIME,EN_DESC AS HDESC,EN_MAP_IMAGE AS MAPIMAGE,EN_ABBREVIATED_ADDR as ShortName,P_ADDRESS_EN as ParkAddress,P_ADDRESS_URL as ParkAddressUrl,P_ADDRESS_LON as ParkLongitude,P_ADDRESS_LAT as ParkLatitude,P_REMARK_EN as ParkRemark,P_PIC_EN as ParkPic,P_COUNT as PCount  ");
            }
            selectSql.Append(@" FROM dic_hos
                                        where ID = @hosId
                                        and IS_DELETED = false");
            var parms = new Dictionary<string, object>();
            parms.Add("@hosId", hosId);
            var result = Query<HosInfoByLanguage>(selectSql.ToString(), parms);
            return result?.FirstOrDefault();
        }

        public List<HosByLanguage> SelectByDoctorByLanguage(int id, int language, int type)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select distinct t1.ID,t1.CN_ABBREVIATED_ADDR AS NAME,t1.CN_CITY as CITY ");
            }
            else
            {
                selectSql.Append(@"select distinct t1.ID,t1.EN_ABBREVIATED_ADDR AS NAME,t1.EN_CITY as CITY ");
            }
            selectSql.Append(@" from dic_hos t1, dic_map_dept_doctor t2
                                    where t1.IS_DELETED = false AND t1.ID = t2.APT_HOS_ID AND t2.IS_DELETED = false
                                    AND t2.USE_OCCASION=@useOccasion
                                    AND t2.APT_DOCTOR_ID = @doctorId");
            var parms = new Dictionary<string, object>();
            parms.Add("@useOccasion", type);
            parms.Add("@doctorId", id);
            var result = Query<HosByLanguage>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<HosByLanguage> SelectByDeptLanguage(int deptId, int language, int type)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select a.ID,a.CN_ABBREVIATED_ADDR as NAME,LONGITUDE,LATITUDE,CN_CITY as CITY ");
            }
            else
            {
                selectSql.Append(@"select a.ID,a.EN_ABBREVIATED_ADDR as NAME,LONGITUDE,LATITUDE,EN_CITY as CITY ");
            }
            selectSql.Append(@" from dic_hos a
                                        JOIN dic_map_hos_dept b
                                        ON a.ID = b.APT_HOS_ID and b.IS_DELETED = false and b.USE_OCCASION=@useOccasion
                                        where a.IS_DELETED = false
                                        and b.APT_DEPT_ID = @deptId");
            var parms = new Dictionary<string, object>();
            parms.Add("@useOccasion", type);
            parms.Add("@deptId", deptId);
            var result = Query<HosByLanguage>(selectSql.ToString(), parms);
            return result?.ToList();
        }

        public List<HosByCity> SelectCity(int language)
        {
            var selectSql = new StringBuilder();
            if (language == 1)
            {
                selectSql.Append(@"select DISTINCT CN_CITY as CITY ");
            }
            else
            {
                selectSql.Append(@"select DISTINCT EN_CITY as CITY ");
            }
            selectSql.Append(@" from dic_hos
        where IS_DELETED = 0");
            var parms = new Dictionary<string, object>();
            //parms.Add("@useOccasion", type);
            //parms.Add("@deptId", deptId);
            var result = Query<HosByCity>(selectSql.ToString(), parms);
            return result?.ToList();

        }

        public List<Dic_Hos> SelectByDoctor(int doctorId, int type, int cityId)
        {
            var selectSql = new StringBuilder();
            var parms = new Dictionary<string, object>();
            selectSql.Append(@"select distinct t1.ID,
        t1.HIS_HOS_ID,
        t1.HIS_HOS_CODE,
        t1.LONGITUDE,
        t1.LATITUDE,
        t1.MAP_URL,
        t1.CN_NAME,
        t1.EN_NAME,
        t1.CN_ADDR,
        t1.EN_ADDR,
        t1.CN_MOBILE,
        t1.EN_MOBILE,
        t1.CN_WORK_TIME,
        t1.EN_WORK_TIME,
        t1.SORT,
        t1.SORT_INDEX,
        t1.CREATE_USER,
        t1.CREATE_TIME,
        t1.UPDATE_USER,
        UPDATE_TIME,
        t1.IS_DELETED,
        t1.PICTURE,
        t1.CN_DESC,
        t1.EN_DESC,
        t1.CN_MAP_IMAGE,
        t1.EN_MAP_IMAGE,
        t1.CN_PARKING_INFO,
        t1.EN_PARKING_INFO,
        t1.CN_ABBREVIATED_ADDR,
        t1.EN_ABBREVIATED_ADDR,
        t1.CN_SMS_ADDR,
        t1.EN_SMS_ADDR,
        t1.IS_NA,
        t1.NA_CN_WORK_TIME,
        t1.NA_EN_WORK_TIME,
        t1.CN_CITY,
        t1.EN_CITY
        from dic_hos t1,
        dic_map_dept_doctor t2
        where t1.IS_DELETED = false
        AND t1.ID = t2.APT_HOS_ID
        AND t2.IS_DELETED = false
        AND t2.USE_OCCASION = @useOccasion
        AND t2.APT_DOCTOR_ID = @doctorId ");
            if (cityId != 0)
            {
                selectSql.Append(@" and t1.CITY_ID = @cityId ");
                parms.Add("@cityId", cityId);
            }

            parms.Add("@useOccasion", type);
            parms.Add("@doctorId", doctorId);

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

        public Dic_Hos GetHosByHosCode(string code)
        {
            var sql = @" select * from  dic_hos where HIS_HOS_CODE = @HisHosCode ";
            return Query<Dic_Hos>(sql, new { HisHosCode = code })?.FirstOrDefault();
        }

        public Dic_Hos SelectByHisCode(string hosCode)
        {
            var sql = @" select *
                                    from dic_hos
                                    where IS_DELETED = false
                                      and HIS_HOS_CODE= @HisHosCode ";
            return Query<Dic_Hos>(sql, new { HisHosCode = hosCode })?.FirstOrDefault();
        }

        public Dic_Hos SelectByPrimaryKey(int hosId)
        {
            var sql = @" select *
                            from dic_hos
                            where IS_DELETED = false
                              and id= @id ";
            return Query<Dic_Hos>(sql, new { id = hosId })?.FirstOrDefault();
        }

        public async Task<List<HosInfo>> GetHosList(string cityName, int language)
        {
            var sql = @" select ";
            if (language == 1)
            {
                sql += @" CN_ABBREVIATED_ADDR as HosName,PICTURE as HosPic,CN_ADDR as HosAddress,CN_WORK_TIME as HosWorkTime,";
            }
            else
            {
                sql += @" EN_ABBREVIATED_ADDR as HosName,PICTURE as HosPic,EN_ADDR as HosAddress,EN_WORK_TIME as HosWorkTime,";
            }
            sql += @" LONGITUDE as HosLongitude,LATITUDE as HosLatitude,Sort,Id from dic_hos where IS_DELETED = 0 "; 
            if (!string.IsNullOrEmpty(cityName))
            {
                sql += @" and CN_CITY = @CN_CITY";
            }
            sql += @" order by Sort";
            return QueryAsync<HosInfo>(sql, new { CN_CITY = cityName }).Result?.ToList();
        }

        public List<Dic_Hos> GetAllHosCity()
        {
            var sql = @" select CN_CITY,EN_CITY from  dic_hos where IS_DELETED = 0 ";
            return Query<Dic_Hos>(sql, null).ToList();
        }

        public List<Dic_Map_Dept_Doctor> GetForDept(int deptId)
        {
            var sql = @" SELECT
	                            APT_HOS_ID,
	                            APT_DEPT_ID
                            FROM
	                            dic_map_hos_dept a
	                            INNER JOIN dic_hos b ON a.APT_HOS_ID = b.ID 
	                            AND b.IS_DELETED = FALSE 
	                            INNER JOIN dic_dept c ON c.ID = a.APT_DEPT_ID AND c.IS_DELETED = FALSE 
                            WHERE
	                            a.APT_DEPT_ID = @DeptId 
	                            AND a.IS_DELETED = FALSE 
                            GROUP BY
	                            APT_HOS_ID,
	                            APT_DEPT_ID ";
            return Query<Dic_Map_Dept_Doctor>(sql, new { DeptId = deptId }).ToList();
        }
    }
}
