You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

279 lines
10 KiB

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using Znyc.Cloudcar.Admin.Commons.Cache;
using Znyc.Cloudcar.Admin.Commons.Json;
using Znyc.Cloudcar.Admin.Commons.Mapping;
using Znyc.Cloudcar.Admin.Commons.Tree;
using Znyc.Cloudcar.Admin.Security.Dtos;
using Znyc.Cloudcar.Admin.Security.Entitys;
using Znyc.Cloudcar.Admin.Security.IRepositories;
using Znyc.Cloudcar.Admin.Security.Repositories;
namespace Znyc.Cloudcar.Admin.Security.Application
{
/// <summary>
/// 地区
/// </summary>
public class AreaApp
{
private readonly IAreaRepository service = new AreaRepository();
#region 适配于管理后端
/// <summary>
/// 树形展开treeview需要,数据字典管理页面
/// </summary>
/// <returns></returns>
public List<TreeViewModel> ItemsTreeViewJson()
{
List<TreeViewModel> list = new List<TreeViewModel>();
List<AreaEntity> listFunction = service.GetListWhere("Layers in (0,1,2)").OrderBy(t => t.SortCode).ToList();
list = TreeViewJson(listFunction, 0);
return list;
}
//public List<TreeViewModel> AreaTreeViewJson()
//{
//string where = "1=1 and Layers in(0,1,2)";
//bool order = orderByDir == "asc" ? false : true;
//if (!string.IsNullOrEmpty(keywords))
//{
// where += string.Format(" and (FullName like '%{0}%' or EnCode like '%{0}%')", keywords);
//}
//List<AreaOutputDto> list = _service.GetListWhere(where).OrderBy(t => t.SortCode).ToList().MapTo<AreaOutputDto>();
//return ToJsonContent(list);
//List<TreeViewModel> list = new List<TreeViewModel>();
//List<Area> listFunction = service.GetListWhere("Layers in (3,4)").OrderBy(t => t.SortCode).ToList();
//list = TreeViewJson(listFunction, "");
//return list;
//}
/// <summary>
/// </summary>
/// <param name="data"></param>
/// <param name="ParentId"></param>
/// <returns></returns>
public List<TreeViewModel> TreeViewJson(List<AreaEntity> data, long ParentId)
{
List<TreeViewModel> list = new List<TreeViewModel>();
List<AreaEntity> ChildNodeList = data.FindAll(t => t.ParentId == ParentId).ToList();
foreach (AreaEntity entity in ChildNodeList)
{
TreeViewModel treeViewModel = new TreeViewModel
{
nodeId = entity.Id,
pid = entity.ParentId,
text = entity.FullName,
nodes = ChildrenTreeViewList(data, entity.Id),
tags = entity.Id
};
list.Add(treeViewModel);
}
return list;
}
/// <summary>
/// </summary>
/// <param name="data"></param>
/// <param name="ParentId"></param>
/// <returns></returns>
public List<TreeViewModel> ChildrenTreeViewList(List<AreaEntity> data, long ParentId)
{
List<TreeViewModel> listChildren = new List<TreeViewModel>();
List<AreaEntity> ChildNodeList = data.FindAll(t => t.ParentId == ParentId).ToList();
foreach (AreaEntity entity in ChildNodeList)
{
TreeViewModel treeViewModel = new TreeViewModel
{
nodeId = entity.Id,
pid = entity.ParentId,
text = entity.FullName,
nodes = ChildrenTreeViewList(data, entity.Id),
tags = entity.Id
};
listChildren.Add(treeViewModel);
}
return listChildren;
}
#endregion 适配于管理后端
#region 用于uniapp下拉选项
/// <summary>
/// 获取所有可用的地区,用于uniapp下拉选项
/// </summary>
/// <returns></returns>
public List<AreaPickerOutputDto> GetAllByEnable()
{
List<AreaPickerOutputDto> list = new List<AreaPickerOutputDto>();
CacheHelper cacheHelper = new CacheHelper();
list = JsonConvert.DeserializeObject<List<AreaPickerOutputDto>>(cacheHelper.Get("Area_Enable_Uniapp")
.ToJson());
if (list == null || list.Count <= 0)
{
List<AreaEntity> listFunction = service.GetAllByIsNotDeleteAndEnabledMark("Layers in (0,1,2)")
.OrderBy(t => t.SortCode).ToList();
list = UniappViewJson(listFunction, 0);
cacheHelper.Add("Area_Enable_Uniapp", list);
}
return list;
}
/// <summary>
/// 获取省、市、县/区三级可用的地区,用于uniapp下拉选项
/// </summary>
/// <returns></returns>
public List<AreaPickerOutputDto> GetProvinceToAreaByEnable()
{
List<AreaPickerOutputDto> list = new List<AreaPickerOutputDto>();
CacheHelper cacheHelper = new CacheHelper();
list = JsonConvert.DeserializeObject<List<AreaPickerOutputDto>>(cacheHelper
.Get("Area_ProvinceToArea_Enable_Uniapp").ToJson());
if (list == null || list.Count <= 0)
{
List<AreaEntity> listFunctionTemp = service.GetAllByIsNotDeleteAndEnabledMark("Layers in (1,2,3)").OrderBy(t => t.Id)
.ToList();
List<AreaEntity> listFunction = new List<AreaEntity>();
foreach (AreaEntity item in listFunctionTemp)
{
if (item.Layers == 1)
{
item.ParentId = 0;
}
listFunction.Add(item);
}
list = UniappViewJson(listFunction, 0);
cacheHelper.Add("Area_ProvinceToArea_Enable_Uniapp", list);
}
return list;
}
/// <summary>
/// </summary>
/// <param name="data"></param>
/// <param name="ParentId"></param>
/// <returns></returns>
public List<AreaPickerOutputDto> UniappViewJson(List<AreaEntity> data, long ParentId)
{
List<AreaPickerOutputDto> list = new List<AreaPickerOutputDto>();
List<AreaEntity> ChildNodeList = data.FindAll(t => t.ParentId == ParentId).ToList();
foreach (AreaEntity entity in ChildNodeList)
{
AreaPickerOutputDto treeViewModel = new AreaPickerOutputDto
{
value = entity.Id,
label = entity.FullName,
children = ChildrenUniappViewList(data, entity.Id)
};
list.Add(treeViewModel);
}
return list;
}
/// <summary>
/// </summary>
/// <param name="data"></param>
/// <param name="ParentId"></param>
/// <returns></returns>
public List<AreaPickerOutputDto> ChildrenUniappViewList(List<AreaEntity> data, long ParentId)
{
List<AreaPickerOutputDto> listChildren = new List<AreaPickerOutputDto>();
List<AreaEntity> ChildNodeList = data.FindAll(t => t.ParentId == ParentId).ToList();
foreach (AreaEntity entity in ChildNodeList)
{
AreaPickerOutputDto treeViewModel = new AreaPickerOutputDto
{
value = entity.Id,
label = entity.FullName,
children = ChildrenUniappViewList(data, entity.Id)
};
listChildren.Add(treeViewModel);
}
return listChildren;
}
#endregion 用于uniapp下拉选项
#region 适用于select2省市县区级联选择
/// <summary>
/// 获取省可用的地区,用于select2下拉选项
/// </summary>
/// <returns></returns>
public List<AreaSelect2OutDto> GetProvinceAll()
{
List<AreaSelect2OutDto> list = new List<AreaSelect2OutDto>();
CacheHelper cacheHelper = new CacheHelper();
list = JsonConvert.DeserializeObject<List<AreaSelect2OutDto>>(cacheHelper
.Get("Area_ProvinceToArea_Select2").ToJson());
if (list == null || list.Count <= 0)
{
list = service.GetAllByIsNotDeleteAndEnabledMark("Layers =1").OrderBy(t => t.Id).ToList()
.MapTo<AreaSelect2OutDto>();
cacheHelper.Add("Area_ProvinceToArea_Select2", list);
}
return list;
}
/// <summary>
/// 获取城市,用于select2下拉选项
/// </summary>
/// <param name="id">省份Id</param>
/// <returns></returns>
public List<AreaSelect2OutDto> GetCityByProvinceId(long id)
{
List<AreaSelect2OutDto> list = new List<AreaSelect2OutDto>();
CacheHelper cacheHelper = new CacheHelper();
list = JsonConvert.DeserializeObject<List<AreaSelect2OutDto>>(cacheHelper
.Get("Area_CityToArea_Enable_Select2" + id).ToJson());
if (list == null || list.Count <= 0)
{
string sqlWhere = string.Format("ParentId='{0}'", id);
list = service.GetAllByIsNotDeleteAndEnabledMark(sqlWhere).OrderBy(t => t.Id).ToList()
.MapTo<AreaSelect2OutDto>();
cacheHelper.Add("Area_CityToArea_Enable_Select2" + id, list);
}
return list;
}
/// <summary>
/// 获取县区,用于select2下拉选项
/// </summary>
/// <param name="id">城市Id</param>
/// <returns></returns>
public List<AreaSelect2OutDto> GetDistrictByCityId(long id)
{
List<AreaSelect2OutDto> list = new List<AreaSelect2OutDto>();
CacheHelper cacheHelper = new CacheHelper();
list = JsonConvert.DeserializeObject<List<AreaSelect2OutDto>>(cacheHelper
.Get("Area_DistrictToArea_Enable_Select2" + id).ToJson());
if (list == null || list.Count <= 0)
{
string sqlWhere = string.Format("ParentId='{0}'", id);
list = service.GetAllByIsNotDeleteAndEnabledMark(sqlWhere).OrderBy(t => t.Id).ToList()
.MapTo<AreaSelect2OutDto>();
cacheHelper.Add("Area_DistrictToArea_Enable_Select2" + id, list);
}
return list;
}
#endregion 适用于select2省市县区级联选择
}
}