453 lines
18 KiB
C#
453 lines
18 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.Basic.Enums;
|
|
using Dlhis.Entity.Device.Entities;
|
|
using Dlhis.Entity.Device.Enums;
|
|
using Dlhis.Entity.Visitor.Entity;
|
|
using Dlhis.Scheduled.Api;
|
|
using Dlhis.Scheduled.Api.Request;
|
|
using Dlhis.Sys.Api;
|
|
using Kurisu.AspNetCore.Authentication.Abstractions;
|
|
using Kurisu.AspNetCore.DataAccess.Dto;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Extensions;
|
|
using Kurisu.AspNetCore.DataAccess.SqlSugar.Services;
|
|
using Kurisu.AspNetCore.Utils.Extensions;
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
using SqlSugar;
|
|
|
|
namespace Dlhis.Basic.Organization.Services.Implements;
|
|
|
|
public class GainfoService : IGainfoService
|
|
{
|
|
private readonly IGainfoStore _gainfoStore;
|
|
private readonly IDbContext _dbContext;
|
|
private readonly IDataProtectionProvider _dataProtectionProvider;
|
|
private readonly ICurrentUser _currentUser;
|
|
private readonly ITenantApi _tenantApi;
|
|
private readonly IHangfireApi _hangfireApi;
|
|
private readonly IDevicePermissionService _devicePermissionService;
|
|
private readonly IGainfoPermissionService _gainfoPermissionService;
|
|
private readonly ILogger<GainfoService> _logger;
|
|
|
|
public GainfoService(IGainfoStore gainfoStore,
|
|
IDbContext dbContext,
|
|
IDataProtectionProvider dataProtectionProvider,
|
|
ICurrentUser currentUser,
|
|
ITenantApi tenantApi,
|
|
IHangfireApi hangfireApi,
|
|
IDevicePermissionService devicePermissionService,
|
|
IGainfoPermissionService gainfoPermissionService,
|
|
ILogger<GainfoService> logger)
|
|
{
|
|
_gainfoStore = gainfoStore;
|
|
_dbContext = dbContext;
|
|
_dataProtectionProvider = dataProtectionProvider;
|
|
_currentUser = currentUser;
|
|
_tenantApi = tenantApi;
|
|
_hangfireApi = hangfireApi;
|
|
_devicePermissionService = devicePermissionService;
|
|
_gainfoPermissionService = gainfoPermissionService;
|
|
_logger = logger;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 档案分页
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
public async Task<Pagination<GainfoPageResponse>> GetPageAsync(GainfoPageRequest request)
|
|
{
|
|
//根据组织架构搜索档案
|
|
var where = LinqExpressionExtensions.False<GainfoEntity>();
|
|
foreach (var item in request.DepartmentSelections)
|
|
{
|
|
var searchDepartments = await GetRecursionDepartmentNosAsync(item.DepartmentNos);
|
|
where = searchDepartments.Any()
|
|
? where.Or(gainfo => gainfo.Factory == item.Tenant && searchDepartments.Contains(gainfo.DepartmentNo))
|
|
: where.Or(gainfo => gainfo.Factory == item.Tenant);
|
|
}
|
|
|
|
//获取所有厂区
|
|
var factories = request.DepartmentSelections.Select(x => x.Tenant).ToList();
|
|
if (factories.Count == 0)
|
|
{
|
|
factories = _currentUser.GetUserClaim("tenants").Split(",").ToList();
|
|
}
|
|
|
|
var today = DateTime.Today;
|
|
|
|
var page = await _gainfoStore.Queryable(request.Status)
|
|
.WhereIF(request.SearchType != GainfoSearchEnumType.BusinessTrip && request.DepartmentSelections?.Any() == true, where)
|
|
.WhereIF(request.SearchType != GainfoSearchEnumType.BusinessTrip && request.DepartmentSelections?.Any() == false, gainfo => factories.Contains(gainfo.Factory))
|
|
.WhereIF(request.SearchType == GainfoSearchEnumType.Internal, gainfo => SqlFunc.Subqueryable<OutsourceEntity>().Where(x => x.Code == gainfo.Code).NotAny())
|
|
.WhereIF(request.SearchType == GainfoSearchEnumType.Outsource, gainfo => SqlFunc.Subqueryable<OutsourceEntity>().Where(x => x.Code == gainfo.Code).Any())
|
|
.WhereIF(request.SearchType == GainfoSearchEnumType.BusinessTrip, gainfo =>
|
|
SqlFunc.Subqueryable<BusinessTripEntity>().Where(x => x.Code == gainfo.Code && factories.Contains(x.Destination) && SqlFunc.DateIsSame(x.BeginDate, today)).Any())
|
|
.WhereIF(request.Search.IsPresent(), gainfo =>
|
|
gainfo.Name.Contains(request.Search)
|
|
|| gainfo.Code == request.Search
|
|
|| gainfo.IdNo == request.Search)
|
|
.LeftJoin<DepartmentEntity>((x, department) => x.DepartmentNo == department.No)
|
|
.LeftJoin<BasicTypeEntity>((x, department, type) => type.Code == x.CardType.ToString() && type.Type == "timeperiod")
|
|
.Select((x, department, type) => new GainfoPageResponse
|
|
{
|
|
DepartmentName = department.Name,
|
|
CardType = x.CardType,
|
|
CardTypeName = type.Display
|
|
}, true)
|
|
.ToPageListAsync(request);
|
|
|
|
//外包人员部门号处理
|
|
if (request.SearchType == GainfoSearchEnumType.Outsource)
|
|
{
|
|
var codes = page.Data.Select(x => x.Code).ToList();
|
|
var outsourceInfos = await _dbContext.Queryable<OutsourceEntity>().Where(x => codes.Contains(x.Code)).Select(x => new { x.Code, x.Supplier }).ToListAsync();
|
|
|
|
page.Data.ForEach(item => { item.DepartmentName = outsourceInfos.FirstOrDefault(x => x.Code == item.Code)?.Supplier; });
|
|
}
|
|
|
|
return page;
|
|
}
|
|
|
|
public async Task<Pagination<GainfoAllPageResponse>> GetPageByCodeAsync(string code)
|
|
{
|
|
var page = await _gainfoStore.Queryable()
|
|
.Where(x => x.Code == code)
|
|
.Select<GainfoAllPageResponse>()
|
|
.ToPageListAsync(new PageDto { PageIndex = 1, PageSize = 1 });
|
|
|
|
var tenantsResult = await _tenantApi.GetTenantsInternalAsync();
|
|
var tenants = tenantsResult.Data;
|
|
|
|
page.Data.ForEach(x =>
|
|
{
|
|
x.Workplace = tenants.FirstOrDefault(item => item.Code == x.Workplace)?.Name;
|
|
x.Factory = tenants.FirstOrDefault(item => item.Code == x.Factory)?.Name;
|
|
});
|
|
|
|
return page;
|
|
}
|
|
|
|
public async Task<GainfoCurrentResponse> CurrentUserInfoAsync()
|
|
{
|
|
var code = _currentUser.GetUserClaim("jinko-code");
|
|
if (code.IsEmpty())
|
|
{
|
|
_logger.LogWarning("工号为空尝试获取人员信息");
|
|
return null;
|
|
}
|
|
|
|
var gainfo = await _gainfoStore.Queryable().Where(x => x.Code == code).SingleAsync();
|
|
if (gainfo == null)
|
|
{
|
|
//开启同步处理
|
|
await _hangfireApi.SyncSapPersonAsync(new SapNoRequest { Code = code });
|
|
return null;
|
|
}
|
|
|
|
var info = await _gainfoStore.Queryable(GainfoStatusEnumType.Working)
|
|
.LeftJoin<DepartmentEntity>((ga, department) => ga.DepartmentNo == department.No)
|
|
.LeftJoin<GainfoMealEntity>((ga, department, mealUser) => ga.No == mealUser.UserNo)
|
|
.LeftJoin<BasicTypeEntity>((ga, department, mealUser, type) => type.Code == ga.CardType.ToString() && type.Type == "timeperiod")
|
|
.Where((ga, department) => ga.Id == gainfo.Id)
|
|
.Select((ga, department, mealUser, type) => new GainfoCurrentResponse
|
|
{
|
|
UserNo = ga.No,
|
|
Birthday = ga.Birthday,
|
|
DepartmentNo = department.No,
|
|
DepartmentName = department.Name,
|
|
Gender = ga.Gender,
|
|
UserCode = ga.Code,
|
|
UserName = ga.Name,
|
|
Factory = ga.Factory,
|
|
MealInfo = new GainfoCurrentMealInfoResponse
|
|
{
|
|
Balance = mealUser.Balance
|
|
},
|
|
CardType = ga.CardType,
|
|
CardTypeName = type.Display
|
|
}).SingleAsync();
|
|
|
|
if (info != null)
|
|
{
|
|
try
|
|
{
|
|
var tenantResult = await _tenantApi.GetTenantByCodeAsync(info.Factory);
|
|
if (tenantResult is { Data: not null })
|
|
{
|
|
info.Factory = tenantResult.Data.Name;
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
// ignored
|
|
}
|
|
}
|
|
|
|
return info;
|
|
}
|
|
|
|
public async Task<object> CheckIdNoAndNameMatchAsync(GainfoCheckIdNoAndNameRequest request)
|
|
{
|
|
var gainfo = await _gainfoStore.Queryable(GainfoStatusEnumType.External).FirstAsync(x => x.IdNo == request.IdNo && x.Name == request.Name)
|
|
?? await _gainfoStore.Queryable(GainfoStatusEnumType.Working).FirstAsync(x => x.IdNo == request.IdNo && x.Name == request.Name);
|
|
|
|
gainfo.ThrowIfNull("验证失败!");
|
|
|
|
var protector = _dataProtectionProvider.CreateProtector("gainfo:external:idno");
|
|
return new
|
|
{
|
|
loginPass = protector.Protect(request.IdNo),
|
|
UserNo = gainfo.No
|
|
};
|
|
}
|
|
|
|
|
|
public async Task ResignAsync(GainfoResignRequest request)
|
|
{
|
|
var gainfo = await _dbContext.Queryable<GainfoEntity>().FirstAsync(x => x.Id == request.Id);
|
|
gainfo.ThrowIfNull("档案数据不存在");
|
|
|
|
if (request.IsOutsource)
|
|
{
|
|
var outsource = await _dbContext.Queryable<OutsourceEntity>().FirstAsync(x => x.Code == gainfo.Code);
|
|
(outsource == null).ThrowIfTrue("非外包人员不可手动离职");
|
|
}
|
|
|
|
await _gainfoPermissionService.ResetPermissionsAsync(new GainfoPermissionResetRequest
|
|
{
|
|
UserNo = gainfo.No,
|
|
IsLeave = true
|
|
});
|
|
|
|
//先写入离职表
|
|
gainfo.IdentityUserId = (int)gainfo.Id; //记录原始id
|
|
gainfo.ResignationDate = request.Date;
|
|
gainfo.Remark = request.Reason;
|
|
gainfo.Id = 0;
|
|
|
|
if (!await _dbContext.Queryable<GainfoEntity>().AS("GainfoLeave").AnyAsync(x => x.Code == gainfo.Code))
|
|
{
|
|
await _dbContext.Client.Insertable(gainfo).AS("GainfoLeave").ExecuteCommandAsync();
|
|
}
|
|
|
|
gainfo.Id = gainfo.IdentityUserId;
|
|
await _dbContext.DeleteReallyAsync(gainfo);
|
|
}
|
|
|
|
public async Task SetSecurityWhiteAsync(string userCode)
|
|
{
|
|
var gainfo = await _gainfoStore.Queryable().Where(x => x.Code == userCode).SingleAsync();
|
|
gainfo.ThrowIfNull("档案信息不存在");
|
|
gainfo.Type = GainfoUserTypeEnumType.SecurityWhite;
|
|
await _dbContext.UpdateAsync(gainfo);
|
|
|
|
var deviceCodes = await _devicePermissionService.GetAssignDeviceCodesAsync(gainfo.No);
|
|
|
|
var dhDevices = await _dbContext.Queryable<DeviceEntity>().LeftJoin<DeviceTemplateEntity>((x, xt) => x.TemplateCode == xt.Code)
|
|
.Where((x, xt) => xt.Brand == "大华")
|
|
.Where(x => deviceCodes.Contains(x.Code))
|
|
.ToListAsync();
|
|
|
|
await _dbContext.InsertAsync(dhDevices.Select(x => new DeviceTaskEntity
|
|
{
|
|
Type = DeviceTaskEnumType.Update,
|
|
DeviceType = x.Type,
|
|
UserId = gainfo.Id,
|
|
UserNo = gainfo.No,
|
|
DeviceUniqueNo = x.UniqueNo,
|
|
Status = DeviceTaskStatusEnumType.Wait,
|
|
Version = 0,
|
|
ResultMessage = null,
|
|
CancellationId = 0,
|
|
Source = DeviceTaskSourceEnumType.Manually,
|
|
Priority = 60,
|
|
TenantId = x.TenantId
|
|
}).ToList());
|
|
}
|
|
|
|
/// <summary>
|
|
/// 档案列表
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
public async Task<Pagination<GainfoListResponse>> GetGainfoListAsync(GainfoListRequest request)
|
|
{
|
|
var page = await _gainfoStore.Queryable(request.Status)
|
|
.LeftJoin<DepartmentEntity>((ga, de) => ga.DepartmentNo == de.No)
|
|
.WhereIF(!request.Name.IsEmpty(), ga => ga.Name == request.Name)
|
|
.WhereIF(!request.IdNo.IsEmpty(), ga => ga.IdNo == request.IdNo)
|
|
.WhereIF(!request.DepartmentNo.IsEmpty(), ga => ga.DepartmentNo == request.DepartmentNo)
|
|
.WhereIF(!request.Code.IsEmpty(), ga => ga.Code == request.Code)
|
|
.Select((ga, de) => new GainfoListResponse
|
|
{
|
|
No = ga.No,
|
|
Name = ga.Name,
|
|
Code = ga.Code,
|
|
IdNo = ga.IdNo,
|
|
Gender = ga.Gender,
|
|
Phone = ga.Phone,
|
|
DepartmentNo = de.No,
|
|
DepartmentName = de.Name,
|
|
Address = ga.Address,
|
|
}).ToPageListAsync(request);
|
|
|
|
return page;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 档案详情
|
|
/// </summary>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
public async Task<GainfoDetailResponse> GetGainfoDetailAsync(GainfoDetailRequest request)
|
|
{
|
|
var info = await _gainfoStore.Queryable(request.Status)
|
|
.LeftJoin<DepartmentEntity>((ga, de) => ga.DepartmentNo == de.No)
|
|
.LeftJoin<PositionEntity>((ga, de, pos) => ga.PositionNo == pos.No)
|
|
.LeftJoin<BasicTypeEntity>((ga, de, pos, type) => type.Code == ga.CardType.ToString() && type.Type == "timeperiod")
|
|
.Where(ga => ga.No == request.No)
|
|
.Select((ga, de, pos, type) => new GainfoDetailResponse
|
|
{
|
|
No = ga.No,
|
|
SapNo = ga.SapNo,
|
|
Name = ga.Name,
|
|
Code = ga.Code,
|
|
IdClass = ga.IdClass,
|
|
IdNo = ga.IdNo,
|
|
Gender = ga.Gender,
|
|
Address = ga.Address,
|
|
Birthday = ga.Birthday,
|
|
Phone = ga.Phone,
|
|
Grade = ga.Grade,
|
|
Title = ga.Title,
|
|
Degree = ga.Degree,
|
|
Werks = ga.Werks,
|
|
CostId = ga.CostId,
|
|
NativeName = ga.Native,
|
|
CountryName = ga.CountryNo,
|
|
NationName = ga.Nation,
|
|
ClassName = ga.IdClass,
|
|
|
|
EntryDate = ga.EntryDate,
|
|
|
|
Workplace = ga.Workplace,
|
|
Factory = ga.Factory,
|
|
|
|
DepartmentName = de.Name,
|
|
Position = pos.Name,
|
|
CardType = ga.CardType,
|
|
CardTypeName = type.Display
|
|
})
|
|
.SingleAsync();
|
|
|
|
if (info != null)
|
|
{
|
|
var searchTypes = new List<string> { "native", "country", "nation", "cls" };
|
|
var types = await _dbContext.Queryable<BasicTypeEntity>().Where(x => searchTypes.Contains(x.Type)).ToListAsync();
|
|
|
|
info.NativeName = types.Where(x => x.Type == "native").FirstOrDefault(x => x.Code == info.NativeName)?.Display;
|
|
info.CountryName = types.Where(x => x.Type == "country").FirstOrDefault(x => x.Code == info.CountryName)?.Display ?? "中国";
|
|
info.NationName = types.Where(x => x.Type == "nation").FirstOrDefault(x => x.Code == info.NationName)?.Display;
|
|
info.ClassName = types.Where(x => x.Type == "cls").FirstOrDefault(x => x.Code == info.ClassName)?.Display ?? "身份证";
|
|
|
|
if (request.WithFace)
|
|
{
|
|
info.Face = await _dbContext.ChangeDb("face").Queryable<GainfoFaceEntity>().Where(x => x.UserCode == info.Code).Select(x => x.Face).SingleAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
var tenantsResult = await _tenantApi.GetTenantsInternalAsync();
|
|
var tenants = tenantsResult.Data;
|
|
|
|
info.Workplace = tenants.FirstOrDefault(x => x.Code == info.Workplace)?.Name;
|
|
info.Factory = tenants.FirstOrDefault(x => x.Code == info.Factory)?.Name;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
//ignore
|
|
}
|
|
}
|
|
|
|
return info;
|
|
}
|
|
|
|
public async Task MigrationAsync(MigrationRequest request)
|
|
{
|
|
//档案数据
|
|
var gainfo = await _gainfoStore.Queryable().Where(x => x.Code == request.Code).FirstAsync();
|
|
if (gainfo == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//卡号为空则更新
|
|
if (gainfo.CardNo.IsEmpty())
|
|
{
|
|
gainfo.CardNo = request.CardNo;
|
|
await _gainfoStore.UpdateAsync(gainfo);
|
|
}
|
|
|
|
//外部传入人脸
|
|
if (request.Face.IsPresent())
|
|
{
|
|
var faceDbContext = _dbContext.ChangeDb("face");
|
|
var exists = await faceDbContext.Queryable<GainfoFaceEntity>().AnyAsync(x => x.UserCode == gainfo.Code);
|
|
//人脸不存在则写入
|
|
if (!exists)
|
|
{
|
|
await faceDbContext.InsertAsync(new GainfoFaceEntity
|
|
{
|
|
UserCode = gainfo.Code,
|
|
Face = request.Face
|
|
});
|
|
|
|
//海宁触发权限下发
|
|
if (gainfo.Workplace == "5")
|
|
{
|
|
await _devicePermissionService.GenerateDeviceTasksAsync(gainfo.No, DeviceTaskEnumType.Add, DeviceTaskSourceEnumType.Migration);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//-------------------------------------------help methods ------------------------------------------------
|
|
|
|
/// <summary>
|
|
/// 递归获取部门No
|
|
/// </summary>
|
|
/// <param name="departmentNos"></param>
|
|
/// <returns></returns>
|
|
private async Task<List<string>> GetRecursionDepartmentNosAsync(List<string> departmentNos)
|
|
{
|
|
if (departmentNos.IsEmpty())
|
|
{
|
|
return new List<string>();
|
|
}
|
|
|
|
var departments = await _dbContext.Queryable<DepartmentEntity>().ToListAsync();
|
|
var currentNos = departments.Where(x => departmentNos.Contains(x.No)).ToList();
|
|
|
|
foreach (var item in currentNos)
|
|
{
|
|
RecursionGetNext(item.No, departments, departmentNos);
|
|
}
|
|
|
|
return departmentNos.Distinct().ToList();
|
|
|
|
//local method
|
|
void RecursionGetNext(string currentNo, List<DepartmentEntity> departments, List<string> result)
|
|
{
|
|
var next = departments.Where(x => x.PNo == currentNo).ToList();
|
|
|
|
foreach (var item in next)
|
|
{
|
|
result.Add(item.No);
|
|
RecursionGetNext(item.No, departments, result);
|
|
}
|
|
}
|
|
}
|
|
} |