This commit is contained in:
freedakgmail
2025-11-23 15:56:24 +08:00
parent 8b32cda811
commit 3375a79ed4
74 changed files with 99918 additions and 1227 deletions
@@ -88,6 +88,175 @@ export async function PATCH(
}
})
// 维护配偶双向关系
if (body.spouseIds !== undefined) {
const newSpouseIds = body.spouseIds || []
const oldSpouseIds = existingMember.spouseIds || []
// 添加新配偶的反向关系
for (const spouseId of newSpouseIds) {
if (!oldSpouseIds.includes(spouseId)) {
const spouse = await prisma.familyMember.findUnique({
where: { id: spouseId }
})
if (spouse && !spouse.spouseIds.includes(memberId)) {
await prisma.familyMember.update({
where: { id: spouseId },
data: {
spouseIds: [...spouse.spouseIds, memberId]
}
})
}
}
}
// 移除旧配偶的反向关系
for (const oldSpouseId of oldSpouseIds) {
if (!newSpouseIds.includes(oldSpouseId)) {
const spouse = await prisma.familyMember.findUnique({
where: { id: oldSpouseId }
})
if (spouse && spouse.spouseIds.includes(memberId)) {
await prisma.familyMember.update({
where: { id: oldSpouseId },
data: {
spouseIds: spouse.spouseIds.filter(id => id !== memberId)
}
})
}
}
}
}
// 维护父母-子女双向关系
// 1. 处理父亲关系变化
if (body.fatherId !== undefined) {
const oldFatherId = existingMember.fatherId
const newFatherId = body.fatherId
// 从旧父亲的子女列表中移除
if (oldFatherId && oldFatherId !== newFatherId) {
const oldFather = await prisma.familyMember.findUnique({
where: { id: oldFatherId }
})
if (oldFather && oldFather.childrenIds.includes(memberId)) {
await prisma.familyMember.update({
where: { id: oldFatherId },
data: {
childrenIds: oldFather.childrenIds.filter(id => id !== memberId)
}
})
}
}
// 添加到新父亲的子女列表
if (newFatherId && newFatherId !== oldFatherId) {
const newFather = await prisma.familyMember.findUnique({
where: { id: newFatherId }
})
if (newFather && !newFather.childrenIds.includes(memberId)) {
await prisma.familyMember.update({
where: { id: newFatherId },
data: {
childrenIds: [...newFather.childrenIds, memberId]
}
})
}
}
}
// 2. 处理母亲关系变化
if (body.motherId !== undefined) {
const oldMotherId = existingMember.motherId
const newMotherId = body.motherId
// 从旧母亲的子女列表中移除
if (oldMotherId && oldMotherId !== newMotherId) {
const oldMother = await prisma.familyMember.findUnique({
where: { id: oldMotherId }
})
if (oldMother && oldMother.childrenIds.includes(memberId)) {
await prisma.familyMember.update({
where: { id: oldMotherId },
data: {
childrenIds: oldMother.childrenIds.filter(id => id !== memberId)
}
})
}
}
// 添加到新母亲的子女列表
if (newMotherId && newMotherId !== oldMotherId) {
const newMother = await prisma.familyMember.findUnique({
where: { id: newMotherId }
})
if (newMother && !newMother.childrenIds.includes(memberId)) {
await prisma.familyMember.update({
where: { id: newMotherId },
data: {
childrenIds: [...newMother.childrenIds, memberId]
}
})
}
}
}
// 3. 处理子女关系变化
if (body.childrenIds !== undefined) {
const newChildrenIds = body.childrenIds || []
const oldChildrenIds = existingMember.childrenIds || []
// 添加新子女的父母关系
for (const childId of newChildrenIds) {
if (!oldChildrenIds.includes(childId)) {
const child = await prisma.familyMember.findUnique({
where: { id: childId }
})
if (child) {
// 根据当前成员的性别设置为父亲或母亲
if (member.gender === 'MALE' && !child.fatherId) {
await prisma.familyMember.update({
where: { id: childId },
data: { fatherId: memberId }
})
} else if (member.gender === 'FEMALE' && !child.motherId) {
await prisma.familyMember.update({
where: { id: childId },
data: { motherId: memberId }
})
}
}
}
}
// 移除旧子女的父母关系
for (const oldChildId of oldChildrenIds) {
if (!newChildrenIds.includes(oldChildId)) {
const child = await prisma.familyMember.findUnique({
where: { id: oldChildId }
})
if (child) {
// 根据当前成员的性别移除父亲或母亲
if (member.gender === 'MALE' && child.fatherId === memberId) {
await prisma.familyMember.update({
where: { id: oldChildId },
data: { fatherId: null }
})
} else if (member.gender === 'FEMALE' && child.motherId === memberId) {
await prisma.familyMember.update({
where: { id: oldChildId },
data: { motherId: null }
})
}
}
}
}
}
// 记录操作日志
await prisma.activityLog.create({
data: {