165 lines
5.4 KiB
C#
165 lines
5.4 KiB
C#
using Dlhis.Basic.Device.Services;
|
|
using Dlhis.Basic.Organization.Request;
|
|
using Dlhis.Basic.Organization.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.DataAccess.Entity;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Basic.Organization.Services.Implements;
|
|
|
|
public class GainfoGroupService : IGainfoGroupService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly ICurrentUser _currentUser;
|
|
private readonly IDevicePermissionService _devicePermissionService;
|
|
|
|
public GainfoGroupService(IDbContext dbContext, ICurrentUser currentUser, IDevicePermissionService devicePermissionService)
|
|
{
|
|
_dbContext = dbContext;
|
|
_currentUser = currentUser;
|
|
_devicePermissionService = devicePermissionService;
|
|
}
|
|
|
|
public async Task<List<GainfoGroupTreeResponse>> GetTreeAsync()
|
|
{
|
|
var gainfoGroups = await _dbContext.Queryable<GainfoGroupEntity>()
|
|
.Select<GainfoGroupTreeResponse>()
|
|
.ToListAsync();
|
|
|
|
var tops = gainfoGroups.Where(x => !x.PCode.HasValue).ToList();
|
|
tops.ForEach(x =>
|
|
{
|
|
RecursionGetNext(x, gainfoGroups);
|
|
});
|
|
|
|
return tops;
|
|
}
|
|
|
|
public async Task SaveAsync(GainfoGroupSaveRequest request)
|
|
{
|
|
await CheckDuplicateNameAsync(request);
|
|
try
|
|
{
|
|
//todo
|
|
_dbContext.Client.QueryFilter.ClearAndBackup<ITenantId>();
|
|
var defaultGroup = await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.IsDefault).FirstAsync();
|
|
|
|
if (!request.PCode.HasValue)
|
|
{
|
|
request.PCode = defaultGroup.Code;
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
_dbContext.Client.QueryFilter.Restore();
|
|
}
|
|
|
|
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var entity = request.Adapt<GainfoGroupEntity>();
|
|
entity.Code = Guid.NewGuid();
|
|
await _dbContext.InsertAsync(entity);
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.Id == request.Id).SingleAsync();
|
|
request.Adapt(entity);
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
|
|
public async Task<List<GainfoPageResponse>> GetGainfosByGroupAsync(Guid code)
|
|
{
|
|
var userNos = await _dbContext.Queryable<GainfoGroupRelationshipEntity>()
|
|
.Where(x => x.GroupCode == code)
|
|
.Select(x => x.UserNo)
|
|
.ToListAsync();
|
|
|
|
var list = await _dbContext.Queryable<GainfoEntity>()
|
|
.Where(x => userNos.Contains(x.No))
|
|
.Select<GainfoPageResponse>()
|
|
.ToListAsync();
|
|
|
|
return list;
|
|
}
|
|
|
|
public async Task AssignGainfosAsync(GainfoAssignRequest request)
|
|
{
|
|
if (request.UserNos.IsPresent())
|
|
{
|
|
var pGrp = await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.Code == request.GroupCode).FirstAsync();
|
|
(pGrp?.Editable == false && pGrp?.IsDefault == true).ThrowIfTrue("不可分配人员到当前分组");
|
|
}
|
|
|
|
var existsNextGroup = await _dbContext.Queryable<GainfoGroupEntity>().Where(x => x.PCode == request.GroupCode).AnyAsync();
|
|
existsNextGroup.ThrowIfTrue("存在下级分组,无法分配人员");
|
|
|
|
await _devicePermissionService.GenerateDeviceTasksAsync(async () =>
|
|
{
|
|
var relations = await _dbContext.Queryable<GainfoGroupRelationshipEntity>().Where(x => x.GroupCode == request.GroupCode).ToListAsync();
|
|
await _dbContext.DeleteReallyAsync(relations);
|
|
|
|
request.UserNos = await _dbContext.Queryable<GainfoEntity>().Where(x => request.UserNos.Contains(x.No)).Select(x => x.No).ToListAsync();
|
|
var entities = request.UserNos.Select(x => new GainfoGroupRelationshipEntity
|
|
{
|
|
GroupCode = request.GroupCode,
|
|
UserNo = x,
|
|
|
|
|
|
}).ToList();
|
|
|
|
await _dbContext.InsertAsync(entities);
|
|
|
|
}, null);
|
|
}
|
|
|
|
//******************************************************* help methods ***********************************************************
|
|
|
|
/// <summary>
|
|
/// 检查重名
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
private async Task CheckDuplicateNameAsync(GainfoGroupSaveRequest request)
|
|
{
|
|
var exists = await _dbContext.Queryable<GainfoGroupEntity>()
|
|
.Where(x => x.Name == request.Name)
|
|
.WhereIF(request.Id.HasValue, x => x.Id != request.Id)
|
|
.AnyAsync();
|
|
|
|
exists.ThrowIfTrue($"重复组名{request.Name}");
|
|
}
|
|
|
|
|
|
private void RecursionGetNext(GainfoGroupTreeResponse current, List<GainfoGroupTreeResponse> gainfoGroups)
|
|
{
|
|
var currentTenantId = _currentUser.GetStringTenantId();
|
|
var next = gainfoGroups.Where(x =>
|
|
{
|
|
if (x.PCode == current.Code)
|
|
{
|
|
if (x.TenantId.IsEmpty())
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return x.TenantId == currentTenantId;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
|
|
}).ToList();
|
|
current.Next = next;
|
|
foreach (var item in next)
|
|
{
|
|
RecursionGetNext(item, gainfoGroups);
|
|
}
|
|
}
|
|
}
|