using Edu.Model.Entity.System;
using Edu.Model.ViewModel.System;
using Edu.Repository.System;
using System.Collections.Generic;
using System.Linq;
using VT.FW.DB;

namespace Edu.Module.System
{
    /// <summary>
    /// 地区处理类
    /// </summary>
    public class DestinationModule
    {
        /// <summary>
        /// 地区仓储层对象
        /// </summary>
        private readonly RB_DestinationRepository destinationRepository = new RB_DestinationRepository();

        /// <summary>
        /// 获取地区分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="where"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<RB_Destination_ViewModel> GetDestinationPageModule(int pageIndex, int pageSize, RB_Destination_ViewModel where, out long count)
        {
            var list = destinationRepository.GetDestinationPageRepository(pageIndex, pageSize, where, out count);
            if (list != null && list.Count > 0)
            {
                string ids = string.Join(",", list.Select(qitem => qitem.ParentID));
                var detailsList = destinationRepository.GetDestinationListRepository(new RB_Destination_ViewModel()
                {
                    Ids = ids
                });
                foreach (var item in list)
                {
                    RB_Destination_ViewModel country = null;
                    RB_Destination_ViewModel provice = null;
                    RB_Destination_ViewModel city = null;
                    switch (item.CodeLevel)
                    {
                        //省份
                        case 2:
                            country = detailsList.Where(qitem => qitem.ID == item.ParentID)?.FirstOrDefault();
                            item.CountryName = country?.Name;
                            break;
                        //市
                        case 3:
                            provice = detailsList.Where(qitem => qitem.ID == item.ParentID)?.FirstOrDefault();
                            if (provice != null)
                            {
                                item.ProvinceName = provice?.Name;
                                country = destinationRepository.GetEntity<RB_Destination_ViewModel>((provice?.ParentID??0));
                                item.CountryName = country?.Name;
                            }
                            break;
                        //区
                        case 4:
                            city = detailsList.Where(qitem => qitem.ID == item.ParentID)?.FirstOrDefault();
                            item.CityName = city?.Name;
                            if (city != null)
                            {
                                provice = destinationRepository.GetEntity<RB_Destination_ViewModel>((city?.ParentID??0));
                                item.ProvinceName = provice?.Name;
                                country = destinationRepository.GetEntity<RB_Destination_ViewModel>((provice?.ParentID??0));
                                item.CountryName = country?.Name;
                            }
                            break;
                    }
                }
            }
            return list;
        }

        /// <summary>
        /// 省市区树形列表
        /// </summary>
        /// <returns></returns>
        public List<AreaTree_ViewModel> GetAreaTreeModule()
        {
            List<AreaTree_ViewModel> treeList = new List<AreaTree_ViewModel>();
            var list = destinationRepository.GetDestinationListRepository(new RB_Destination_ViewModel() { });
            if (list != null && list.Count > 0)
            {
                var firstList = list.Where(qitem => qitem.CodeLevel == 2).ToList();
                if (firstList != null && firstList.Count > 0)
                {
                    foreach (var fItem in firstList)
                    {
                        AreaTree_ViewModel fModel = new AreaTree_ViewModel()
                        {
                            ID = fItem.ID,
                            Name = fItem.Name,
                            ParentID = fItem.ParentID,
                            ChildList = new List<AreaTree_ViewModel>()
                        };
                        var secondList = list.Where(qitem => qitem.CodeLevel == 3 && qitem.ParentID == fItem.ID).ToList();
                        if (secondList != null && secondList.Count > 0)
                        {
                            foreach (var sItem in secondList)
                            {
                                AreaTree_ViewModel sModel = new AreaTree_ViewModel()
                                {
                                    ID = sItem.ID,
                                    Name = sItem.Name,
                                    ParentID = sItem.ParentID,
                                    ChildList = new List<AreaTree_ViewModel>()
                                };
                                var thirdList= list.Where(qitem => qitem.CodeLevel == 4 && qitem.ParentID == sItem.ID).ToList();
                                if (thirdList != null && thirdList.Count > 0)
                                {
                                    foreach (var tItem in thirdList)
                                    {
                                        sModel.ChildList.Add(new AreaTree_ViewModel()
                                        {
                                            ID = tItem.ID,
                                            Name = tItem.Name,
                                            ParentID = tItem.ParentID,
                                            ChildList = new List<AreaTree_ViewModel>()
                                        });
                                    }
                                }
                                fModel.ChildList.Add(sModel);
                            }
                        }
                        treeList.Add(fModel);
                    }
                }
            }
            return treeList;
        }

