2025-09-25 14:37:10 +08:00
|
|
|
|
/*
|
|
|
|
|
*所有关于HR_EmployeeSync类的业务代码应在此处编写
|
|
|
|
|
*可使用repository.调用常用方法,获取EF/Dapper等信息
|
|
|
|
|
*如果需要事务请使用repository.DbContextBeginTransaction
|
|
|
|
|
*也可使用DBServerProvider.手动获取数据库相关信息
|
|
|
|
|
*用户信息、权限、角色等使用UserContext.Current操作
|
|
|
|
|
*HR_EmployeeSyncService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
|
|
|
|
|
*/
|
2025-09-26 13:35:45 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
2025-09-25 14:37:10 +08:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Linq.Expressions;
|
2025-09-26 13:35:45 +08:00
|
|
|
|
using System.Net.Http.Headers;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using VOL.Core.BaseProvider;
|
|
|
|
|
using VOL.Core.CacheManager;
|
|
|
|
|
using VOL.Core.DBManager;
|
2025-09-25 14:37:10 +08:00
|
|
|
|
using VOL.Core.Extensions;
|
2025-09-26 13:35:45 +08:00
|
|
|
|
using VOL.Core.Extensions.AutofacManager;
|
|
|
|
|
using VOL.Core.Utilities;
|
|
|
|
|
using VOL.DingTalk.Models.Biz;
|
|
|
|
|
using VOL.DingTalk.Services.Biz;
|
|
|
|
|
using VOL.Entity.DomainModels;
|
2025-09-25 14:37:10 +08:00
|
|
|
|
using VOL.HR.IRepositories;
|
2025-09-26 13:35:45 +08:00
|
|
|
|
using VOL.YSErp.Models.Biz;
|
|
|
|
|
using VOL.YSErp.Services.Biz;
|
2025-09-25 14:37:10 +08:00
|
|
|
|
|
|
|
|
|
namespace VOL.HR.Services
|
|
|
|
|
{
|
|
|
|
|
public partial class HR_EmployeeSyncService
|
|
|
|
|
{
|
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
private readonly IHR_EmployeeSyncRepository _repository;//访问数据库
|
2025-09-26 13:35:45 +08:00
|
|
|
|
private readonly DingTalkService _dingTalkService;
|
|
|
|
|
private readonly YSERPService _ysService;
|
2025-09-25 14:37:10 +08:00
|
|
|
|
|
2025-09-26 13:35:45 +08:00
|
|
|
|
[ActivatorUtilitiesConstructor]
|
|
|
|
|
private ICacheService _cacheService;
|
2025-09-25 14:37:10 +08:00
|
|
|
|
[ActivatorUtilitiesConstructor]
|
|
|
|
|
public HR_EmployeeSyncService(
|
|
|
|
|
IHR_EmployeeSyncRepository dbRepository,
|
2025-09-26 13:35:45 +08:00
|
|
|
|
IHttpContextAccessor httpContextAccessor,
|
|
|
|
|
ICacheService cacheService
|
2025-09-25 14:37:10 +08:00
|
|
|
|
)
|
|
|
|
|
: base(dbRepository)
|
|
|
|
|
{
|
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
|
|
|
|
_repository = dbRepository;
|
|
|
|
|
//多租户会用到这init代码,其他情况可以不用
|
|
|
|
|
//base.Init(dbRepository);
|
2025-09-26 13:35:45 +08:00
|
|
|
|
|
|
|
|
|
_cacheService = cacheService;
|
|
|
|
|
|
|
|
|
|
_dingTalkService = new DingTalkService(new DingTalk.Models.SystemToken(),new DingTalk.Models.DingTalkConfig());
|
|
|
|
|
_ysService = new YSERPService(new YSErp.Models.SystemToken(), new YSErp.Models.YSConfig());
|
|
|
|
|
}
|
|
|
|
|
public Task<List<YSERPEmployee>> GetAllYSEmployees()
|
|
|
|
|
{
|
|
|
|
|
if (_cacheService.Exists("YS_EMP_CACHE"))
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(_cacheService.Get<List<YSERPEmployee>>("YS_EMP_CACHE"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var emps = _ysService.GetAllEmployeesAsync();
|
|
|
|
|
if(emps != null)
|
|
|
|
|
{
|
|
|
|
|
_cacheService.Add("YS_EMP_CACHE", JsonConvert.SerializeObject(emps.Result), 600);
|
|
|
|
|
}
|
|
|
|
|
return emps;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public Task<List<DingTalkEmployee>> GetAllDingTalkEmployees()
|
|
|
|
|
{
|
|
|
|
|
if (_cacheService.Exists("DingTalk_EMP_CACHE"))
|
|
|
|
|
{
|
|
|
|
|
return Task.FromResult(_cacheService.Get<List<DingTalkEmployee>>("DingTalk_EMP_CACHE"));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var emps = _dingTalkService.GetAllEmployeesAsync();
|
|
|
|
|
if(emps != null)
|
|
|
|
|
{
|
|
|
|
|
_cacheService.Add("DingTalk_EMP_CACHE", JsonConvert.SerializeObject(emps.Result), 600);
|
|
|
|
|
}
|
|
|
|
|
return emps;
|
|
|
|
|
}
|
2025-09-25 14:37:10 +08:00
|
|
|
|
}
|
2025-09-26 13:35:45 +08:00
|
|
|
|
|
|
|
|
|
public Task GenEmpSystemShip()
|
|
|
|
|
{
|
|
|
|
|
List<DingTalkEmployee> dingTalkEmployees = [];
|
|
|
|
|
List<YSERPEmployee> ysERPEmployees = [];
|
|
|
|
|
|
|
|
|
|
var ysTask = GetAllYSEmployees().ContinueWith(x =>
|
|
|
|
|
{
|
|
|
|
|
ysERPEmployees = x.Result;
|
|
|
|
|
});
|
|
|
|
|
var dingTalkTask = GetAllDingTalkEmployees().ContinueWith(x =>
|
|
|
|
|
{
|
|
|
|
|
dingTalkEmployees = x.Result;
|
|
|
|
|
});
|
|
|
|
|
Task.WaitAll(ysTask, dingTalkTask);
|
|
|
|
|
if (ysERPEmployees.Count != 0)
|
|
|
|
|
{
|
|
|
|
|
var newList = new List<HR_EmployeeSync>();
|
|
|
|
|
ysERPEmployees.ForEach(emp =>
|
|
|
|
|
{
|
|
|
|
|
var dingTalkInfo = dingTalkEmployees?.FirstOrDefault(e => e.Mobile == emp.mobile);
|
|
|
|
|
if (dingTalkInfo != null)
|
|
|
|
|
{
|
|
|
|
|
//_repository.AddRange()
|
|
|
|
|
newList.Add(new HR_EmployeeSync
|
|
|
|
|
{
|
|
|
|
|
EmpYSID = emp.id,
|
|
|
|
|
EmpYSMobile = emp.mobile,
|
|
|
|
|
EmpYSDeptID = emp.dept_id,
|
|
|
|
|
EmpJobNumber = emp.code,
|
|
|
|
|
|
|
|
|
|
EmpDingDingID = dingTalkInfo.UserId,
|
|
|
|
|
EmpDingDingMobile = dingTalkInfo.Mobile,
|
|
|
|
|
EmpLastSyncInfo = "Init generate for system.",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
//_repository.AddRange(newList)
|
|
|
|
|
var db = DBServerProvider.SqlSugarClient;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
db.BeginTran();
|
|
|
|
|
// 清空重新生成
|
|
|
|
|
db.Deleteable<HR_EmployeeSync>();
|
|
|
|
|
db.Fastest<HR_EmployeeSync>().BulkCopy(newList);
|
|
|
|
|
db.CommitTran();
|
|
|
|
|
}
|
|
|
|
|
catch (Exception)
|
|
|
|
|
{
|
|
|
|
|
db.RollbackTran();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-25 14:37:10 +08:00
|
|
|
|
}
|