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
{
///
/// 地区处理类
///
public class DestinationModule
{
///
/// 地区仓储层对象
///
private readonly RB_DestinationRepository destinationRepository = new RB_DestinationRepository();
///
/// 获取地区分页列表
///
///
///
///
///
///
public List 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();
item.ProvinceName = provice?.Name;
country = destinationRepository.GetEntity(provice?.ParentID);
item.CountryName = country?.Name;
break;
//区
case 4:
city = detailsList.Where(qitem => qitem.ID == item.ParentID)?.FirstOrDefault();
item.CityName = city?.Name;
provice = destinationRepository.GetEntity(city?.ParentID);
item.ProvinceName = provice?.Name;
country = destinationRepository.GetEntity(provice?.ParentID);
item.CountryName = country?.Name;
break;
}
}
}
return list;
}
///
/// 省市区树形列表
///
///
public List GetAreaTreeModule()
{
List treeList = new List();
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()
};
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()
};
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()
});
}
}
fModel.ChildList.Add(sModel);
}
}
treeList.Add(fModel);
}
}
}
return treeList;
}
///
/// 根据查询条件获取地区列表
///
///
///
public List GetAreaListModule(RB_Destination_ViewModel query)
{
var list = destinationRepository.GetDestinationListRepository(query);
return list;
}
///
/// 根据编号获取地区详情
///
///
///
public RB_Destination_ViewModel GetDestinationModule(object Id)
{
var model = destinationRepository.GetEntity(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(model.ParentID);
model.CountryName = country?.Name;
model.CountryId = country?.ID??0;
break;
//市
case 3:
provice = destinationRepository.GetEntity(model.ParentID);
model.ProvinceName = provice?.Name;
model.ProviceId = provice?.ID ?? 0;
country = destinationRepository.GetEntity(provice?.ParentID);
model.CountryName = country?.Name;
model.CountryId = country?.ID ?? 0;
break;
//区
case 4:
city = destinationRepository.GetEntity(model.ParentID);
model.CityName = city?.Name;
model.CityId = city?.ID ?? 0;
provice = destinationRepository.GetEntity(city?.ParentID);
model.ProvinceName = provice?.Name;
model.ProviceId = provice?.ID ?? 0;
country = destinationRepository.GetEntity(provice?.ParentID);
model.CountryName = country?.Name;
model.CountryId = country?.ID ?? 0;
break;
}
}
return model;
}
///
/// 新增修改地区
///
///
///
public bool SetDestinationModule(RB_Destination_ViewModel model)
{
bool flag = false;
if (model.ID > 0)
{
Dictionary fileds = new Dictionary()
{
{ 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;
}
///
/// 删除地区数据
///
///
///
public bool RemoveAreaModule(int ID)
{
Dictionary fileds = new Dictionary()
{
{ nameof(RB_Destination_ViewModel.Status),(int)Common.Enum.DateStateEnum.Delete}
};
return destinationRepository.Update(fileds, new WhereHelper(nameof(RB_Destination_ViewModel.ID), ID));
}
}
}