        /// <summary>
        /// 根据查询条件获取地区列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Destination_ViewModel> GetAreaListModule(RB_Destination_ViewModel query)
        {
            var list = destinationRepository.GetDestinationListRepository(query);
            return list;
        }

        /// <summary>
        /// 根据编号获取地区详情
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public RB_Destination_ViewModel GetDestinationModule(object Id)
        {
            var model = destinationRepository.GetEntity<RB_Destination_ViewModel>(Id);
            if (model != null && model.ID > 0)
            {
                RB_Destination_ViewModel country = null;
                RB_Destination_ViewModel provice = null;
                RB_Destination_ViewModel city = null;
                switch (model.CodeLevel)
                {
                    //省份
                    case 2:
                        country = destinationRepository.GetEntity<RB_Destination_ViewModel>(model.ParentID);
                        model.CountryName = country?.Name;
                        model.CountryId = country?.ID??0;
                        break;
                    //市
                    case 3:
                        provice = destinationRepository.GetEntity<RB_Destination_ViewModel>(model.ParentID);
                        model.ProvinceName = provice?.Name;
                        model.ProviceId = provice?.ID ?? 0;
                        country = destinationRepository.GetEntity<RB_Destination_ViewModel>(provice?.ParentID);
                        model.CountryName = country?.Name;
                        model.CountryId = country?.ID ?? 0;
                        break;
                    //区
                    case 4:
                        city = destinationRepository.GetEntity<RB_Destination_ViewModel>(model.ParentID);
                        model.CityName = city?.Name;
                        model.CityId = city?.ID ?? 0;
                        provice = destinationRepository.GetEntity<RB_Destination_ViewModel>(city?.ParentID);
                        model.ProvinceName = provice?.Name;
                        model.ProviceId = provice?.ID ?? 0;
                        country = destinationRepository.GetEntity<RB_Destination_ViewModel>(provice?.ParentID);
                        model.CountryName = country?.Name;
                        model.CountryId = country?.ID ?? 0;
                        break;
                }
             
            }
            return model;
        }

        /// <summary>
        /// 新增修改地区
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool SetDestinationModule(RB_Destination_ViewModel model)
        {
            bool flag = false;
            if (model.ID > 0)
            {
                Dictionary<string, object> fileds = new Dictionary<string, object>()
                {
                    { nameof(RB_Destination_ViewModel.Name),model.Name},
                    { nameof(RB_Destination_ViewModel.ParentID),model.ParentID},
                    { nameof(RB_Destination_ViewModel.CodeLevel),model.CodeLevel},
                };
                flag = destinationRepository.Update(fileds, new WhereHelper(nameof(RB_Destination_ViewModel.ID),model.ID));
            }
            else
            {
                var newId = destinationRepository.Insert(model);
                model.ID = newId;
                flag = newId > 0;
            }
            return flag;
        }

        /// <summary>
        /// 删除地区数据
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool RemoveAreaModule(int ID)
        {
            Dictionary<string, object> fileds = new Dictionary<string, object>()
            {
                { nameof(RB_Destination_ViewModel.Status),(int)Common.Enum.DateStateEnum.Delete}
            };
            return destinationRepository.Update(fileds, new WhereHelper(nameof(RB_Destination_ViewModel.ID), ID));
        }

    }
}