Files
jinko.yikatong/Src/Dlhis.Visitor/Appointment/Services/Implements/AppointmentService.cs
2024-05-21 17:07:28 +08:00

212 lines
8.3 KiB
C#

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 Dlhis.Visitor.Appointment.Request;
using Dlhis.Visitor.Appointment.Response;
using Kurisu.AspNetCore.Utils.Extensions;
using Kurisu.Core.CustomClass;
using Kurisu.Core.DataAccess.Dto;
using Kurisu.SqlSugar.Extensions;
using Kurisu.SqlSugar.Services;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
namespace Dlhis.Visitor.Appointment.Services.Implements;
public class AppointmentService : IAppointmentService
{
private readonly IDbContext _dbContext;
private readonly IDataProtectionProvider _dataProtectionProvider;
public AppointmentService(IDbContext dbContext, IDataProtectionProvider dataProtectionProvider)
{
_dbContext = dbContext;
_dataProtectionProvider = dataProtectionProvider;
}
public async Task ReceiveAsync(AppointmentReceiveRequest request)
{
var exists = await _dbContext.Queryable<VisitorAppointmentEntity>().Where(x => x.FlowId == request.FlowId).AnyAsync();
exists.ThrowIfTrue("已经存在申请");
var area = await _dbContext.Queryable<AppointmentAreaEntity>()
.Where(x => x.Name == request.ApplyArea)
.Where(x => x.TenantId == request.Factory)
.FirstAsync();
area.ThrowIfNull("预约区域不存在");
var where = LinqExpressionExtensions.False<VisitorAppointmentDetailEntity>();
request.Items.ForEach(item => { where = where.Or(x => !(x.End < request.Start || x.Start > request.End)).And(x => x.IdNo == item.IdNo); });
var existsDetails = await _dbContext.Queryable<VisitorAppointmentDetailEntity>()
.Where(where)
.Where(x => x.Status == AppointmentDetailEnumType.Wait || x.Status == AppointmentDetailEnumType.Arrived)
.ToListAsync();
existsDetails.ThrowIfExists($"{existsDetails.Select(x => x.Name).Join(",")}存在预约时间冲突,请检查");
var id = await _dbContext.InsertReturnIdentityAsync(new VisitorAppointmentEntity
{
FlowId = request.FlowId,
Name = request.Name,
Code = request.Code,
Description = request.Description,
Title = request.Title,
Company = request.Company,
ApplyTime = request.ApplyTime,
AreaCustomCode = area.CustomCode,
});
foreach (var item in request.Items)
{
var no = await _dbContext.Queryable<GainfoEntity>().Where(x => x.IdNo == item.IdNo).Select(x => x.No).SingleAsync();
var isExternal = no.IsEmpty();
if (isExternal)
{
var gainfoExternal = await _dbContext.Queryable<GainfoExternalEntity>().Where(x => x.IdNo == item.IdNo).FirstAsync();
if (gainfoExternal == null)
{
gainfoExternal = new GainfoExternalEntity
{
No = Guid.NewGuid().ToString(),
Code = item.IdNo,
IdNo = item.IdNo,
Name = item.Name,
Company = request.Company,
Phone = item.Phone
};
await _dbContext.InsertAsync(gainfoExternal);
}
else
{
gainfoExternal.Phone = item.Phone;
gainfoExternal.Company = request.Company;
await _dbContext.UpdateAsync(gainfoExternal);
}
no = gainfoExternal.No;
}
var detail = new VisitorAppointmentDetailEntity
{
UserNo = no,
AppointmentId = id,
FlowId = request.FlowId,
Start = request.Start,
End = request.End,
IdNo = item.IdNo,
Name = item.Name,
Phone = item.Phone,
Remark = item.Remark,
Status = AppointmentDetailEnumType.Wait,
IsExternal = isExternal,
};
await _dbContext.InsertAsync(detail);
}
}
public async Task<Pagination<AppointmentPageResponse>> GetPageAsync(AppointmentPageRequest request)
{
return await _dbContext.Queryable<VisitorAppointmentEntity>()
.WhereIF(request.SearchDateTimePeriod?.Start.HasValue == true && request.SearchDateTimePeriod?.End.HasValue == true,
x => x.ApplyTime >= request.SearchDateTimePeriod.Start && x.ApplyTime <= request.SearchDateTimePeriod.End)
.WhereIF(request.Search.IsPresent(), x => x.Code == request.Search || x.Name == request.Search)
.Select<AppointmentPageResponse>()
.ToPageListAsync(request);
}
public async Task<List<AppointmentDetailResponse>> GetAppointmentDetailsByIdAsync(long id)
{
return await _dbContext.Queryable<VisitorAppointmentDetailEntity>()
.Where(x => x.AppointmentId == id)
.Select<AppointmentDetailResponse>()
.ToListAsync();
}
public async Task ConfirmAsync(AppointmentConfirmRequest request)
{
var detail = await _dbContext.Queryable<VisitorAppointmentDetailEntity>().Where(x => x.Id == request.Id).SingleAsync();
detail.ThrowIfNull("预约信息不存在");
var mst = await _dbContext.Queryable<VisitorAppointmentEntity>().Where(x => x.Id == detail.AppointmentId).SingleAsync();
//当前分配的设备codes
var deviceCodes = await _dbContext.Queryable<AppointmentAreaAssignmentEntity>()
.Where(x => x.CustomCode == mst.AreaCustomCode)
.Select(x => x.DeviceCode)
.ToListAsync();
var deviceUniqueNos = await _dbContext.Queryable<DeviceEntity>().Where(x => deviceCodes.Contains(x.Code)).Select(x => x.UniqueNo).ToListAsync();
var userNo = detail.IsExternal
? await _dbContext.Queryable<GainfoExternalEntity>().Where(x => x.IdNo == detail.IdNo).Select(x => x.No).FirstAsync()
: await _dbContext.Queryable<GainfoEntity>().Where(x => x.IdNo == detail.IdNo).Select(x => x.No).FirstAsync();
await _dbContext.InsertAsync(deviceUniqueNos.Select(x => new DeviceTaskEntity
{
CancellationId = 0,
DeviceType = DeviceTypeEnumType.Door,
DeviceUniqueNo = x,
Status = DeviceTaskStatusEnumType.Wait,
UserNo = userNo,
Type = DeviceTaskEnumType.Add,
Priority = 100,
Source = DeviceTaskSourceEnumType.Appointment,
Version = 0
}).ToList());
detail.Status = AppointmentDetailEnumType.Arrived;
await _dbContext.UpdateAsync(detail);
}
//********************************************************** 用户操作 *******************************************************************
public async Task<AppointmentDetailResponse> GetTodayByIdNoAsync(string idNo)
{
var protector = _dataProtectionProvider.CreateProtector("gainfo:external:idno");
try
{
idNo = protector.Unprotect(idNo);
}
catch (Exception)
{
throw new UserFriendlyException("凭证有误");
}
var detail = await _dbContext.Queryable<VisitorAppointmentDetailEntity>()
.Where(x => x.IdNo == idNo)
.Where(x => SqlFunc.DateIsSame(x.Start, DateTime.Today, DateType.Day))
.OrderBy(x => x.Start)
.Select<AppointmentDetailResponse>()
.FirstAsync();
return detail;
}
public async Task<Pagination<AppointmentDetailResponse>> GetRecordPageByIdNoAsync([FromQuery] AppointmentIdNoPageRequest request)
{
var protector = _dataProtectionProvider.CreateProtector("gainfo:external:idno");
try
{
request.IdNo = protector.Unprotect(request.IdNo);
}
catch (Exception)
{
throw new UserFriendlyException("凭证有误");
}
var page = await _dbContext.Queryable<VisitorAppointmentDetailEntity>()
.Where(x => x.IdNo == request.IdNo)
.OrderBy(x => x.Start)
.Select<AppointmentDetailResponse>()
.ToPageListAsync(request);
return page;
}
}