205 lines
7.3 KiB
C#
205 lines
7.3 KiB
C#
using Infrastructure.Attribute;
|
|
using Microsoft.AspNetCore.Http;
|
|
using SqlSugar;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Infrastructure;
|
|
using ARW.Model;
|
|
using ARW.Repository;
|
|
using ARW.Model.Dto.Business.Custom.Regions;
|
|
using ARW.Model.Models.Business.Custom.Regions;
|
|
using ARW.Model.Vo.Business.Custom.Regions;
|
|
using ARW.Repository.Business.Custom.Regions;
|
|
using ARW.Service.Business.IBusinessService.Custom.Regions;
|
|
|
|
namespace ARW.Service.Business.BusinessService.Custom.Regions
|
|
{
|
|
/// <summary>
|
|
/// 省市区数据表接口实现类
|
|
/// </summary>
|
|
[AppService(ServiceType = typeof(IRegionService), ServiceLifetime = LifeTime.Transient)]
|
|
public class RegionServiceImpl : BaseService<Region>, IRegionService
|
|
{
|
|
private readonly RegionRepository _RegionRepository;
|
|
|
|
public RegionServiceImpl(RegionRepository RegionRepository)
|
|
{
|
|
_RegionRepository = RegionRepository;
|
|
}
|
|
|
|
#region 业务逻辑代码
|
|
|
|
|
|
/// <summary>
|
|
/// 查询省市区数据表树形列表
|
|
/// </summary>
|
|
public async Task<List<RegionVo>> GetRegionTreeList(RegionQueryDto parm)
|
|
{
|
|
//开始拼装查询条件
|
|
var predicate = Expressionable.Create<Region>();
|
|
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.RegionName), s => s.RegionName.Contains(parm.RegionName));
|
|
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.RegionCode), s => s.RegionCode == parm.RegionCode);
|
|
|
|
var query = _RegionRepository
|
|
.Queryable()
|
|
.LeftJoin<Region>((s, c) => s.RegionPid == c.RegionId)
|
|
.Where(predicate.ToExpression())
|
|
.OrderBy(s => s.RegionId, OrderByType.Asc)
|
|
.Select((s, c) => new RegionVo
|
|
{
|
|
RegionId = s.RegionId,
|
|
RegionName = s.RegionName,
|
|
RegionPid = s.RegionPid,
|
|
RegionCode = s.RegionCode,
|
|
RegionLevel = s.RegionLevel,
|
|
ParentName = c.RegionName,
|
|
});
|
|
|
|
return await query.ToTreeAsync(it => it.Children, it => it.RegionPid, 0);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 查询省市区数据表树形列表(二级)
|
|
/// </summary>
|
|
public async Task<List<SecondRegionVo>> GetSecondRegionTreeList(RegionQueryDto parm)
|
|
{
|
|
//开始拼装查询条件
|
|
var predicate = Expressionable.Create<Region>();
|
|
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.RegionName), s => s.RegionName.Contains(parm.RegionName));
|
|
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.RegionCode), s => s.RegionCode == parm.RegionCode);
|
|
predicate = predicate.And(s => s.RegionLevel <= 2);
|
|
|
|
var query = _RegionRepository
|
|
.Queryable()
|
|
.LeftJoin<Region>((s, c) => s.RegionPid == c.RegionId)
|
|
.Where(predicate.ToExpression())
|
|
.OrderBy(s => s.RegionId, OrderByType.Asc)
|
|
.Select((s, c) => new SecondRegionVo
|
|
{
|
|
Id = s.RegionId,
|
|
Label = s.RegionName,
|
|
Pid = s.RegionPid,
|
|
Code = s.RegionCode,
|
|
ParentName = c.RegionName
|
|
});
|
|
|
|
return await query.ToTreeAsync(it => it.Children, it => it.Pid, 0);
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 查询省市区数据表列表
|
|
/// </summary>
|
|
public Task<List<RegionVo>> GetRegionList(RegionQueryDto parm)
|
|
{
|
|
//开始拼装查询条件d
|
|
var predicate = Expressionable.Create<Region>();
|
|
|
|
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.RegionName), it => it.RegionName.Contains(parm.RegionName));
|
|
predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.RegionCode), it => it.RegionCode == parm.RegionCode);
|
|
var query = _RegionRepository
|
|
.Queryable()
|
|
.Where(predicate.ToExpression())
|
|
.OrderBy(s => s.RegionId, OrderByType.Asc)
|
|
.Select(s => new RegionVo
|
|
{
|
|
RegionId = s.RegionId,
|
|
RegionName = s.RegionName,
|
|
RegionPid = s.RegionPid,
|
|
RegionCode = s.RegionCode,
|
|
RegionLevel = s.RegionLevel,
|
|
});
|
|
|
|
|
|
return query.ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 添加或修改省市区数据表
|
|
/// </summary>
|
|
public async Task<string> AddOrUpdateRegion(Region model)
|
|
{
|
|
if (model.RegionId != 0)
|
|
{
|
|
var type = await _RegionRepository.GetListAsync(s => s.RegionPid == model.RegionId);
|
|
if (type != null)
|
|
{
|
|
foreach (var item in type)
|
|
{
|
|
if (model.RegionPid == item.RegionId) throw new CustomException("上级菜单不能选择自己的子级!");
|
|
}
|
|
}
|
|
if (model.RegionPid == model.RegionId) throw new CustomException("上级菜单不能选择与当前菜单一样的!");
|
|
var response = await _RegionRepository.UpdateAsync(model);
|
|
return "修改成功!";
|
|
}
|
|
else
|
|
{
|
|
var info = _RegionRepository.GetFirst(it => it.RegionId == model.RegionPid);
|
|
|
|
var response = await _RegionRepository.InsertAsync(model);
|
|
return "添加成功!";
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 获取省市区Id
|
|
/// </summary>
|
|
/// <param name="RegionCode">省市区编码</param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public async Task<int> GetRegionId(string RegionCode, int level)
|
|
{
|
|
var region = await _RegionRepository.GetFirstAsync(s => s.RegionCode == RegionCode && s.RegionLevel == level);
|
|
if (region == null) throw new Exception(RegionCode + "编码不正确");
|
|
return region.RegionId;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 获取省市区完整名称
|
|
/// </summary>
|
|
/// <param name="provinceId">省id</param>
|
|
/// <param name="cityId">市id</param>
|
|
/// <param name="regionId">区id</param>
|
|
/// <returns></returns>
|
|
public async Task<string> GetRegionFullName(int provinceId, int cityId, int regionId)
|
|
{
|
|
var regionName = "";
|
|
var province = await _RegionRepository.GetFirstAsync(s => s.RegionId == provinceId);
|
|
regionName += province.RegionName;
|
|
var city = await _RegionRepository.GetFirstAsync(s => s.RegionId == cityId);
|
|
regionName += city.RegionName;
|
|
var region = await _RegionRepository.GetFirstAsync(s => s.RegionId == regionId);
|
|
regionName += region.RegionName;
|
|
|
|
return regionName;
|
|
}
|
|
|
|
|
|
#region Excel处理
|
|
|
|
|
|
/// <summary>
|
|
/// Excel数据导出处理
|
|
/// </summary>
|
|
public async Task<List<RegionVo>> HandleExportData(List<RegionVo> data)
|
|
{
|
|
return data;
|
|
}
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
}
|