diff --git a/ARW.Model/Dto/Business/Regions/RegionDto.cs b/ARW.Model/Dto/Business/Regions/RegionDto.cs new file mode 100644 index 0000000..4f74907 --- /dev/null +++ b/ARW.Model/Dto/Business/Regions/RegionDto.cs @@ -0,0 +1,38 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using ARW.Model.Models.Business.Regions; + +namespace ARW.Model.Dto.Business.Regions +{ + /// + /// 省市区数据表输入对象 + /// + public class RegionDto + { + public int RegionId { get; set; } + [Required(ErrorMessage = "区划名称不能为空")] + public string RegionName { get; set; } + [Required(ErrorMessage = "父级ID不能为空")] + public int RegionPid { get; set; } + [Required(ErrorMessage = "区划编码不能为空")] + public string RegionCode { get; set; } + [Required(ErrorMessage = "层级(1省级 2市级 3区/县级)不能为空")] + public int RegionLevel { get; set; } + } + + + /// + /// 省市区数据表查询对象 + /// + public class RegionQueryDto : PagerInfo + { + public string RegionName { get; set; } + + public string ids { get; set; } + } + + + + +} diff --git a/ARW.Model/Models/Business/Regions/Region.cs b/ARW.Model/Models/Business/Regions/Region.cs new file mode 100644 index 0000000..097c3d2 --- /dev/null +++ b/ARW.Model/Models/Business/Regions/Region.cs @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using SqlSugar; +using OfficeOpenXml.Attributes; +using Newtonsoft.Json; + +namespace ARW.Model.Models.Business.Regions +{ + /// + /// 省市区数据表,数据实体对象 + /// + /// @author admin + /// @date 2023-06-05 + /// + [SugarTable("tb_region")] + public class Region : BusinessBase + { + + /// + /// 描述 :区划信息ID + /// 空值 : false + /// + [EpplusTableColumn(Header = "区划信息ID")] + [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = "region_id")] + public int RegionId { get; set; } + + + /// + /// 描述 :区划名称 + /// 空值 : false + /// + [EpplusTableColumn(Header = "区划名称")] + [SugarColumn(ColumnName = "region_name")] + public string RegionName { get; set; } + + + /// + /// 描述 :父级ID + /// 空值 : false + /// + [EpplusTableColumn(Header = "父级ID")] + [SugarColumn(ColumnName = "region_pid")] + public int RegionPid { get; set; } + + + /// + /// 描述 :区划编码 + /// 空值 : false + /// + [EpplusTableColumn(Header = "区划编码")] + [SugarColumn(ColumnName = "region_code")] + public string RegionCode { get; set; } + + + /// + /// 描述 :层级(1省级 2市级 3区/县级) + /// 空值 : false + /// + [EpplusTableColumn(Header = "层级(1省级 2市级 3区/县级)")] + [SugarColumn(ColumnName = "region_level")] + public int RegionLevel { get; set; } + + + + + + + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + [SugarColumn(IsIgnore = true)] + public List Children { get; set; } + } +} \ No newline at end of file diff --git a/ARW.Model/Vo/Business/Regions/RegionVo.cs b/ARW.Model/Vo/Business/Regions/RegionVo.cs new file mode 100644 index 0000000..6355b38 --- /dev/null +++ b/ARW.Model/Vo/Business/Regions/RegionVo.cs @@ -0,0 +1,56 @@ +using Newtonsoft.Json; +using OfficeOpenXml.Attributes; +using SqlSugar; +using System; +using ARW.Model.Models.Business.Regions; +using System.Collections.Generic; + +namespace ARW.Model.Vo.Business.Regions +{ + /// + /// 省市区数据表展示对象 + /// + public class RegionVo + { + + /// + /// 描述 :区划信息ID + /// + [EpplusIgnore] + [SugarColumn(IsTreeKey = true)] + public int RegionId { get; set; } + + /// + /// 描述 :区划名称 + /// + [EpplusTableColumn(Header = "区划名称")] + public string RegionName { get; set; } + + /// + /// 描述 :父级ID + /// + [EpplusTableColumn(Header = "父级ID")] + public int RegionPid { get; set; } + + + /// + /// 描述 :区划编码 + /// + [EpplusTableColumn(Header = "区划编码")] + public string RegionCode { get; set; } + + /// + /// 描述 :层级(1省级 2市级 3区/县级) + /// + [EpplusTableColumn(Header = "层级(1省级 2市级 3区/县级)")] + public int RegionLevel { get; set; } + + [EpplusIgnore] + public string ParentName { get; set; } + + [EpplusIgnore] + [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] + [SugarColumn(IsIgnore = true)] + public List Children { get; set; } + } +} diff --git a/ARW.Repository/Business/Regions/RegionRepository.cs b/ARW.Repository/Business/Regions/RegionRepository.cs new file mode 100644 index 0000000..d420e67 --- /dev/null +++ b/ARW.Repository/Business/Regions/RegionRepository.cs @@ -0,0 +1,20 @@ +using System; +using Infrastructure.Attribute; +using ARW.Repository.System; +using ARW.Model.Models.Business.Regions; + +namespace ARW.Repository.Business.Regions +{ + /// + /// 省市区数据表仓储 + /// + /// @author admin + /// @date 2023-06-05 + /// + [AppService(ServiceLifetime = LifeTime.Transient)] + public class RegionRepository : BaseRepository + { + #region 业务逻辑代码 + #endregion + } +} \ No newline at end of file diff --git a/ARW.Service/Business/BusinessService/Regions/RegionService.cs b/ARW.Service/Business/BusinessService/Regions/RegionService.cs new file mode 100644 index 0000000..367f14d --- /dev/null +++ b/ARW.Service/Business/BusinessService/Regions/RegionService.cs @@ -0,0 +1,135 @@ +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.Repository.Business.Regions; +using ARW.Service.Business.IBusinessService.Regions; +using ARW.Model.Dto.Business.Regions; +using ARW.Model.Models.Business.Regions; +using ARW.Model.Vo.Business.Regions; + +namespace ARW.Service.Business.BusinessService.Regions +{ + /// + /// 省市区数据表接口实现类 + /// + [AppService(ServiceType = typeof(IRegionService), ServiceLifetime = LifeTime.Transient)] + public class RegionServiceImpl : BaseService, IRegionService + { + private readonly RegionRepository _RegionRepository; + + public RegionServiceImpl(RegionRepository RegionRepository) + { + this._RegionRepository = RegionRepository; + } + + #region 业务逻辑代码 + + + /// + /// 查询省市区数据表树形列表 + /// + public async Task> GetRegionTreeList(RegionQueryDto parm) + { + //开始拼装查询条件 + var predicate = Expressionable.Create(); + + var query = _RegionRepository + .Queryable() + .LeftJoin((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); + } + + + /// + /// 查询省市区数据表列表 + /// + public Task> GetRegionList(RegionQueryDto parm) + { + //开始拼装查询条件d + var predicate = Expressionable.Create(); + + predicate = predicate.AndIF(!string.IsNullOrEmpty(parm.RegionName), it => it.RegionName.Contains(parm.RegionName)); + 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(); + } + + /// + /// 添加或修改省市区数据表 + /// + public async Task 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 "添加成功!"; + } + } + + #region Excel处理 + + + /// + /// Excel数据导出处理 + /// + public async Task> HandleExportData(List data) + { + return data; + } + + #endregion + + + + #endregion + + } +} diff --git a/ARW.Service/Business/IBusinessService/Regions/IRegionService.cs b/ARW.Service/Business/IBusinessService/Regions/IRegionService.cs new file mode 100644 index 0000000..d8e05d1 --- /dev/null +++ b/ARW.Service/Business/IBusinessService/Regions/IRegionService.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ARW.Model; +using ARW.Model.Dto.Business.Regions; +using ARW.Model.Models.Business.Regions; +using ARW.Model.Vo.Business.Regions; + +namespace ARW.Service.Business.IBusinessService.Regions +{ + public interface IRegionService : IBaseService + { + /// + /// 获取省市区数据表树形列表 + /// + /// + /// + Task> GetRegionTreeList(RegionQueryDto parm); + + /// + /// 获取省市区数据表列表 + /// + /// + /// + Task> GetRegionList(RegionQueryDto parm); + + + /// + /// 添加或修改省市区数据表 + /// + /// + /// + Task AddOrUpdateRegion(Region parm); + + + + /// + /// Excel导出 + /// + Task> HandleExportData(List data); + + + } +} diff --git a/ARW.WebApi/Controllers/Business/Regions/RegionController.cs b/ARW.WebApi/Controllers/Business/Regions/RegionController.cs new file mode 100644 index 0000000..9191e8d --- /dev/null +++ b/ARW.WebApi/Controllers/Business/Regions/RegionController.cs @@ -0,0 +1,140 @@ +using Infrastructure; +using Infrastructure.Attribute; +using Infrastructure.Enums; +using Infrastructure.Model; +using Mapster; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using ARW.Admin.WebApi.Extensions; +using ARW.Admin.WebApi.Filters; +using ARW.Common; +using ARW.Model.Dto.Business.Regions; +using ARW.Service.Business.IBusinessService.Regions; +using ARW.Admin.WebApi.Controllers; +using ARW.Model.Models.Business.Regions; +using ARW.Model.Vo.Business.Regions; +using Microsoft.AspNetCore.Authorization; +using ARW.Admin.WebApi.Framework; + + +namespace ARW.WebApi.Controllers.Business.Regions +{ + /// + /// 省市区数据表控制器 + /// + [Verify] + [Route("business/[controller]")] + public class RegionController : BaseController + { + private readonly IRegionService _RegionService; + + /// + /// 依赖注入 + /// + /// 省市区数据表服务 + public RegionController(IRegionService RegionService) + { + _RegionService = RegionService; + } + + + /// + /// 获取省市区数据表树形列表 + /// + /// 查询参数 + /// + [HttpGet("getRegionTreeList")] + [ActionPermissionFilter(Permission = "business:region:treelist")] + public async Task GetRegionList([FromQuery] RegionQueryDto parm) + { + var res = await _RegionService.GetRegionTreeList(parm); + res ??= new List(); + + return SUCCESS(res); + } + + /// + /// 添加或修改省市区数据表 + /// + /// + /// + [HttpPost("addOrUpdateRegion")] + [ActionPermissionFilter(Permission = "business:region:addOrUpdate")] + [Log(Title = "添加或修改省市区数据表", BusinessType = BusinessType.ADDORUPDATE)] + public async Task AddOrUpdateRegion([FromBody] RegionDto parm) + { + if (parm == null) { throw new CustomException("请求参数错误"); } + + var modal = new Region(); + if (parm.RegionId != 0) modal = parm.Adapt().ToUpdate(HttpContext); + else modal = parm.Adapt().ToCreate(HttpContext); + + var res = await _RegionService.AddOrUpdateRegion(modal); + return SUCCESS(res); + } + + /// + /// 删除省市区数据表 + /// + /// + [HttpDelete("{ids}")] + [ActionPermissionFilter(Permission = "business:region:delete")] + [Log(Title = "省市区数据表删除", BusinessType = BusinessType.DELETE)] + public IActionResult Delete(string ids) + { + long[] idsArr = Tools.SpitLongArrary(ids); + if (idsArr.Length <= 0) { return ToResponse(ApiResult.Error($"删除失败Id 不能为空")); } + var response = _RegionService.Delete(idsArr); + return SUCCESS("删除成功!"); + } + + + /// + /// 导出省市区数据表 + /// + /// + [Log(Title = "省市区数据表导出", BusinessType = BusinessType.EXPORT, IsSaveResponseData = false)] + [HttpGet("exportRegion")] + [ActionPermissionFilter(Permission = "business:region:export")] + public async Task ExportExcel([FromQuery] RegionQueryDto parm) + { + var list = await _RegionService.GetRegionList(parm); + var data = list; + + // 选中数据 + if (!string.IsNullOrEmpty(parm.ids)) + { + int[] idsArr = Tools.SpitIntArrary(parm.ids); + var selectDataList = new List(); + //foreach (var item in idsArr) + //{ + // var select_data = data.Where(s => s.RegionId == item).First(); + // selectDataList.Add(select_data); + + // // 查看当前数据有没有子级 + // var newRegions = data.FindAll(delegate (RegionVo region) + // { + // string[] parentRegionId = region.RegionAncestralGuid.Split(",", StringSplitOptions.RemoveEmptyEntries); + // return parentRegionId.Contains(select_data.RegionGuid.ToString()); + // }); + // string[] regionArr = newRegions.Select(x => x.RegionGuid.ToString()).ToArray(); + // var ancestorArr = data.Where(s => regionArr.Contains(s.RegionGuid.ToString())).ToList(); + // selectDataList.AddRange(ancestorArr); + //} + data = selectDataList; + } + + + + // 导出数据处理 + var handleData = await _RegionService.HandleExportData(data); + + string sFileName = ExportExcel(handleData, "Region", "省市区数据表列表"); + return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName }); + } + + + + + } +}