/// <summary> /// 同步单个组织 (无租户验证版本) /// </summary> /// <param name="wx_dept"></param> /// <returns>更新的本地Id</returns> public void MatchSingleDepartmentWithoutTenant(SyncDepartment wx_dept, int?tenant_id) { if (wx_dept != null) { using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.MayHaveTenant)) { var entity = _organizationUnitRepository.FirstOrDefault(o => o.WXDeptId == wx_dept.id && o.TenantId == tenant_id); var base_org = _baseOrgRepository.FirstOrDefault(o => o.WxId == wx_dept.id.ToString()); var result_id = entity?.Id ?? null; var parent_entity = wx_dept.parentid == 0 ? null : (_organizationUnitRepository.FirstOrDefault(o => o.WXDeptId == wx_dept.parentid && o.TenantId == tenant_id)); var parent_id = parent_entity?.Id ?? null; var parent_code = parent_entity?.Code ?? ""; switch (wx_dept.changetype) { case "delete_party": if (result_id.HasValue) { _organizationUnitRepository.Delete(result_id.Value); } if (base_org != null) { _baseOrgRepository.Delete(base_org); } break; case "create_party": if (entity == null) { entity = new AbpOrganizationUnitExtend { TenantId = tenant_id,//AbpSession.TenantId, WXDeptId = wx_dept.id, DisplayName = wx_dept.name, WXParentDeptId = wx_dept.parentid, ParentId = parent_id, Code = "" }; result_id = _organizationUnitRepository.InsertAndGetId(entity); //CurrentUnitOfWork.SaveChanges(); entity.Code = string.IsNullOrEmpty(parent_code) ? result_id.ToString() : parent_code + ":" + result_id.Value; entity.Id = result_id.Value; _organizationUnitRepository.Update(entity); } if (base_org == null) { if (parent_id == 1) { _baseOrgRepository.Insert(new BaseUserOrg { Name = wx_dept.name, OrgGuid = Guid.NewGuid().ToString(), OrgOrderNo = wx_dept.id, OrgParentGuid = "0", ParentId = 0, WxId = wx_dept.id.ToString() }); } else { var parent_org = _baseOrgRepository.FirstOrDefault(x => x.WxId == wx_dept.parentid.ToString()); if (parent_org != null) { _baseOrgRepository.Insert(new BaseUserOrg { Name = wx_dept.name, OrgGuid = Guid.NewGuid().ToString(), OrgOrderNo = wx_dept.id, OrgParentGuid = parent_org.OrgGuid, ParentId = parent_org.Id, WxId = wx_dept.id.ToString() }); } } } break; case "update_party": if (result_id.HasValue) { /** * 2019-04-28 miansheng.luo 企业微信回调有更新忽略。修改时请注意修改下列字段判断方式 */ if (wx_dept.parentid.HasValue) { entity.ParentId = parent_id; entity.Code = string.IsNullOrEmpty(parent_code) ? result_id.ToString() : parent_code + ":" + result_id.Value; entity.WXParentDeptId = wx_dept.parentid; } entity.TenantId = tenant_id; entity.WXDeptId = wx_dept.id; entity.DisplayName = string.IsNullOrEmpty(wx_dept.name) ? entity.DisplayName : wx_dept.name; _organizationUnitRepository.Update(entity); } if (base_org != null) { if (wx_dept.parentid.HasValue && wx_dept.parentid == 1) { base_org.OrgParentGuid = "0"; base_org.ParentId = 0; _baseOrgRepository.Update(base_org); } else if (wx_dept.parentid.HasValue) { } } break; default: break; } } } }
/// <summary> /// 同步单个组织 /// </summary> /// <param name="wx_dept"></param> /// <returns>更新的本地Id</returns> public void MatchSingleDepartment(SyncDepartment wx_dept, int?tenant_id) { if (wx_dept != null) { var entity = _organizationUnitRepository.FirstOrDefault(o => o.WXDeptId == wx_dept.id && o.TenantId == tenant_id); var result_id = entity?.Id ?? null; var parent_entity = wx_dept.parentid == 0 ? null : (_organizationUnitRepository.FirstOrDefault(o => o.WXDeptId == wx_dept.parentid && o.TenantId == tenant_id)); var parent_id = parent_entity?.Id ?? null; var parent_code = parent_entity?.Code ?? ""; switch (wx_dept.changetype) { case "delete_party": if (result_id.HasValue) { _organizationUnitRepository.Delete(result_id.Value); } break; case "create_party": if (entity == null) { entity = new AbpOrganizationUnitExtend { TenantId = tenant_id,//AbpSession.TenantId, WXDeptId = wx_dept.id, DisplayName = wx_dept.name, WXParentDeptId = wx_dept.parentid, ParentId = parent_id, Code = "" }; result_id = _organizationUnitRepository.InsertAndGetId(entity); //CurrentUnitOfWork.SaveChanges(); entity.Code = string.IsNullOrEmpty(parent_code) ? result_id.ToString() : parent_code + ":" + result_id.Value; entity.Id = result_id.Value; _organizationUnitRepository.Update(entity); } break; case "update_party": if (result_id.HasValue) { /** * 2019-04-28 miansheng.luo 企业微信回调有更新忽略。修改时请注意修改下列字段判断方式 */ if (wx_dept.parentid.HasValue) { entity.ParentId = parent_id; entity.Code = string.IsNullOrEmpty(parent_code) ? result_id.ToString() : parent_code + ":" + result_id.Value; entity.WXParentDeptId = wx_dept.parentid; } entity.TenantId = tenant_id; entity.WXDeptId = wx_dept.id; entity.DisplayName = string.IsNullOrEmpty(wx_dept.name) ? entity.DisplayName : wx_dept.name; _organizationUnitRepository.Update(entity); } break; default: break; } } }
public async Task <ReceiveSyncResultDto> SyncSingleOrganizationUnits(ReceiveSyncOrganizationDto input) { var current_tenantid = 2; var action = input.ActionName.ToLower(); using (CurrentUnitOfWork.SetTenantId(current_tenantid)) { var parent = await _organizationUnitRepository.FirstOrDefaultAsync(x => x.WXDeptId == input.WXParentDeptId); if (action != "delete_dept") { if (parent == null) { return(new ReceiveSyncResultDto(33, "父级部门不存在")); } } var entity = await _organizationUnitRepository.FirstOrDefaultAsync(x => x.WXDeptId == input.WXDeptId); var base_org = _baseOrgRepository.FirstOrDefault(o => o.WxId == input.WXDeptId.ToString()); var parent_org = _baseOrgRepository.FirstOrDefault(x => x.WxId == input.WXParentDeptId.ToString()); if (action == "create_dept") { if (entity != null) { return(new ReceiveSyncResultDto(30, "部门已存在")); } entity = new AbpOrganizationUnitExtend { TenantId = current_tenantid,//AbpSession.TenantId, WXDeptId = input.WXDeptId, DisplayName = input.DisplayName, WXParentDeptId = input.WXParentDeptId, ParentId = parent.Id, Code = "" }; var result_id = _organizationUnitRepository.InsertAndGetId(entity); //CurrentUnitOfWork.SaveChanges(); entity.Code = string.IsNullOrEmpty(parent.Code) ? result_id.ToString() : parent.Code + ":" + result_id; entity.Id = result_id; _organizationUnitRepository.Update(entity); if (parent.Id == 1) { _baseOrgRepository.Insert(new BaseUserOrg { Name = input.DisplayName, OrgGuid = Guid.NewGuid().ToString(), OrgOrderNo = input.WXDeptId, OrgParentGuid = "0", ParentId = 0, WxId = input.WXDeptId.ToString() }); } else { if (parent_org != null) { _baseOrgRepository.Insert(new BaseUserOrg { Name = input.DisplayName, OrgGuid = Guid.NewGuid().ToString(), OrgOrderNo = input.WXDeptId, OrgParentGuid = parent_org.OrgGuid, ParentId = parent_org.Id, WxId = input.WXDeptId.ToString() }); } } } else if (action == "modify_dept") { if (entity == null) { return(new ReceiveSyncResultDto(31, "部门不存在")); } entity.TenantId = current_tenantid; entity.DisplayName = input.DisplayName; entity.WXParentDeptId = parent.WXDeptId; entity.ParentId = parent.Id; entity.Code = string.IsNullOrEmpty(parent.Code) ? entity.Id.ToString() : parent.Code + ":" + entity.Id; await _organizationUnitRepository.UpdateAsync(entity); base_org.Name = input.DisplayName; base_org.OrgParentGuid = parent_org != null ? parent_org.OrgGuid : "0"; base_org.ParentId = parent_org != null ? parent_org.Id : 0; _baseOrgRepository.Update(base_org); } else if (action == "delete_dept") { if (entity == null) { return(new ReceiveSyncResultDto(31, "部门不存在")); } var any_user = await _userOrganizationUnitRepository.GetAll().AnyAsync(x => x.OrganizationUnitId == entity.Id); if (any_user) { return(new ReceiveSyncResultDto(32, "部门内仍有用户")); } await _organizationUnitRepository.DeleteAsync(entity); await _baseOrgRepository.DeleteAsync(base_org); return(new ReceiveSyncResultDto(0, "")); } else { return(new ReceiveSyncResultDto(2, "无法识别Action")); } return(new ReceiveSyncResultDto(0, "")); } }