不再同步erp的员工表

This commit is contained in:
2024-11-01 16:48:37 +08:00
parent f83990b9ad
commit 8c1d98456c

View File

@@ -1501,17 +1501,17 @@ namespace Tnb.ProductionMgr
try try
{ {
var erpdb = _db.AsTenant().GetConnection("erpdb"); var erpdb = _db.AsTenant().GetConnection("erpdb");
List<ErpUserDto> persons = await erpdb.Queryable<ErpBdPsndoc>() // List<ErpUserDto> persons = await erpdb.Queryable<ErpBdPsndoc>()
.Where(x=>x.ENABLESTATE!=1) // .Where(x=>x.ENABLESTATE!=1)
.Select(x=>new ErpUserDto // .Select(x=>new ErpUserDto
{ // {
PERSON_ID = x.ID, // PERSON_ID = x.ID,
CODE = x.CODE, // CODE = x.CODE,
NAME = x.NAME, // NAME = x.NAME,
TYPE = "1", // TYPE = "1",
ENABLESTATE = x.ENABLESTATE // ENABLESTATE = x.ENABLESTATE
}) // })
.ToListAsync(); // .ToListAsync();
List<ErpUserDto> users = await erpdb.Queryable<ErpSmUser>() List<ErpUserDto> users = await erpdb.Queryable<ErpSmUser>()
.Where(x=>x.ENABLESTATE!=1) .Where(x=>x.ENABLESTATE!=1)
.Select(x=>new ErpUserDto .Select(x=>new ErpUserDto
@@ -1525,125 +1525,193 @@ namespace Tnb.ProductionMgr
}) })
.ToListAsync(); .ToListAsync();
persons.AddRange(users); // persons.AddRange(users);
List<ErpExtendField> erpExtendFields = await _db.Queryable<ErpExtendField>().Where(x=>x.table_name=="base_user").ToListAsync(); List<ErpExtendField> erpExtendFields = await _db.Queryable<ErpExtendField>().Where(x=>x.table_name=="base_user").ToListAsync();
List<UserEntity> insertUsers = new List<UserEntity>();
List<UserEntity> existsUsers = await _db.Queryable<UserEntity>().ToListAsync(); List<UserEntity> existsUsers = await _db.Queryable<UserEntity>().ToListAsync();
List<UserEntity> insertUsers = new List<UserEntity>();
List<UserRelationEntity> insertUserRelations = new List<UserRelationEntity>(); List<UserRelationEntity> insertUserRelations = new List<UserRelationEntity>();
List<ErpExtendField> insertErpExtendFields = new List<ErpExtendField>(); List<ErpExtendField> insertErpExtendFields = new List<ErpExtendField>();
await _db.Ado.BeginTranAsync(); List<UserEntity> insertUsers2 = new List<UserEntity>();
List<UserRelationEntity> insertUserRelations2 = new List<UserRelationEntity>();
List<ErpExtendField> insertErpExtendFields2 = new List<ErpExtendField>();
foreach (var person in persons) await _db.Ado.BeginTranAsync();
foreach (var user in users)
{ {
if ((person.TYPE=="1" && erpExtendFields.All(x => x.person_id != person.PERSON_ID)) || (person.TYPE=="2" && erpExtendFields.All(x => x.user_id != person.USER_ID))) List<ErpExtendField> userErpExtendFields = erpExtendFields.Where(x => x.user_id == user.USER_ID).ToList();
if (userErpExtendFields == null || userErpExtendFields.IsEmpty() || userErpExtendFields.Count == 1)
{ {
if (person.TYPE == "2" && insertErpExtendFields.FindIndex(x=>x.person_id==person.PERSON_ID)!=-1 && person.PERSON_ID!="~")
UserEntity userEntity = new UserEntity();
if (userErpExtendFields.Count == 1)
{ {
ErpExtendField eef = insertErpExtendFields.Find(x => x.person_id == person.PERSON_ID); userEntity = existsUsers.Find(x => x.Id == erpExtendFields[0].table_id);
eef.user_id = person.USER_ID; int state = user.ENABLESTATE == 2 ? 1 : 0;
if (userEntity != null)
UserEntity existUser = insertUsers.Find(x => x.Id == eef.table_id);
if (existUser != null)
{ {
int state = person.ENABLESTATE == 2 ? 1 : 0; if (userEntity.EnabledMark != state)
if (existUser.EnabledMark != state)
{
existUser.EnabledMark = state;
}
}
}
else
{
UserEntity userEntity = new UserEntity();
if (existsUsers.Exists(x => x.Account == person.CODE))
{
userEntity = existsUsers.Find(x => x.Account == person.CODE);
if (userEntity != null)
{
int state = person.ENABLESTATE == 2 ? 1 : 0;
if (userEntity.EnabledMark != state)
{
await _db.Updateable<UserEntity>()
.SetColumns(x => x.EnabledMark == state)
.Where(x => x.Id == userEntity.Id)
.ExecuteCommandAsync();
}
}
}
else
{
userEntity.Id = SnowflakeIdHelper.NextId();
userEntity.Account = person.CODE;
userEntity.RealName = person.NAME;
userEntity.Gender = 1;
userEntity.OrganizeId = WmsWareHouseConst.AdministratorOrgId;
userEntity.RoleId = "30327535942933";
userEntity.Secretkey = Guid.NewGuid().ToString();
userEntity.Password = MD5Encryption.Encrypt(MD5Encryption.Encrypt(CommonConst.DEFAULTPASSWORD) + userEntity.Secretkey);
userEntity.EntryDate = DateTime.Now;
userEntity.EnabledMark = person.ENABLESTATE == 2 ? 1 : 0;
userEntity.CreatorTime = DateTime.Now;
insertUsers.Add(userEntity);
UserRelationEntity userRelationEntity = new UserRelationEntity();
userRelationEntity.Id = SnowflakeIdHelper.NextId();
userRelationEntity.UserId = userEntity.Id;
userRelationEntity.ObjectType = "Role";
userRelationEntity.ObjectId = "30327535942933";
userRelationEntity.CreatorTime = DateTime.Now;
insertUserRelations.Add(userRelationEntity);
UserRelationEntity userRelationEntity2 = new UserRelationEntity();
userRelationEntity2.Id = SnowflakeIdHelper.NextId();
userRelationEntity2.UserId = userEntity.Id;
userRelationEntity2.ObjectType = "Organize";
userRelationEntity2.ObjectId = WmsWareHouseConst.AdministratorOrgId;
userRelationEntity2.CreatorTime = DateTime.Now;
insertUserRelations.Add(userRelationEntity2);
}
ErpExtendField extendField = new ErpExtendField();
extendField.org_id = WmsWareHouseConst.AdministratorOrgId;
extendField.table_name = "base_user";
extendField.table_id = userEntity.Id;
extendField.person_id = person.PERSON_ID;
extendField.user_id = person.USER_ID;
insertErpExtendFields.Add(extendField);
}
}
else
{
ErpExtendField user = erpExtendFields.Find(x => x.person_id == person.PERSON_ID || x.user_id == person.USER_ID);
if (user != null)
{
UserEntity ue = await _db.Queryable<UserEntity>().Where(x=>x.Id==user.table_id).FirstAsync();
if (ue != null)
{
int state = person.ENABLESTATE == 2 ? 1 : 0;
if (ue.EnabledMark != state)
{ {
await _db.Updateable<UserEntity>() await _db.Updateable<UserEntity>()
.SetColumns(x => x.EnabledMark == state) .SetColumns(x => x.EnabledMark == state)
.Where(x => x.Id == ue.Id) .Where(x => x.Id == userEntity.Id)
.ExecuteCommandAsync(); .ExecuteCommandAsync();
} }
continue;
} }
} }
userEntity.Id = SnowflakeIdHelper.NextId();
userEntity.Account = user.CODE;
userEntity.RealName = user.NAME;
userEntity.Gender = 1;
userEntity.OrganizeId = WmsWareHouseConst.AdministratorOrgId;
userEntity.RoleId = "30327535942933";
userEntity.Secretkey = Guid.NewGuid().ToString();
userEntity.Password = MD5Encryption.Encrypt(MD5Encryption.Encrypt(CommonConst.DEFAULTPASSWORD) +
userEntity.Secretkey);
userEntity.EntryDate = DateTime.Now;
userEntity.EnabledMark = user.ENABLESTATE == 2 ? 1 : 0;
userEntity.CreatorTime = DateTime.Now;
insertUsers.Add(userEntity);
UserRelationEntity userRelationEntity = new UserRelationEntity();
userRelationEntity.Id = SnowflakeIdHelper.NextId();
userRelationEntity.UserId = userEntity.Id;
userRelationEntity.ObjectType = "Role";
userRelationEntity.ObjectId = "30327535942933";
userRelationEntity.CreatorTime = DateTime.Now;
insertUserRelations.Add(userRelationEntity);
UserRelationEntity userRelationEntity2 = new UserRelationEntity();
userRelationEntity2.Id = SnowflakeIdHelper.NextId();
userRelationEntity2.UserId = userEntity.Id;
userRelationEntity2.ObjectType = "Organize";
userRelationEntity2.ObjectId = WmsWareHouseConst.AdministratorOrgId;
userRelationEntity2.CreatorTime = DateTime.Now;
insertUserRelations.Add(userRelationEntity2);
ErpExtendField erpExtendField = new ErpExtendField();
erpExtendField.org_id = WmsWareHouseConst.AdministratorOrgId;
erpExtendField.table_name = "base_user";
erpExtendField.table_id = userEntity.Id;
erpExtendField.person_id = user.PERSON_ID;
erpExtendField.user_id = user.USER_ID;
insertErpExtendFields.Add(erpExtendField);
}
else
{
} }
} }
await _db.Insertable(insertUsers).ExecuteCommandAsync(); if (!insertUsers.IsEmpty())
await _db.Insertable(insertUserRelations).ExecuteCommandAsync(); {
await _db.Insertable(insertErpExtendFields).ExecuteCommandAsync(); existsUsers.AddRange(insertUsers);
await _db.Insertable(insertUsers).ExecuteCommandAsync();
}
if (!insertUserRelations.IsEmpty())
{
await _db.Insertable(insertUserRelations).ExecuteCommandAsync();
}
if (!insertErpExtendFields.IsEmpty())
{
erpExtendFields.AddRange(insertErpExtendFields);
await _db.Insertable(insertErpExtendFields).ExecuteCommandAsync();
}
// foreach (var person in persons)
// {
// List<ErpExtendField> userErpExtendFields = erpExtendFields.Where(x => x.person_id == person.PERSON_ID).ToList();
// if (userErpExtendFields == null || userErpExtendFields.IsEmpty() || userErpExtendFields.Count == 1)
// {
//
// UserEntity userEntity = new UserEntity();
// if (userErpExtendFields.Count == 1)
// {
// userEntity = existsUsers.Find(x => x.Id == erpExtendFields[0].table_id);
// int state = person.ENABLESTATE == 2 ? 1 : 0;
// if (userEntity != null)
// {
// if (userEntity.EnabledMark != state)
// {
// await _db.Updateable<UserEntity>()
// .SetColumns(x => x.EnabledMark == state)
// .Where(x => x.Id == userEntity.Id)
// .ExecuteCommandAsync();
// }
// continue;
// }
// }
//
//
// userEntity.Id = SnowflakeIdHelper.NextId();
// userEntity.Account = person.CODE;
// userEntity.RealName = person.NAME;
// userEntity.Gender = 1;
// userEntity.OrganizeId = WmsWareHouseConst.AdministratorOrgId;
// userEntity.RoleId = "30327535942933";
// userEntity.Secretkey = Guid.NewGuid().ToString();
// userEntity.Password = MD5Encryption.Encrypt(MD5Encryption.Encrypt(CommonConst.DEFAULTPASSWORD) +
// userEntity.Secretkey);
// userEntity.EntryDate = DateTime.Now;
// userEntity.EnabledMark = person.ENABLESTATE == 2 ? 1 : 0;
// userEntity.CreatorTime = DateTime.Now;
// insertUsers2.Add(userEntity);
//
// UserRelationEntity userRelationEntity = new UserRelationEntity();
// userRelationEntity.Id = SnowflakeIdHelper.NextId();
// userRelationEntity.UserId = userEntity.Id;
// userRelationEntity.ObjectType = "Role";
// userRelationEntity.ObjectId = "30327535942933";
// userRelationEntity.CreatorTime = DateTime.Now;
// insertUserRelations2.Add(userRelationEntity);
//
// UserRelationEntity userRelationEntity2 = new UserRelationEntity();
// userRelationEntity2.Id = SnowflakeIdHelper.NextId();
// userRelationEntity2.UserId = userEntity.Id;
// userRelationEntity2.ObjectType = "Organize";
// userRelationEntity2.ObjectId = WmsWareHouseConst.AdministratorOrgId;
// userRelationEntity2.CreatorTime = DateTime.Now;
// insertUserRelations2.Add(userRelationEntity2);
//
// ErpExtendField erpExtendField = new ErpExtendField();
// erpExtendField.org_id = WmsWareHouseConst.AdministratorOrgId;
// erpExtendField.table_name = "base_user";
// erpExtendField.table_id = userEntity.Id;
// erpExtendField.person_id = person.PERSON_ID;
// erpExtendField.user_id = person.USER_ID;
// insertErpExtendFields2.Add(erpExtendField);
// }
// else
// {
//
// }
// }
//
// if (!insertUsers2.IsEmpty())
// {
// await _db.Insertable(insertUsers2).ExecuteCommandAsync();
//
// }
// if (!insertUserRelations2.IsEmpty())
// {
// await _db.Insertable(insertUserRelations2).ExecuteCommandAsync();
// }
//
// if (!insertErpExtendFields2.IsEmpty())
// {
// await _db.Insertable(insertErpExtendFields2).ExecuteCommandAsync();
// }
await _db.Ado.CommitTranAsync(); await _db.Ado.CommitTranAsync();
msg = $"新增用户{insertUsers.Count}条"; msg = $"新增用户{insertUsers.Count+insertUsers2.Count}条";
} }
catch (Exception e) catch (Exception e)
{ {