using ASHermed.SinoInterface.Api.ContractModel.Basicdata.Entiey;
using ASHermed.SinoInterface.Api.IService.Basicdata;
using ASHermed.SinoInterface.ContractModel;
using PoJun.Util.Webs.Clients;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using ASHermed.SinoInterface.Tools;
using ASHermed.SinoInterface.IFramework;

namespace ASHermed.SinoInterface.Api.Service.Basicdata
{
    /// <summary>
    /// 基础数据查询 -- 中台
    /// </summary>
    public class BasicDataService : IBasicDataService
    {
        #region 初始化
        private readonly AppConfigurtaion _appConfigurtaion;
        private readonly IRbAptService _rbAptService;
        /// <summary>
        /// 初始化
        /// </summary>
        public BasicDataService(IHttpClientFactory clientFactory, IRbAptService rbAptService, AppConfigurtaion appConfigurtaion)
        {
            _appConfigurtaion = appConfigurtaion;
            _rbAptService = rbAptService;
        }
        #endregion

        /// <summary>
        /// 查询省份信息
        /// </summary>
        /// <returns></returns>
        public BaseResponse<dynamic> GetProvincesAll()
        {
            var resu = new BaseResponse<dynamic>();
            var apiName = "api/v1/Province/GetProvincesAll";
            var url = _appConfigurtaion.GetBasicdataURL;// "http://180.169.186.154:9092/";
            Dictionary<string, string> Dictionary = new Dictionary<string, string>();
            var _CityList = _rbAptService.HttpGetAsync<BaseResponse<ProvinceList>>(url, apiName, Dictionary).Result;
            if (_CityList != null && _CityList.Data != null && _CityList.Data.Provinces != null && _CityList.Data.Provinces.Count() > 0)
            {
                resu.Code = 1;
                resu.Msg = "成功";
                resu.Data = _CityList.Data.Provinces.Select(x => new { text = x.Name, value = x.ID, label = x.Name }).ToList();
            }
            else
            {
                resu.Code = 0;
                resu.Msg = "未找到数据";
            }
            return resu;
        }

        /// <summary>
        /// 通过省份Id查询城市信息
        /// </summary>
        /// <param name="provinceId">省份Id</param>
        /// <returns></returns>
        public BaseResponse<dynamic> GetCitysByProvinceId(int provinceId)
        {
            var resu = new BaseResponse<dynamic>();
            var apiName = "api/v1/City/GetCitysByProvinceId";
            var url = _appConfigurtaion.GetBasicdataURL;
            Dictionary<string, string> Dictionary = new Dictionary<string, string>();
            Dictionary.Add("provinceId", provinceId.ToString());
            var _CityList = _rbAptService.HttpPostAsync<BaseResponse<CityList>>(url, apiName, Dictionary).Result;
            if (_CityList != null && _CityList.Data != null && _CityList.Data.Citys != null && _CityList.Data.Citys.Count() > 0)
            {
                resu.Code = 1;
                resu.Msg = "成功";
                resu.Data = _CityList.Data.Citys.Select(x => new { text = x.Name, value = x.ID, label = x.Name }).ToList();
            }
            else
            {
                resu.Code = 0;
                resu.Msg = "未找到数据";
            }
            return resu;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public BaseResponse<dynamic> GetAreaByCityId(int cityId)
        {
            var resu = new BaseResponse<dynamic>();
            var apiName = "api/v1/City/GetAreas";
            var url = _appConfigurtaion.GetBasicdataURL;
            Dictionary<string, string> Dictionary = new Dictionary<string, string>();
            Dictionary.Add("cityId", cityId.ToString());
            var area = _rbAptService.HttpPostAsync<BaseResponse<AreaList>>(url, apiName, Dictionary).Result;
            if (area != null && area.Data != null && area.Data.Areas != null && area.Data.Areas.Count() > 0)
            {
                resu.Code = 1;
                resu.Msg = "成功";
                resu.Data = area.Data.Areas.Select(x => new { text = x.Name, value = x.Id, label = x.Name }).ToList();
            }
            else
            {
                resu.Code = 0;
                resu.Msg = "未找到数据";
            }
            return resu;
        }

   


    }
}
