fix 访客预约代码调整
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
using Dlhis.Basic.Appointment.Request;
|
||||
using Dlhis.Basic.Appointment.Response;
|
||||
using Dlhis.Entity.Basic.Entity;
|
||||
using Dlhis.Entity.Device.Entities;
|
||||
using Dlhis.Entity.Device.Enums;
|
||||
using Dlhis.Entity.Visitor.Entity;
|
||||
using Dlhis.Entity.Visitor.Enums;
|
||||
using Kurisu.AspNetCore.DataAccess.Dto;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
||||
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
||||
using Kurisu.AspNetCore.Utils.Extensions;
|
||||
using Mapster;
|
||||
|
||||
namespace Dlhis.Basic.Appointment.Services.Implements;
|
||||
|
||||
public class AppointmentAreaService : IAppointmentAreaService
|
||||
{
|
||||
private readonly IDbContext _dbContext;
|
||||
|
||||
public AppointmentAreaService(IDbContext dbContext)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
}
|
||||
|
||||
public async Task SaveAsync(AppointmentAreaSaveRequest request)
|
||||
{
|
||||
if (!request.Id.HasValue)
|
||||
{
|
||||
var entity = request.Adapt<AppointmentAreaEntity>();
|
||||
entity.CustomCode = request.CustomCode;
|
||||
entity.Type = request.Type;
|
||||
await _dbContext.InsertAsync(entity);
|
||||
}
|
||||
else
|
||||
{
|
||||
var entity = await _dbContext.Queryable<AppointmentAreaEntity>().Where(x => x.Id == request.Id).SingleAsync();
|
||||
entity.ThrowIfNull("预约区域不存在");
|
||||
request.Adapt(entity);
|
||||
await _dbContext.UpdateAsync(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Pagination<AppointmentAreaPageResponse>> GetPageAsync(AppointmentAreaPageRequest request)
|
||||
{
|
||||
return await _dbContext.Queryable<AppointmentAreaEntity>()
|
||||
.WhereIF(request.Type.HasValue, x => x.Type == request.Type)
|
||||
.WhereIF(request.Search.IsPresent(), x => x.CustomCode == request.Search || x.Name.Contains(request.Search))
|
||||
.Select<AppointmentAreaPageResponse>()
|
||||
.ToPageListAsync(request);
|
||||
}
|
||||
|
||||
public async Task AssignAsync(AppointmentAreaAssignRequest request)
|
||||
{
|
||||
(await _dbContext.Queryable<AppointmentAreaEntity>().Where(x => x.CustomCode == request.CustomCode).AnyAsync())
|
||||
.ThrowIfFalse("区域不存在");
|
||||
|
||||
var userBeforePermissions = await GetMyPermissionsAsync(request.CustomCode);
|
||||
var userNos = userBeforePermissions.Select(x => x.Key).ToList();
|
||||
|
||||
//删除原来的配置
|
||||
await _dbContext.Client.Deleteable<AppointmentAreaAssignmentEntity>()
|
||||
.Where(x => x.CustomCode == request.CustomCode)
|
||||
.ExecuteCommandAsync();
|
||||
|
||||
//添加当前区域的设置
|
||||
var data = request.DeviceCodes.Select(x => new AppointmentAreaAssignmentEntity
|
||||
{
|
||||
CustomCode = request.CustomCode,
|
||||
DeviceCode = x
|
||||
}).ToList();
|
||||
|
||||
await _dbContext.InsertAsync(data);
|
||||
|
||||
var userCurrentPermissions = await GetMyPermissionsAsync(request.CustomCode);
|
||||
|
||||
var allDeviceCodes = userCurrentPermissions.SelectMany(x => x.Value);
|
||||
var devices = await _dbContext.Queryable<DeviceEntity>().Where(x => allDeviceCodes.Contains(x.Code)).ToListAsync();
|
||||
|
||||
var tasks = new List<DeviceTaskEntity>();
|
||||
foreach (var userNo in userNos)
|
||||
{
|
||||
var before = userBeforePermissions[userNo];
|
||||
var current = userCurrentPermissions[userNo];
|
||||
|
||||
//被删除
|
||||
var deletedItems = before.Where(x => !current.Contains(x)).ToList();
|
||||
//新增
|
||||
var addItems = current.Where(x => !before.Contains(x)).ToList();
|
||||
|
||||
if (!deletedItems.Any() && !addItems.Any())
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
tasks.AddRange(deletedItems.Select(x => new DeviceTaskEntity
|
||||
{
|
||||
Type = DeviceTaskEnumType.Delete,
|
||||
DeviceType = DeviceTypeEnumType.Door,
|
||||
UserNo = userNo,
|
||||
DeviceUniqueNo = devices.First(item => item.Code == x).UniqueNo,
|
||||
Status = DeviceTaskStatusEnumType.Wait,
|
||||
Version = 0,
|
||||
CancellationId = 0,
|
||||
Source = DeviceTaskSourceEnumType.Appointment,
|
||||
Priority = 99
|
||||
}));
|
||||
|
||||
tasks.AddRange(addItems.Select(x => new DeviceTaskEntity
|
||||
{
|
||||
Type = DeviceTaskEnumType.Add,
|
||||
DeviceType = DeviceTypeEnumType.Door,
|
||||
UserNo = userNo,
|
||||
DeviceUniqueNo = devices.First(item => item.Code == x).UniqueNo,
|
||||
Status = DeviceTaskStatusEnumType.Wait,
|
||||
Version = 0,
|
||||
CancellationId = 0,
|
||||
Source = DeviceTaskSourceEnumType.Appointment,
|
||||
Priority = 99
|
||||
}));
|
||||
}
|
||||
|
||||
await _dbContext.InsertAsync(tasks);
|
||||
}
|
||||
|
||||
|
||||
//********************************************************* help methods ***************************************************************
|
||||
|
||||
|
||||
private async Task<Dictionary<string, List<Guid>>> GetMyPermissionsAsync(string areaCode)
|
||||
{
|
||||
//获取已经到达的数据
|
||||
var arrivedInfos = await _dbContext.Queryable<VisitorAppointmentDetailEntity>()
|
||||
.Where(x => x.DoorStatus == VisitorAppointmentDetailStatusEnumType.Arrived || x.WorkplaceStatus == VisitorAppointmentDetailStatusEnumType.Arrived)
|
||||
.Select(x => new
|
||||
{
|
||||
x.UserNo,
|
||||
x.DoorStatus,
|
||||
x.WorkplaceStatus,
|
||||
x.IsEnterDoorArea,
|
||||
x.ApplyDoorArea,
|
||||
x.IsEnterWorkplace,
|
||||
x.ApplyWorkplaceAreas
|
||||
})
|
||||
.ToListAsync();
|
||||
|
||||
var userAreaCodes = arrivedInfos.GroupBy(x => x.UserNo).ToDictionary(p => p.Key, ls =>
|
||||
{
|
||||
return ls.SelectMany(x =>
|
||||
{
|
||||
var list = new List<string>();
|
||||
if (x.DoorStatus == VisitorAppointmentDetailStatusEnumType.Arrived)
|
||||
{
|
||||
list.Add(x.ApplyDoorArea);
|
||||
}
|
||||
|
||||
if (x.WorkplaceStatus == VisitorAppointmentDetailStatusEnumType.Arrived)
|
||||
{
|
||||
list.AddRange(x.ApplyWorkplaceAreas);
|
||||
}
|
||||
|
||||
return list;
|
||||
});
|
||||
});
|
||||
|
||||
userAreaCodes = userAreaCodes.Where(x => x.Value.Contains(areaCode)).ToDictionary(x => x.Key, x => x.Value);
|
||||
|
||||
var userBeforePermissions = new Dictionary<string, List<Guid>>(userAreaCodes.Count);
|
||||
foreach ((string currentUserNo, IEnumerable<string> currentUseAreaCode) in userAreaCodes)
|
||||
{
|
||||
var deviceCodes = await _dbContext.Queryable<AppointmentAreaAssignmentEntity>()
|
||||
.Where(x => currentUseAreaCode.Contains(x.CustomCode))
|
||||
.Select(x => x.DeviceCode)
|
||||
.ToListAsync();
|
||||
|
||||
userBeforePermissions.Add(currentUserNo, deviceCodes);
|
||||
}
|
||||
|
||||
return userBeforePermissions;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user