109 lines
4.0 KiB
C#
109 lines
4.0 KiB
C#
using Dlhis.Basic.Device.Services;
|
|
using Dlhis.Basic.Organization.Request;
|
|
using Dlhis.Basic.Organization.Response;
|
|
using Dlhis.Entity.Basic.Entity;
|
|
using Dlhis.Entity.Device.Enums;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Mapster;
|
|
|
|
namespace Dlhis.Basic.Organization.Services.Implements;
|
|
|
|
public class GainfoCardService : IGainfoCardService
|
|
{
|
|
private readonly IDbContext _dbContext;
|
|
private readonly IGainfoStore _gainfoStore;
|
|
private readonly IDevicePermissionAnalyzeService _devicePermissionAnalyzeService;
|
|
private readonly IGainfoPermissionService _gainfoPermissionService;
|
|
|
|
public GainfoCardService(IDbContext dbContext,
|
|
IGainfoStore gainfoStore,
|
|
IDevicePermissionAnalyzeService devicePermissionAnalyzeService,
|
|
IGainfoPermissionService gainfoPermissionService)
|
|
{
|
|
_dbContext = dbContext;
|
|
_gainfoStore = gainfoStore;
|
|
_devicePermissionAnalyzeService = devicePermissionAnalyzeService;
|
|
_gainfoPermissionService = gainfoPermissionService;
|
|
}
|
|
|
|
public async Task ChangeCardAsync(GainfoChangeCardRequest request)
|
|
{
|
|
if (await _gainfoStore.Queryable().Where(x => x.CardNo == request.CardNo).AnyAsync())
|
|
{
|
|
true.ThrowIfTrue("卡号已分配");
|
|
}
|
|
|
|
var gainfo = await _gainfoStore.Queryable().Where(x => x.No == request.UserNo).FirstAsync();
|
|
gainfo.ThrowIfNull("档案不存在");
|
|
if (gainfo.CardNo == request.CardNo)
|
|
{
|
|
true.ThrowIfTrue("卡号未发生变化");
|
|
}
|
|
|
|
if (gainfo.CardNo.IsPresent())
|
|
{
|
|
var oldCardRecord = new GainfoCardRecordEntity
|
|
{
|
|
UserNo = gainfo.No,
|
|
CardNo = gainfo.CardNo,
|
|
Reason = "换卡",
|
|
Remark = $"换卡前卡号:{gainfo.CardNo}"
|
|
};
|
|
oldCardRecord.TenantId = gainfo.Factory;
|
|
await _dbContext.InsertAsync(oldCardRecord);
|
|
}
|
|
|
|
gainfo.CardNo = request.CardNo;
|
|
await _gainfoStore.UpdateAsync(gainfo);
|
|
|
|
await _gainfoPermissionService.ResetPermissionsAsync(gainfo.No, DeviceTaskEnumType.Update, null, null, "更新卡号下发");
|
|
}
|
|
|
|
public async Task<List<ChangeCardResponse>> GetChangeRecordsAsync(string userNo)
|
|
{
|
|
return await _dbContext.Queryable<GainfoCardRecordEntity>()
|
|
.LeftJoin<GainfoEntity>((x, ga) => x.UserNo == ga.No)
|
|
.Where(x => x.UserNo == userNo)
|
|
.Select((x, ga) => new ChangeCardResponse
|
|
{
|
|
UserNo = x.UserNo,
|
|
UserCode = ga.Code,
|
|
UserName = ga.Name,
|
|
CardNo = x.CardNo,
|
|
Reason = x.Reason,
|
|
Remark = x.Remark,
|
|
CreateTime = x.CreateTime
|
|
})
|
|
.ToListAsync();
|
|
}
|
|
|
|
|
|
public async Task EditCardTypeAsync(List<string> userCodes, int typeId)
|
|
{
|
|
var gainfos = await _dbContext.Queryable<GainfoEntity>().Where(x => userCodes.Contains(x.Code)).ToListAsync();
|
|
(gainfos.Count == userCodes.Count).ThrowIfFalse("人员不存在");
|
|
|
|
var result = await _devicePermissionAnalyzeService.GetUserAssignDeviceCodesWithTypeInTenantAsync(gainfos.Select(x => x.No).ToList());
|
|
foreach (var gainfo in gainfos)
|
|
{
|
|
var userNo = gainfo.No;
|
|
var myDeviceCodes = result[userNo].SelectMany(x => x.AssignTypes.SelectMany(p => p.Codes)).Distinct().ToList();
|
|
|
|
var deviceCodes = await _dbContext.Queryable<DeviceEntity>()
|
|
.Where(x => myDeviceCodes.Contains(x.Code))
|
|
.Where(x => x.EnableTimePeriod)
|
|
.Select(x => x.Code)
|
|
.ToListAsync();
|
|
|
|
gainfo.CardType = typeId;
|
|
|
|
if (deviceCodes.IsPresent())
|
|
{
|
|
await _gainfoPermissionService.ResetPermissionsAsync(userNo, DeviceTaskEnumType.Update, null, deviceCodes, "卡类型编辑下发");
|
|
}
|
|
}
|
|
|
|
await _dbContext.UpdateAsync(gainfos);
|
|
}
|
|
} |