init commit

This commit is contained in:
路 范
2022-03-30 17:54:33 +08:00
parent df01841625
commit 904bdd16cd
500 changed files with 217251 additions and 0 deletions

View File

@@ -0,0 +1,183 @@
using Ewide.Core.Extension;
using Ewide.Core.Service.Area.Dto;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using Mapster;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Ewide.Core.Service.Area
{
public class AreaCodeService : IAreaCodeService, ITransient
{
private readonly IRepository<SysAreaCode> _areaCodeRep;
private readonly IRepository<SysOrg> _sysOrgRep;
private readonly ISysCacheService _sysCacheService;
private readonly IUserManager _userManager;
public AreaCodeService(IRepository<SysAreaCode> AreaCodeRep, ISysCacheService sysCacheService, IRepository<SysOrg> sysOrgRep, IUserManager userManager)
{
_areaCodeRep = AreaCodeRep;
_sysCacheService = sysCacheService;
_sysOrgRep = sysOrgRep;
_userManager = userManager;
}
/// <summary>
/// 添加一个区域
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task AddAreaCode(AreaCodeInput input)
{
CheckInput(input);
CheckCode(input);
CheckArea(input);
await _areaCodeRep.InsertNowAsync(input.Adapt<SysAreaCode>());
#if DEBUG
#else
await _sysCacheService.SetAreaCode(await _areaCodeRep.DetachedEntities.ToListAsync());
#endif
}
/// <summary>
/// 删除一个区域
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task DeleteAreaCode(DeleteAreaCodeInput input)
{
var AreaCode = await _areaCodeRep.FirstOrDefaultAsync(p => p.Code == input.Code);
if (await _sysOrgRep.DetachedEntities.AnyAsync(o => o.AreaCode == input.Code))
{
throw Oops.Oh("不能删除已包含区域的组织");
}
await AreaCode.DeleteNowAsync();
#if DEBUG
#else
await _sysCacheService.SetAreaCode(await _areaCodeRep.DetachedEntities.ToListAsync());
#endif
}
/// <summary>
/// 根据区域代码获取区域信息
/// </summary>
/// <param name="code">区域代码</param>
/// <returns></returns>
public async Task<SysAreaCode> GetAreaCode(string code)
{
var cachedAreaCodes = await _sysCacheService.GetAreaCode();
if (cachedAreaCodes == null || cachedAreaCodes.Count < 0)
{
cachedAreaCodes = await _areaCodeRep.DetachedEntities.ToListAsync();
#if DEBUG
#else
await _sysCacheService.SetAreaCode(cachedAreaCodes);
#endif
}
return cachedAreaCodes.FirstOrDefault(a => a.Code == code);
}
public async Task<List<SysAreaCode>> GetAreaCodeWithChildren(string code)
{
return await _areaCodeRep.DetachedEntities.Where(a => a.Code.StartsWith(code)).ToListAsync();
}
public async Task<dynamic> QueryAreaCodePageList(AreaCodeInput input)
{
var AreaCodes = await _areaCodeRep.DetachedEntities
.Where(input.LevelType.HasValue, a => a.LevelType == input.LevelType)
.Where(!string.IsNullOrEmpty(input.Code), a => a.Code.Contains(input.Code))
.Where(!string.IsNullOrEmpty(input.AdCode), a => a.AdCode.Contains(input.AdCode))
.Where(!string.IsNullOrEmpty(input.Name), a => a.Name.Contains(input.Name))
.Where(!string.IsNullOrEmpty(input.Pcode), a => a.Code.StartsWith(input.Pcode))
.ToPageData(input);
return PageDataResult<SysAreaCode>.PageResult(AreaCodes);
}
/// <summary>
/// 更新区域信息
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public async Task UpdateAreaCode(AreaCodeInput input)
{
CheckInput(input);
CheckArea(input);
var area = input.Adapt<SysAreaCode>();
await area.UpdateNowAsync();
#if DEBUG
#else
await _sysCacheService.SetAreaCode(await _areaCodeRep.DetachedEntities.ToListAsync());
#endif
}
private bool CheckInput(AreaCodeInput input)
{
//检查level和区域长度
if (false)
{
throw Oops.Oh("数据异常");
}
//检查是否有子节点
return true;
}
private bool CheckArea(AreaCodeInput input)
{
var any = _areaCodeRep.DetachedEntities.Any(p => p.AdCode == input.AdCode && p.Code != input.Code);
if (any)
{
throw Oops.Oh("区域编码重复");
}
return true;
}
private bool CheckCode(AreaCodeInput input)
{
var any = _areaCodeRep.DetachedEntities.Any(p => p.Code == input.Code);
if (any)
{
throw Oops.Oh("编码重复");
}
return true;
}
public async Task<List<string>> GetAreaCodeListByOrgId(List<string> orgIdList)
{
return await _sysOrgRep.DetachedEntities.Where(p => orgIdList.Contains(p.Id)).Select(p => p.AreaCode).Distinct().ToListAsync();
}
/// <summary>
/// 获取区域目录树
/// </summary>
/// <returns></returns>
public async Task<List<AreaTreeNode>> GetAreaCodeTree(int? level)
{
level = level.GetValueOrDefault(100);
var cachedAreaCodes = await _sysCacheService.GetAreaCode();
if (cachedAreaCodes == null || cachedAreaCodes.Count < 0)
{
cachedAreaCodes = await _areaCodeRep.DetachedEntities.ToListAsync();
#if DEBUG
#else
await _sysCacheService.SetAreaCode(cachedAreaCodes);
#endif
}
if (!_userManager.SuperAdmin)
{
var userAreaList = await _userManager.GetUserAllAreaList();
cachedAreaCodes = cachedAreaCodes.Where(a => userAreaList.Contains(a.Code)).ToList();
}
return new TreeBuildUtil<AreaTreeNode>().DoTreeBuild(cachedAreaCodes.Select(u => new AreaTreeNode
{
Code = u.Code,
AdCode = u.AdCode,
ParentCode = u.ParentCode,
Name = u.Name,
LevelType = u.LevelType,
Note = u.Note
}).ToList());
}
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ewide.Core.Service.Area.Dto
{
public class AreaCodeInput : InputBase
{
public string Code { get; set; }
public string AdCode { get; set; }
public string Name { get; set; }
public int? LevelType { get; set; }
public string Note { get; set; }
public int Sort { get; set; }
public string Pcode { get; set; }
}
public class UpdateAreaCodeInput
{
public string AdCode { get; set; }
public string Name { get; set; }
public int? LevelType { get; set; }
public string Note { get; set; }
public int Sort { get; set; }
}
public class DeleteAreaCodeInput
{
[Required(ErrorMessage ="区域代码不可为空")]
public string Code { get; set; }
}
}

View File

@@ -0,0 +1,33 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ewide.Core.Service.Area.Dto
{
public class AreaCodeOutput:AreaCodeInput
{
public string ParentCode { get; set; }
}
public class AreaTreeNode : AreaCodeOutput, ITreeNode
{
public List<AreaTreeNode> Children { get; set; } = new List<AreaTreeNode>();
public string GetId()
{
return Code;
}
public string GetPid()
{
return ParentCode;
}
public void SetChildren(IList children)
{
Children = (List<AreaTreeNode>)children;
}
}
}

View File

@@ -0,0 +1,25 @@
using Ewide.Core.Service.Area.Dto;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Ewide.Core.Service.Area
{
public interface IAreaCodeService
{
Task AddAreaCode(AreaCodeInput input);
Task DeleteAreaCode(DeleteAreaCodeInput input);
Task UpdateAreaCode(AreaCodeInput input);
Task<SysAreaCode> GetAreaCode(string code);
Task<List<SysAreaCode>> GetAreaCodeWithChildren(string code);
Task<List<AreaTreeNode>> GetAreaCodeTree(int? level);
Task<dynamic> QueryAreaCodePageList([FromQuery] AreaCodeInput input);
Task<List<string>> GetAreaCodeListByOrgId(List<string> orgIdList);
}
}