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.Services; using Znyc.Cloudcar.Admin.Security.Dtos; using Znyc.Cloudcar.Admin.Security.Entitys; using Znyc.Cloudcar.Admin.Security.IRepositories; using Znyc.Cloudcar.Admin.Security.IServices; namespace Znyc.Cloudcar.Admin.Security.Services { /// /// 地区信息 /// public class AreaService : BaseService, IAreaService { private readonly IAreaRepository _repository; public AreaService(IAreaRepository repository) : base(repository) { _repository = repository; } #region 用于uniapp下拉选项 /// /// 获取所有可用的地区,用于uniapp下拉选项 /// /// public List GetAllByEnable() { List list = new List(); CacheHelper cacheHelper = new CacheHelper(); list = JsonConvert.DeserializeObject>(cacheHelper.Get("Area_Enable_Uniapp") .ToJson()); if (list == null || list.Count <= 0) { List listFunction = _repository.GetAllByIsNotDeleteAndEnabledMark("Layers in (0,1,2)") .OrderBy(t => t.SortCode).ToList(); list = UniappViewJson(listFunction, 0); cacheHelper.Add("Area_Enable_Uniapp", list); } return list; } /// /// 获取省、市、县/区三级可用的地区,用于uniapp下拉选项 /// /// public List GetProvinceToAreaByEnable() { List list = new List(); CacheHelper cacheHelper = new CacheHelper(); list = JsonConvert.DeserializeObject>(cacheHelper .Get("Area_ProvinceToArea_Enable_Uniapp").ToJson()); if (list == null || list.Count <= 0) { List listFunctionTemp = _repository.GetAllByIsNotDeleteAndEnabledMark("Layers in (1,2,3)") .OrderBy(t => t.Id).ToList(); List listFunction = new List(); 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; } /// /// /// /// /// public List UniappViewJson(List data, long ParentId) { List list = new List(); List 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; } /// /// /// /// /// public List ChildrenUniappViewList(List data, long ParentId) { List listChildren = new List(); List 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下拉选项 } }