You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Core/Job/UserSyncWorker.cs

106 lines
3.9 KiB
C#

using Furion;
using Furion.DataEncryption;
using Furion.Logging;
using Furion.TaskScheduler;
using Myshipping.Core.Entity;
using Myshipping.Core.Service;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Myshipping.Core.Job
{
/// <summary>
/// 和川用户同步
/// </summary>
public class UserSyncWorker : ISpareTimeWorker
{
[SpareTime(60000, "UserSyncWorker", Description = "同步用户", DoOnce = false, StartNow = true, ExecuteType = SpareTimeExecuteTypes.Serial)]
public void SyncUser(SpareTimer timer, long count)
{
Log.Information($"UserSyncWorker {DateTime.Now}");
var tenantId = 142307070918780L;
var roleId = 175633246363717L;//普通用户
var _sugerClient = App.GetService<ISqlSugarClient>();
_sugerClient.QueryFilter.Clear();
var _repPingTaiUser = App.GetService<SqlSugarRepository<PingTaiUser>>();
var _repUser = App.GetService<SqlSugarRepository<SysUser>>();
var _repEmp = App.GetService<SqlSugarRepository<SysEmp>>();
var _repOrg = App.GetService<SqlSugarRepository<SysOrg>>();
var _repPos = App.GetService<SqlSugarRepository<SysPos>>();
var _repEmpPos = App.GetService<SqlSugarRepository<SysEmpPos>>();
var _repUserRole = App.GetService<SqlSugarRepository<SysUserRole>>();
var users = _repPingTaiUser.Where(u => u.CompId == "5913ab4b-ff5e-4d31-8b6c-09911bdfc0d4").ToList();
var sysUsers = _repUser.Where(x => x.TenantId == tenantId).ToList();
Log.Information($"准备同步用户数据,共{users.Count}条");
var keyDES = App.GetOptions<EncryptKeyOptions>().DES;
var org = _repOrg.FirstOrDefault(x => x.TenantId == tenantId);
var pos = _repPos.FirstOrDefault(x => x.TenantId == tenantId);
_sugerClient.Ado.BeginTran(System.Data.IsolationLevel.ReadCommitted);
try
{
foreach (var user in users)
{
var existUser = sysUsers.Count(x => x.Account == user.CODENAME) > 0;
SysUser sysUser = null;
if (!existUser)
{
sysUser = new SysUser();
}
else
{
sysUser = sysUsers.First(x => x.Account == user.CODENAME);
}
sysUser.Account = user.CODENAME;
sysUser.Password = DESCEncryption.Encrypt(user.PASSWORD, keyDES);
sysUser.TenantId = tenantId;
sysUser.Name = user.SHOWNAME;
sysUser.AdminType = AdminType.None;
if (!existUser)
{
var newUser = _repUser.Insert(sysUser);
var emp = new SysEmp();
emp.Id = sysUser.Id;
emp.OrgId = org.Id;
emp.OrgName = org.Name;
_repEmp.Insert(emp);
var empPos = new SysEmpPos();
empPos.SysEmpId = emp.Id;
empPos.SysPosId = pos.Id;
_repEmpPos.Insert(empPos);
var userrole = new SysUserRole();
userrole.SysRoleId = roleId;
userrole.SysUserId = sysUser.Id;
_repUserRole.Insert(userrole);
}
else
{
_repUser.Update(sysUser);
}
}
_sugerClient.Ado.CommitTran();
}
catch
{
_sugerClient.Ado.RollbackTran();
throw;
}
}
}
}