Files
jinko.yikatong/Src/Dlhis.External/Appointment/Services/Implements/AppointmentService.cs

119 lines
4.1 KiB
C#

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<VisitorAppointmentEntity>().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,
Company = request.Company,
ApplyTime = request.ApplyTime,
ApplyArea = request.ApplyArea,
Factory = request.Factory,
Reason = request.Reason,
Start = request.Start,
End = request.End,
Remark = request.Remark,
IsEnterWorkplace = request.IsEnterWorkplace,
});
foreach (var item in request.Items)
{
var gainfo = await _dbContext.Queryable<GainfoEntity>().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.Company,
Phone = item.Phone
};
await _dbContext.InsertAsync(gainfo);
}
else
{
if (gainfo.Type == GainfoUserTypeEnumType.External)
{
gainfo.Source = request.Company;
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,
AppointmentId = id,
No = request.No,
Name = item.Name,
Phone = item.Phone,
Remark = item.Remark,
Status = AppointmentDetailEnumType.Wait,
LicensePlate = item.LicensePlate
};
await _dbContext.InsertAsync(detail);
}
}
private async Task<string> 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<GainfoEntity>()
.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');
}
}