using ASHermed.SinoInterface.Api.ContractModel.App;
using ASHermed.SinoInterface.Api.ContractModel.App.Model;
using ASHermed.SinoInterface.Api.IRepository.App;
using ASHermed.SinoInterface.Api.IService.Basicdata;
using ASHermed.SinoInterface.Code;
using ASHermed.SinoInterface.ContractModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ASHermed.SinoInterface.Api.Service.Basicdata
{
    /// <summary>
    /// 
    /// </summary>
    public class ProvincesService : IProvincesService
    {
        #region 初始化

        /// <summary>
        /// 省市区表[仓储层]
        /// </summary>
        private readonly ICnBaseDistrictRepository cnBaseDistrictRepository;

        /// <summary>
        /// 省市区服务
        /// </summary>
        /// <param name="_cnBaseDistrictRepository"></param>
        public ProvincesService(ICnBaseDistrictRepository _cnBaseDistrictRepository)
        {
            cnBaseDistrictRepository = _cnBaseDistrictRepository;
        }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        /// <param name="cityId"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        public async Task<BaseResponse<dynamic>> GetAreaByCityId(int cityId, int language)
        {
            BaseResponse<dynamic> baseResponse = new BaseResponse<dynamic>();
            var areas = await cnBaseDistrictRepository.GetAreaByCityIdAndLanguage(cityId, language);
            if (areas == null || areas.Count == 0)
            {
                baseResponse.Code = 0;
                baseResponse.Msg = "暂无数据";
                return baseResponse;
            }
            if (baseResponse.Data == null)
                baseResponse.Data = new GetAreasModel();

            baseResponse.Code = 1;
            baseResponse.Msg = "成功";
            baseResponse.Data = areas.Select(x => new { text = x.Name, value = x.ID, label = x.Name }).ToList();
            return baseResponse;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="provinceId"></param>
        /// <param name="language"></param>
        /// <returns></returns>
        public async Task<BaseResponse<dynamic>> GetCitysByProvinceId(int provinceId, int language)
        {
            BaseResponse<dynamic> baseResponse = new BaseResponse<dynamic>();
            var citys = await cnBaseDistrictRepository.GetCitysByPIdAndLanguage(provinceId, language);
            if (citys == null || citys.Count == 0)
            {
                baseResponse.Code = 0;
                baseResponse.Msg = "暂无数据";
                return baseResponse;
            }
            if (baseResponse.Data == null)
                baseResponse.Data = new GetCitysModel();

            baseResponse.Code = 1;
            baseResponse.Msg = "成功";
            baseResponse.Data = citys.Select(x => new { text = x.Name, value = x.ID, label = x.Name }).ToList();
            return baseResponse;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="language"></param>
        /// <returns></returns>
        public async Task<BaseResponse<dynamic>> GetProvincesAll(int language)
        {
            BaseResponse<dynamic> baseResponse = new BaseResponse<dynamic>();
            var provinces = await cnBaseDistrictRepository.GetProvincesByLanguage(language);
            if (provinces == null || provinces.Count == 0)
            {
                baseResponse.Code = 0;
                baseResponse.Msg = "暂无数据";
                return baseResponse;
            }
            if (baseResponse.Data == null)
                baseResponse.Data = new GetProvincesModel();

            baseResponse.Code = 1;
            baseResponse.Msg = "成功";
            baseResponse.Data = provinces.Select(x => new { text = x.Name, value = x.ID, label = x.Name }).ToList();
            return baseResponse;
        }

        public List<Dic_Nationality_Tc> GetTcNationAll()
        {
            return cnBaseDistrictRepository.GetTcNationAll();
        }
    }
}
