using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.ContractModel.App.Model;
using ASHermed.SinoInterface.Api.ContractModel.Basicdata.Entiey;
using ASHermed.SinoInterface.Api.IRepository.App;
using ASHermed.SinoInterface.Repository.MySql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.Api.MySqlRepository.App
{
    public class CnBaseDistrictRepository : BaseRepositoryToASHermed_SinoInterface<Cn_Base_District>, ICnBaseDistrictRepository
    {
        /// <summary>
        /// 获取所有的省份
        /// language：1 中文，2英文
        /// </summary>
        /// <param name="language"></param>
        /// <returns></returns>
        public async Task<List<ProvinceEntity>> GetProvincesByLanguage(int language)
        {
            string sqlStr = string.Empty;
            if (language == 1)
            {
                sqlStr = @"select district_id ID, district_name as Name from cn_base_district where district_parent_id=0 order by district_name";
            }
            else
            {
                sqlStr = @"select district_id ID, district_name_en as Name from cn_base_district where district_parent_id=0 order by district_name_en";
            }
            var result = await QueryAsync<ProvinceEntity>(sqlStr, null);
            return result?.ToList();
        }

        public ProvinceEntity GetProvincesByCityId(int cityId)
        {
            string sqlStr = @"select district_parent_id ID from cn_base_district where district_id=@cityID ";

            var result = Query<ProvinceEntity>(sqlStr, new { cityID = cityId });
            return result?.FirstOrDefault();
        }

        /// <summary>
        /// 通过省份来获取城市
        /// </summary>
        /// <param name="provinceId"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        public async Task<List<CityEntity>> GetCitysByPIdAndLanguage(int provinceId, int language)
        {
            var parms = new Dictionary<string, object>();
            string sqlStr = string.Empty;
            if (language == 1)
            {
                sqlStr = @"select district_id ID, district_name as Name from cn_base_district where district_parent_id=@PId order by district_name";
            }
            else
            {
                sqlStr = @"select district_id ID, district_name_en as Name from cn_base_district where district_parent_id=@PId order by district_name_en";
            }
            parms.Add("@PId", provinceId);
            var result = await QueryAsync<CityEntity>(sqlStr, parms);
            return result?.ToList();
        }

        /// <summary>
        /// 根据城市来获取区或县
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        public async Task<List<AreasEntity>> GetAreaByCityIdAndLanguage(int cityId, int language)
        {
            var parms = new Dictionary<string, object>();
            string sqlStr = string.Empty;
            if (language == 1)
            {
                sqlStr = @"select district_id ID, district_name as Name from cn_base_district where district_parent_id=@PId order by district_name";
            }
            else
            {
                sqlStr = @"select district_id ID, district_name_en as Name from cn_base_district where district_parent_id=@PId order by district_name_en";
            }
            parms.Add("@PId", cityId);
            var result = await QueryAsync<AreasEntity>(sqlStr, parms);
            return result?.ToList();
        }


        public List<Dic_Nationality_Tc> GetTcNationAll(int id = 0)
        {
            var parms = new Dictionary<string, object>();
            string sqlStr = @"select * from dic_nationality_tc";
            if (id > 0)
            {
                sqlStr += " where id = @id";
                parms.Add("@id", id);
            }
            var result = Query<Dic_Nationality_Tc>(sqlStr, parms);
            return result?.ToList();
        }

        public async Task<Dic_Nationality_Tc> GetNationalityByNameOrCodeAsync(string name, string code)
        {
            const string sql = "select * from dic_nationality_tc where `Name`=@name or TcCode=@code ";

            var parms=new Dictionary<string,object>
            {
                {"@name",name},
                {"@code",code}
            };

            var result = await QueryAsync<Dic_Nationality_Tc>(sql, parms);

            return result.FirstOrDefault();
        }

        public CityEntity GetCityById(int cityId, int language)
        {
            var parms = new Dictionary<string, object>();
            string sqlStr = string.Empty;
            if (language == 1)
            {
                sqlStr = @"select district_id ID, district_name as Name from cn_base_district where district_id = @district_id";
            }
            else
            {
                sqlStr = @"select district_id ID, district_name_en as Name from cn_base_district where district_id = @district_id";
            }
            parms.Add("@district_id", cityId);
            var result = Query<CityEntity>(sqlStr, parms);
            return result?.FirstOrDefault();
        }

    }
}
