0.0.5.1
This commit is contained in:
@@ -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: {
|
||||
|
||||
@@ -12,12 +12,32 @@ const createMemberSchema = z.object({
|
||||
fullName: z.string().min(1),
|
||||
gender: z.nativeEnum(Gender),
|
||||
generation: z.number().int().positive(),
|
||||
generationName: z.string().optional(),
|
||||
courtesyName: z.string().optional(),
|
||||
artName: z.string().optional(),
|
||||
posthumousName: z.string().optional(),
|
||||
rank: z.number().optional(),
|
||||
birthDate: z.string().optional(),
|
||||
deathDate: z.string().optional(),
|
||||
isLunarDate: z.boolean().optional(),
|
||||
ancestralHome: z.string().optional(),
|
||||
birthPlace: z.string().optional(),
|
||||
burialPlace: z.string().optional(),
|
||||
phone: z.string().optional(),
|
||||
telephone: z.string().optional(),
|
||||
email: z.string().optional(),
|
||||
address: z.string().optional(),
|
||||
fatherId: z.string().optional(),
|
||||
motherId: z.string().optional(),
|
||||
spouseIds: z.array(z.string()).optional(),
|
||||
// ... 其他字段
|
||||
childrenIds: z.array(z.string()).optional(),
|
||||
spouseFatherName: z.string().optional(),
|
||||
spouseMotherName: z.string().optional(),
|
||||
bio: z.string().optional(),
|
||||
tags: z.array(z.string()).optional(),
|
||||
avatarUrl: z.string().optional(),
|
||||
avatarImageId: z.string().optional(),
|
||||
photoIds: z.array(z.string()).optional(),
|
||||
})
|
||||
|
||||
// 获取家族树的所有成员
|
||||
@@ -116,6 +136,81 @@ export async function POST(
|
||||
}
|
||||
})
|
||||
|
||||
// 维护配偶双向关系
|
||||
if (data.spouseIds && data.spouseIds.length > 0) {
|
||||
for (const spouseId of data.spouseIds) {
|
||||
const spouse = await prisma.familyMember.findUnique({
|
||||
where: { id: spouseId }
|
||||
})
|
||||
|
||||
if (spouse && !spouse.spouseIds.includes(member.id)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: spouseId },
|
||||
data: {
|
||||
spouseIds: [...spouse.spouseIds, member.id]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 维护父母-子女双向关系
|
||||
// 1. 如果设置了父亲,将当前成员添加到父亲的子女列表
|
||||
if (data.fatherId) {
|
||||
const father = await prisma.familyMember.findUnique({
|
||||
where: { id: data.fatherId }
|
||||
})
|
||||
|
||||
if (father && !father.childrenIds.includes(member.id)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: data.fatherId },
|
||||
data: {
|
||||
childrenIds: [...father.childrenIds, member.id]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 如果设置了母亲,将当前成员添加到母亲的子女列表
|
||||
if (data.motherId) {
|
||||
const mother = await prisma.familyMember.findUnique({
|
||||
where: { id: data.motherId }
|
||||
})
|
||||
|
||||
if (mother && !mother.childrenIds.includes(member.id)) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: data.motherId },
|
||||
data: {
|
||||
childrenIds: [...mother.childrenIds, member.id]
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 如果设置了子女,将当前成员设置为子女的父亲或母亲
|
||||
if (data.childrenIds && data.childrenIds.length > 0) {
|
||||
for (const childId of data.childrenIds) {
|
||||
const child = await prisma.familyMember.findUnique({
|
||||
where: { id: childId }
|
||||
})
|
||||
|
||||
if (child) {
|
||||
// 根据当前成员的性别设置为父亲或母亲
|
||||
if (data.gender === 'MALE' && !child.fatherId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: childId },
|
||||
data: { fatherId: member.id }
|
||||
})
|
||||
} else if (data.gender === 'FEMALE' && !child.motherId) {
|
||||
await prisma.familyMember.update({
|
||||
where: { id: childId },
|
||||
data: { motherId: member.id }
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 记录操作日志
|
||||
await prisma.activityLog.create({
|
||||
data: {
|
||||
|
||||
Reference in New Issue
Block a user