using Dlhis.Entity.Basic.Entity; using Dlhis.Entity.Basic.Enums; using Dlhis.Entity.Visitor.Entity; using Dlhis.Entity.Visitor.Enums; using Dlhis.External.Appointment.Request; using Kurisu.AspNetCore.DataAccess.SqlSugar.Services; using Kurisu.AspNetCore.Utils; using Kurisu.AspNetCore.Utils.Extensions; using SqlSugar; namespace Dlhis.External.Appointment.Services.Implements; public class AppointmentService : IAppointmentService { private readonly IDbContext _dbContext; private readonly RedisCache _redisCache; public AppointmentService(IDbContext dbContext, RedisCache redisCache) { _dbContext = dbContext; _redisCache = redisCache; } public async Task ReceiveAsync(AppointmentReceiveRequest request) { var exists = await _dbContext.Queryable().Where(x => x.No == request.No).AnyAsync(); exists.ThrowIfTrue("已经存在申请"); var id = await _dbContext.InsertReturnIdentityAsync(new VisitorAppointmentEntity { Id = 0, CreatedBy = 0, CreateTime = default, ModifiedBy = 0, ModifiedTime = default, No = request.No, Title = request.Title, Code = request.Code, Source = request.Source, Date = request.Date, ApplyDoorArea = request.ApplyDoorArea, Factory = request.Factory, Reason = request.Reason, DoorStart = request.DoorStart, DoorEnd = request.DoorEnd, Remark = request.Remark, IsEnterWorkplace = request.IsEnterWorkplace, ApplyWorkplaceArea = request.ApplyWorkplaceArea, WorkplaceStart = request.WorkplaceStart, WorkplaceEnd = request.WorkplaceEnd }); foreach (var item in request.Items) { var gainfo = await _dbContext.Queryable().Where(x => x.IdNo == item.IdNo || x.Code == item.IdNo).SingleAsync(); //外部人员 if (gainfo == null) { gainfo = new GainfoEntity { No = Guid.NewGuid().ToString(), Code = await GetExternalCodeAsync(), Type = GainfoUserTypeEnumType.External, IdNo = item.IdNo, Name = item.Name, Source = request.Source, Phone = item.Phone }; await _dbContext.InsertAsync(gainfo); } else { if (gainfo.Type == GainfoUserTypeEnumType.External) { gainfo.Source = request.Source; gainfo.Phone = item.Phone; await _dbContext.UpdateAsync(gainfo); } } (gainfo.Type == GainfoUserTypeEnumType.External && request.IsEnterWorkplace && item.ImageUrl.IsEmpty()).ThrowIfTrue($"{item.Name}的社保照片不可为空"); var detail = new VisitorAppointmentDetailEntity { UserNo = gainfo.No, No = request.No, IdType = item.IdType, IdNo = item.IdNo, Name = item.Name, Phone = item.Phone, Remark = item.Remark, Status = AppointmentDetailEnumType.Wait, LicensePlate = item.LicensePlate }; await _dbContext.InsertAsync(detail); } } private async Task GetExternalCodeAsync() { await using var handler = await _redisCache.LockAsync("gainfo:external:code-generate", expiry: TimeSpan.FromSeconds(3), retryInterval: TimeSpan.FromSeconds(1), retryCount: 3); handler.Acquired.ThrowIfFalse("生成虚拟工号失败"); int code; var entity = await _dbContext.Queryable() .Where(x => x.Type == GainfoUserTypeEnumType.External && x.Code.StartsWith("EXT")) .OrderBy(x => x.Id, OrderByType.Desc) .FirstAsync(); if (entity == null) { code = 1; } else { code = int.Parse(entity.Code.Replace("EXT", "").TrimStart('0')) + 1; } return "EXT" + code.ToString().PadLeft(6, '0'); } }