85 lines
2.6 KiB
C#
85 lines
2.6 KiB
C#
using Dlhis.Basic.Organization.Request;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Kurisu.AspNetCore.Utils;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Kurisu.SqlSugar.Services;
|
|
using Mapster;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Basic.Organization.Services.Implements;
|
|
|
|
public class GainfoOutSourceService : IGainfoOutSourceService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly RedisCache _redisCache;
|
|
|
|
public GainfoOutSourceService(IDbContext dbContext, RedisCache redisCache)
|
|
{
|
|
_dbContext = dbContext;
|
|
_redisCache = redisCache;
|
|
}
|
|
|
|
public async Task EntryAsync(GainfoOutEntryRequest request)
|
|
{
|
|
await SaveAsync(request);
|
|
}
|
|
|
|
public async Task ResignAsync(GainfoOutResignRequest request)
|
|
{
|
|
//todo 生成任务处理
|
|
}
|
|
|
|
public async Task EditAsync(GainfoOutEntryRequest request)
|
|
{
|
|
await SaveAsync(request);
|
|
}
|
|
|
|
|
|
//********************************************************************** help methods ***********************************************************************************
|
|
|
|
private async Task<string> GetOutSourceCodeAsync()
|
|
{
|
|
await using var handler = await _redisCache.LockAsync("gainfo:outsource:code-generate", expiry: TimeSpan.FromSeconds(3), retryInterval: TimeSpan.FromSeconds(1), retryCount: 3);
|
|
handler.Acquired.ThrowIfFalse("生成虚拟工号失败");
|
|
|
|
int code;
|
|
var entity = await _dbContext.Queryable<OutSourceEntity>().OrderBy(x => x.Id, OrderByType.Desc).FirstAsync();
|
|
if (entity == null)
|
|
{
|
|
code = 1;
|
|
}
|
|
else
|
|
{
|
|
code = int.Parse(entity.Code.Replace("LS", "").Replace("0", "")) + 1;
|
|
}
|
|
|
|
return "LS" + code.ToString().PadLeft(6, '0');
|
|
}
|
|
|
|
private async Task SaveAsync(GainfoOutEntryRequest request)
|
|
{
|
|
if (!request.Id.HasValue)
|
|
{
|
|
var entity = request.Adapt<GainfoEntity>();
|
|
entity.No = Guid.NewGuid().ToString();
|
|
entity.Code = await GetOutSourceCodeAsync();
|
|
|
|
await _dbContext.InsertAsync(entity);
|
|
await _dbContext.InsertAsync(new OutSourceEntity
|
|
{
|
|
Code = entity.Code
|
|
});
|
|
}
|
|
else
|
|
{
|
|
var entity = await _dbContext.Queryable<GainfoEntity>().FirstAsync(x => x.Id == request.Id);
|
|
entity.ThrowIfNull("档案数据不存在");
|
|
|
|
(await _dbContext.Queryable<OutSourceEntity>().AnyAsync(x => x.Code == entity.Code)).ThrowIfFalse("不可编辑非外包人员档案数据");
|
|
|
|
request.Adapt(entity);
|
|
|
|
await _dbContext.UpdateAsync(entity);
|
|
}
|
|
}
|
|
} |