Files
jinko.yikatong/Src/Dlhis.Basic/Organization/Services/Implements/GainfoCardService.cs

87 lines
3.3 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)
{
var gainfo = await _gainfoStore.Queryable().Where(x => x.No == request.UserNo).FirstAsync();
gainfo.ThrowIfNull("档案不存在");
gainfo.CardNo = request.CardNo;
await _gainfoStore.UpdateAsync(gainfo);
await _dbContext.InsertAsync(request.Adapt<GainfoCardRecordEntity>());
}
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, deviceCodes, "卡类型编辑下发");
}
}
await _dbContext.UpdateAsync(gainfos);
}
}