
using New_College.IServices;
using New_College.Model.Models;
using New_College.Services.BASE;
using New_College.IRepository.Base;
using New_College.Model.ViewModels;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Linq;
using New_College.Common;

namespace New_College.Services
{
    public class SysRegionServices : BaseServices<SysRegion>, ISysRegionServices
    {
        private readonly IBaseRepository<SysRegion> _dal;
        public SysRegionServices(IBaseRepository<SysRegion> dal)
        {
            this._dal = dal;
            base.BaseDal = dal;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Caching(AbsoluteExpiration = 10)]
        public async Task<List<SysRegion>> GetListByParentId(string id)
        {
            return await this.Query(w => w.ParentCode == id);
        }

        /// <summary>
        /// 获取省市区
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public async Task<List<SysRegionResult>> GetRegionList(SysRegionQuery query)
        {
            if (query.Code == "0")
                query.Code = "100000";
            var info = await _dal.Query(x => x.ParentCode == query.Code);
            if (info == null)
                return new List<SysRegionResult>() { };
            return info.Select(x => new SysRegionResult()
            {
                Code = x.RegionCode,
                Name = x.RegionName,
                Id = x.KeyId
            }).ToList();
        }
    }
}