206 lines
7.8 KiB
C#
206 lines
7.8 KiB
C#
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 exists = await _dbContext.Queryable<AppointmentAreaEntity>().Where(x => x.CustomCode == request.CustomCode).AnyAsync();
|
|
exists.ThrowIfTrue("编码已存在");
|
|
|
|
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("区域不存在");
|
|
|
|
(await _dbContext.Queryable<DeviceGroupEntity>()
|
|
.Where(x => request.DeviceGroupCodes.Contains(x.Code))
|
|
.Where(x => x.AsAppointment == false)
|
|
.AnyAsync())
|
|
.ThrowIfTrue("存在非可设置访客预约的分组");
|
|
|
|
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.DeviceGroupCodes.Select(x => new AppointmentAreaAssignmentEntity
|
|
{
|
|
CustomCode = request.CustomCode,
|
|
DeviceGroupCode = 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,
|
|
Source = "预约区域和设备组关系变更生成",
|
|
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,
|
|
Source = "预约区域和设备组关系变更生成",
|
|
Priority = 99
|
|
}));
|
|
}
|
|
|
|
await _dbContext.InsertAsync(tasks);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task<List<Guid>> GetAssignmentsAsync(string customCode)
|
|
{
|
|
return await _dbContext.Queryable<AppointmentAreaAssignmentEntity>()
|
|
.LeftJoin<DeviceGroupEntity>((x, dg) => x.DeviceGroupCode == dg.Code)
|
|
.Where((x, dg) => x.CustomCode == customCode && dg.AsAppointment)
|
|
.Select(x => x.DeviceGroupCode)
|
|
.ToListAsync();
|
|
}
|
|
|
|
|
|
//********************************************************* 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 deviceGroupCodes = await _dbContext.Queryable<AppointmentAreaAssignmentEntity>()
|
|
.Where(x => currentUseAreaCode.Contains(x.CustomCode))
|
|
.Select(x => x.DeviceGroupCode)
|
|
.ToListAsync();
|
|
|
|
var deviceCodes = await _dbContext.Queryable<DeviceGroupRelationshipEntity>()
|
|
.Where(x => deviceGroupCodes.Contains(x.GroupCode))
|
|
.Select(x => x.DeviceCode)
|
|
.Distinct()
|
|
.ToListAsync();
|
|
|
|
userBeforePermissions.Add(currentUserNo, deviceCodes);
|
|
}
|
|
|
|
return userBeforePermissions;
|
|
}
|
|
} |